Web Calculators Based on HTML, JS and CSS

1 Introduction

EE308FZ Software Engineering Practice

This blog records the work and process of creating a calculator with a visual interface in the first assignment of EE308FZ software engineering practice.

In this project, our goal is to create a web version calculator that can perform basic addition, subtraction, multiplication, division, and clearing, as well as to complete advanced functions such as exponential and trigonometric functions.

Link to the finished project code:Link here

2 Information Table

The Link Your ClassEE308FZ Class
The Link of Requirement of This AssignmentSoftware Engineering Practice First Assignment
The Aim of This AssignmentMake an interface visual calculator
MU STU ID and FZU STU ID21126488_832101211

3 PSP Table

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning
• Estimate4530
Development
• Analysis3030
• Design Spec1520
• Design Review1520
• Coding Standard1515
• Design6090
• Coding180240
• Code Review6060
• Test6070
Reporting
• Test Repor2015
• Size Measurement1015
• Postmortem & Process Improvement Plan3030
Sum535635

4 Description of problem-solving ideas

In this project, the learning focus of calculator development should be on the development of visual interfaces, with the basic goal of completing functions including input numbers and addition, subtraction, multiplication, division, subtraction, as well as subsequent implementation of advanced functions.

HTML, CSS, and JS are the three fundamental technologies for building web pages and web applications. So for this project, I chose to build my Web visualization calculator by learning to use HTML, CSS, and JS.

5 Design and implementation proces

The flowchart is shown in the figure:
在这里插入图片描述

6 Code description

*HTML code

Use HTML for web page construction and content creation for calculators, including number buttons, operator buttons, display area boxes, etc.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Calculator in HTML CSS & JavaScript</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <input type="text" class="display" />

      <div class="buttons">
        <button class="operator" data-value="AC">AC</button>
        <button class="operator" data-value="DEL">DEL</button>
        <button class="operator" data-value="%">%</button>
        <button class="operator" data-value="/">/</button>

        <button data-value="7">7</button>
        <button data-value="8">8</button>
        <button data-value="9">9</button>
        <button class="operator" data-value="*">*</button>

        <button data-value="4">4</button>
        <button data-value="5">5</button>
        <button data-value="6">6</button>
        <button class="operator" data-value="-">-</button>

        <button data-value="1">1</button>
        <button data-value="2">2</button>
        <button data-value="3">3</button>
        <button class="operator" data-value="+">+</button>

        <button data-value="0">0</button>
        <button data-value="00">00</button>
        <button data-value=".">.</button>
        <button class="operator" data-value="=">=</button>
      </div>
    </div>

    <script src="script.js"></script>
  </body>
</html>

*CSS code

Use CSS to define the style of the page, such as content color, width, height, font size, padding, margins, etc.

@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap");
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #eee;
}
.container {
  position: relative;
  max-width: 300px;
  width: 100%;
  border-radius: 12px;
  padding: 10px 20px 20px;
  background: #ffffff;
  box-shadow: 0 5px 10px rgba(4, 4, 4, 0.05);
}
.display {
  height: 80px;
  width: 100%;
  outline: none;
  border: none;
  text-align: right;
  margin-bottom: 10px; 
  font-size: 25px;
  color: #000e1a;
  pointer-events: none;
}
.buttons {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(4, 1fr);
}
.buttons button {
  padding: 10px;
  border-radius: 6px;
  border: none;
  font-size: 20px;
  cursor: pointer;
  background-color: #cbe6f1;
}
.buttons button:active {
  transform: scale(0.99);
}
.operator {
  color: #2d98f6;
}

*JS code

Use JS to achieve interactive functions, such as handling number button clicks, operator button clicks, and so on, to complete the logical operation function of the computer.

const display = document.querySelector(".display");
const buttons = document.querySelectorAll("button");
const specialChars = ["%", "*", "/", "-", "+", "="];
let output = "";

//Define function to calculate based on button clicked.
const calculate = (btnValue) => {
  display.focus();
  if (btnValue === "=" && output !== "") {
    output = eval(output.replace("%", "/100"));
  } else if (btnValue === "AC") {
    output = "";
  } else if (btnValue === "DEL") {
    output = output.toString().slice(0, -1);
  } else {
    if (output === "" && specialChars.includes(btnValue)) return;
    output += btnValue;
  }

  display.value = output;
};

buttons.forEach((button) => {
  button.addEventListener("click", (e) => calculate(e.target.dataset.value));
});

7 Results Display

在这里插入图片描述

8 Summary

In this project, I first started from the requirement analysis, determined the way to complete and selected tools, learned the basic knowledge, and constantly solved problems to improve my code. For the first time, I learned the basic techniques of HTML, CSS and JS to build web pages and web applications, learned front-end knowledge, and applied it to visual development.

The whole task not only made me familiar with the knowledge of project design and development, but also improved my ability to solve problems. At the same time, writing a blog for the first time and participating in the programming community also exercised my ability to search, learn and summarize.

In the future, I will continue to optimize and upgrade the functionality of this basic calculator.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值