contents
4.Ideas before Launching the Project
5.Design and Implementation Process
(3) Set the function of clearing, deleting and calculating results:
1.Introduction
This project uses the LIve Server module in Visual Studio to realize all the functions of a basic calculator in the local end of edge web page based on c language.The Live Server is a local development server that can reload a dynamic or static page.
This calculator can realize the basic function of addition, subtraction, multiplication and division.
Link to the project code:cleversis/myrep (github.com)https://github.com/cleversis/myrep
2.Basic Information
The link of my class | https://bbs.csdn.net/forums/ssynkqtd-04 |
The Link of Requirement of This Assignment | https://bbs.csdn.net/topics/617332156 |
The Aim of This Assignment | Create a calculator with a visual interface |
MU STU ID and FZU STU ID | 21126984_832101312 |
3.PSP Table
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 30 | 50 |
Estimate | 30 | 40 |
Development | 400 | 500 |
Analysis | 30 | 60 |
Design Spec | 15 | 30 |
Design Review | 25 | 40 |
Coding Standard | 10 | 30 |
Design | 60 | 80 |
Coding | 240 | 300 |
Code Review | 30 | 20 |
Test | 10 | 10 |
Reporting | 90 | 120 |
Postmortem & Process Improvement Plan | 15 | 25 |
Sum | 985 | 1305 |
4.Ideas before Launching the Project
Since this project is a visual calculator, it is only necessary to write the running logic of the calculator and design the interface size, and there is no need to involve the development of the server and other data interaction content.
After referring to a large number of materials in CSDN and Baidu, I decided to implement the front end of the calculator through the local web page and conduct the programming of this project based on c language.
5.Design and Implementation Process
(1) First of all, the size of each button on the interface is designed to a reasonable size.
(2) We set up a variable that can directly receive the combination of operations from the front-end input, and complete the operation under the c language's own operation logic through the idea of string to mathematical operations.
(3) Write a result display function to display the result of the operation on the interface.Write several function keys at the same time, such as zero key, delete key and so on to achieve the basic functions of the calculator.
6.Code Description
(1) Set interface layout:
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>计算器</title>
<style>
#top {
width: 450px;
height: 50px;
margin: auto;
background-color: #4278bb;
border-radius: 10px 10px 0px 0px;
}
#calc-title {
font-size: 22px;
color: white;
float: left;
margin: 10px 10px 0px 200px;
}
#result {
width: 445px;
height: 60px;
margin: auto;
font-size: 40px;
background-color: white;
border: solid 3px #4278bb;
}
#button {
width: 450px;
height: 422px;
background-color: #4278bb;
margin: auto;
}
#button div {
width: 108px;
height: 80px;
float: left;
background-color: #7ecbff;
margin: 2px;
line-height: 80px;
text-align: center;
font-size: 26px;
}
#button div:hover {
background-color: rgb(48, 149, 203);
}
(2)Transmission function:
Write a transfer function that transfers the operators entered into the front end and the numbers to the variables stored by the program.
</style>
<script type="text/javascript">
function clickNumber(number) {
var result = document.getElementById("result");
result.innerHTML = result.innerHTML + number;
}
function clickOperator(operator) {
var result = document.getElementById("result");
var string = result.innerHTML;
var len = string.length;
var last = string.charAt(len - 1);
if (last == "+" || last == "-" || last == "*" || last == "/" || last == "%") {
var temp = string.substr(0, len - 1) + operator;
result.innerHTML = temp;
} else {
result.innerHTML += operator;
}
}
function clickPonit() {
var result = document.getElementById("result");
var string = result.innerHTML;
var len = string.length;
var last = string.charAt(len - 1);
if (last !== ".") {
result.innerHTML = result.innerHTML + ".";
} else {
result.innerHTML = result.innerHTML;
}
}
(3) Set the function of clearing, deleting and calculating results:
function clearresult() {
var result = document.getElementById("result");
result.innerHTML = "";
}
function del() {
var result = document.getElementById("result");
var string = result.innerHTML;
if (string.length > 0) {
result.innerHTML = string.slice(0, -1);
}
}
function doCalc() {
var result = document.getElementById("result");
var expression = result.innerHTML;
result.innerHTML = eval(expression);
}
(4) Set each key name:
<body>
<div id="top">
<div id="calc-title">calculator</div>
</div>
<div id="result">
</div>
<div id="button">
<div onclick="clearresult()">AC</div>
<div onclick="clickOperator('/')">÷</div>
<div onclick="clickOperator('*')">x</div>
<div onclick="del()">delate</div>
<div onclick="clickNumber(7)">7</div>
<div onclick="clickNumber(8)">8</div>
<div onclick="clickNumber(9)">9</div>
<div onclick="clickOperator('%')">%</div>
<div onclick="clickNumber(4)">4</div>
<div onclick="clickNumber(5)">5</div>
<div onclick="clickNumber(6)">6</div>
<div onclick="clickOperator('-')">-</div>
<div onclick="clickNumber(1)">1</div>
<div onclick="clickNumber(2)">2</div>
<div onclick="clickNumber(3)">3</div>
<div onclick="clickOperator('+')">+</div>
<div></div>
<div onclick="clickNumber(0)">0</div>
<div onclick="clickPonit()">.</div>
<div onclick="doCalc()">=</div>
</div>
</body>
(5)Complete code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#top {
width: 450px;
height: 50px;
margin: auto;
background-color: #4278bb;
border-radius: 10px 10px 0px 0px;
}
#calc-title {
font-size: 22px;
color: white;
float: left;
margin: 10px 10px 0px 200px;
}
#result {
width: 445px;
height: 60px;
margin: auto;
font-size: 40px;
background-color: white;
border: solid 3px #4278bb;
}
#button {
width: 450px;
height: 422px;
background-color: #4278bb;
margin: auto;
}
#button div {
width: 108px;
height: 80px;
float: left;
background-color: #7ecbff;
margin: 2px;
line-height: 80px;
text-align: center;
font-size: 26px;
}
#button div:hover {
background-color: rgb(48, 149, 203);
}
</style>
<script type="text/javascript">
function clickNumber(number) {
var result = document.getElementById("result");
result.innerHTML = result.innerHTML + number;
}
function clickOperator(operator) {
var result = document.getElementById("result");
var string = result.innerHTML;
var len = string.length;
var last = string.charAt(len - 1);
if (last == "+" || last == "-" || last == "*" || last == "/" || last == "%") {
var temp = string.substr(0, len - 1) + operator;
result.innerHTML = temp;
} else {
result.innerHTML += operator;
}
}
function clickPonit() {
var result = document.getElementById("result");
var string = result.innerHTML;
var len = string.length;
var last = string.charAt(len - 1);
if (last !== ".") {
result.innerHTML = result.innerHTML + ".";
} else {
result.innerHTML = result.innerHTML;
}
}
function clearresult() {
var result = document.getElementById("result");
result.innerHTML = "";
}
function del() {
var result = document.getElementById("result");
var string = result.innerHTML;
if (string.length > 0) {
result.innerHTML = string.slice(0, -1);
}
}
function doCalc() {
var result = document.getElementById("result");
var expression = result.innerHTML;
result.innerHTML = eval(expression);
}
</script>
</head>
<body>
<div id="top">
<div id="calc-title">calculator</div>
</div>
<div id="result">
</div>
<div id="button">
<div onclick="clearresult()">AC</div>
<div onclick="clickOperator('/')">÷</div>
<div onclick="clickOperator('*')">x</div>
<div onclick="del()">delate</div>
<div onclick="clickNumber(7)">7</div>
<div onclick="clickNumber(8)">8</div>
<div onclick="clickNumber(9)">9</div>
<div onclick="clickOperator('%')">%</div>
<div onclick="clickNumber(4)">4</div>
<div onclick="clickNumber(5)">5</div>
<div onclick="clickNumber(6)">6</div>
<div onclick="clickOperator('-')">-</div>
<div onclick="clickNumber(1)">1</div>
<div onclick="clickNumber(2)">2</div>
<div onclick="clickNumber(3)">3</div>
<div onclick="clickOperator('+')">+</div>
<div></div>
<div onclick="clickNumber(0)">0</div>
<div onclick="clickPonit()">.</div>
<div onclick="doCalc()">=</div>
</div>
</body>
</html>
(6)Displaying Result
7.Summary
Through the design of this simple visual calculator, I initially learned the specific process of how to make a software front end and back end, and knew some basic knowledge needed for software development. However, I still have some shortcomings. For example, I can design a scientific calculator, including more functions, which can be gradually improved in the subsequent learning process. I hope that on the basis of this study, I can have more development experience in software production and gain something in the process of learning this course.