Back-End Separation Calculator Programming

1. Assignment Description

- Basic Calculator Functions

- Extended Function: Scientific calculator

2. Personal Software Process (PSP) Form

3. Problem-Solving Ideas

4. Design and Implementation Process

- Design Process

- Implementation Process

5. Code Description

Front - End Part

Backend - End Part

6. Result Displaying and Text Description

7. Summary


1. Assignment Description

In the last assignment, we have completed the most basic design of a calculator function, so now continue to achieve more perfect calculator function. Because we understand that everyone basically has a certain programming foundation, this assignment allows the use of any visualization technology: web, Android, applet of WeChat, etc., but to reflect the frontend and backend separation to achieve the following functions.

- Basic calculator functions

Function 1: addition, subtraction, multiplication, division, remainder

  • Basic +, -, *, /, %,operations, requiring the correct order of operations, the correct result, and store the input string and the result in a back-end database
    Function 2: Read history

  • Can use the ans button to return the previous calculation result and view the history to read the last ten string formula and the corresponding calculation result, must be read from the back-end database, cannot use the cache.

- Extended function: Scientific calculator

  • Calculate radical sign, trigonometric function, log, scientific notation.
  • Implement bracket computation.
  • Better looking UI interface.

2. Personal Software Process (PSP) Form

Personal Software Process StagesEstimated Time(/minutes)Actual Time(/minutes)
Planning6050
Estimate5050
Analysis4040
Design Specification6050
Design Review5050
Coding Standard4050
Design4050
Coding5050
Code Review3040
Test6050
Reporting6060
Test Report6050
Size Measurement3060
Postmortem & Process Improvement Plan3050
Sum660700

3. Problem - Solving Ideas

We require to make a simple web calculator and utilize the back-end database to store figures and data. In this module, I use PyCharm and MySQL to realize the database. To solve the problem, I just utilize software WebStorm as front-end and PyCharm and MySQL as back-end to realize the interaction of the calculator.


4. Design and Implementation Process

  1. Adjust the size of the calculator panel and the size of the button. Select text styles to modify.
  2. If a number (0,1,2,3,4,5,6,7,8,9) and “(”, “)”, “.” is clicked, it will be displayed in the input text box above.
  3. If an operator button (such as +, -, *, /, sin, cos, tan, e^x) is clicked, it will perform related operations and output results.
  4. If the key “C” is clicked, the string content is cleared, and the output box is also cleared.
  5. If you click “=”, the result of the calculation is displayed in the output box and the content is cleared.
  6. If you click “ans”, it will return the last calculation result and if you click “history”, it will get the history data from database.
Design and modify the website
Operate on the calculater
Display the result

5. Code Description

Front - End Part

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
    <style>
        .title-button {
            width: 350px; /* 设置按钮宽度 */
            height: 30px; /* 设置按钮高度 */
            font-size: 20px; /* 设置按钮文本字体大小 */
            margin: 5px; /* 设置按钮之间的间距 */
        }
        /* 定义按钮的样式 */
        .calculator-button {
            width: 60px; /* 设置按钮宽度 */
            height: 60px; /* 设置按钮高度 */
            font-size: 20px; /* 设置按钮文本字体大小 */
            margin: 5px; /* 设置按钮之间的间距 */
        }
         .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh; /* 100%视口高度 */
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Calculator</h1>
        <form name="Calculator">
            <input type="text" class="title-button"name="display" id="display" disabled>
            <input type="button"class="calculator-button" value="ans" onclick="">
            <br>
            <input type="button" class="calculator-button" value="7" onclick="addToDisplay('7')">
            <input type="button" class="calculator-button" value="8" onclick="addToDisplay('8')">
            <input type="button" class="calculator-button" value="9" onclick="addToDisplay('9')">
            <input type="button" class="calculator-button" value="/" onclick="addToDisplay('/')">
            <input type="button"class="calculator-button" value="sin" onclick="fSin()">
            <input type="button" class="calculator-button" value="C" onclick="clearDisplay()">
            <br>
            <input type="button" class="calculator-button" value="4" onclick="addToDisplay('4')">
            <input type="button" class="calculator-button" value="5" onclick="addToDisplay('5')">
            <input type="button" class="calculator-button" value="6" onclick="addToDisplay('6')">
            <input type="button" class="calculator-button" value="*" onclick="addToDisplay('*')">
            <input type="button" class="calculator-button" value="cos" onclick="fCos()">
            <input type="button"class="calculator-button" value="%" onclick="addToDisplay('%')">
            <br>
            <input type="button" class="calculator-button" value="1" onclick="addToDisplay('1')">
            <input type="button" class="calculator-button" value="2" onclick="addToDisplay('2')">
            <input type="button" class="calculator-button" value="3" onclick="addToDisplay('3')">
            <input type="button" class="calculator-button" value="-" onclick="addToDisplay('-')">
            <input type="button" class="calculator-button" value="tan" onclick="fTan()">
            <input type="button" class="calculator-button" value="(" onclick="addToDisplay('(')">
            <br>
            <input type="button" class="calculator-button" value="." onclick="addToDisplay('.')">
            <input type="button" class="calculator-button" value="0" onclick="addToDisplay('0')">
            <input type="button" class="calculator-button" value="=" onclick="calculate()">
            <input type="button" class="calculator-button" value="+" onclick="addToDisplay('+')">
            <input type="button" class="calculator-button" value="exp" onclick="fExp()">  
            <input type="button" class="calculator-button" value=")" onclick="addToDisplay(')')">
        </form>

        <script>
            function addToDisplay(value) {
                document.Calculator.display.value += value;

            }

            function clearDisplay() {
                document.Calculator.display.value = '';
            }

           function fSin() {
                
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                const result = Math.sin(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
           function fCos() {
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                const result = Math.cos(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
           function fTan() {
                const input = document.Calculator.display.value;
                const angle = parseFloat(input);
                if (input == 90|| input == 270||input == -90||input == -270){
	   document.Calculator.display.value = "Error!"
	   return
            }
                const result = Math.tan(angle * (Math.PI / 180)); // 将角度转换为弧度
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
           function fExp() {
                const input = document.Calculator.display.value;
                const exponent= parseFloat(input);
                const result = Math.exp(exponent); 
                document.Calculator.display.value = result.toFixed(5); // 保留5位小数
            }
            function calculate() {
                try {
                    const result = eval(document.Calculator.display.value);
 	    document.Calculator.display.value = result.toFixed(1);
	    if (isNaN(result) || !isFinite(result)) {
                        throw new Error("error");
                }
                }catch(err) {
                    document.Calculator.display.value = 'Error!';
                }
            }
        </script>
    </div>
</body>
</html>

First, I use < head > part to define the style and the size of the interface. In the < body > part, also define the button and connect them to the function constructed blow in logic. At the same time, make it possible to display in the screen.

Then for function part, I just import the Math () function to achieve the exponentiation and trigonometric operations. For basic calculation like plus or minus, it can be achieved by its original library.

Back - End Part

Just use python code and MySQL to create interfaces between the front and back end.

from flask import Flask,render_template,request
import pymysql

app = Flask(__name__)

@app.route("/add/user", methods=["GET","POST"])
def add_user(): //send GET request, return page, enter and POST to the website.
   if request.method == "GET":
       return render_template("add_user.html")
   calculation = request.form.get("calculation")
   result = request.form.get("result")

   conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="Asd12345.", charset='utf8', db='history')
   cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

   sql = "insert into history(calculation, result) values(%s,%s)"
   cursor.execute(sql, [calculation, result])
   conn.commit()

   cursor.close()
   conn.close()
   return render_template("add_user.html")

@app.route("/show/user")
def show_user(): //show the data in database.
   conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', password="Asd12345.", charset='utf8', db='history')
   cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
   sql = "select * from history"
   cursor.execute(sql)
   data_list = cursor.fetchall()

   cursor.close()
   conn.close()

   print(data_list)

   return render_template("show_user.html",data_list=data_list)

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

Following codes are used to connect, execute and close port.

//创建链接
	conn = pymysql.connect(host, port, user, password, charset='utf8', db)
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
//执行命令
    sql = "The command to execute"
    cursor.execute()
//关闭接口
    cursor.close()
    conn.close()

These are some orders to add and search the historical figures for front end part.

  • add_user: send GET request, return page, enter and POST to the website.
  • show_user: show the data in database.
  • connecting_process: First, we need to make a connection, then execute the command in MySQL, finally close the connection.

6. Result Displaying and Text Description

Assignment2

Successfully displaying the calculator, we can get the history data from the database using MySQL. Having constructed the environment of database, we can get historical figures and equations.

figures in MySQL

7. Summary

To achieve a simple version of the web calculator, I just apply the HTML codes to construct the website. By adjust the interface to be more twinkle, I modify the head of the html codes like size and distance between them. Using CSDN to blog in order to record the development, I learn the complete progresses to create and achieve my own project.

In addition, I add the back-end part in this module. Having learned MySQL database, I achieve the interaction between front-end and back-end parts by using some orders.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 油水分离的前沿技术包括: 1. 超声波技术:通过高频声波在油水混合物中产生微小空泡,使油和水分离。 2. 动力学分离技术:通过油水混合物的物理性质,如密度、流动性等差异,将油和水分离。 3. 光学分离技术:利用油和水在光学特性上的差异,如光吸收、折射率等,将油和水分离。 4. 膜技术:通过膜的选择性透过性,将油和水分离。 5. 化学吸附技术:通过合适的化学吸附剂,吸附油,使油和水分离。 这些技术具有不同的优缺点,根据实际应用情况,应选择合适的技术。 ### 回答2: 油水分离的前沿技术是指在处理含油废水时使用的最新科技和方法。由于油污水的治理一直是一个全球性的环境问题,因此研究人员一直在努力开发更有效的方法来分离油水,以减少对环境的污染。 一种前沿技术是利用纳米材料进行油水分离。纳米材料具有大比表面积和特殊的化学和物理性质,可以吸附或分解油污染物。这种技术通过将纳米材料应用于分离设备中,可以高效地分离油水混合物。纳米材料的使用还可以提高设备的储油容量和改善处理效率。 另一种前沿技术是利用膜分离技术进行油水分离。膜分离技术利用特殊的膜材料将油和水分离开来。这种技术相对传统的方法具有更高的分离效率和选择性。同时,膜分离技术还可以实现连续操作和减少处理成本。 此外,一种新兴的技术是利用电化学方法进行油水分离。该方法通过电场效应使油和水分离开来。这种技术具有高效、环保和可控性的优势,可以有效地处理不同种类的油污染物。 总之,油水分离的前沿技术为解决油污染问题提供了新的方法和可能性。这些技术在提高分离效率、降低处理成本和减少环境污染方面具有重要意义,对于推动可持续发展和保护环境具有重要作用。 ### 回答3: 油水分离的前沿技术是一种用于将油和水分离的先进技术。油水分离是一项重要的环境工程技术,用于处理由油污染引起的水体和废水。过去,常用的油水分离方法包括重力分离、漂浮、离心分离等,但这些方法存在一些局限性。 随着科技的进步,油水分离的前沿技术不断涌现。一个前沿技术是电化学油水分离法。该技术利用电解作用将水中的油脂离子化,然后利用电极的特殊性质将油脂吸附并分离出来。这种方法具有高效、节能、环保等优点,可以有效地从废水中分离出油脂。 另一个前沿技术是膜分离技术。膜分离技术利用特殊的薄膜材料,如聚合物膜、陶瓷膜等,通过渗透、过滤和离子交换等机制实现油水分离。这种技术具有高效、节能、可持续等特点,可以有效地去除水中的油污染物。 此外,纳米技术也被应用于油水分离的前沿技术中。纳米材料具有巨大的比表面积和特殊的物理化学性质,可以用于油水分离膜、吸附材料等的制备。通过纳米材料的使用,油水分离的效率和效果能够得到显著提高。 总之,油水分离的前沿技术不断涌现,为处理油污染带来了新的可能性。电化学油水分离、膜分离技术和纳米技术等都是重要的前沿技术,将为环境保护和资源开发提供有力支持。随着科学技术的发展,我们可以期待更多创新的油水分离技术的出现。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值