From b2ec05610650347cd87a49ac12907f55a5bb0ef0 Mon Sep 17 00:00:00 2001 From: Aastha Jain <140010647+Aashi19234@users.noreply.github.com> Date: Wed, 7 Feb 2024 12:16:12 +0530 Subject: [PATCH] Added Complex Number Calculator (#562) --- .../Complex-Number-Calculator/README.md | 14 ++++ .../Complex-Number-Calculator/index.html | 27 ++++++ .../Complex-Number-Calculator/script.js | 82 +++++++++++++++++++ .../Complex-Number-Calculator/style.css | 62 ++++++++++++++ index.html | 14 ++++ 5 files changed, 199 insertions(+) create mode 100644 Calculators/Complex-Number-Calculator/README.md create mode 100644 Calculators/Complex-Number-Calculator/index.html create mode 100644 Calculators/Complex-Number-Calculator/script.js create mode 100644 Calculators/Complex-Number-Calculator/style.css diff --git a/Calculators/Complex-Number-Calculator/README.md b/Calculators/Complex-Number-Calculator/README.md new file mode 100644 index 000000000..96180cd04 --- /dev/null +++ b/Calculators/Complex-Number-Calculator/README.md @@ -0,0 +1,14 @@ +#

Complex Number Calculator

+ +## Description :- + +It is a calculator which handles complex numbers and their operations, such as addition, subtraction, multiplication, and division, and provides both rectangular and polar forms of the result. + +## Tech Stacks:- + +- HTML +- CSS +- JavaScript + +### Screenshots:- +![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/5276968a-c78f-4028-9fc6-c9838a72cf32) diff --git a/Calculators/Complex-Number-Calculator/index.html b/Calculators/Complex-Number-Calculator/index.html new file mode 100644 index 000000000..6a6d4fd7e --- /dev/null +++ b/Calculators/Complex-Number-Calculator/index.html @@ -0,0 +1,27 @@ + + + + + + Complex Number Calculator + + + +
+

Complex Number Calculator

+ + + + + + + + + +
+
+ + + + + diff --git a/Calculators/Complex-Number-Calculator/script.js b/Calculators/Complex-Number-Calculator/script.js new file mode 100644 index 000000000..82e9b21be --- /dev/null +++ b/Calculators/Complex-Number-Calculator/script.js @@ -0,0 +1,82 @@ +function add() { + if (validateInput()) { + var real1 = parseFloat(document.getElementById('real1').value); + var imaginary1 = parseFloat(document.getElementById('imaginary1').value); + var real2 = parseFloat(document.getElementById('real2').value); + var imaginary2 = parseFloat(document.getElementById('imaginary2').value); + var resultReal = real1 + real2; + var resultImaginary = imaginary1 + imaginary2; + displayResult(resultReal, resultImaginary); + } + } + + function subtract() { + if (validateInput()) { + var real1 = parseFloat(document.getElementById('real1').value); + var imaginary1 = parseFloat(document.getElementById('imaginary1').value); + var real2 = parseFloat(document.getElementById('real2').value); + var imaginary2 = parseFloat(document.getElementById('imaginary2').value); + var resultReal = real1 - real2; + var resultImaginary = imaginary1 - imaginary2; + displayResult(resultReal, resultImaginary); + } + } + + function multiply() { + if (validateInput()) { + var real1 = parseFloat(document.getElementById('real1').value); + var imaginary1 = parseFloat(document.getElementById('imaginary1').value); + var real2 = parseFloat(document.getElementById('real2').value); + var imaginary2 = parseFloat(document.getElementById('imaginary2').value); + var resultReal = real1 * real2 - imaginary1 * imaginary2; + var resultImaginary = real1 * imaginary2 + real2 * imaginary1; + displayResult(resultReal, resultImaginary); + } + } + + function divide() { + if (validateInput()) { + var real1 = parseFloat(document.getElementById('real1').value); + var imaginary1 = parseFloat(document.getElementById('imaginary1').value); + var real2 = parseFloat(document.getElementById('real2').value); + var imaginary2 = parseFloat(document.getElementById('imaginary2').value); + if (real2 === 0 && imaginary2 === 0) { + document.getElementById('result').innerHTML = 'Cannot divide by zero'; + return; + } + var denominator = real2 * real2 + imaginary2 * imaginary2; + var resultReal = (real1 * real2 + imaginary1 * imaginary2) / denominator; + var resultImaginary = (imaginary1 * real2 - real1 * imaginary2) / denominator; + displayResult(resultReal, resultImaginary); + } + } + + function reset() { + document.getElementById('real1').value = ''; + document.getElementById('imaginary1').value = ''; + document.getElementById('real2').value = ''; + document.getElementById('imaginary2').value = ''; + document.getElementById('result').innerHTML = ''; + } + + function validateInput() { + var real1 = document.getElementById('real1').value; + var imaginary1 = document.getElementById('imaginary1').value; + var real2 = document.getElementById('real2').value; + var imaginary2 = document.getElementById('imaginary2').value; + if (real1 === '' || imaginary1 === '' || real2 === '' || imaginary2 === '') { + document.getElementById('result').innerHTML = 'Enter a valid value'; + return false; + } + return true; + } + + function displayResult(real, imaginary) { + var result = document.getElementById('result'); + var rectForm = 'Rectangular Form: ' + real + ' + ' + imaginary + 'i
'; + var magnitude = Math.sqrt(real * real + imaginary * imaginary); + var angle = Math.atan2(imaginary, real); + var polarForm = 'Polar Form: ' + magnitude.toFixed(2) + ' * (cos(' + angle.toFixed(2) + ') + i * sin(' + angle.toFixed(2) + '))'; + result.innerHTML = rectForm + polarForm; + } + diff --git a/Calculators/Complex-Number-Calculator/style.css b/Calculators/Complex-Number-Calculator/style.css new file mode 100644 index 000000000..df5739d41 --- /dev/null +++ b/Calculators/Complex-Number-Calculator/style.css @@ -0,0 +1,62 @@ +body { + font-family: Arial, sans-serif; + background: linear-gradient(135deg, #ff6e7f, #bfe9ff); + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + margin: 0; + } + + .container { + max-width: 400px; + padding: 20px; + border-radius: 20px; + background-color: rgba(255, 255, 255, 0.3); + backdrop-filter: blur(10px); + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1); + } + + input[type="number"] { + width: calc(100% - 22px); + padding: 10px; + margin: 5px 0; + box-sizing: border-box; + border: 1px solid #ccc; + border-radius: 5px; + } + + button { + width: 100%; + padding: 10px; + background-color: #4CAF50; + color: white; + border: none; + border-radius: 5px; + cursor: pointer; + margin-top: 10px; + transition: background-color 0.3s ease; + } + + button:hover { + background-color: #45a049; + } + + button:active { + background-color: #3e8e41; + } + + h1 { + color: #fff; + text-align: center; + margin-bottom: 20px; + } + + #result { + margin-top: 20px; + padding: 15px; + background-color: rgba(255, 255, 255, 0.7); + border-radius: 5px; + } + + diff --git a/index.html b/index.html index b8281dc47..834596634 100644 --- a/index.html +++ b/index.html @@ -1695,6 +1695,20 @@

Calculates the time required to finish reading a book.

+
+
+

Complex Number Calculator

+

Perfoms mathematical operations on complex numbers.

+ +
+