python based web visual calculator

本文介绍了如何将视觉计算器作为软件工程作业进行开发,包括需求分析、界面设计、功能实现(如加减乘除等)、使用Flask框架编写代码以及存在的缺点和未来改进期望。开发者分享了设计过程和使用搜索引擎、社区资源获取信息的方法。
摘要由CSDN通过智能技术生成

catalogue

Introduction

Assignment Table

PSP form

Problem-Solving Ideas

Find information

Design and Implementation Process

Design

Implementation

Code section 

results display

Disadvantage and Future expectation

Disadvantage:

Future expectation:

Conclusion


Introduction

This is a homework on software engineering

In this job we will make a web version of the visual calculator, this calculator includes addition, subtraction, multiplication, division, zero and other functions

Link to the project:https://github.com/leoliaoleo/calculator.git

Assignment Table

Link of Classhttps://bbs.csdn.net/forums/ssynkqtd-04
Link of Requirement of This Assignmenthttp://t.csdnimg.cn/mt5IU
Aim of This AssignmentCreate a web version of the visual calculator
MU STU ID and FZU STU ID21124981(MU)/832101328(FZU)

PSP form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning100130
Estimate90110
Development120140
Analysis2040
Design Spec1010
Design Review1010
Coding Standard3045
Design1015
Coding1010
Code Review2020
Test1510
Reporting120100
Test Report00
Size Measurement00
Postmortem & Process Improvement Plan4070
Sum595710

Problem-Solving Ideas

Making a visual calculator has the following aspects to consider: interface design, layout and components, component event binding, data processing and computational logic, input and output processing, error handling, testing and optimization. At the same time, the use of appropriate development tools and technology, can better realize the function and effect of the calculator.

Find information

[1] Online search: use search engines such as Google, Baidu, etc., use keywords such as "make visual calculator", "calculator UI design", "calculator development tutorial", etc., to find related documents, tutorials, blogs, forums and other resources. [2] Developer communities and forums: Join developer communities and forums, such as Stack Overflow, CSDN, GitHub, etc., to search for related topics, see other developers' experiences to share, ask questions, or participate in discussions. You can get some practical solutions and implementation ideas.

Design and Implementation Process

Design

1.Determine the needs: clarify the basic functions and characteristics of the calculator, such as the types of mathematical operations supported (addition, subtraction, multiplication and division, square roots, etc.), the display format of the display screen (decimal places, scientific counting, etc.), whether continuous calculation is supported.

2.Interface layout: The calculator interface is divided into different areas, such as the number button area, the operator button area, and the display area. Determine the location and size of each area so that components can be added later.

3.Add components: Add components, such as buttons and text boxes, to each area one by one based on the interface layout. You can use the layout manager to better control the arrangement and placement of components.

4.Display processing: The user's input data is displayed on the display of the calculator, and the calculation results are updated to the display. Need to consider the formatting of the data display, such as decimal places, scientific notation, etc.

Implementation

1.Determine the development platform and tools: Select the right development platform and tools based on your needs and development technology. 

2.Add event handlers: Add event handlers for each button. According to the function of the button, the corresponding event processing logic is implemented. 

3.Processing computational logic: Processing computational logic based on user input and operations.

4.Test and debug with optimization and improvement:Test the calculator, optimize and improve the calculator according to the test results.

Code section 

from flask import Flask, request, render_template_string
import math

app = Flask(__name__)


@app.route('/')
def calculator():
    return render_template_string('''
        <html>
        <head>
            <title>Calculator</title>
            <style>
                .calculator {
                    text-align: center;
                    margin-top: 100px;
                }

                .button {
                    width: 50px;
                    height: 50px;
                    margin: 5px;
                }
            </style>
            <script>
                function addToExpression(value) {
                    document.getElementById("expression").value += value;
                }

                function calculate() {
                    let expression = document.getElementById("expression").value;
                    let result = eval(expression);
                    document.getElementById("expression").value = result;
                }

                function clearExpression() {
                    document.getElementById("expression").value = '';
                }

                function calculateTrigFunction() {
                    let expression = document.getElementById("expression").value;
                                     // 将角度转换为弧度
                    let radians = eval(expression) * Math.PI / 180;
                    let result = Math.sin(radians);  // 计算正弦函数
                    document.getElementById("expression").value = result;
                }

                function calculateExponent() {
                    let expression = document.getElementById("expression").value;
                    let result = Math.exp(eval(expression));  // 计算指数函数
                    document.getElementById("expression").value = result;
                }

                function calculateLogarithm() {
                    let expression = document.getElementById("expression").value;
                    let result = Math.log10(eval(expression));  // 计算以10为底的对数
                    document.getElementById("expression").value = result;
                }
            </script>
        </head>
        <body>
            <div class="calculator">
                <h1>Calculator</h1>
                <form action="/calculate" method="POST">
                    <input type="text" id="expression" name="expression" required readonly>
                    <br>
                    <button class="button" type="button" onclick="addToExpression('1')">1</button>
                    <button class="button" type="button" onclick="addToExpression('2')">2</button>
                    <button class="button" type="button" onclick="addToExpression('3')">3</button>
                    <button class="button" type="button" onclick="addToExpression('+')">+</button>
                    <br>
                    <button class="button" type="button" onclick="addToExpression('4')">4</button>
                    <button class="button" type="button" onclick="addToExpression('5')">5</button>
                    <button class="button" type="button" onclick="addToExpression('6')">6</button>
                    <button class="button" type="button" onclick="addToExpression('-')">-</button>
                    <br>
                    <button class="button" type="button" onclick="addToExpression('7')">7</button>
                    <button class="button" type="button" onclick="addToExpression('8')">8</button>
                    <button class="button" type="button" onclick="addToExpression('9')">9</button>
                    <button class="button" type="button" onclick="addToExpression('*')">*</button>
                    <br>
                    <button class="button" type="button" onclick="addToExpression('0')">0</button>
                    <button class="button" type="button" onclick="addToExpression('.')">.</button>
                    <button class="button" type="button" onclick="calculate()">=</button>
                    <button class="button" type="button" onclick="addToExpression('/')">/</button>
                    <br>
                    <button class="button" type="button" onclick="clearExpression()">C</button>
                    <button class="button" type="button" onclick="calculateTrigFunction()">sin</button>
                    <button class="button" type="button" onclick="calculateExponent()">exp</button>
                    <button class="button" type="button" onclick="calculateLogarithm()">log</button>
                </form>
            </div>
        </body>
        </html>
    ''')


if __name__ == '__main__':
    app.run(debug=True)

results display

Disadvantage and Future expectation

Disadvantage:

The value estimation of trigonometric function is not completely accurate, and there are errors within the acceptable range

The style of the calculator is not beautiful enough, and the function of the calculator is not comprehensive enough

Future expectation:

It is hoped that the style and function of the computer can be improved, the design can be more humane, and the function can be more intelligent

Conclusion

By making web calculators, I learned some front-end technologies and improved my problem-solving ability

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值