Web Calculator


Catalogue


The Link My Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link My Classhttps://bbs.csdn.net/topics/617332156
The Aim of This AssignmentCreate a calculator with a visual interface and write a blog to record your work content and process.
MU STU ID and FZU STU ID21124558 and 832102127

1. Description

Task

· Create a calculator with a visual interface.
· Write a blog to record your work content and process.

Program Requirements

· Basic requirement: Implement addition, subtraction, multiplication, division, and clear functions.
· Advanced requirement: Implement functionality for exponentiation, trigonometric functions, and more.
· Before tackling the more complex requirements, you must fulfill the basic requirements.

Coding Requirements

· Use any of C++, Java, Python or other language you prefer to complete the work.
· Develop appropriate code specifications and conduct tests.
· Use Git for version control.

Blog Requirements

· Give the PSP form for this work.
· Description of problem-solving ideas. This is the process of how to think and how to find information after getting the title at the beginning.
· Design and implementation process. The design includes how the code is organized and the flow chart of the key functions.
· Code description. Show the key code of the project and explain the idea.
· Displaying result functions with screenshots ((or gifs) and text descriptions.
· Summarize this assignment.

2. Work Procedure

· Create a GitHub repository for the job.
· Write the program and commit actively.
· Iteratively update and optimize the code.
· Complete your work and write a blog.

3. Personal Software Process (PSP) Form

Personal Software Process StagesEstimated Time(/minutes)Actual Time(/minutes)
Planning6050
Estimate5050
Analysis4040
Design Specification6050
Design Review5050
Coding Standard4050
Design4050
Coding5050
Code Review3040
Test5050
Reporting6050
Test Report5050
Size Measurement3040
Postmortem & Process Improvement Plan3050
Sum640670

4. Problem-solving Ideas

In this assignment, we are asked to make a simple web calculator. This assignment needs to design the front end of the calculator to visualize it. Here are three ways to achieve it:

  • HTML: a language used to describe the web page, responsible for the architecture of the web page

  • CSS: a language used to lay out color, text content of the web page, responsible for the style of the web page, beautification

  • JS: a language that runs on the client side and is responsible for the behavior of web pages

The way I use to solve the problem is to use programming software to write HTML. At last, successfully realize the functions of a simple web calculator.

5. Design and Implementation Process

Design process

A basic calculator should include:
Numbers: integers from 0 to 9
Operators: the basic four operations addition, subtraction, multiplication and division
Function: empty, backspace, parentheses, equal functions

  • It should be noticed that the Advanced requirement: Trigonometric function and exponentiation are also performed in my project.

Implementation Process

  1. Adjust the size of the calculator panel and the size of the button. Select text styles to modify.
  2. If a number (0,1,2,3,4,5,6,7,8,9) and “(”, “)”, “.” is clicked, it will be displayed in the input text box above.
  3. If an operator button (such as +, -, *, /, sin, cos, tan, e^x) is clicked, it will perform related operations and output results.
  4. If the key “C” is clicked, the string content is cleared, and the output box is also cleared.
  5. If you click “=”, the result of the calculation is displayed in the output box and the content is cleared.
Design and modify the website
Operate on the calculater
Display the result

6. Code Description

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
    <style>
        .title-button {
            width: 250px; /* 设置按钮宽度 */
            height: 30px; /* 设置按钮高度 */
            font-size: 20px; /* 设置按钮文本字体大小 */
            margin: 5px; /* 设置按钮之间的间距 */
        }
        /* 定义按钮的样式 */
        .calculator-button {
            width: 60px; /* 设置按钮宽度 */
            height: 60px; /* 设置按钮高度 */
            font-size: 20px; /* 设置按钮文本字体大小 */
            margin: 5px; /* 设置按钮之间的间距 */
        }
         .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh; /* 100%视口高度 */
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Calculator</h1>
        <form name="Calculator">
            <input type="text" class="title-button"name="display" id="display" disabled>
            <br>
            <input type="button"class="calculator-button" value="sin" onclick="fSin()">
            <input type="button" class="calculator-button" value="cos" onclick="fCos()">
            <input type="button" class="calculator-button" value="tan" onclick="fTan()">
            <input type="button" class="calculator-button" value="exp" onclick="fExp()">  
            <br>
            <input type="button" class="calculator-button" value="7" onclick="addToDisplay('7')">
            <input type="button" class="calculator-button" value="8" onclick="addToDisplay('8')">
            <input type="button" class="calculator-button" value="9" onclick="addToDisplay('9')">
            <input type="button" class="calculator-button" value="/" onclick="addToDisplay('/')">
            <br>
            <input type="button" class="calculator-button" value="4" onclick="addToDisplay('4')">
            <input type="button" class="calculator-button" value="5" onclick="addToDisplay('5')">
            <input type="button" class="calculator-button" value="6" onclick="addToDisplay('6')">
            <input type="button" class="calculator-button" value="*" onclick="addToDisplay('*')">
            <br>
            <input type="button" class="calculator-button" value="1" onclick="addToDisplay('1')">
            <input type="button" class="calculator-button" value="2" onclick="addToDisplay('2')">
            <input type="button" class="calculator-button" value="3" onclick="addToDisplay('3')">
            <input type="button" class="calculator-button" value="-" onclick="addToDisplay('-')">
            <br>
            <input type="button" class="calculator-button" value="C" onclick="clearDisplay()">
            <input type="button" class="calculator-button" value="0" onclick="addToDisplay('0')">
            <input type="button" class="calculator-button" value="=" onclick="calculate()">
            <input type="button" class="calculator-button" value="+" onclick="addToDisplay('+')">
        </form>

        <script>
            function addToDisplay(value) {
                document.Calculator.display.value += value;
            }

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

           function fSin() {
                
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                const result = Math.sin(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
           function fCos() {
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                const result = Math.cos(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
            function fTan() {
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                if (input == 90|| input == 270||input == -90||input == -270){
	                document.Calculator.display.value = "Error!"
	                return
                }
                const result = Math.tan(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
           function fExp() {
                const input = document.Calculator.display.value;
                const exponent= parseFloat(input);
                const result = Math.exp(exponent); 
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
            function calculate() {
                try {
                    const result = eval(document.Calculator.display.value);
 	    document.Calculator.display.value = result;
	    if (isNaN(result) || !isFinite(result)) {
                        throw new Error("error");
                }
                }catch(err) {
                    document.Calculator.display.value = 'Error!';
                }
            }
        </script>
    </div>
</body>
</html>

First, I use < head > part to define the style and the size of the interface. In the < body > part, also define the button and connect them to the function constructed blow in logic. At the same time, make it possible to display in the screen.

Then for function part, I just import the Math () function to achieve the exponentiation and trigonometric operations. For basic calculation like plus or minus, it can be achieved by its original library.

7. Result Displaying and Text Description

Demo for calculator

In the demo above, I test each key and do some related operations. Simple computing tasks like plus or minus are achieved easily. in addition, I just consider some other special cases such as numbers over zero and tan45degree should be set to “Error!”. Blessly, all the functions do well in each calculation. By Importing the Math() library, successfully solve out the trigonometric functions and exponential functions. In the following works, I can pay more attention to the more complex computation.

8. Summary

To achieve a simple version of the web calculator, I just apply the HTML codes to construct the website. By adjust the interface to be more twinkle, I modify the head of the html codes like size and distance between them. Using CSDN to blog in order to record the development, I learn the complete progresses to create and achieve my own project.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值