Back-end separation calculator programming

Course for This AssignmentMU - SE2301
Assignment Requirementshttps://bbs.csdn.net/topics/617378696
Objectives of This AssignmentAdded new algorithms and history
MU STU ID and FZU STU ID21126232 832102108
GitHub Code
Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning5050
• Estimate5050
Development400420
• Analysis5050
• Design Spec4030
• Design Review3540
• Coding Standard3540
• Design7090
• Coding180180
• Code Review9070
• Test8080
Reporting5565
• Test Repor1010
• Size Measurement1515
• Postmortem & Process Improvement Plan3040
Sum505535
  1. Introduction
    This is a Front and rear end separated calculator

  2. Frontend

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
    <style>
        input[type="text"], input[type="button"] {
            width: 50px;
            height: 50px;
            font-size: 18px;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <td><input type="text" id="display" readonly></td>
        </tr>
        <tr>
            <td><input type="button" value="1" onclick="appendToDisplay('1')"></td>
            <td><input type="button" value="2" onclick="appendToDisplay('2')"></td>
            <td><input type="button" value="3" onclick="appendToDisplay('3')"></td>
            <td><input type="button" value="+" onclick="appendToDisplay('+')"></td>
        </tr>
        <tr>
            <td><input type="button" value="4" onclick="appendToDisplay('4')"></td>
            <td><input type="button" value="5" onclick="appendToDisplay('5')"></td>
            <td><input type="button" value="6" onclick="appendToDisplay('6')"></td>
            <td><input type="button" value="-" onclick="appendToDisplay('-')"></td>
        </tr>
        <tr>
            <td><input type="button" value="7" onclick="appendToDisplay('7')"></td>
            <td><input type="button" value="8" onclick="appendToDisplay('8')"></td>
            <td><input type="button" value="9" onclick="appendToDisplay('9')"></td>
            <td><input type="button" value="*" onclick="appendToDisplay('*')"></td>
        </tr>
        <tr>
            <td><input type="button" value="0" onclick="appendToDisplay('0')"></td>
            <td><input type="button" value="." onclick="appendToDisplay('.')"></td>
            <td><input type="button" value="=" onclick="calculate()"></td>
            <td><input type="button" value="/" onclick="appendToDisplay('/')"></td>
        </tr>
        <tr>
            <td><input type="button" value="ans" onclick="appendToDisplay(getAns())"></td>
        </tr>
    </table>

    <script>
        function appendToDisplay(value) {
            document.getElementById('display').value += value;
        }

        function calculate() {
            try {
                document.getElementById('display').value = eval(document.getElementById('display').value);
            } catch (error) {
                document.getElementById('display').value = 'Error';
            }
        }

        function getAns() {
            return document.getElementById('display').value;
        }
    </script>
</body>
</html>


  1. Backend
from flask import Flask, request, jsonify

app = Flask(__name)
history = []

@app.route('/add', methods=['POST'])
def add():
    data = request.get_json()
    result = data['num1'] + data['num2']
    history.append(f"{data['num1']} + {data['num2']} = {result}")
    return jsonify({"result": result})

@app.route('/subtract', methods=['POST'])
def subtract():
    data = request.get_json()
    result = data['num1'] - data['num2']
    history.append(f"{data['num1']} - {data['num2']} = {result}")
    return jsonify({"result": result})

@app.route('/multiply', methods=['POST'])
def multiply():
    data = request.get_json()
    result = data['num1'] * data['num2']
    history.append(f"{data['num1']} * {data['num2']} = {result}")
    return jsonify({"result": result})

@app.route('/divide', methods=['POST'])
def divide():
    data = request.get_json()
    if data['num2'] == 0:
        return jsonify({"error": "Division by zero"})
    result = data['num1'] / data['num2']
    history.append(f"{data['num1']} / {data['num2']} = {result}")
    return jsonify({"result": result})

@app.route('/history', methods=['GET'])
def get_history():
    return jsonify({"history": history[-10:]})

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

  1. Display在这里插入图片描述
    在这里插入图片描述

  2. Summary
    This assignment improve the ability of coding in both frontend and backend.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值