计算机四则运算

一、需求分析:

1、当单击开始的时候,开始进行运算,单击回车键数字可以随机变换数字,单击停止跳转到Form2进行测试。

2、程序随机产生两个1-10的随机数,由用户输入结果。

3、停止时会输出正确率信息。

二、设计思路:

1、先建两个窗体,拉进去自己需要的控件,然后编写代码。

2、定义正确和总的题数的变量。

3、Random产生随机数。

4、textBox3计算加减乘除结果,单击开始运行。

5、停止转到Form2进行测试。 

具体控件如下表

控件 属性
Form1 Text 加法测试
label1 Text +
label2 Text =
textBox1 Enbled false
textBox2 Enbled false
button1 Text 开始
button2 Text 停止
button3 Text +
button4 Text -
button5 Text *
button6 Text /
textBox3 Enbled false

Form2

 

   
label1 Text 总计:
label2 Text 正确:
label3 Text 正确率:
textBox1 Enbled false
textBox2 Enbled false
textBox3 Enbled false

三、代码实现:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Chapter7_8

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        public static int count = 0;//题目总数

        public static int right = 0;//正确的题目总数

        private void button1_Click(object sender, EventArgs e)

        {

          

            Random();

 

        }

//产生1-10的随机数

        private void Random()

        {

            Random ran = new Random();

            int n1, n2;

            n1 = ran.Next(1, 11);

            n2 = ran.Next(1, 11);

            textBox1.Text = n1.ToString();

            textBox2.Text = n2.ToString();

            textBox3.Text="";

            count++;

 

         }

//当按下回车键表示输入结果

        private void textBox3_KeyDown(object sender, KeyEventArgs e)

        {

            int count;

            string c = label2.Text;

            switch (c)

            {

                case "+":

                    count = int.Parse(textBox1.Text) + int.Parse(textBox2.Text);

                    break;

                case "-":

                    count = int.Parse(textBox1.Text) - int.Parse(textBox2.Text);

                    break;

                case "x":

                    count = int.Parse(textBox1.Text) * int.Parse(textBox2.Text);

                    break;

                default:

                    count = int.Parse(textBox1.Text) / int.Parse(textBox2.Text);

                    break;

                  }

                    if (e.KeyCode == Keys.Enter)

                    {

                        if (textBox3.Text == count.ToString())

                            right++;

                        Random();

 

                    }

            }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textBox3.Enabled = false;

            Form2 frm2 = new Form2();

            frm2.ShowDialog();

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            label2.Text = "+";

            Random();

        }

 

        private void button4_Click(object sender, EventArgs e)

        {

            label2.Text = "-";

            Random();

        }

 

        private void button5_Click(object sender, EventArgs e)

        {

            label2.Text =" *";

            Random();

        }

 

        private void button6_Click(object sender, EventArgs e)

        {

            label2.Text = "/";

            Random();

        }

        }

        

    }

 

//Form2窗体代码编写

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Chapter7_8
{
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.right / (double)(Form1.count)) * 100).ToString() + "%";
}

四、测试

       

五、psp耗时分析:

psp Personal Software Process Stages Time(h)
planning .计划 8
.Estimate .估计这个任务需要多长时间 6
Development .开发 70
.Analysis .需求分析 5
.Design Spec .生成设计文档 6
.Design Review .设计复审 4
• Coding Standard .代码规范 2
 • Design .具体设计 10
 • Coding .具体编码 15
• Code Review .代码复审 8
 • Text .测试 22
Reporting .报告 6
• Test Report .测试报告 3
• Size Measurement .计算工作量 0.5
 • Postmortem&Process Improvement Plan .事后总结并提出改进计划 4

六、总结:

       在写代码的时候自己犯了可多错误,本来自己对编程学的都不太好,现在让编代码真的有点难度,不过我是看老师的例子做的,我们学的跟这可相似,自己底子不好就稍微改了一下,我一定要多努力自己写出来代码,而不是现在改我们学过的例子的代码,总的来说,这次四则运算不是太难,只要我们平常多用点心都能做出来。本来是写控制台的,不过控制台正确率总是出不来,,所以只好用窗体了,相对来说窗体简单点。希望自己下次做的更好,加油!!!

思考题

只要把

   private void Random()

 

        {

 

            Random ran = new Random();

 

            int n1, n2;

 

            n1 = ran.Next(1, 11);

 

            n2 = ran.Next(1, 11);

 

            textBox1.Text = n1.ToString();

 

            textBox2.Text = n2.ToString();

 

            textBox3.Text="";

 

            count++;

//换成

 

   private void Random()

 

        {

 

            Random ran = new Random();

 

            int n1, n2;

 

            n1 = ran.Next(1, 101);

 

            n2 = ran.Next(1, 101);

 

            textBox1.Text = n1.ToString();

 

            textBox2.Text = n2.ToString();

 

            textBox3.Text="";

 

            count++;//就行了

 

posted @ 2015-10-03 15:13  嘿,MyDarling  阅读(675)  评论(2编辑  收藏  举报