结对编程-四则运算练习器
一、组员
姓名:廖云涛
学号:1500802076 博客地址
姓名:高原:
学号:1500802075 博客地址
二、代码地址:代码地址
三、上传截图:
四、功能实现:
提示运算器的注意事项
实现代码: public Form1()
{
i = 0;
Num0fQuestion = 0;
TrueAnswer = 0;
MessageBox.Show("输入整数运算结果时,若有小数,请保留两位小数。例如:0.50。\n输入分数运算结果是,请注意格式,例如:1'3/4或是-1'2/3");
InitializeComponent();
}
四则运算器的基本界面
输入题目数和数字取值范围,若未输入,则提示输入
生成题目后开始答题,在规定时间内答题,如果未在规定时间内完成,提示时间到并进入下一题
生成整数题目的实现:
private void button1_Click(object sender, EventArgs e)
{
this.button10_Click(sender, e);
bug = true;
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("请输入题目数量和范围!!!");
return;
}
size = Convert.ToInt32(textBox1.Text);
high = Convert.ToInt32(textBox2.Text);
if (i >= size)
{
i++;
MessageBox.Show("题目已做完!");
return;
}
label5.Text = T.ToString();
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
double num_1;
double num_2;
int oper;
char[] opera = new char[4] { '+', '-', '', '/' };
Random ra = new Random();
num_1 = ra.Next(0, high);
num_2 = ra.Next(0, high);
oper = ra.Next(0, 4);
while (num_2 == 0 && opera[oper] == '/')
{
num_2 = ra.Next(0, high);
}
textBox3.AppendText(Convert.ToString(num_1) + Convert.ToString(opera[oper]) + Convert.ToString(num_2) + "=\n");
if (opera[oper] == '+')
Result_1.Add(num_1 + num_2);
if (opera[oper] == '-')
Result_1.Add(num_1 - num_2);
if (opera[oper] == '')
Result_1.Add(num_1 * num_2);
if (opera[oper] == '/')
Result_1.Add(Math.Round(num_1 / num_2, 2));
i++;
}
对于计时的实现:
private void timer1_Tick(object sender, EventArgs e)
{
if (T <= 0 && i <= size)
{
timer1.Enabled = false;
textBox5.Enabled = false;
User_Result.Add("");
MessageBox.Show("时间到!");
textBox5.Enabled = true;
this.button1_Click(sender, e);
}
if (i > size)
{
textBox5.Enabled = false;
timer1.Stop();
label5.Text = "0";
return;
}
T--;
label5.Text = T.ToString();
}
查看所有题目的答案
查看自己输入的答案
查看答题的正确率
记录并生成记录,可以方便下次重启时查看答题记录
真分数运算的生成
真分数生成的实现:
private void button2_Click(object sender, EventArgs e)
{
this.button10_Click(sender, e);
bug = false;
if (textBox1.Text == "")
{
MessageBox.Show("请输入题目数量!!!");
return;
}
size = Convert.ToInt32(textBox1.Text);
if (i >= size)
{
i++;
MessageBox.Show("题目已做完!");
return;
}
label5.Text = T.ToString();
timer2.Enabled = true;
timer2.Interval = 1000;
timer2.Start();
int oper;
char[] opera = new char[4] { '+', '-', '', '/' };
Random ra = new Random();
num1 = ra.Next(0, 10);
num2 = ra.Next(0, 10);
while (num2 == 0)
num2 = ra.Next(0, 10);
Num_1.Add(num1);
Num_2.Add(num2);
Fraction(ref num1, ref num2);
if (temp != 0)
{
if (num1 != 0)
textBox3.AppendText(temp + "'" + Convert.ToString(num1) + "/" + Convert.ToString(num2));
else
textBox3.AppendText(temp.ToString());
}
if (temp == 0)
{
if (num1 != 0)
textBox3.AppendText(Convert.ToString(num1) + "/" + Convert.ToString(num2));
else
textBox3.AppendText("0");
}
oper = ra.Next(0, 4);
textBox3.AppendText(" " + opera[oper].ToString() + " ");
num1 = ra.Next(0, 10);
num2 = ra.Next(0, 10);
while (num2 == 0)
num2 = ra.Next(0, 10);
while (num1 == 0 && opera[oper] == '/')
num1 = ra.Next(0, 10);
int child = num1;
int mother = num2;
Fraction(ref child, ref mother);
if (temp != 0)
{
if (child != 0)
textBox3.AppendText(temp + "'" + Convert.ToString(child) + "/" + Convert.ToString(mother) + "=\n");
else
textBox3.AppendText(temp + "=\n");
}
if (temp == 0)
{
if (child != 0)
textBox3.AppendText(Convert.ToString(child) + "/" + Convert.ToString(mother) + "=\n");
else
textBox3.AppendText("0" + "=\n");
}
if (opera[oper] == '+')
{
Result_2.Add(Num_1[i] num2 + Num_2[i] * num1);
Result_3.Add(num2 * Num_2[i]);
}
if (opera[oper] == '-')
{
Result_2.Add(Num_1[i] * num2 - Num_2[i] * num1);
Result_3.Add(num2 * Num_2[i]);
}
if (opera[oper] == '')
{
Result_2.Add(Num_1[i] num1);
Result_3.Add(num2 * Num_2[i]);
}
if (opera[oper] == '/')
{
Result_2.Add(Num_1[i] * num2);
Result_3.Add(num1 * Num_2[i]);
}
int child1;
int mother1;
Result_4.Add(0);
child1 = Result_2[i];
mother1 = Result_3[i];
Fraction(ref child1, ref mother1, i);
if (child1 < 0)
Result_2[i] = -child1;
Result_2[i] = child1;
Result_3[i] = mother1;
if (Result_4[i] != 0)
{
if (Result_2[i] != 0)
{
if (child1 < 0)
ResultOfFrac.Add(Result_4[i] + "'" + Convert.ToString(-Result_2[i]) + "/" + Convert.ToString(Result_3[i]));
else
ResultOfFrac.Add(Result_4[i] + "'" + Convert.ToString(Result_2[i]) + "/" + Convert.ToString(Result_3[i]));
}
else
ResultOfFrac.Add(Convert.ToString(Result_4[i]));
}
if (Result_4[i] == 0)
{
if (Result_2[i] != 0)
ResultOfFrac.Add(Convert.ToString(Result_2[i]) + "/" + Convert.ToString(Result_3[i]));
else
ResultOfFrac.Add(Convert.ToString(Result_4[i]));
}
i++;
}
输入答案并查看答案
分数运算正确率
文件的生成记录和写入的代码截图
对于记录已做题数和答对题数,因为要求在程序重新运行时能保存以往的记录,所以要有文件的创建读取和修改。
private void button8_Click(object sender, EventArgs e)
{
if (!File.Exists("F:\TestTxt.txt"))
{
FileStream File = new FileStream(@"F:\TestTxt.txt", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(File);
sw.WriteLine(Num0fQuestion + "\n" + TrueAnswer);
MessageBox.Show("答题总数:" + Num0fQuestion + "\n答对题数:" + TrueAnswer);
sw.Close();
File.Close();
Num0fQuestion = 0;
TrueAnswer = 0;
}
else
{
FileStream File = new FileStream(@"F:\TestTxt.txt", FileMode.Open, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(File);
StreamReader sr = new StreamReader(File);
int A = Convert.ToInt32(sr.ReadLine());
int B = Convert.ToInt32(sr.ReadLine());
sw.Close();
sr.Close();
File.Close();
A += Num0fQuestion;
B += TrueAnswer;
MessageBox.Show("答题总数:" + A + "\n答对题数:" + B);
File = new FileStream(@"F:\TestTxt.txt", FileMode.Truncate, FileAccess.Write);
sw = new StreamWriter(File);
sw.WriteLine(A + "\n" + B);
sw.Close();
File.Close();
Num0fQuestion = 0;
TrueAnswer = 0;
}
}
五、结对编程的心得体会:
此次作业是用C#的Winform进行编写的,因为之前在选修课有学过C#的相关知识,所以对于Winform开发还是比较熟悉的。整个结对编程过程中,我负责了前期代码的编写,而我的队友则负责监督代码中的错误以及bug测试。大一大二这两年基本上编程都是自己一个人完成的,遇到不会的问题会自己从网上查些资料,或是借鉴其他人的项目。而通过这次结对编程,我明显体会到了编程效率的提高,两个人在一起编程可以有效避免一些可笑的低级错误,例如数组的溢出,动态数组的定义等等小问题,所以编程质量也有了很大的提高,我认为结对编程适合于时间紧迫的开发任务,两个人可以在一起分析问题解决问题,通过不停的头脑风暴和知识碰撞来解决问题。但是也会因为两个人的编程习惯不同而导致任务进行的过程中出现分歧,在算法逻辑方面会有不同的争议,这个时候就需要两个人来妥善解决,是迁就还是改变。所以结对编程在解决一个人完成比较吃力的项目时是有很大好处的,毕竟一个人的能力,知识等方面不如两个人那么全面,结对编程可以弥补个人能力不足的问题。