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

同组人:郝哲 黄云龙

评价:在这次作业完成中我们都积极地查询了有关资料,并向大神咨询获得了帮助,学习的过程中更更加深入的了解了C#的编写。

感谢:感谢同组同学的帮助,感谢大神的辅导,感谢百度贴吧大神的讲解。

//事件响应

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 WindowsFormsApplication1

{

    public partial class Form1 : Form

    {  

        private int a=0,b=0;//数据

        private string op;//运算符

        private int result;///结果

        private Random rd = new Random();///随机数

        

        public Form1()

        {

            InitializeComponent();

        }

        private void buttonQeustion_Click(object sender, EventArgs e)

        { 

            int c = 0; 

            c=rd.Next(4);

            a=rd.Next(9);

            b = rd.Next(9);

           /*while (c!=0|c!=2){

                if (c == 1)

                {

                    c = rd.Next(4);

                }

                else if(b==0){

                    b = rd.Next(9);

                }

            }*/

           

            switch(c){

                case 0: op = "+"; result = a + b; break;

                case 1: op = "-"; if (a >= b) //去除a-b小于0的情况

                                    {

                                        result = a - b;

                                    }

                                    else

                                    {

                                        a += rd.Next(9,10);

                                        result = a - b;

                                    };break;

                case 2: op = "*"; result = a * b; break;

                case 3: op = "/"; if (b != 0)///分母为零的情况

                                    { 

                                        result = a / b;

                                    }

                                    else 

                                    { 

                                        b = a;

                                        result = a / b;

                                    };  break;

            }

            labela.Text = a.ToString();

            labelb.Text = b.ToString();

            labelop.Text = op;

            textAnswer.Text = "";

        }

        private void buttonJudge_Click(object sender, EventArgs e)

        {

            string stringAnswer = textAnswer.Text;

            double resultAnswer = double.Parse(stringAnswer);

            string showStar = "" + a + op + b + "=" + stringAnswer;

            if (textAnswer.Text != "")

            {

                if (resultAnswer == result)

                {

                    showStar += "       *";

                }

                else

                {

                    showStar += "       X";

                }

            }

            else { 

                 

                 textAnswer.Text="答案栏目不能为空";

                 return;

            }

            listBox1.Items.Add(showStar);

        }

        private void buttonClose_Click(object sender, EventArgs e)

        {

           Close();

        }

       

    }

}

/////控件定义及其设置

namespace WindowsFormsApplication1

{

    partial class Form1

    {

        /// <summary>

        /// 必需的设计器变量。

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// 清理所有正在使用的资源。

        /// </summary>

        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows 窗体设计器生成的代码

        /// <summary>

        /// 设计器支持所需的方法 不要

        /// 使用代码编辑器修改此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {

            this.labela = new System.Windows.Forms.Label();

            this.labelop = new System.Windows.Forms.Label();

            this.labelb = new System.Windows.Forms.Label();

            this.buttonQeustion = new System.Windows.Forms.Button();

            this.buttonJudge = new System.Windows.Forms.Button();

            this.listBox1 = new System.Windows.Forms.ListBox();

            this.buttonClose = new System.Windows.Forms.Button();

            this.textAnswer = new System.Windows.Forms.TextBox();

            this.equals = new System.Windows.Forms.Label();

            this.SuspendLayout();

            // 

            // labela

            // 

            this.labela.AutoSize = true;

            this.labela.Location = new System.Drawing.Point(40, 23);

            this.labela.Name = "labela";

            this.labela.Size = new System.Drawing.Size(0, 12);

            this.labela.TabIndex = 1;

            // 

            // labelop

            // 

            this.labelop.AutoSize = true;

            this.labelop.Location = new System.Drawing.Point(90, 25);

            this.labelop.Name = "labelop";

            this.labelop.Size = new System.Drawing.Size(0, 12);

            this.labelop.TabIndex = 2;

            // 

            // labelb

            // 

            this.labelb.AutoSize = true;

            this.labelb.Location = new System.Drawing.Point(139, 23);

            this.labelb.Name = "labelb";

            this.labelb.Size = new System.Drawing.Size(47, 12);

            this.labelb.TabIndex = 3;

            this.labelb.Text = "       ";

            // 

            // buttonQeustion

            // 

            this.buttonQeustion.Location = new System.Drawing.Point(38, 92);

            this.buttonQeustion.Name = "buttonQeustion";

            this.buttonQeustion.Size = new System.Drawing.Size(75, 23);

            this.buttonQeustion.TabIndex = 6;

            this.buttonQeustion.Text = "出题";

            this.buttonQeustion.UseVisualStyleBackColor = true;

            this.buttonQeustion.Click += new System.EventHandler(this.buttonQeustion_Click);

            // 

            // buttonJudge

            // 

            this.buttonJudge.Location = new System.Drawing.Point(154, 92);

            this.buttonJudge.Name = "buttonJudge";

            this.buttonJudge.Size = new System.Drawing.Size(75, 23);

            this.buttonJudge.TabIndex = 7;

            this.buttonJudge.Text = "判定";

            this.buttonJudge.UseVisualStyleBackColor = true;

            this.buttonJudge.Click += new System.EventHandler(this.buttonJudge_Click);

            // 

            // listBox1

            // 

            this.listBox1.FormattingEnabled = true;

            this.listBox1.ItemHeight = 12;

            this.listBox1.Location = new System.Drawing.Point(38, 137);

            this.listBox1.Name = "listBox1";

            this.listBox1.Size = new System.Drawing.Size(271, 112);

            this.listBox1.TabIndex = 8;

            // 

            // buttonClose

            // 

            this.buttonClose.Location = new System.Drawing.Point(276, 91);

            this.buttonClose.Name = "buttonClose";

            this.buttonClose.Size = new System.Drawing.Size(75, 23);

            this.buttonClose.TabIndex = 9;

            this.buttonClose.Text = "关闭";

            this.buttonClose.UseVisualStyleBackColor = true;

            this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);

            // 

            // textAnswer

            // 

            this.textAnswer.Location = new System.Drawing.Point(254, 19);

            this.textAnswer.Name = "textAnswer";

            this.textAnswer.Size = new System.Drawing.Size(100, 21);

            this.textAnswer.TabIndex = 10;

            // 

            // equals

            // 

            this.equals.AutoSize = true;

            this.equals.Location = new System.Drawing.Point(200, 25);

            this.equals.Name = "equals";

            this.equals.Size = new System.Drawing.Size(11, 12);

            this.equals.TabIndex = 11;

            this.equals.Text = "=";

            // 

            // Form1

            // 

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(393, 261);

            this.Controls.Add(this.equals);

            this.Controls.Add(this.textAnswer);

            this.Controls.Add(this.buttonClose);

            this.Controls.Add(this.listBox1);

            this.Controls.Add(this.buttonJudge);

            this.Controls.Add(this.buttonQeustion);

            this.Controls.Add(this.labelb);

            this.Controls.Add(this.labelop);

            this.Controls.Add(this.labela);

            this.Name = "Form1";

            this.Text = "小学四则运算训练";

            this.ResumeLayout(false);

            this.PerformLayout();

        }

        #endregion

     

        private System.Windows.Forms.Label labela;

        private System.Windows.Forms.Label labelop;

        private System.Windows.Forms.Label labelb;

    

        private System.Windows.Forms.Button buttonQeustion;

        private System.Windows.Forms.Button buttonJudge;

        private System.Windows.Forms.ListBox listBox1;

        private System.Windows.Forms.Button buttonClose;

        private System.Windows.Forms.TextBox textAnswer;

        private System.Windows.Forms.Label equals;

    }

}

//////主程序 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication1

{

    static class Program

    {

        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }

    }

}

////运行效果图

原文地址:https://www.cnblogs.com/HAOZHE/p/5357952.html