• 作业四


    1. 结对编程项目---四则运算

    基本功能要求:

    1) 实现一个带有用户界面的四则运算。

    2) 生成的题目不能重复。

    3) 支持负数,例如-1,-1/2,-3‘4/5等。

    需要支持的基本设定参数

    1) 题目的数量(个人项目的要求)

    2) 数值的范围(个人项目的要求)

    3) 题目中最多几个运算符

    4) 题目中或运算过程中有无有分数(比如进行整数除法的时候不能除尽)

    5) 题目中是否有乘除法

    6) 题目中是否有括号

    7) 题目中或运算过程中有无负数

    小组成员:

    王梓萱    博客地址:http://home.cnblogs.com/u/ccxx/

    工作分配:

    高亚南:设计窗体,代码规划。

    王梓萱:代码规划实现,功能调试。

      可以设定随机整数的范围,那么我首先想到的是在给出随机数的时候设定一个范围,其实很简单。拖出两个文本框,比如textBox4,textBox5,然后让这两个文本框的值转换成int类型,并让Random()方法从给出的这个范围出题。

     对于设定答题的题目数量,添加一个文本框让它存放我要答题的数量,并且判断一下,当答题数量等于预先设定的答题数量时,让它弹出Form2。

    选择计算类型时,先用数组把四种运算存了起来,然后从这个数组里随机选出一种运算显示在label3里面,这样就可以switch(label3.Text)了。 

    代码如下

    Form1.cs

    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("wrong");
                        }
                        else
                        {
                            result = int.Parse(textBox5.Text) / int.Parse(textBox6.Text);
                        }
                        return;
                }
            }
    
            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("不符合标准");
                        }
                        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.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace _Random
    11 {
    12     public partial class Form2 : Form
    13     {
    14         public Form2()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void Form2_Load(object sender, EventArgs e)
    20         {
    21             textBox1.Text = Form1.Count.ToString();
    22             textBox2.Text = Form1.right.ToString();
    23             textBox3.Text = (Form1.Count - Form1.right).ToString();
    24         }
    25 
    26     }
    27 }

    运行图如下

    总结:

    在结对编程的时候,我和王梓萱俩个先在一起列出了设计思路,然后参照书籍和网上的资料进行操作。

    王梓萱同学的优点有很多:比如有缜密的思路,心态不错,有很好的创造力。 缺点是缺乏一些耐心,瑕不掩瑜。一起编程的能使两人性格互补,然后分工更明确,效率远比一个人单干要强很多。也更能体会到编程的乐趣

  • 相关阅读:
    VML编程之shape多边型.shapetype模版.shape与curve曲线《VML极道教程》原著:沐缘华
    VML编程之image图片《VML极道教程》原著:沐缘华
    软件开发项目的风险管理 (转)
    VML编程之polyline多边型《VML极道教程》原著:沐缘华
    VML编程之标记实战与line线《VML极道教程》原著:沐缘
    xml操作类,封装了常用的对XML文件的操作功能....
    Bugzilla 安装手册
    IT项目管理之<<少林练步拳>>(转)
    Atlas学习手记(8):调用本地Web Service简单介绍(转摘)
    WF从设计器出发,到对从设计器出来的工作流的调用加载,已经完成了整个工作流的详细设计,目前工作流设计器已经完成!
  • 原文地址:https://www.cnblogs.com/GGGGGG7/p/5361903.html
Copyright © 2020-2023  润新知