Web calculator based on c language

contents

1.Introduction

2.Basic Information 

3.PSP Table

4.Ideas before Launching the Project

5.Design and Implementation Process

6.Code Description

(1) Set interface layout:

(2)Transmission function:

(3) Set the function of clearing, deleting and calculating results:

(4) Set each key name:

(5)Complete code:

(6)Displaying Result

           7.Summary


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)icon-default.png?t=N7T8https://github.com/cleversis/myrep

2.Basic Information 

The link of my classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface
MU STU ID and FZU STU ID21126984_832101312

3.PSP Table

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning3050
 Estimate3040
Development400500
 Analysis3060
Design Spec1530
Design Review2540
Coding Standard1030
Design6080
Coding240300
Code Review3020
Test1010
Reporting90120
Postmortem & Process Improvement Plan1525
Sum9851305

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.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值