基于GUI的四则运算

时间:2021-06-07 17:50:22

基于GUI的四则运算

*强 201421123028 连永刚 201421123014 林方言 201421123023

coding 地址 https://git.coding.net/lizhiqiang0x01/GUI-sizeyunsuan.git

一、简述题目要求:


1、除了整数之外,还要支持真分数的四则运算,真分数的运算,例如:1/6 + 1/8 = 7/24

2、运算符为 +, −, ×, ÷

3、并且要求能处理用户的输入,并判断错误,打分统计正确率

4、要求能处理用户输入的真分数,如 1/2, 5/12 等

5、程序基于GUI 界面

6、记录用户的对错总数,程序退出再启动的时候,能把以前的对错数量保存并在此基础上增量计算

7、有计时功能,能显示用户开始答题后的消耗时间

8、界面支持中文简体/中文繁体/英文,用户选择一种


二、实现步骤:


a、需求分析

写一段程序可以生成GUI界面,在GUI 界面上可以进行中文简体/中文繁体/英文切换,在做题过程中需要计时并显示用户答题时间,在用户输入答案时,需要对输入的参数进行过滤,当用户输入非法文字,例如:“#,@,我不知道”等会被检测到,并给予相应的错误提示,在用户结束答题后,需要显示用户答题后的各种参数并实现可以查看错题库功能。

b、功能设计

逻辑结构图

基于GUI的四则运算

基本功能

计时功能,切换语言功能,兼容获取分式和整数参数功能,生成真分式题目以及最简分式功能,运算功能,校验答案功能。

扩展功能

过滤非法字符功能,题目进度功能,生成用户错题库功能

高级功能

多级运算功能

c、设计实现

为满足功能需求,下面主要写了八个函数:

public void add();      //加法运算
public void sub(); //减法运算
public void mul(); //乘法运算
public void div(); //除法运算
public void ran(); //生成题目
public void check(); //校验答案
public string StringClear(string ai) //过滤非法字符
public void thread() //计时功能

d、代码说明[source file]

  • 生成窗体功能
        private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "请输入题目数(满分100)";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(, );
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = "v1.00";
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "开始做题";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(, );
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(, );
this.textBox2.TabIndex = ;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.CheckAlign = System.Drawing.ContentAlignment.TopLeft;
this.radioButton1.Checked = true;
this.radioButton1.Location = new System.Drawing.Point(, );
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(, );
this.radioButton1.TabIndex = ;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "中文";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(, );
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(, );
this.radioButton2.TabIndex = ;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "English";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(, );
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(, );
this.radioButton3.TabIndex = ;
this.radioButton3.Text = "中文繁體";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.radioButton3);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "中文";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout(); }
  • 计时功能
        public void thread()   //创建线程实现计时功能
{
int sec = ;
int min = ;
int hour = ; while (true)
{
sec++;
if (sec >=)
{
min++;
sec =;
if (min >=)
{
hour++;
min =;
}
}
s.label10.Text = hour + ":" + min + ":" + sec;
Thread.Sleep();
if (s.next == ) {
break;
}
} } ThreadStart th = new ThreadStart(thread);
Console.WriteLine("In Main: Creating the Child thread");
Thread the = new Thread(th);
the.Start();
  • 切换语言功能
        private void radioButton2_CheckedChanged(object sender, EventArgs e)    //切换成英文
{
this.label1.Text = "Please enter the subject number";
this.button1.Text = "start";
s.label8.Text = "Time";
s.label1.Text = "Schedule";
s.button1.Text = "NEXT";
this.Text = "English";
s.Text = "English";
} private void radioButton1_CheckedChanged(object sender, EventArgs e) //切换成中文简体
{
this.label1.Text = "请输入题目数(满分100)";
this.button1.Text = "开始做题";
s.label8.Text = "时间";
s.label1.Text = "题目进度";
s.button1.Text = "下一题";
this.Text = "中文";
s.Text = "中文";
} private void radioButton3_CheckedChanged(object sender, EventArgs e) //切换成中文繁体
{
this.label1.Text = "請輸入題目數(滿分100)";
this.button1.Text = "開始做題";
s.label8.Text = "時間";
s.label1.Text = "題目進度";
s.button1.Text = "下壹題";
this.Text = "中文繁體";
s.Text = "中文繁體";
}
  • 过滤非法字符功能
        public string StringClear(string ai)
{
bool temp = bolnum(ai);
if (temp == true)
{
return ai;
}
else
{
cro = ;
if (this.Text == "中文")
{
MessageBox.Show("输入错误,请正确输入字符!例如:12,1/2");
}
else if (this.Text == "English")
{
MessageBox.Show("Input error, please correct input character! For example:12,1/2");
}
else
{
MessageBox.Show("輸入錯誤,請正確輸入字符!例如:12,1/2");
}
this.textBox1.Text = "";
return "";
}
} public bool bolnum(string temp) //若为非法字符,返回false
{
for (int i = ; i < temp.Length; i++)
{
byte tempByte = Convert.ToByte(temp[i]);
if ((tempByte > ) && (tempByte < ))
return true;
}
return false;
}
  • 生成用户错题库功能
      FileStream f = new FileStream("cuoti.txt", FileMode.Append, FileAccess.Write);
StreamWriter fi = new StreamWriter(f);
fs = "你的答案:" + this.label2.Text + this.label4.Text + this.label5.Text + this.label9.Text + ai + " 正确答案:" + au + " " + "\r\n";
fi.Write(fs);
fi.Flush();
fi.Close();
f.Close();
  • 多级运算功能
        public void select()
{
int l1, l2;
Random ra = new Random();
int x = ra.Next(, );
switch (x) //产生一级运算
{
case : add(); break;
case : sub(); break;
case : mul(); break;
case : div(); break;
}
int y = ra.Next(, );
switch (y) //产生二级运算
{
case :
if (q6 == ) this.label12.Text = q5.ToString();
else this.label12.Text = q5 + "/" + q6;
this.label13.Text = "+";
this.label14.Text = "";
this.label15.Text = "";
l1 = r1;l2=r2;
r1 = l1 * q6 + l2 * q5;
r2 = l2 * q6;
break;
case :
if (q6 == ) this.label12.Text = q5.ToString();
else this.label12.Text = q5 + "/" + q6;
this.label13.Text = "-";
this.label14.Text = "(";
this.label15.Text = ")";
l1 = r1;l2=r2;
r1 = l1 * q5 - l2 * q6;
r2 = l2 * q6;
break;
case :
if (q6 == ) this.label12.Text = q5.ToString();
else this.label12.Text = q5 + "/" + q6;
this.label13.Text = "×";
this.label14.Text = "(";
this.label15.Text = ")";
l1 = r1;l2=r2;
r1 = l1 * q5;
r2 = l2 * q6;
break;
case :
if (q6 == ) this.label12.Text = q5.ToString();
else this.label12.Text = q5 + "/" + q6;
this.label13.Text = "÷";
this.label14.Text = "(";
this.label15.Text = ")";
l1 = r1;l2=r2;
this.r1 = this.q5 * l2;
this.r2 = this.q6 * l1;
break;
case :
this.label12.Text = "";
this.label13.Text = "";
this.label14.Text = "";
this.label15.Text = "";
break;
}
}
  • 校验答案功能
        public void check()
{
int w = ;
for (int i = ; i < ; i++)
{
if (r1 % i == && r2 % i == )
{
w = i;
}
}
r1 = r1 / w; r2 = r2 / w;
ai = StringClear(this.textBox1.Text);
if (cro != )
{
if (r2 == )
au = this.r1.ToString();
if (r2 != )
au = r1 + "/" + r2;
if (ai == au)
{
n1++;
}
if (ai != au)
{
FileStream f = new FileStream("cuoti.txt", FileMode.Append, FileAccess.Write);
StreamWriter fi = new StreamWriter(f);
fs = "你的答案:" + this.label2.Text + this.label4.Text + this.label5.Text + this.label9.Text + ai + " 正确答案:" + au + " " + "\r\n";
fi.Write(fs);
fi.Flush();
fi.Close();
f.Close();
if (this.Text == "中文")
{
MessageBox.Show("回答错误!正确答案:" + au);
}
else if (this.Text == "English")
{
MessageBox.Show("Wrong answer!The correct answer:" + au);
}
else
{
MessageBox.Show("回答錯誤!正確答案:" + au);
}
}
}
}

e、测试运行

1.首页(中文)及过滤错误字符:

基于GUI的四则运算

基于GUI的四则运算

2.检查答案,得出成绩报告单:

基于GUI的四则运算

基于GUI的四则运算

3.查看错题及其他语言的部分截图:

基于GUI的四则运算

基于GUI的四则运算基于GUI的四则运算

4、多级运算高级功能

基于GUI的四则运算

GIF过程浏览

基于GUI的四则运算


三、PSP


PSP2.1 Personal Software Process Stages Estimated time(h) actual time(h)
Planning 计划 1 1.5
· Estimate 估计这个任务需要多少时间 24 40
Development 开发 24 34
· Analysis 需求分析 (包括学习新技术) 1 1.2
· Design Spec 生成设计文档 7 10
· Design Review 设计复审 0.5 0.5
· Coding Standard 代码规范 0.5 0.8
· Design 具体设计 1.5 1.5
· Coding 具体编码 20 30
· Code Review 代码复审 10 15
· Test 测试(自我测试,修改代码,提交修改) 1 3
Reporting 报告 20 25
· 测试报告 1 1.5
· 计算工作量 0.5 1
· 并提出过程改进计划 2 4

四、总结


      先来一片面包:队友之前作业都是用c++写的,都对c#比较熟悉,代码以我的为基础进行GUI转换。

      再把肉放上:首先我们设计了一套流程图,根据流程图具体分析问题,发现问题,解决问题,问题的解决方法都在博客上做了总结,我们共同编写生成题目功能,实现动态计时功能,错题库功能,语言切换功能,字符过滤功能,对话框的弹出功能,在代码复审期间我们做了加减乘除运算优化升级和代码的重构,代码格式的规范,碰到难点,便和队友lianyg(连永刚)Dialect(林方言)探讨,共同解决,连永刚同学缺点是做事慢了一点,优点持之以恒,最终找到解决方法,林方言同学缺点是对代码语法不是太熟悉,不过他确实挺好问,理解能力好。

      然后再来一片面包:经过了两天的共同奋斗,从刚开始对GUI的一无所知,到将四个GUI窗体灵活调用,最终能实现所有功能,并让界面最大程度简洁化,提高界面整洁度。

由于要用C#做成GUI界面,并且内容新加了其他功能,下面总结一下这次我们所遇到难点

问题一:如何产生GUI界面

解决该问提主要是借助于VS2013平台可自动生成窗体控件,然后阅读自动生成的源码,从中学习并加以利用。

问题二:计时功能

由于要计时功能,使时间在窗体上动态变化,这就意味这要使用线程,从而可以保证主线程可以继续监听消息。

问题三:过滤功能

在用户输入答案,如果输入非法字符就会被过滤并得到提示,在这里主要是将用户输入的值转化成 char ,然后将其与 ASCII 值进行对比,从而实现此功能。

   We are a team and we work as a team!

   基于GUI的四则运算基于GUI的四则运算