832401106_ChenXin_First Assignment——Front-End and Back-End Separated Calculator System

832401106_陈欣 832401106_陈欣 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

832401106_ChenXin_First Assignment——Front-End and Back-End Separated Calculator System

CourseEE308 Software Engineering, Telecommunications Engineering Class 1
Assignment RequirementsFirst Assignment — Front-End and Back-End Separation Calculator System
Assignment ObjectiveBuild a front-end/back-end separated calculator and practice API communication, expression parsing, database persistence, testing, and deployment
Name, Student IDXinChen, 832401106
ReferencesFastAPI Official Documentation, Shunting-yard Algorithm Tutorial

Table of Contents


  1. Live Project Website:https://laid-band-dow-begin.trycloudflare.com/
  2. Frontend Repository: https://gitee.com/xena2006/832401106_calculator_frontend
  3. Backend Repository: https://gitee.com/xena2006/832401106_calculator_backend
  4. Frontend Code Style: https://gitee.com/xena2006/832401106_calculator_frontend/blob/main/codestyle.md
  5. Backend Code Style: https://gitee.com/xena2006/832401106_calculator_backend/blob/main/codestyle.md

PSP Table

Task StageEstimated Time (mins)Actual Time (mins)
Requirements Analysis2030
System Design6050
Frontend Development120150
Backend Development180200
Expression Parser Module90110
Database Design4030
History Module Development7060
Frontend-Backend Integration6080
Function Testing3040
Project Deployment5080
Blog Writing120120
Total840940

Product Demo

For each test below, run the calculator locally, take a screenshot.

1. Basic Operation & Precedence Test

在这里插入图片描述

Test basic operator precedence, input 1+2*3, get result 7, which means the precedence handling works correctly, and the history record is added successfully.

2. Parentheses Priority Test

在这里插入图片描述

Test parentheses priority, input (1+2)*3, get result 9, which means parentheses priority works correctly, the inner part is calculated first.

3. Negative Number Test

在这里插入图片描述

Test unary minus, input 3*-2, get result -6, which means we can correctly distinguish minus operator and negative sign, negative number calculation works.

4. Decimal Operation Test

在这里插入图片描述

Test decimal operation, input 1.5+2.5, get result 4, decimal calculation works normally, supports floating point numbers.

5. Error Handling Test

在这里插入图片描述

Test divide by zero error, input 1/0, get error prompt, which means we can handle invalid operations correctly, avoid program crash.

6. History Persistence Test

在这里插入图片描述

Test history persistence, after calculating multiple records, refresh the page, the history records are not lost, which means the database persistence works correctly.

7. Delete History Test

在这里插入图片描述


在这里插入图片描述

Test delete history, after clicking the delete button, the corresponding history record is removed successfully, the list updates automatically, delete function works.

8.Invalid expressions

在这里插入图片描述


Design and Implementation

1. Overall Architecture

This project strictly follows the frontend-backend separation architecture:

  • Frontend: Only responsible for UI interaction, sending HTTP requests, and displaying results, no calculation logic at frontend
  • Backend: Responsible for expression parsing & calculation, database operations, providing REST API
  • Database: Backend uses SQLite to store calculation history, ensures data persistence.

Architecture Diagram:

flowchart TD
    A["Frontend Browser"] -->|"HTTP/JSON Request"| B["Backend FastAPI Service"]
    B -->|"SQLite"| C["SQLite Database"]

2. Function Structure

flowchart LR
    subgraph FE["Frontend"]
        A["UI Input & Interaction"]
        B["API Request Wrapper"]
        C["Result & History UI"]
    end
    subgraph BE["Backend"]
        D["Expression Parsing"]
        E["REST API Interfaces"]
        F["DB CRUD Operations"]
    end
    A --> B --> E
    E --> D
    E --> F
    F --> E
    E --> B --> C

3. API Interface Design

We designed 3 core APIs:

  1. POST /api/calculate: Submit expression, calculate and save history
  2. GET /api/history: Get all calculation history
  3. DELETE /api/history/{id}: Delete specified history record

4. Database Design

History table structure:
| Field | Type | Description |
| ---- | ---- | ---- |
| id | INTEGER | Primary key, auto increment |
| expression | TEXT | The calculation expression |
| result | REAL | Calculation result |
| created_at | TIMESTAMP | Calculation time, default current time |

5. Calculation Module Design

The calculation module uses Shunting-yard algorithm, converts infix expression to postfix expression, then calculates the postfix expression. This way we can safely handle operator precedence and parentheses, we never use eval(), which avoids security issues.


Key Code Explanation

1. Expression Parsing Algorithm

The core Shunting-yard algorithm, handles precedence and parentheses:

def shunting_yard(self, tokens):
    output = []
    operators = []
    for token in tokens:
        if re.match(r'\d+\.?\d*', token):
            output.append(token)
        elif token == '(':
            operators.append(token)
        elif token == ')':
            while operators and operators[-1] != '(':
                output.append(operators.pop())
            operators.pop()
        else:
            while operators and operators[-1] != '(' and self.precedence[operators[-1]] >= self.precedence[token]:
                output.append(operators.pop())
            operators.append(token)
    while operators:
        output.append(operators.pop())
    return output

This algorithm automatically handles operator precedence, for example * has higher priority than +, so multiplication will be calculated first, and it also handles parentheses priority.

2.Unary Minus Handling

We distinguish minus operator and negative sign during tokenization, for example the - in 3*-2 is a negative sign, not a minus operator:

if token == '-':
    if i == 0 or tokens[i-1] in '+-*/(':
        processed_tokens.append('u-')
    else:
        processed_tokens.append('-')

This way we can handle negative numbers correctly.

Problems and Lessons Learned

Problems Encountered

  1. At first I made a mistake in git config command, which caused commit failure, I fixed it by correcting the command.
  2. For deployment, ngrok can not connect due to domestic network restriction, so I used local test to verify the functions.
  3. Gitee Pages service for personal repositories has been shut down, so I use cloudflared to expose both frontend static page and backend API to public network. The frontend static web page is served locally and exposed via cloudflared tunnel.

Lessons Learned

Through this assignment, I mastered the development workflow of frontend-backend separation, understood the division of responsibilities between frontend and backend. I learned the classic expression parsing algorithm, also learned how to use FastAPI and basic git operations, I have a deeper understanding of software engineering development process.

Deployment Notes

  • Gitee Pages service for personal repositories has been shut down, so I use cloudflared tunnel to expose both frontend static page and backend API to public network.
  • Frontend static web page is served locally and exposed via cloudflared tunnel, backend API is also exposed via cloudflared.
...全文

相关推荐

基于MATLAB的复合弹性模量分析,采用混合法则,结合纤维体积分数变化和图形结果。.zip

1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

海神之光

基于MATLAB的自适应纯追踪曲线跟踪机器人仿真,采用多路径、自适应速度控制和图形界面可视化。.zip

1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

海神之光

【数据库优化】通信行业B/O域查询性能提升:基于分区裁剪与覆盖索引的秒级溯源系统实现

内容概要:本文围绕通信行业中海量话单与信令数据的查询性能问题,系统阐述了从全表扫描到秒级响应的数据库优化路径。文章以运营商 cdr_detail 表为案例,揭示了因使用函数操作索引列(如DATE(call_start_time))和深分页(LIMIT offset, size)导致的全表扫描问题,并提出完整的优化方案:通过时间范围RANGE分区实现分区裁剪、构建msisdn与时间的联合覆盖索引、消除索引列函数运算、采用游标分页替代OFFSET等方式,使查询性能从P95 8.3秒降至290毫秒。同时介绍了执行计划分析(EXPLAIN)在调优中的关键作用,并展望了HTAP架构、向量化执行与AI自治优化器等未来方向。; 适合人群:具备一定数据库基础知识,从事通信行业数据开发、DBA或后端研发的技术人员,尤其是面对大规模时序数据查询性能瓶颈的工程师;; 使用场景及目标:①解决营业厅用户详单查询、网间结算对账、VoNR信令分析等高并发、低延迟查询场景下的性能问题;②掌握如何通过分区策略、索引设计和SQL改写实现从全表扫描到精准索引访问的优化落地; 阅读建议:建议结合实际生产环境中的大表查询案例,对照本文的反例与正例进行执行计划对比分析,并在测试环境中实践分区表创建、覆盖索引构建及游标分页改写,深入理解每项优化技巧的实际效果。

智能化咨询

石墨提纯全球市场结构与产业发展趋势分析报告(QYResearch最新研究).docx

石墨提纯全球市场结构与产业发展趋势分析报告(QYResearch最新研究)

行业报告QY

政府科技管理者如何提升科技成果转化政策的精准匹配与执行效率?.docx

政府科技管理者如何提升科技成果转化政策的精准匹配与执行效率?

BIZKEEN
评论
收藏 已收藏
分享
返回顶部