作业四: 结对编程项目---四则运算

运行截图:

代码:

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

namespace 四则运算

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义一个运算类型的枚举
        private enum OpentPly
        {
            Oper_加,
            Oper_减,
            Oper_乘,
            Oper_除,
        }
        
        //定义全局变量
        private int m_nEnd = 0;//计算结果
        private int m_nRN = 0;//所做题目中正确的个数
        private int m_nWN = 0;//所做题目中错误的个数
        private int m_nSN = 0;//已做题目
        private int m_nNumSum = 0;//所有题目的总题数
        private int m_nNumber1 = 0;//第一个参与计算的数
        private int m_nNumber2 = 0;//第二个参与计算的数
        private int m_nArea = 0;//取值范围

        private string[] strOper ={ "+", "-", "×", "÷" };

        private OpentPly m_enumTol = OpentPly.Oper_加;//选定默认的类型为加法
        private DateTime SumTime;

        //界面的初次显示的界面
        private void Form1_Load(object sender, EventArgs e)
        {
            listView属性.Items.Clear();//清除里面所有的东西
            comboBox类型.SelectedIndex = 0;
            comboBox范围.SelectedIndex = 0;
            comboBox题Number.SelectedIndex=0;
            this.buttonOK.Focus();//把焦点定在“开始”按钮上
        }
        //对按钮“开始”的处理
        #region 对[开始]按钮的处理
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (m_nSN > 0 && m_nNumSum > m_nSN)
            {
                if (MessageBox.Show("题目还没有做完,是否要重新开始?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, 
                    MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    this.textBoxInput.Focus();
                    return;
                }
            }

            SumTime = DateTime.Now;

            m_nRN = 0;
            m_nSN = 0;
            m_nWN = 0;
            
            this.label正确.Text = m_nRN.ToString();
            this.label错误.Text = m_nWN.ToString();
            this.label剩余题数.Text = String.Format("{0}", m_nNumSum - m_nSN-1);

            listView属性.Items.Clear();
            
            groupBox题目.Enabled = true;

            FindTwoNum();
            textBoxInput.Focus();
        }
        #endregion

        //定义一个方法来计算相关的算法
        #region 加减乘除的相关计算
        private void FindTwoNum()
        {
            switch (m_enumTol)
            {
                case OpentPly.Oper_加:
                    m_nNumber1 = Rand.Prent1(m_nArea);
                    m_nNumber2 = Rand.Prent1(m_nArea-m_nNumber1);
                    m_nEnd = m_nNumber1 + m_nNumber2;
                    break;
                case OpentPly.Oper_减:
                    m_nNumber1 = Rand.Prent1(m_nArea);
                    m_nNumber2 = Rand.Prent1(m_nNumber1);
                    m_nEnd = m_nNumber1-m_nNumber2;
                    break;
                case OpentPly.Oper_乘:
                    m_nNumber1 = Rand.Prent1(m_nArea/2);
                    m_nNumber2 = Rand.Prent2(m_nNumber1, m_nArea);
                    m_nEnd = m_nNumber1 * m_nNumber2;
                    break;
                case OpentPly.Oper_除:
                    m_nNumber1 = Rand.Prent1(m_nArea);
                    if (m_nNumber1 == 0)
                    {
                        m_nNumber2 = Rand.Prent2(1, m_nArea);
                    }
                    else if (m_nNumber1 < 4)
                    {
                        m_nNumber2 = m_nNumber1;
                    }
                    else
                    {
                        m_nNumber2 = Rand.Prent2(1,m_nNumber1);
                        while (m_nNumber1 % m_nNumber2 != 0)
                        {
                            m_nNumber2 = Rand.Prent2(1, m_nNumber1);
                        }
                    }
                    m_nEnd =(int)( m_nNumber1 / m_nNumber2);
                    #region 
                    #endregion
                    break;
                default:
                    break;
            }
     
               //在题目界面里面显示相关的东西
            labelNumber1.Text = m_nNumber1.ToString();
            labelNumber2.Text = m_nNumber2.ToString();
            label符号.Text = strOper[(int)m_enumTol];

     
        }
        #endregion
        //对按钮“答”的相关设置
        #region button答_Click
        private void button答_Click(object sender, EventArgs e)
        {
            try
            {
                int m_nInSum = int.Parse(this.textBoxInput.Text.Trim());
                m_nSN++;

                string[] strList ={ m_nNumber1.ToString() + strOper[(int)m_enumTol] + m_nNumber2, m_nEnd.ToString(),m_nInSum.ToString() };
                
                //当textBoxInput中没有输入任何东西时
                if (textBoxInput.Text == "")
                {
                    textBoxInput.Focus();
                    return;
                }
                //当题目做完时
                if (m_nSN ==m_nNumSum)
                {
                    TimeSpan Time = DateTime.Now - SumTime;
                    MessageBox.Show("已做完!对" + m_nRN.ToString() + "题,错" + m_nWN.ToString() + "题!用时"+Time.ToString()+"分钟", "恭喜提示", MessageBoxButtons.OK, 
                        MessageBoxIcon.Warning);
                    groupBox题目.Enabled = false;

                    //button答_Click(null, null);
                    return;
                }
                //当m_nInSum = m_nEnd时
                if (m_nInSum == m_nEnd)
                {
                    m_nRN++;
                    
                    this.label正确.Text = m_nRN.ToString();
                    this.label错误.Text = m_nWN.ToString();
                    this.label剩余题数.Text =String.Format("{0}", m_nNumSum-m_nSN-1);
                    listView属性.Items.Add(new ListViewItem(strList, 1));
                }
                //当m_nInSum != m_nEnd时
                else
                {
                    m_nWN++;
                    
                    this.label正确.Text = m_nRN.ToString();
                    this.label错误.Text = m_nWN.ToString();
                    this.label剩余题数.Text = String.Format("{0}", m_nNumSum - m_nSN-1);

                    listView属性.Items.Add(new ListViewItem(strList, 0));
                }
                this.textBoxInput.Text = "";
                
                FindTwoNum();//出下一题
                textBoxInput.Focus();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        #endregion

        #region 设置里面的项选定之后
        private void comboBox范围_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strNum = this.comboBox范围.Text.Trim();
            strNum = strNum.Substring(0, strNum.IndexOf("以内"));
            m_nArea = int.Parse(strNum);
            buttonOK_Click(null, null);
        }

        private void comboBox题Number_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strScal = this.comboBox题Number.Text.Trim();
            m_nNumSum = int.Parse(strScal);
            m_nNumSum += 1;
            buttonOK_Click(null, null);
        }

        private void comboBox类型_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_enumTol = (OpentPly)comboBox类型.SelectedIndex;
            buttonOK_Click(null, null);
        }
        #endregion

        #region 在textBoxInput中单击Enter键时执行button答_Click事件
        private void textBoxInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button答_Click(null, null);
            }
        }
        #endregion

        private void Form1_FormClosed_1(object sender, FormClosedEventArgs e)
        {
            if (MessageBox.Show("你真的要退出 <四则运算>吗?", "提示", MessageBoxButtons.YesNo,
                                      MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.Close();
                //Application.Exit();
            }
        }

        

        
    }
}

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 calculation
{
    public partial class Form1 : Form
    {
        int n1, n2,count,max,min,op,opmax,opmin;  //定义两个随机数,最小值最大值,符号的最多与最少
        Random ran;

        public Form1()
        {
            InitializeComponent();
            ran = new Random();    //生成新的随机数
        }

        private void textBox1_TextChanged(object sender, EventArgs e)  //题目数量根据写入得
        {
            if (count == int.Parse(textBox1.Text))
            {
                count = int.Parse(textBox1.Text);
            }
        }

       
        private void textBox3_TextChanged(object sender, EventArgs e) //取值范围最小值
        {
            min = int.Parse(textBox3.Text);
        }

        
       
        private void textBox4_TextChanged(object sender, EventArgs e) //取值范围最大值
        {
            max = int.Parse(textBox4.Text);
        }

       
        
        private void button2_Click(object sender, EventArgs e) //退出按钮
        {
            this.Dispose();
        }

       
        
        private void button1_Click(object sender, EventArgs e) //开始按钮
        {
            max = 100;
            min = 0;
            opmax = 4;
            opmin = 0;

            for (int i = 0; i < count; i++)
            {
                n1 = ran.Next(min, max + 1); //定义这两个随机数是根据给的最大值最小值之间随机的
                n2 = ran.Next(min, max + 1);
                op = ran.Next(opmin, opmax); //最少符号数量与最多符号数量
                switch (op)                 //定义四个运算符 + - * /
                {
                    case 0:
                        listBox1.Items.Add(n1 + "+" + n2 + "=");
                        break;
                    case 1:
                        listBox1.Items.Add(n1 + "-" + n2 + "=");
                        break;
                    case 2:
                        listBox1.Items.Add(n1 + "*" + n2 + "=");
                        break;
                    case 3:
                        listBox1.Items.Add(n1 + "/" + n2 + "=");
                        break;
                    default:break;
                }
            }
        }

       

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true && checkBox2.Checked == true)//+-*/
            {
                opmax = 4;
                opmin = 0;
            }
            else if (checkBox1.Checked = true && checkBox2.Checked == false)//+-
            {
                opmax = 2;
                opmin = 0;    
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == true)//*/
            {
                opmax = 4;
                opmin = 2;
            }
            else
            {
                MessageBox.Show("请至少选择一种模式!");
            }
        }
    }
}

总结:

这次的作业对我们来说很难,尤其对于我来说更难,本身就对C#接触太少,之前跟着老师学习的时候学习能力比较低,每次都是紧跟着老师做小项目,因为下课前必须交本节课的作业,有分数的,所以跟着老师打代码的时候没有太多时间去理解这些代码是什么意义,少问了一些为什么,只是用最快的速度跟上老师的步伐,要是有一点跟不上可能这节课的成绩就废了,这个科目不能及格是很麻烦的,所以刚开始接触一个题目时,看着好像有点眼熟,但是就是不知道怎么去做,所以我和高原同学想法一样,先到网上去学习别人的成品,先试着做一个,就是第一个作业,等我们学习点东西了,就完全自己弄一个,就是第二个,虽然很不完善,做得很次,但是跟我们自己比已经是很好了,毕竟水平摆在那里,这次作业我稍微学到了C#的一点东西,往后会继续努力。

(1)我的小伙伴是高原,学号是130201206,博客地址为http://www.cnblogs.com/gao666/p/5360572.html

 

(2)我们这次合作项目是四则运算,实现的主要功能有:首先可以由使用者输入题目数量,计算数值的范围,以及计算题里面所包含的运算符,加减乘除,括号,分数等等,使用者可以输入答案,最右方可以出现题目答案。

 

(3)高原同学负责版面的设计,我们一起编写代码,我在编写代码的时候查阅了很多资料,瞬间感觉知识的掌握提升了!模板很好弄,界面的设计高原很在行,最后我们一起根据模板编写代码。

 

(4)我们的优点很明显,就是大概什么都不会,学东西很快能记住,就像一杯只有半杯水的杯子。缺点也很明显,就是什么都不会,别人可能每个点都会,几分钟就搞定,我们可能几个小时都搞不定,效率低到爆表。

 

(5) 我们的想法特别容易产生一致,反正都不会,没有什么自己的想法,所以合作起来很顺,不至于打架,目标也很明确,虽然效率很低,但是都是用在编程上而不是争吵上。

 

 

posted @ 2016-04-06 19:09  hao豪  阅读(275)  评论(1编辑  收藏  举报