1. خانه
  2. مقالات
  3. آموزش برنامه نویسی

ماشین حساب با استفاده از HTML و CSS

ماشین حساب با استفاده از HTML و CSS

ماشین حساب ساده با استفاده از

HTML

CSS

JavaScript

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ماشین حساب آنلاین نیو تری‌دی</title>
<style>
body {
font-family: Tahoma, Arial, sans-serif;
background: linear-gradient(135deg, #667eea, #764ba2);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.calculator {
background: #222;
padding: 25px;
border-radius: 20px;
box-shadow: 0 15px 35px rgba(0,0,0,0.5);
width: 320px;
}
.display {
background: #333;
color: #0f0;
font-size: 2.5em;
text-align: left;
padding: 20px;
border-radius: 12px;
margin-bottom: 15px;
letter-spacing: 2px;
min-height: 70px;
word-wrap: break-word;
box-shadow: inset 0 5px 10px rgba(0,0,0,0.6);
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 12px;
}
button {
padding: 20px;
font-size: 1.4em;
border: none;
border-radius: 12px;
cursor: pointer;
transition: all 0.2s;
font-weight: bold;
}
button:hover {
transform: scale(1.05);
}
.num {
background: #444;
color: white;
}
.operator {
background: #ff9500;
color: white;
}
.equal {
background: #4cd137;
color: white;
grid-column: span 2;
}
.clear {
background: #e74c3c;
color: white;
grid-column: span 2;
}
.zero {
grid-column: span 2;
}
</style>
</head>
<body>

<div class="calculator">
<div class="display" id="display">0</div>

<div class="buttons">
<button class="clear" onclick="clearDisplay()">پاک کردن</button>
<button class="operator" onclick="deleteLast()">⌫</button>
<button class="operator" onclick="appendToDisplay('/')">/</button>

<button class="num" onclick="appendToDisplay('7')">7</button>
<button class="num" onclick="appendToDisplay('8')">8</button>
<button class="num" onclick="appendToDisplay('9')">9</button>
<button class="operator" onclick="appendToDisplay('*')">×</button>

<button class="num" onclick="appendToDisplay('4')">4</button>
<button class="num" onclick="appendToDisplay('5')">5</button>
<button class="num" onclick="appendToDisplay('6')">6</button>
<button class="operator" onclick="appendToDisplay('-')">−</button>

<button class="num" onclick="appendToDisplay('1')">1</button>
<button class="num" onclick="appendToDisplay('2')">2</button>
<button class="num" onclick="appendToDisplay('3')">3</button>
<button class="operator" onclick="appendToDisplay('+')">+</button>

<button class="num zero" onclick="appendToDisplay('0')">0</button>
<button class="num" onclick="appendToDisplay('.')">.</button>
<button class="equal" onclick="calculate()">=</button>
</div>
</div>

<script>
let display = document.getElementById('display');
let currentInput = '0';
let shouldResetDisplay = false;

function appendToDisplay(value) {
if (shouldResetDisplay) {
currentInput = '0';
shouldResetDisplay = false;
}
if (currentInput === '0' && value !== '.') {
currentInput = value;
} else {
currentInput += value;
}
display.textContent = currentInput;
}

function clearDisplay() {
currentInput = '0';
display.textContent = currentInput;
}

function deleteLast() {
if (currentInput.length > 1) {
currentInput = currentInput.slice(0, -1);
} else {
currentInput = '0';
}
display.textContent = currentInput;
}

function calculate() {
try {
// تبدیل × و − به * و -
let expression = currentInput.replace(/×/g, '*').replace(/−/g, '-');
let result = eval(expression);

// گرد کردن به 8 رقم اعشار
result = Number(result.toFixed(8));

display.textContent = result;
currentInput = result.toString();
shouldResetDisplay = true;
} catch (error) {
display.textContent = 'خطا';
shouldResetDisplay = true;
}
}

// پشتیبانی از کیبورد
document.addEventListener('keydown', function(e) {
if (e.key >= '0' && e.key <= '9') appendToDisplay(e.key);
if (e.key === '.') appendToDisplay('.');
if (e.key === '+' || e.key === '-') appendToDisplay(e.key);
if (e.key === '*') appendToDisplay('*');
if (e.key === '/') appendToDisplay('/');
if (e.key === 'Enter' || e.key === '=') calculate();
if (e.key === 'Escape') clearDisplay();
if (e.key === 'Backspace') deleteLast();
});
</script>

</body>
</html>

پیشنهاد ما

مقالات مرتبط

مشاهده همه

Vue.js چیست؟

Vue.js چیست؟ نویسنده: عطا ایروانی بررسی جامع مزایای Vue.js Vue.js یکی از محبوب‌ترین فریم‌ورک‌های جاوااسکریپت در دنیا است که برای توسعه رابط کاربری و وب‌اپلیکیشن‌های مدرن استفاده می‌شود. این فریم‌ورک توسط Evan You ساخته…

ASP.NET Core چیست؟

ASP.NET Core چیست؟ نویسنده: محمد جواد امانی بررسی جامع مزایای استفاده از ASP.NET Core ASP.NET Core یکی از قدرتمندترین و مدرن‌ترین فریم‌ورک‌های توسعه وب در جهان است که توسط شرکت مایکروسافت توسعه یافته است. این فریم‌ورک به…

وردپرس چیست؟

وردپرس چیست؟ بررسی کامل مزایا و معایب وردپرس وردپرس ( WordPress ) یکی از محبوب‌ترین سیستم‌های مدیریت محتوای جهان است که بیش از ۴۳ درصد از وب‌سایت‌های دنیا با آن ساخته شده‌اند. بسیاری از افراد و کسب‌وکارها برای ساخت…

پروژه بعدی‌ات را با ما شروع کن

یک تماس کوتاه کافی است تا ایده‌ات را بررسی کنیم و بهترین مسیر فنی و هزینه را پیشنهاد بدهیم — مشاوره اولیه رایگان است.