結對程式設計項目---四則運算
一、基本要求
- 實作一個帶有使用者界面的四則運算。
- 生成的題目不能重複。
- 支援負數,例如-1,-1/2,-3‘4/5等。
- 題目的數量(個人項目的要求)
- 數值的範圍
- 題目中最多幾個運算符
- 題目中是否有乘除法
- 題目中或運算過程中有無負數
根據老師的要求我們做的程式中解決了以上的問題。因為在第一次制作的四則運算中我的代碼就已經制作了一個使用者操作的界面,隻是比較粗糙不夠完善,是以這次我和我的組員完善了四則運算的界面和一些具體的功能。在這些要求中的運算符有幾個的問題裡,第一次制作的四則運算的運算符是做了三個,是以這次依舊使用三個運算符,沒有進行大的修改。
二、小組成員
小組成員:李雲龍、劉科宇

三、源代碼
源代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 四則運算;
namespace _RandomNum
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static int Count = 0;
private int t = 60;
public static int right = 0;
private void button1_Click(object sender, EventArgs e)
{
label2.Text = t.ToString();
timer1.Enabled = true;
timer1.Interval = 1000;
timer1.Start();
}
private void RDN()
{
Random rd = new Random();
int r1, r2;
if (textBox2.Text == "" && textBox3.Text == "")
{
MessageBox.Show("請輸入取值範圍!");
return;
}
r1 = rd.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text));
r2 = rd.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text));
textBox1.Text = r1.ToString();
textBox2.Text = r2.ToString();
string[] fuhao = new string[] { "+", "-", "×", "÷" };
label3.Text = fuhao[rd.Next(0, 5)];
int result = 0;
switch (label3.Text)
{
case "+":
result = int.Parse(textBox4.Text) + int.Parse(textBox5.Text);
return;
case "-":
if (int.Parse(textBox4.Text) >= int.Parse(textBox5.Text))
{
result = int.Parse(textBox4.Text) - int.Parse(textBox5.Text);
}
else
{
MessageBox.Show("請回車進行下一題!此題不計入答題總數!");
}
return;
case "×":
result = int.Parse(textBox5.Text) * int.Parse(textBox6.Text);
return;
case "÷":
if (textBox5.Text == "0")
{
MessageBox.Show("分母為0,不計入答題總數,請回車繼續答題!");
}
else
{
result = int.Parse(textBox5.Text) / int.Parse(textBox6.Text);
}
return;
}
}
private void RandomNum()
{
Random ran = new Random();
int n1, n2;
if (textBox2.Text == "" && textBox3.Text == "")
{
MessageBox.Show("請輸入取值範圍!");
return;
}
n1 = ran.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text));
n2 = ran.Next(int.Parse(textBox2.Text), int.Parse(textBox3.Text));
textBox1.Text = n1.ToString();
textBox2.Text = n2.ToString();
textBox3.Text = "";
}
private void timer1_Tick(object sender, EventArgs e)
{
}
private void button10_Click(object sender, EventArgs e)
{
timer1.Stop();
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int result = 0;
string s = label3.Text;
if (Count == int.Parse(textBox6.Text))
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
switch (s)
{
case "+":
result = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
break;
case "-":
if (int.Parse(textBox1.Text) >= int.Parse(textBox2.Text))
{
result = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
}
else
{
MessageBox.Show("請回車進行下一題!此題不計入答題總數!");
}
break;
case "×":
result = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
break;
case "÷":
if (textBox2.Text == "0")
{
MessageBox.Show("分母為0,不計入答題總數,請回車繼續答題!");
}
else
{
result = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);
}
break;
}
if (e.KeyCode == Keys.Enter)
{
if (textBox3.Text == result.ToString())
{
right++;
Count++;
MessageBox.Show("回答正确!");
}
else
{
if (textBox2.Text == "0" || int.Parse(textBox1.Text) - int.Parse(textBox2.Text) < 0)
{
RandomNum();
}
else
{
MessageBox.Show("答題錯誤!");
RandomNum();
Count++;
}
}
RandomNum();
}
}
private void button3_Click(object sender, EventArgs e)
{
label7.Text = button1.Text;
RandomNum();
}
private void button4_Click(object sender, EventArgs e)
{
label7.Text = button2.Text;
RandomNum();
}
private void button5_Click(object sender, EventArgs e)
{
label7.Text = button3.Text;
RandomNum();
}
private void button6_Click(object sender, EventArgs e)
{
label7.Text = button4.Text;
RandomNum();
}
}
}
form2:
using _RandomNum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 四則運算
{
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();
textBox3.Text = (Form1.Count - Form1.right).ToString();
}
}
}
四、運作截圖
五、總結
由于我們兩個底子薄,本次作業難度便較大,但經過我們倆查資料和請教同學把 本次作業完成了,但是還有很多地方不足希望老師批請指正,通過本次作業我們也認識到程式設計的重要性,在以後的學習中更加努力。