前后端交互计算器

这个作业属于哪个课程https://bbs.csdn.net/forums/ssynkqtd-05
这个作业要求在哪里https://bbs.csdn.net/topics/617377308
这个作业的目标完成一个前后端交互计算器
其他参考文献

Gitcode仓库地址

Gitcode

PSP表格

PSPPersonal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划2025
• Estimate• 估计这个任务需要多少时间1515
Development开发100150
• Analysis• 需求分析 (包括学习新技术)500550
• Design Spec• 生成设计文档4040
• Design Review• 设计复审2530
• Coding Standard• 代码规范 (为目前的开发制定合适的规范)4550
• Design• 具体设计8095
• Coding• 具体编码300450
• Code Review• 代码复审6060
• Test• 测试(自我测试,修改代码,提交修改)6060
Reporting报告7575
• Test Repor• 测试报告3030
• Size Measurement• 计算工作量2020
• Postmortem & Process Improvement Plan• 事后总结, 并提出过程改进计划3030
合计13001620

成品展示

科学计算器

1、简单计算

进行简单的加减运算

2、科学计算

三角函数、对数、取余等运算
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3、报错

在这里插入图片描述

4、清零

在这里插入图片描述

5、返回

在这里插入图片描述

6、历史记录

在这里插入图片描述

利率计算器

虽然做出了页面,代码也敲完了,但是得不出结果。

在这里插入图片描述

功能结构图

在这里插入图片描述

// An highlighted block
var foo = 'bar';

原型设计

设计实现过程

前端:html+css+js三件套,设计好页面
后端:数据库,使用python
设计好页面、按钮,实现基本的运算逻辑,过程中使用一些函数实现
后端使用python实现和数据库的数据的存取。前提是先在数据库建立好表。

代码展示

前端:

结果计算以及发送给数据库

function Calculate()
{  
    try
    {
        txt=document.getElementById('t1').value;
        temp=txt.replace(/sin/g, 'Math.sin');
        temp=temp.replace(/cos/g, 'Math.cos');
        temp=temp.replace(/tan/g, 'Math.tan');
        temp=temp.replace(/sqrt/g, 'Math.sqrt');
        temp=temp.replace(/lg/g, 'Math.log10');
        temp=temp.replace(/\^/g, '**');
        temp=temp.replace(/π/g, 'Math.PI');
        txt=eval(temp);
        if(isNaN(txt))
        {
            document.getElementById('t2').value ='除数不能为0';
        }
        else if(txt=='Infinity')
        { 
            document.getElementById('t2').value ='除数不能为0';

        }
        else
        {
            document.getElementById('t2').value = txt;
        }
    }
    catch
    {
        document.getElementById('t2').value = '输入错误';  
    }

    expression = document.getElementById('t1').value;
    result = document.getElementById('t2').value;
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:5000/to_history', true); 
    xhr.setRequestHeader('Content-type', 'application/json');
    xhr.onreadystatechange = function () 
    {
    if (xhr.readyState === 4 && xhr.status === 200) {
        console.log(xhr.responseText);
    }
    };
    const data = {
        expression: expression,
        result: result
        };

        xhr.send(JSON.stringify(data));

    txt=document.getElementById('t1').value;
}

从数据库读取历史记录

function get()
{
    const xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:5000/get_history', true);
    xhr.onreadystatechange = function () 
    {
      if (xhr.readyState === 4) 
      {
        if (xhr.status === 200) 
        {
            Data = JSON.parse(xhr.responseText);
            array = Data['data'];
            let string="";
            for(let i=0;i<array.length;i++)
            {
                string +=array[i][0];
                string+=" = ";
                string+=array[i][1]+"\n";
            }
            document.getElementById('t1').value = string;
        } else 
        {
            console.error('错误: ' + xhr.status);
        }
      }
    };
    xhr.send();
}

function SwitchRate() 
{
    document.querySelector(".calculator").style.display = "none";
    document.getElementById("RateCalculator").style.display = "block";
}

function SwithNormal() 
{
    document.getElementById("RateCalculator").style.display = "none";
    document.querySelector(".calculator").style.display = "block";
}

清零和返回

```function AC()
{
    txt=document.getElementById('t1').value;
    txt = '';
    document.getElementById('t1').value = '';
    document.getElementById('t2').value = '';
    txt=document.getElementById('t1').value;
}

function Delete() 
{
    txt=document.getElementById('t1').value;
    txt = txt.slice(0, -1);
    document.getElementById('t1').value = txt;
    txt=document.getElementById('t1').value;
}

后端代码

发送历史记录:

@app.route('/to_history', methods=['POST'])
def to_history(): 
    try:
        data = request.get_json() 
        expression = data.get('expression')
        result = data.get('result')
        time = datetime.datetime.now()
        data = (time, expression, result)
        insert = "INSERT INTO HISTORY VALUES (%s, %s, %s)" 
        cursor.execute(insert, data)
        conn.commit()
        response_message = "ok"
        return jsonify({"message": response_message})

    except Exception as e:
        return jsonify({"error"})

@app.route('/get_history', methods=['GET'])

读取历史记录:

@app.route('/get_history', methods=['GET'])
def get_history():
        cursor.execute("SELECT expression, result FROM HISTORY ORDER BY time DESC LIMIT 10")
        data = cursor.fetchall()
        return jsonify({"data": data})

税率计算器:
前端:

var money='';
var time='';
var rate='';
var type='';
var Data;

function Huo()
{
    type="Huo";
}

function Dead()
{
    type="Dead";
}

function Loan()
{
    type="Loan";
}

function money(){
    money = document.getElementById('money').value;
}

function time(){
    time = document.getElementById('time').value;
}

function Taxcalculate()
{
    const xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:5000/Taxcalculate', true);
    xhr.setRequestHeader('Content-type', 'application/json');
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) 
        {
            if (xhr.status === 200) 
            {
                Data = JSON.parse(xhr.responseText);
                document.getElementById('rate').value=Data["result"];
            } 
        }
    };

    const data = {
        time : time,
        money: money,
        type: type
    };
    xhr.send(JSON.stringify(data));
}

后端:

@app.route('/Taxcalculate', methods=['POST'])
def Taxcalculate():
        data = request.get_json()  
        money = data.get('money')
        time = data.get('time')
        type= data.get('type')
        time = int(time)
        insert = ""
        if type == "Huo":
            insert = "SELECT rate FROM Huo"
        if type == "Dead":
            if time <6:
                 month=3
            elif time <12:
                 month=6
            elif time <24:
                 month=12
            elif time <36:
                 month=24
            elif time < 60:
                month=36
            else:
                 month=60
        insert = "SELECT rate FROM Dead WHERE month = " + month
        if type == "Loan":
            
            if time < 6:
                month = 6
            elif time < 12:
                month = 12
            elif time < 36:
                month = 36
            elif time <60:
                month = 60
            else:
                 month=61;
            insert = "SELECT rate1 FROM l WHERE month = " + month

心路历程和收获

通过这次作业,我终于实现了前后端分离的计算器,提升了错误处理和异常处理的能力,最主要的是学习了很多的新的知识,有待进一步的提高。及时反思和总结经验,以便在未来的项目中能够更好地应用所学。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值