第二次作业

 

​一、实验背景

实验目的:

① 掌握软件开发的基本流程。

② 掌握常用的软件开发方式和工具。

实验内容:

① 设计并实现一个包含登录界面的计算器软件。

② 软件需具备历史记录功能,能保存用户计算记录。

二、实验要求

1.完成软件的UI设计、使用Visio设计软件中所涉及的所有流程图。

2.选择合适的集成开发环境和工具完成计算器软件的开发

3.将开发好软件进行测试并截图

4.将本次实验过程写成实验报告提交在本次作业的链接中

5.关键代码部分以代码块格式粘贴在实验报告正文中

6.软件架构以及开发技术不限

7.流程概述:

(1)创建一个登录界面,用户在此输入他们的凭据。

(2)当用户点击“登录”按钮时,验证输入的用户名和密码是否与数据库中的记录匹配。

如果凭据有效,用户将被重定向到计算器界面。如果凭据无效,显示错误消息并返回到登录界面。

(3)在计算器界面上,用户可以输入他们想要计算的表达式。

(4)当用户按下“计算”按钮时,执行计算并将结果保存在数据库中。

(5)显示计算结果,并显示一个选项让用户查看他们的历史记录。

(6)如果用户选择查看历史记录,从数据库中提取并显示历史记录。

三、实验内容

1、使用Python和SQLite创建一个简单的计算器软件。

创建一个数据库和表格:

 

python

import sqlite3 

 

def create_database(): 

    conn = sqlite3.connect('calculator.db') 

    c = conn.cursor() 

 

    c.execute('''CREATE TABLE IF NOT EXISTS calculations 

                (id INTEGER PRIMARY KEY AUTOINCREMENT, 

                expression TEXT, 

                result REAL)''') 

 

    conn.commit() 

    conn.close() 

 

create_database()

编写一个函数来处理计算:

 

python

def calculate(expression): 

    try: 

        result = eval(expression) 

        return result 

    except: 

        return None

需要一个函数来将计算结果保存到数据库中:

 

python

def save_calculation(expression, result): 

    conn = sqlite3.connect('calculator.db') 

    c = conn.cursor() 

 

    c.execute("INSERT INTO calculations (expression, result) VALUES (?, ?)",  

              (expression, result)) 

 

    conn.commit() 

    conn.close()

创建一个主函数,处理用户的输入和输出:

python

def main(): 

    print("Welcome to the calculator!") 

    while True: 

        expression = input("Enter an expression: ") 

        result = calculate(expression) 

        if result is None: 

            print("Invalid expression!") 

        else: 

            print("Result: ", result) 

            save_calculation(expression, result)

运行主函数:

python

if __name__ == "__main__": 

main()

运算结果:

989-456

 

 

9开根号

 

 

99+9

94/8

 

 

9/0

 

 

2、UI设计

public partial class FormLogin : LayeredForm

 

 

 

        private void FormLogin_Load(object sender, EventArgs e)

        {

            yezi = new Bitmap(90, 80);

            using (Graphics g = Graphics.FromImage(yezi))

            {

                g.DrawImage(Resources.yezi3, 10, 0);

            }

            timer1.Start();

         

            string result = AccountHelper.ReadAccount();

            if (string.IsNullOrEmpty(result) == false)

            {

                string[] temp = result.Split(',');

                if (temp != null && temp.Length == 2)

                {

                    txtAccount.Text = temp[0];

                    txtPassword.Text = temp[1];

                }

            }

        }

 

 protected override void OnLayeredPaint(LayeredSkin.LayeredEventArgs e)

        {

            Graphics g = e.Graphics;

            if (cloudX > this.Width - Cloud.Width)

            {

                cloudX = 0;

            }

            else

            {

                cloudX += 0.5f;

            }

            g.DrawImage(Cloud, cloudX, 0);

 

            if (angle > 10)

            {

                RotationDirection = false;

            }

            if (angle < -10)

            {

                RotationDirection = true;

            }

            if (RotationDirection)

            {

                angle += 1;

            }

            else

            {

                angle -= 1;

            }

            using (Image temp = LayeredSkin.ImageEffects.RotateImage(yezi, angle, new Point(25, 3)))

            {

                g.DrawImage(temp, 140, 70);

            }

            base.OnLayeredPaint(e);

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            LayeredPaint();

            GC.Collect();

        }

 

        private void FormMoveMouseDown(object sender, MouseEventArgs e)

        {

            LayeredSkin.NativeMethods.MouseToMoveControl(this.Handle);

        }

则UI的设计就已经完成了

3.登录逻辑

一般的登录窗体都会有一个窗口的登录

(1)登录逻辑

首先在客户端进行初步的空值或者字符验证

 

                if (string.IsNullOrEmpty(txtAccount.Text))

                {

                    layeredLabel5.Text = "用户名不能为空!";

                    layeredLabel5.ForeColor = Color.Red;

                    txtAccount.Focus();

                    return;

                }

                if (string.IsNullOrEmpty(txtPassword.Text))

                {

                    layeredLabel5.Text = "密码不能为空!";

                    layeredLabel5.ForeColor = Color.Red;

                    txtPassword.Focus();

                    return;

                }

 

然后这里调用服务端的账号密码验证方法,来确定登录是否正确,同时返回用户信息,以供后面调用

 bool result = _client.TLoginCheckLogin(out message, out dto, txtAccount.Text, txtPassword.Text);

(2)保存密码

这里通过本地文本的读写来实现密码的保存和自动填充功能

 //保存账号信息

 if (cbRemind.Checked)

    AccountHelper.WriteAccount(txtAccount.Text, txtPassword.Text);

(4)流程图

登录流程图

 

计算流程图

 

 

历史记录查询流程图

 

 

posted @ 2023-12-04 01:08  Woljy  阅读(55)  评论(0)    收藏  举报