Visual calculator with front-end and back-end separation

目录

1. Introduce

2. Assignment Table

3. PSP Form

4. Solution Idea

5. Design and Implementation

6. Code description

7. Computer Function Demonstration

8. Summary


1. Introduce

        This blog focuses on a simple visual calculator. The calculator uses the elementplus component library with vue3 as the front end. With python as the back end, the database is sqlite3. Let it perform basic calculations and call history functions. The following describes the design process and detailed code for each part, as well as a visual demonstration of the program.

2. Assignment Table

The Link Your Classhttps://bbs.csdn.net/forums/ssynkqtd-04
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/617378696
The Aim of This AssignmentCreate a calculator with a visual interface
MU STU ID and FZU STU ID21126330_832101106

3. PSP Form

Personal Software Process StagesEstimated Time(minutes)Actual Time(minutes)
Planning4040
• Estimate4040
Development650685
• Analysis3030
• Design Spec3030
• Design Review5060
• Coding Standard2015
• Design120140
• Coding300320
• Code Review4040
• Test6050
Reporting6075
• Test Repor1015
• Size Measurement00
• Postmortem & Process Improvement Plan5060
Sum750800

4. Solution Idea

For a visual calculator programmed in python in the job, it was difficult for me personally to complete this job as a front-end file, so I chose vue3 as the front end, used the elementplus component library, used sqlite3 as the database, and used python as the back-end connection to achieve this task.

5. Design and Implementation

在这里插入图片描述

6. Code description

src/App.vue (frontend)

<template>
  <div style="padding: 240px;">
    <el-row :gutter="80">
      <el-col :span="12">
        <div class="grid-content ep-bg-purple"/>
        <div class='calculator'>
          <div class='showPanel'>
            <span class='expression'>{{ expression }}</span>
          </div>
          <div class='caculator-button'>

            <el-button @click="getResult('(')">(</el-button>
            <el-button @click="getResult(')')">)</el-button>
            <el-button @click="getResult('lg(')">lg</el-button>
            <el-button @click="getResult('ans')" type='primary' class="ans" :loading="historyLoading">ans</el-button>

            <el-button @click="getResult('c')">c</el-button>
            <el-button @click="getResult('del')">del</el-button>
            <el-button @click="getResult('/')">/</el-button>
            <el-button @click="getResult('*')">*</el-button>

            <el-button @click="getResult('7')">7</el-button>
            <el-button @click="getResult('8')">8</el-button>
            <el-button @click="getResult('9')">9</el-button>
            <el-button @click="getResult('-')">-</el-button>

            <el-button @click="getResult('4')">4</el-button>
            <el-button @click="getResult('5')">5</el-button>
            <el-button @click="getResult('6')">6</el-button>
            <el-button @click="getResult('+')">+</el-button>

            <el-button @click="getResult('1')">1</el-button>
            <el-button @click="getResult('2')">2</el-button>
            <el-button @click="getResult('3')">3</el-button>
            <el-button @click="getResult('%')">%</el-button>

            <el-button @click="getResult('0')" class="zero">0</el-button>
            <el-button @click="getResult('.')">.</el-button>
            <el-button @click="getResult('=')" type='danger' :loading="calcLoading">=</el-button>

          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content ep-bg-purple"/>
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>历史记录(最近10条)</span>
            </div>
          </template>
          <el-table v-if="tableData?.length > 0" border :data="tableData" style="width: 100%">
            <el-table-column prop="expression" label="表达式"/>
            <el-table-column prop="result" label="结果"/>
          </el-table>
          <div v-else>空</div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script setup>
import {ref} from 'vue'
import axios from 'axios'
import {useAsyncState} from '@vueuse/core'
import {ElMessage} from 'element-plus'


const expression = ref('')

const {isLoading: historyLoading, state: tableData, execute: getHistory} = useAsyncState(
  () => axios.get('http://localhost:5000/history').then(res => res.data.result),
  [],
  {immediate: false}
)


const {isLoading: calcLoading, execute: calc} = useAsyncState(
  () => axios.post('http://localhost:5000/calculate', {
    expression: expression.value
  }).then(res => {
    if (res.data.errMsg) {
      ElMessage.error(res.data.errMsg)
    } else {
      expression.value = res.data.result
    }
  }),
  [],
  {immediate: false}
)

async function getResult(e) {
  switch (e) {
    case 'del':
      expression.value = expression.value.slice(0, expression.value.length - 1)
      break
    case 'ans':
      getHistory(0)
      break
    case '=':
      calc(0)
      // expression.value = eval(expression.value)
      break
    case 'c':
      expression.value = ''
      break
    default:
      expression.value += e
  }
}

</script>

<style lang='scss' scoped>
.calculator {
  border: solid 1px #dcdcdc;
  padding: 10px;
  background-color: #ffffff;
  border-radius: 4px;
  box-shadow: 0 0 2px #dcdcdc;
  width: 100%;

  .showPanel {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    padding: 2px 20px;
    height: 80px;
    border: 1px #dcdcdc solid;
    width: 100%;
    border-radius: 4px;
    box-sizing: border-box;
    margin-bottom: 8px;
    justify-content: space-evenly;

    .expression {
      font-size: 24px;
      font-weight: 700;
    }
  }
}

.el-button {
  margin: 0 !important;
  padding: 10px;
  font-weight: 600;
  height: 50px;
  width: 100%;
  font-size: 18px;
}

.caculator-button {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
  border-radius: 4px;
  background-color: #fffffff1;
}

.zero {
  grid-column: 1/3;
}
.ans {
  //grid-column: 3/5;
}
</style>

main.py (backend)

from flask import Flask, request, jsonify
from flask_cors import CORS
import sqlite3
import math


def lg(x):
    return math.log(x, 10)


def ln(x):
    return math.log(x, math.e)


app = Flask(__name__)
CORS(app)


# 定义路由
@app.route('/calculate', methods=['POST'])
def calculate():
    # 创建或连接到SQLite数据库
    conn = sqlite3.connect('expressions.db')

    try:
        expression = request.json['expression']
        result = eval(expression)
        c = conn.cursor()
        c.execute("INSERT INTO expressions (expression, result) VALUES (?, ?)", (expression, str(result)))
        conn.commit()
        return jsonify({'result': result})
    except Exception:
        return jsonify({'result': None, 'errMsg': '表达式异常,请检查输入'})
    finally:
        # 关闭连接
        conn.close()


@app.route('/history', methods=['GET'])
def history():
    conn = sqlite3.connect('expressions.db')
    try:
        c = conn.cursor()
        c.execute("SELECT * FROM expressions")
        rows = c.fetchall()

        history = []
        for row in rows:
            history.append({
                'expression': row[0],
                'result': row[1]
            })
        history.reverse()
        history = history[:10]

        return jsonify({'result': history})
    finally:
        # 关闭连接
        conn.close()


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

7. Computer Function Demonstration

As the video review is not complete, use gif instead.

The video is 3:46 in length.

8. Summary

        On the frontend, I learned to use Vue 3 as a front-end framework, leveraging its powerful tools and components to build user interfaces. Vue 3's component-based approach to development allows me to better organize my code and implement reusable interface elements. I also learned how to use the Element Plus component library to quickly build beautiful and interactive interfaces, saving me a lot of development time. I gained a deeper understanding of core concepts of Vue 3 such as components, directives, reactive data, and lifecycle hooks. I learned how to use Vue's template syntax to bind data and events and update the interface dynamically.
        For the backend, I chose Python as the development language and SQLite3 as the database. Python's concise and readable syntax makes back-end development much easier to get started with. I learned how to use Python's features to handle calculations and interact with databases. With SQLite3, I was able to easily create and manage a database and store a user's history in it.
        Throughout the project, I learned how to connect the front end to the back end and transfer data through HTTP requests and responses.

GitHub - Bill7wu/backendLink to backend code: GitHub - Bill7wu/backend

GitHub - Bill7wu/frontendLink to frontend code: GitHub - Bill7wu/frontend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
这个链接是一个在线的 JavaScript 计算器,它的实现方式与上面提到的类似,不过它使用了更为完整和复杂的 JavaScript 代码。 具体来说,这个计算器的实现步骤如下: 1. HTML 部分 在 HTML 文件中,创建一个表单,其中包含一个输入框和一些按钮,如下所示: ```html <form name="calculator"> <table> <tr> <td colspan="4"> <input type="text" name="result" id="result" readonly> </td> </tr> <tr> <td><input type="button" value="1" onclick="calculator.result.value += '1'"></td> <td><input type="button" value="2" onclick="calculator.result.value += '2'"></td> <td><input type="button" value="3" onclick="calculator.result.value += '3'"></td> <td><input type="button" value="/" onclick="calculator.result.value += '/'"></td> </tr> <tr> <td><input type="button" value="4" onclick="calculator.result.value += '4'"></td> <td><input type="button" value="5" onclick="calculator.result.value += '5'"></td> <td><input type="button" value="6" onclick="calculator.result.value += '6'"></td> <td><input type="button" value="*" onclick="calculator.result.value += '*'"></td> </tr> <tr> <td><input type="button" value="7" onclick="calculator.result.value += '7'"></td> <td><input type="button" value="8" onclick="calculator.result.value += '8'"></td> <td><input type="button" value="9" onclick="calculator.result.value += '9'"></td> <td><input type="button" value="-" onclick="calculator.result.value += '-'"></td> </tr> <tr> <td><input type="button" value="C" onclick="calculator.result.value = ''"></td> <td><input type="button" value="0" onclick="calculator.result.value += '0'"></td> <td><input type="button" value="=" onclick="calculator.result.value = eval(calculator.result.value)"></td> <td><input type="button" value="+" onclick="calculator.result.value += '+'"></td> </tr> </table> </form> ``` 在这个表单中,我们也是使用了一个只读的文本框用于显示计算结果,以及一些按钮用于输入数字和运算符。不同的是,这里的按钮并没有使用函数调用,而是直接在 onclick 属性中编写了 JavaScript 代码。 2. JavaScript 部分 在 JavaScript 文件中,我们需要编写更为复杂的代码来实现计算器的功能。 首先,我们需要定义一个 eval() 函数,用于计算表达式的值: ```javascript function eval() { try { return eval(arguments[0]); } catch (e) { return 'Error'; } } ``` 在这个函数中,我们使用了 try-catch 语句来捕获可能的运行时错误,避免程序崩溃。 接着,我们需要编写一个 calc() 函数,用于将字符串表达式转换为可计算的表达式: ```javascript function calc() { calculator.result.value = eval(calculator.result.value); } ``` 在这个函数中,我们使用了 eval() 函数来计算表达式的值,并将计算结果赋值给文本框。 最后,我们需要编写一些函数来处理一些特殊情况,例如小数点、正负号、百分号等。这些函数的具体实现可以参考链接中给出的代码。 通过上述这些步骤,我们就可以实现一个完整的 JavaScript 计算器了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值