Tabii ki! Aşağıda, HTML, CSS ve JavaScript kullanarak oluşturduğum, bilimsel hesaplamalar yapabilen kompleks bir hesap makinesinin kodlarını bulacaksınız. Bu hesap makinesi temel matematik işlemlerinin yanı sıra karekök, üs alma, logaritma, trigonometri (sin, cos, tan) gibi bilimsel hesaplamaları da yapabilmektedir.
Dosya Adı: calculator.html
Kodları kopyalayıp tek bir calculator.html dosyasına yapıştırın. Çünkü CSS ve JavaScript kodlarını da HTML dosyasının içinde <style> ve <script> etiketleri arasında tanımladık.
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bilimsel Hesap Makinesi</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.calculator {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#display {
width: 100%;
height: 40px;
margin-bottom: 20px;
padding: 10px;
font-size: 24px;
text-align: right;
border: 1px solid #ddd;
border-radius: 5px;
}
.row {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}
button {
width: 60px;
height: 60px;
margin: 0 5px;
font-size: 18px;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #f9f9f9;
transition: background-color 0.2s;
}
button:hover {
background-color: #eaeaea;
}
button.operation {
background-color: #4CAF50;
color: white;
}
button.operation:hover {
background-color: #45a049;
}
button.equal {
background-color: #008CBA;
color: white;
}
button.equal:hover {
background-color: #007B9E;
}
button.clear {
background-color: #f44336;
color: white;
}
button.clear:hover {
background-color: #e53935;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="row">
<button onclick="clearDisplay()">C</button>
<button onclick="backspace()">DEL</button>
<button onclick="calculate('%')" class="operation">%</button>
<button onclick="calculate('/')" class="operation">÷</button>
</div>
<div class="row">
<button onclick="appendNumber(7')">7</button>
<button onclick="appendNumber(8')">8</button>
<button onclick="appendNumber(9')">9</button>
<button onclick="calculate('*')" class="operation">×</button>
</div>
<div class="row">
<button onclick="appendNumber(4')">4</button>
<button onclick="appendNumber(5')">5</button>
<button onclick="appendNumber(6')">6</button>
<button onclick="calculate('-')" class="operation">-</button>
</div>
<div class="row">
<button onclick="appendNumber(1')">1</button>
<button onclick="appendNumber(2')">2</button>
<button onclick="appendNumber(3')">3</button>
<button onclick="calculate('+')" class="operation">+</button>
</div>
<div class="row">
<button onclick="appendNumber(0')">0</button>
<button onclick="appendPoint()">.</button>
<button onclick="calculateMathFunction('sqrt')">√</button>
<button onclick="calculateMathFunction('square')">x²</button>
</div>
<div class="row">
<button onclick="calculateMathFunction('sin')">sin</button>
<button onclick="calculateMathFunction('cos')">cos</button>
<button onclick="calculateMathFunction('tan')">tan</button>
<button onclick="calculateMathFunction('log')">log</button>
</div>
<div class="row">
<button onclick="calculateMathFunction('pow')">x^y</button>
<button onclick="calculateMathFunction('exp')">e^x</button>
<button onclick="calculateMathFunction('pi')">π</button>
<button onclick="calculate('=')" class="equal">=</button>
</div>
</div>
<script>
let display = document.getElementById('display');
let currentValue = '';
let isResultDisplayed = false;
// Ekranı temizleme
function clearDisplay() {
currentValue = '';
display.value = '';
isResultDisplayed = false;
}
// Geri silme
function backspace() {
currentValue = currentValue.slice(0, -1);
display.value = currentValue;
}
// Nokta ekleme
function appendPoint() {
if (!currentValue.includes('.')) {
currentValue += '.';
display.value = currentValue;
}
}
// Rakamları ekleme
function appendNumber(num) {
if (isResultDisplayed) {
currentValue = num.toString();
isResultDisplayed = false;
} else {
currentValue += num.toString();
}
display.value = currentValue;
}
// Matematiksel işlemler
function calculate(operation) {
if (operation === '=') {
try {
// × işaretini * ile değiştir (çarpma işlemi için)
currentValue = currentValue.replace('×', '*').replace('÷', '/');
let result = eval(currentValue);
display.value = result;
currentValue = result.toString();
isResultDisplayed = true;
} catch (e) {
display.value = 'Hata!';
currentValue = '';
}
} else {
if (isResultDisplayed) {
isResultDisplayed = false;
}
currentValue += operation;
display.value = currentValue;
}
}
// Bilimsel fonksiyonlar
function calculateMathFunction(func) {
try {
let value = parseFloat(currentValue);
if (isNaN(value)) {
display.value = 'Hata!';
currentValue = '';
return;
}
switch (func) {
case 'sqrt':
if (value < 0) {
display.value = 'Hata!';
currentValue = '';
} else {
currentValue = Math.sqrt(value).toString();
display.value = currentValue;
}
break;
case 'square':
currentValue = (value * value).toString();
display.value = currentValue;
break;
case 'sin':
currentValue = Math.sin(value * Math.PI / 180).toString(); // derece
display.value = currentValue;
break;
case 'cos':
currentValue = Math.cos(value * Math.PI / 180).toString();
display.value = currentValue;
break;
case 'tan':
if (value % 90 === 0 && value % 180 !== 0) {
display.value = 'Tanımsız!';
currentValue = '';
} else {
currentValue = Math.tan(value * Math.PI / 180).toString();
display.value = currentValue;
}
break;
case 'log':
if (value <= 0) {
display.value = 'Hata!';
currentValue = '';
} else {
currentValue = Math.log10(value).toString();
display.value = currentValue;
}
break;
case 'pow':
currentValue += '**';
display.value = currentValue;
break;
case 'exp':
currentValue = Math.exp(value).toString();
display.value = currentValue;
break;
case 'pi':
currentValue += Math.PI.toString();
display.value = currentValue;
break;
default:
break;
}
isResultDisplayed = true;
} catch (e) {
display.value = 'Hata!';
currentValue = '';
}
}
</script>
</body>
</html>
Özellikler:
- Temel İşlemler: Toplama (
+), Çıkarma (-), Çarpma (×), Bölme (÷)
- Ekstra Fonksiyonlar:
C: Ekranı temizler.
DEL: Son karakteri siler.
%: Yüzde hesaplama yapar.
√: Karekök hesaplar.
x²: Sayının karesini alır.
sin, cos, tan: Trigonometrik hesaplar (derece cinsinden).
log: 10 tabanlı logaritma hesaplar.
x^y: Üs alma işlemi (** operatörü ile).
e^x: e üzeri x hesaplar (exp fonksiyonu).
π: Pi sayısını ekler.
- Hata Yakalama:
- Geçersiz işlemlerde (örn: negatif karekök, 0'a bölme) ekranda "Hata!" yazar.
- Tanımsız işlemlerde (örn: tan(90)) "Tanımsız!" uyarısı verir.
Nasıl Çalışır?
- Yukarıdaki tüm kodu kopyalayın.
- Bir metin düzenleyici açın (Notepad, VS Code gibi).
- Yeni bir dosya oluşturup
calculator.html adıyla kaydedin.
- Kodu yapıştırın ve dosyayı kaydedin.
- Tarayıcıda (Chrome, Firefox, Edge)
calculator.html dosyasını açın.
- Hesap makinesi arayüzü görünecek, işlem yapmaya başlayabilirsiniz!
Örnek Kullanımlar:
- Karekök Hesabı:
25 yazıp √ tuşuna basın, sonuç 5 gelir.
- Üs Alma:
2 yaz, x^y tuşuna bas, sonra 3 yaz ve = tuşuna bas (sonuç: 8).
- Trigonometri:
45 yaz, sin tuşuna bas (sonuç: 0.7071...).
- Logaritma:
100 yaz, log tuşuna bas (sonuç: 2).
Notlar:
eval() fonksiyonu basit işlemler için güvenli olsa da, çok ileri düzey projelerde güvenlik riski yaratabilir. Daha profesyonel projelerde "expression parser" kütüphaneleri tercih edilmelidir.
- Trigonometrik fonksiyonlar derece cinsinden hesaplanır (radyan değil).
Artık kullanıma hazır, bilimsel hesaplamalar yapabileceğiniz güzel bir hesap makineniz var! Kolay gelsin.