数据库之计算器

本文介绍了如何使用数据库对算式题目进行存储和查询,并通过答题系统实现学生答题与反馈,包括题目的增加、计时功能、答案验证及成绩统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、具体代码

类代码

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace szys
{
    class Class2
    {
        public int Sum;
        
        public int Answer
        {
            get { return Sum; }

        }
        public int Max(int n1,int n2,char yunsuanfu)
        {
            if (yunsuanfu == '+')
            {
                return Sum = n1 + n2;
            }
            else if (yunsuanfu == '-')
            {
                if (n1 > n2)
                {
                    return Sum = n1 - n2;
                }
                else
                {
                    MessageBox.Show("第一个数要大于第二个数!");
                    
                }
            }
            else if (yunsuanfu == '/')
            {
                if (n2 == 0 || n2 < 0)
                {
                    MessageBox.Show("分母不能为0且大于0!");
                }
                else
                {
                    return Sum = n1 / n2;
                }
            }
            else if (yunsuanfu == '*')
            {
                return Sum = n1 * n2;

            }
            return Sum;


        }

    }
    }

 

Form1代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace szys
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        public static int right = 0;
        public static int Count = 0;
        private int t;
     
        private void button5_Click(object sender, EventArgs e)
        {
           
            string strCon = @"Data Source=.;Initial Catalog=jisuanqi;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strCon);
            try
            {
                sqlcon.Open();
                string slt = @"insert into jisuan(Number1,Operator,Number2)" + " values('" + textBox1.Text + "','" + textBox4.Text + "','" + textBox2.Text + "')";
                SqlCommand cmd = new SqlCommand(slt, sqlcon);
                cmd.ExecuteNonQuery();
                MessageBox.Show("出题成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("出题失败"+ex.ToString());
            }
            sqlcon.Close();

            textBox1.Clear();
            textBox4.Clear();
            textBox2.Clear();

        }
      
        private void button2_Click(object sender, EventArgs e)
        {
            
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox4.Enabled = false;
            textBox1.BackColor = Color.Yellow;
            textBox4.BackColor = Color.Yellow;
            textBox2.BackColor = Color.Yellow;
            textBox3.BackColor = Color.Yellow;
            string strCon = @"Data Source=.;Initial Catalog=jisuanqi;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strCon);
            int i = 1;
            sqlcon.Open();
            string slt = @"select * from jisuan";
            SqlCommand cmd = new SqlCommand(slt, sqlcon);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            textBox1.Text = dt.Rows[i][0].ToString();
            textBox4.Text = dt.Rows[i][1].ToString();
            textBox2.Text = dt.Rows[i][2].ToString();
            sqlcon.Close();
            
            
        }

        private void textBox3_MouseClick(object sender, MouseEventArgs e)//计时;
        {
            label3.Text = t.ToString();
            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            t = t + 1;
            label3.Text = t.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("一共用时" + label3.Text + "");
            Form2 frm2 = new Form2();
            frm2.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int a = int.Parse(textBox1.Text);
            int b = int.Parse(textBox2.Text);
            Char c = Convert.ToChar(textBox4.Text.Trim());
            Class2 con = new Class2();
            con.Max(a, b, c);
            if (con.Answer == int.Parse(textBox3.Text))
            {
                MessageBox.Show("恭喜你,小朋友!回答正确!");
                right++;
                Count++;
            }

            else
            {

                MessageBox.Show("回答错误,继续加油!小朋友");
                Count++;

            }
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
           
        }


    }
}

Form2代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace szys
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.Count.ToString();
            textBox2.Text = Form1.right.ToString();
            textBox4.Text = ((Form1.right / (double)(Form1.Count)) * 100).ToString() + "%";

        }

     }
 }

 

二、测试

 

 

三、总结

         用数据库对已出算式题的保存打开就是对数据库的增加查询,首先建一个表,列名有第一个数,运算符,第二个数,然后编写增加查询语句。

转载于:https://www.cnblogs.com/twinkle-0908/p/5083616.html

### 创建基于Access数据库的乘法计算器 #### 设计思路 为了构建一个简单的乘法计算器,在Microsoft Access中主要依赖于窗体设计和VBA (Visual Basic for Applications) 编程来完成基本逻辑。核心在于利用`Eval()`函数执行数学运算,该函数能够解析并计算作为参数传递给它的字符串形式的表达式[^1]。 #### 步骤概述 #### 数据库对象准备 - **创建新数据库**:启动Access应用程序,并新建空白桌面型数据库。 - **建立用户界面(窗体)**: - 插入一个新的窗体用于接收用户的输入。 - 添加两个文本框控件分别命名为txtNumber1, txtNumber2用来获取被乘数与乘数;再加第三个只读文本框名为txtResult展示最终的结果。 - 设置标签说明各字段用途以便使用者理解如何操作此工具。 - 加入命令按钮cmdCalculate触发实际的相乘动作。 #### VBA编程实现业务逻辑 当点击“计算”按钮时,应调用一段宏或编写事件过程来进行具体的数值处理: ```vba Private Sub cmdCalculate_Click() Dim num1 As Double Dim num2 As Double ' 获取用户输入的数据 If IsNumeric(Me.txtNumber1.Value) And IsNumeric(Me.txtNumber2.Value) Then num1 = CDbl(Me.txtNumber1.Value) num2 = CDbl(Me.txtNumber2.Value) ' 使用 Eval 函数进行乘法运算并将结果显示出来 Me.txtResult.Value = Eval(num1 & "*" & num2)[^1] Else MsgBox "请输入有效的数字", vbExclamation, "提示" End If End Sub ``` 上述代码片段展示了如何捕获来自前端UI组件的信息并通过内置方法快速得出答案。值得注意的是这里采用了严格的数据验证措施确保只有合法的浮点数才会参与后续的操作流程之中。 #### 测试和完善 最后一步是对整个程序进行全面测试,确认其能正常工作无误之后还可以考虑增加额外的功能比如历史记录保存等功能提升用户体验感。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值