A Simple Web Calculator

Ⅰ. Introduction

In this blog, I will introduce the process of making a web calculator in HTML, CSS and JavaScript.

The calculator can implement functions including addition, subtraction, multiplication, division, clear, delete, exponentiation, trigonometric, logarithmic and power functions.

The code of github is: the git hub link

Ⅱ. Author Information Table

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This Assignmentmake a web calculator
MU STU ID and FZU STU ID<21125953_832101113>

Ⅲ. PSP Form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate3035
Development
• Analysis4040
• Design Spec1010
• Design Review1010
• Coding Standard1010
• Design3030
• Coding150190
• Code Review4030
• Test6070
Reporting
• Test Repor5080
• Size Measurement1010
• Postmortem & Process Improvement Plan1515
Sum455530

Ⅳ. Description of Problem-solving Ideas

  • Firstly, this project requires a visual interface and implements the corresponding interactive functions.
  • So I naturally considered about HTML, CSS and Javascript because of the aim.
  • Then I search some references and code standards from the Internet to help my work.

Ⅴ. Design and Implementation Process

1. How to build the code framework

Step 1: Create the HTML Structure

  • Begin by creating an HTML file to establish the structure of your calculator.
  • Use HTML elements to create numeric buttons, operator buttons (+', ‘-’, ‘*’, ‘/’, etc.), and a display screen.
  • Organize these elements to form a clear and organized layout for your calculator. We can use classes and IDs to target specific elements in our JavaScript code later.

Step 2: Enhance the Appearance with CSS

  • In a separate CSS file, we can enhance the visual design and layout of our calculator.
  • Apply styles to various elements to make them more visually appealing. For example, we can set background colors, fonts, padding, and borders for buttons to improve their appearance.
  • Use CSS to ensure that our calculator looks attractive and user-friendly. We can also create responsive designs to adapt to different screen sizes.

Step 3: Implement Functionality with JavaScript

  • JavaScript is used to add interactivity and functionality to our calculator.
  • Set up event listeners for button clicks. When a numeric or operator button is clicked, we’ll capture that event and handle it in our JavaScript code.
  • Implement logic for accepting user input (appending digits and operators to the input string), performing calculations, and displaying results on the screen.
  • JavaScript will enable our calculator to respond to user actions, perform calculations, and display the outcomes in real-time.

2. The flow chart of the process

在这里插入图片描述

Ⅵ. Code Description

1. Code for HTML

Mainly for creating numeric buttons, operator buttons (+', ‘-’, ‘*’, ‘/’, etc.), and a display screen.

<!DOCTYPE html>
<html>
<head>
    <title>web calculator</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div id="calculator">
        <input type="text" id="display" readonly>
        <div id="buttons">
            <button>(</button>
            <button>)</button>
            <button>C</button>
            <button>DEL</button>
            <button>1</button>
            <button>2</button>
            <button>3</button>
            <button>+</button>
            <button>4</button>
            <button>5</button>
            <button>6</button>
            <button>-</button>
            <button>7</button>
            <button>8</button>
            <button>9</button>
            <button>*</button>
            <button>.</button>
            <button>0</button>
            <button>=</button>
            <button>/</button>
        </div>
        <div id="advanced-buttons">
            <button>e^x</button>
            <button>ln</button>
            <button>x^y</button>
            <button>sin(°)</button>
            <button>cos(°)</button>
        </div>
    </div>
<script src="calculator.js"></script>
</body>
</html>

2. Code for CSS

Mainly for setting background colors, fonts, padding, and borders for buttons to improve the appearance.

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
  background-color: #fff;
  font-family: Arial, sans-serif;
}

#calculator {
  width: 530px;
}

#display {
  width: 100%;
  height: 80px;
  text-align: right;
  margin: 0;
  padding-right: 10px;
  box-sizing: border-box;
  border: none;
  background-color: #303030;
  color: #fff;
  font-size: 50px
}

#buttons, #advanced-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
}

#buttons button, #advanced-buttons button {
  height: 80px;
  border: none;
  border-top: 1px solid #303030;
  background-color: #505050;
  color: #00ff90;
  font-size: 30px;
  outline: none;
}

#buttons button:active, #advanced-buttons button:active {
  background-color: #505050;
}

3. Code for JavaScript

Mainly for interacting and implementing functions.

var display = document.getElementById('display');

function appendToScreen(btnVal) {
  display.value += btnVal;
}

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

function deleteCharacter() {
  if (display.value) {
    display.value = display.value.slice(0, -1);
  }
}

// Symbol button click event
document.getElementById("buttons").addEventListener("click", function(e) {
  if(e.target.matches("button")) {
    var btnVal = e.target.innerText;
    switch(btnVal) {
      case '=':
        try {
          var result = new Function('return ' + display.value)();
          display.value = result.toString();
        } catch(err) {
          display.value = "错误";
        }
        break;
      case 'C':
        clearScreen();
        break;
      case 'DEL':
        deleteCharacter();
        break;
      default:
        appendToScreen(btnVal);
    }
  }
});

//Advanced computing button click event
document.getElementById("advanced-buttons").addEventListener("click", function(e) {
  if(e.target.matches("button")) {
    var op = e.target.innerText;
    var curVal = eval(display.value);
    switch(op) {
      case 'e^x':
        display.value = Math.exp(curVal).toString();
        break;
      case 'ln':
        display.value = Math.log(curVal).toString();
        break;
      case 'x^y':
        display.value = 'Math.pow(' + display.value + ',y';   // y is the desired variable
        break;
      case 'sin':
            var rad = curVal * (Math.PI / 180);   // Convert angles to radians
        display.value = Math.sin(rad).toString();
        break;
      case 'cos':
            var rad = curVal * (Math.PI / 180);   // Convert angles to radians
        display.value = Math.cos(rad).toString();
        break;
    }
  }
});

Ⅶ. Displaying Result Functions

Trigonometric functions are measured in degrees (°).
在这里插入图片描述

Ⅷ. Summarize

Creating a calculator with a visual interface is an engaging and challenging project. It combines fundamental front-end development skills and allows me to build a practical tool while enhancing my web development skills.

Honestly, the calculator is not perfect and probably remains some flaws, but I have learned basic operations of HTML, CSS and JavaScript and gradually got familiar with them during the project. Also, the project encouraged me to make some plans and designs, which cultivated my abilities in these aspects.

In summary, I enjoy the process of implementing the functions of the calculator and try to improve this calculator in the future.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值