C#窗体随机四则运算 (第四次作业)

---恢复内容开始---

增量内容:1)处理用户的错误输入,比如输入字母或符号等,处理除法运算中分母为0的情况,处理结果为负数的情况,保证是小学水平不出现负数,比如不能出现5-8=-3这种情况;2)用户可以设定倒计时;  3)用户可以设定随机整数的范围和题目数量;4)用户可以选择哪种计算类型,比如加减乘除,或可选择软件随机生成四则运算中的一种;  5)用户可以选择随机生成的题目中是否带有小括号,比如(2+3)*5,如果是gui程序,添加这个功能可以用复选框实现;    6)保证生成过的题目不再重复出现。

设计思路:上次作业我们用的是控制台,这次原本还想用控制台,但是我的队友洪亮建议我用窗体,所以这次做这道题完全是把开始的作业都做了一遍,开始的时候其实还蛮顺利的,知道要实现用户可以输入范围的时候遇到了一些小问题,但是很快就解决了,完成其它实现的时候我们也会有一些分歧,不如要不要做个提示告诉用户在同一个界面上出现你已经做了多少道题,我说不要,他要,但是还是写上了,但是等到界面全部完成后,感觉加上还蛮合适的,就这样我们每个人负责一些任务,遇到问题我们就在一起解决,然而比一个

人做轻松了好多。下面是我们结对时的照片!!!

需求分析:增加的内容前几个还好啦,不是很难,但是要实现混合运算有点想不到方法,我们也上查了一下,但是网上介绍的方法比如栈,树的方式不是很懂,我们也实验了好多可是都没成功,有好多疑问,因为时间比较紧,所以课后做好了。

namespace _000000
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int Count = 0;
        public static int right = 0;
        public static int fault = 0;  
private void RandomNum() { Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (a >= b) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } else if (a < b) { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } } private void timer1_Tick(object sender, EventArgs e) { if (Convert.ToInt32(textBox8.Text) <= 0) {

                  timer1.Enabled = false;
                  textBox3.Enabled = false;
                  MessageBox.Show("时间到!");
                  textBox3.Enabled = false;
                  Form2 frm2 = new Form2();
                  frm2.ShowDialog()

            }

              int t = Convert.ToInt32(textBox8.Text);

              t = t - 1;

              textBox8.Text = t.ToString();

        }

private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            int sum = 0;
            string m = label3.Text;

            if (m == "+")
            {

                sum = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    {
                        fault++;
                        RandomNum();
                    }
                }
            }

            else if (m == "-")
            {
                sum = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    {
                        fault++;
                        RandomNum();
                    }
                }
            }



            else if (m == "x")
            {
                sum = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);
                if (e.KeyCode == Keys.Enter)
                {
                    if (textBox3.Text == sum.ToString())
                    {
                        right++;

                        RandomNum();
                    }
                    else
                    { fault++;
                    RandomNum();
                    }
                }
            }


            else
            {
                sum = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);

            }
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            label3.Text = "+";
int t = Convert.ToInt32(textBox8.Text); label2.Text
= t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button2_Click(object sender, EventArgs e) { label3.Text = "-";
int t = Convert.ToInt32(textBox8.Text); label2.Text
= t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button3_Click(object sender, EventArgs e) { label3.Text = "x";
int t = Convert.ToInt32(textBox8.Text); label2.Text
= t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); RandomNum(); } private void button4_Click(object sender, EventArgs e) { label3.Text = "/";
int t = Convert.ToInt32(textBox8.Text); label2.Text
= t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); Random ran = new Random(); int a, b; string m; a = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); b = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text)); if (b != 0) { textBox1.Text = a.ToString(); textBox2.Text = b.ToString(); textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } else { m = b.ToString(); textBox2.Text = a.ToString(); textBox1.Text = m; textBox3.Text = ""; Count++; textBox6.Text = Form1.Count.ToString(); if (textBox7.Text == textBox6.Text) { MessageBox.Show("你已做完计算!"); } } } private void button7_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.ShowDialog(); } private void textBox4_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox4.Text, out result) == false) { if (!(textBox4.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox4.Clear(); } } } private void textBox5_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox5.Text, out result) == false) { if (!(textBox5.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox5.Clear(); } } } private void textBox7_TextChanged(object sender, EventArgs e) { int result; if (int.TryParse(textBox7.Text, out result) ==false) { if (!(textBox7.Text == "")) { MessageBox.Show("请输入数字!", "提示"); textBox7.Clear(); } } } private void button5_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); textBox5.Clear(); textBox7.Clear(); } } }

 

PsP耗时

总结:没到看到和自己的伙伴完成一个项目时,顿时感觉付出的辛苦和努力都很值得,特别是看到队友那嘴角上扬的笑容,他心里应该会很自豪吧,但我也会感到非常快乐。结对编程是第二次做,比起第一次感觉配合的更默契一点,希望以后还是结对编程。

最后有一个小小的请求,希望老师能提示一下实现混合运算的方法如果老师能有个例子就更好了!!!

 

 

 

 


             
           
             

           

         

---恢复内容结束---

转载于:https://www.cnblogs.com/daidaide/p/4905351.html

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值