結對編程-四則運算練習器git
姓名:廖雲濤
學號: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();
}dom
真分數生成的實現:
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;
}
}this
這次做業是用C#的Winform進行編寫的,由於以前在選修課有學過C#的相關知識,因此對於Winform開發仍是比較熟悉的。整個結對編程過程當中,我負責了前期代碼的編寫,而個人隊友則負責監督代碼中的錯誤以及bug測試。大一大二這兩年基本上編程都是本身一我的完成的,遇到不會的問題會本身從網上查些資料,或是借鑑其餘人的項目。而經過此次結對編程,我明顯體會到了編程效率的提升,兩我的在一塊兒編程能夠有效避免一些好笑的低級錯誤,例如數組的溢出,動態數組的定義等等小問題,因此編程質量也有了很大的提升,我認爲結對編程適合於時間緊迫的開發任務,兩我的能夠在一塊兒分析問題解決問題,經過不停的頭腦風暴和知識碰撞來解決問題。可是也會由於兩我的的編程習慣不一樣而致使任務進行的過程當中出現分歧,在算法邏輯方面會有不一樣的爭議,這個時候就須要兩我的來妥善解決,是遷就仍是改變。因此結對編程在解決一我的完成比較吃力的項目時是有很大好處的,畢竟一我的的能力,知識等方面不如兩我的那麼全面,結對編程能夠彌補我的能力不足的問題。