How To Construct A Visual Calculator on Web

Introduction

        This blog is written in order to help those who have just started learning sotfware and this blog is a piece of homework. In this blog I will show you how to build a web calculator using HTML, CSS and JavaScript. 

        You may need this link to find my Github.JY-Chii/Assignment: Tasks at college while studying Software Engineering (github.com)

Information table

Link of Classhttps://bbs.csdn.net/forums/ssynkqtd-04
Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
Aim of This Assignmentcalculator with a visual interface
MU STU ID and FZU STU ID21126119_832101108

PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning 
•Estimate3030
Development 
•Analysis 1520
•Design Spec1010
•Design Review1010
•Coding Standard1010
•Design4530
•Coding90150
•Code Review4545
•Test1515
Reporting
•Test Report00
•Size Measurement 00
•Postmortem & Process Improvement Plan60120
Sum330440

Problem-Solving Ideas

        The problem-solving approach for this project is to create a functional calculator with basic arithmetic operations and advanced features such as trigonometric functions. The code is organized into modular functions to handle user input, calculations, and error handling. This approach ensures a clear and maintainable code structure while meeting the project's requirements.

        To solve this problem, I choose to use HTML, CSS as well as JS to construct my web calculator.

Design and Implementation Process

Design

  1. User Interface: The user interface was designed to be visually appealing and user-friendly. A header with the title "JY Chii's Calculator" was added to give a clear identity to the calculator.

  2. Button Layout: Buttons were organized into a grid layout, utilizing CSS Grid, to create a structured and organized look. The grid layout enhances the visual aspect of the calculator and makes it easy for users to find and click the buttons.

  3. Functionality: The design ensured that buttons for basic arithmetic operations (+, -, *, /) and functions like trigonometric calculations (sin, cos, tan) were clearly labeled and easily accessible. A "C" button was added for clearing the input field.

  4. Degrees to Radians Conversion: To provide additional functionality, the design included a "deg" button. When this button is pressed, it appends "* Math.PI / 180" to the input, allowing users to input angles in degrees and have them automatically converted to radians for trigonometric calculations.

Implementation

  1. User Input Handling: JavaScript functions were implemented to handle user inputs. When buttons are clicked, the corresponding values or operations are appended to the input field.

  2. Degrees to Radians Conversion: The "deg" button appends "* Math.PI / 180" to the input field, enabling automatic conversion from degrees to radians.

  3. Calculation: The "calculate" button evaluates the mathematical expression in the input field using the eval() function. If the input contains trigonometric functions, they are evaluated with degrees converted to radians.

  4. Error Handling: The code includes a try-catch block to handle potential errors, ensuring that the calculator displays "Error" when an invalid expression is entered.

  5. Clear Function: The "C" button clears the input field.

Code description

        Firstly, use HTML to construct the basic project, leaving the CSS and JS part alone and defining the buttons and doing some setup.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>CalBeta</title>

    <link rel="stylesheet" href="./cal.css">
    <style>
        </style>

</head>

<body>

<div class="calculator">

<header class="cal-header">JY Chii's Calculator</header>

<input type="text" class="display" disabled>

<button onclick="getNumber('(')">(</button>
<button onclick="getNumber(')')">)</button>
<button onclick="getNumber('%')">%</button>
<button onclick="clearDisplay()">C</button> 

<button onclick="getNumber('Math.sin(')">sin</button>  
<button onclick="getNumber('Math.cos(')">cos</button>
<button onclick="getNumber('Math.tan(')">tan</button>
<button onclick="toRadian()">deg</button>

<button onclick="getNumber('7')">7</button>
<button onclick="getNumber('8')">8</button>
<button onclick="getNumber('9')">9</button>
<button onclick="getOperator('/')">/</button>

<button onclick="getNumber('4')">4</button>
<button onclick="getNumber('5')">5</button>
<button onclick="getNumber('6')">6</button>
<button onclick="getOperator('*')">*</button>

<button onclick="getNumber('1')">1</button>  
<button onclick="getNumber('2')">2</button>
<button onclick="getNumber('3')">3</button>
<button onclick="getOperator('-')">-</button>

<button onclick="getNumber('0')">0</button> 
<button onclick="getNumber('.')">.</button>
<button onclick="calculate()">=</button>
<button onclick="getOperator('+')">+</button>



</div>

<script src="./calBeta.js"></script>


</body>
</html>

        Then finish the JSS part, which is about the calculation function part.

let display = document.querySelector('.display');

function getNumber(num) {
  display.value += num;
}

function getOperator(op) {
  display.value += op;
}

function toRadian() {
  if(display.value.includes('deg')) {
    display.value = display.value.replace('deg', '* Math.PI / 180');
  } else {
    display.value += '* Math.PI / 180';
  }
}

function calculate() {
  if(display.value.includes('deg')) {
    toRadian();
  }
  
  try {
    display.value = eval(display.value).toFixed(2);
  } catch {
    display.value = 'Error';
  }
}

function clearDisplay() {
  display.value = '';
} 

        Finally complete the CSS part.


.calculator {
    width: 400px;
    margin: 0 auto;
    padding: 20px;
    background: #ddd;
    border-radius: 10px;  
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-gap: 5px;
  }
  
  .display {
    grid-column: span 4;
    height: 50px;
    font-size: 20px;
    margin-bottom: 10px;
    padding: 5px;
    border: none;
    background: #fff;
  }
  
  button {
    width: 100%;
    height: 40px;
    font-size: 14px;
    margin: 5px;
    border-radius: 5px;
    border: none;
    background: orange;
    cursor: pointer;
  }

        Remember: There lies a problem in displaying that when the user pressed any button containing trigonometric functions like sin, the screen will show "math.sin" instead of "sin", but it will do no harm to the calculation. That's the reason I named some files with "beta".

Result Displaying

Summary

        This project aimed to create a functional calculator with a user-friendly interface, using HTML, CSS, and JavaScript. The primary goal was to implement basic arithmetic operations and advanced features like trigonometric functions. It followed a structured development approach, using GitHub for version control.

        The calculator's interface features numerical input buttons, parentheses, percentage calculations, and a clear button. Advanced functions include sine, cosine, tangent, and degree-to-radian conversion.

        The JavaScript code handled user input, operators, trigonometric calculations, and error management. It also enabled degree-to-radian conversion before trigonometric operations. This project provides a practical and visually appealing calculator for mathematical tasks.

        Anyway, thanks for rating up this article if it really did some help to you.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值