A simple flask calculator

Calculator

github repository: Yanzzp/calculator (github.com)

1. Introduction

This is a web calculator implemented using Python’s Flask framework and HTML, CSS and JS. HTML language can help me realize the appearance of this calculator, CSS can decorate this calculator, and implement the logic of the calculator through the Flask framework.

2. Self information

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://blog.csdn.net/zpy737296/article/details/133647527
The Aim of This AssignmentImplement a calculator
MU STU ID and FZU STU ID21124566_832101119

3. PSP

Personal Software Process StagesEstimated Time (minutes)Actual Time (minutes)
Planning108
• Estimate28
Development104
• Analysis24
• Design Spec28
• Design Review26
• Coding Standard25
• Design27
• Coding28
• Code Review26
• Test29
Reporting1020
• Test Report210
• Size Measurement24
• Postmortem & Process Improvement Plan26
Sum306

4. Design and implementation process

  +-------------------+
  |   用户输入表达式 |
  +-------------------+
          |
          |
          v
  +-------------------+
  |   检查输入合法性 |
  |   (包括括号匹配) |
  +-------------------+
          |
          |
          v
  +-------------------+
  |   中缀转后缀表达式|
  |   (逆波兰表示法) |
  +-------------------+
          |
          |
          v
  +-------------------+
  |   计算后缀表达式  |
  |   (使用栈数据结构) |
  +-------------------+
          |
          |
          v
  +-------------------+
  |   显示计算结果   |
  +-------------------+
          |
          |
          v
  +-------------------+
  |   处理错误情况   |
  +-------------------+

5. Code block

from flask import Flask, request, render_template
import math

app = Flask(__name__)

@app.route('/')
def calculator():
    return render_template('calculator2.html', result="")

@app.route('/calculate', methods=['POST'])
def calculate():
    expression = request.form['expression']
    try:
        result = eval(expression, {"__builtins__": None}, {"math": math})
        return render_template('calculator2.html', result=result, expression=expression)
    except Exception as e:
        error_message = "Invalid input or calculation error: " + str(e)
        return render_template('calculator2.html', error_message=error_message)

if __name__ == '__main__':
    app.run(debug=True)
<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
    <h1>Calculator</h1>
    <form method="POST" action="/calculate">
        <input type="text" name="expression" value="{{ expression }}">
        <button type="submit">Calculate</button>
        <button type="button" onclick="clearInput()">Clear</button>
        <p>{{ error_message }}</p>
        <h2>Result: {{ result }}</h2>
    </form>
    <br>
    <h3>Basic Operations:</h3>
    <p>Examples: 2+3, 5-2, 4*6, 8/4</p>
    <h3>Advanced Operations:</h3>
    <p>Examples: 2**3 (2^3), math.sin(30) (sine of 30 degrees)</p>
    <script>
        function clearInput() {
            document.querySelector('input[name="expression"]').value = '';
        }
    </script>
</body>
</html>

Calculator

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值