C#窗体——四则运算-程序员宅基地

技术标签: c#  

用户需求:
程序能接收用户输入的整数答案,并判断对错
程序结束时,统计出答对、答错的题目数量。
补充说明:0——10的整数是随机生成的
用户可以选择四则运算中的一种
用户可以结束程序的运行,并显示统计结果。
在此基础上,做增量开发。

增量内容:

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

设计思路:

              我的想法是,先生成两个数运算的表达式,就和上次的一样。先让两个数进行加减计算,得到一个结果,之后再与第三个数进行乘除运算。如果要是这样做的话就要让它产生两个运算符,那就会有先算乘除后算加减了。就不能达到(2+3)*5这种先加减后乘除的运算了。并且也不能一次就生成一个三个数运算的表达式。所以就把这种想法舍弃了。
             因此呢,我们就把第一个和第二个文本框合并成了一个文本框,让它来存一个运算表达式。不管是生成两个数亦或是三个数的表达式都可以,最后只需要对这个字符串表达式进行计算就可以了。这才只是生成一个式子。难点在于对这个式子进行计算,要把这个式子取出来,用到队列,把它的每一个字符都取出来放到了栈里之后再进行计算。

代码实现:

Form1.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 Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18         public static int select = 0;
 19         public static int Count = 0;
 20         private int t = 60;
 21         public static int right = 0;
 22 
 23         private void button1_Click(object sender, EventArgs e)
 24         {
 25             label2.Text = t.ToString();
 26             timer1.Enabled = true;
 27             timer1.Interval = 1000;
 28             timer1.Start();
 29         }
 30 
 31         private void RDN()
 32         {
 33             Random ran=new Random();
 34             int n1,n2;
 35             if (textBox4.Text==""&&textBox5.Text=="")
 36             {
 37                 MessageBox.Show("请输入取值范围!");
 38                 return;
 39             }
 40             if (checkBox1.Checked == true)
 41                 select = 1;
 42             for (int i = 0; i < int.Parse(textBox6.Text); i++)
 43             {
 44                 textBox1.Clear();
 45                 switch (select)
 46                 {
 47                     case 1:
 48                         {
 49                             n1 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
 50                             n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
 51                             int n3 = ran.Next(1, int.Parse(textBox5.Text));
 52                             textBox1.Text += "(";
 53                             if (n1 < 0) textBox1.Text += "(" + n1 + "";
 54                             else textBox1.Text += n1;
 55                             if (ran.Next(0, 2) == 1) textBox1.Text += "+";
 56                             else textBox1.Text += "-";
 57                             if (n2 < 0) textBox1.Text += "(" + n2 + ")";
 58                             else textBox1.Text += n2 + ")";
 59                             if (ran.Next(0, 2) == 1) textBox1.Text += "*"+n3;
 60                             else textBox1.Text += "/" + n3;
 61                             break;
 62                         }
 63                 }
 64                 
 65                 textBox3.Text = "";
 66             }                
 67         }
 68 
 69         private void RandomNumjia()
 70         {
 71             textBox1.Clear();
 72             textBox3.Clear();
 73             if (textBox4.Text == "" && textBox5.Text == "")
 74             {
 75                 MessageBox.Show("请输入取值范围!");
 76                 return;
 77             }
 78 
 79             Random ran = new Random();
 80             int n1 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
 81             int n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
 82             if (n1 < 0) textBox1.Text += "(" + n1 + ")";
 83             else textBox1.Text += n1;
 84             textBox1.Text += "+";
 85             if (n2 < 0) textBox1.Text += "(" + n2 + ")";
 86             else textBox1.Text += n2; 
 87         }
 88 
 89         private void RandomNumjian()
 90         {
 91             textBox1.Clear();
 92             textBox3.Clear();
 93             if (textBox4.Text == "" && textBox5.Text == "")
 94             {
 95                 MessageBox.Show("请输入取值范围!");
 96                 return;
 97             }
 98 
 99             Random ran = new Random();
100             int n1 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
101             int n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
102             if (n1 < 0) textBox1.Text += "(" + n1 + ")";
103             else textBox1.Text += n1;
104             textBox1.Text += "-";
105             if (n2 < 0) textBox1.Text += "(" + n2 + ")";
106             else textBox1.Text += n2;
107         }
108 
109         private void RandomNumcheng()
110         {
111             textBox1.Clear();
112             textBox3.Clear();
113             if (textBox4.Text == "" && textBox5.Text == "")
114             {
115                 MessageBox.Show("请输入取值范围!");
116                 return;
117             }
118 
119             Random ran = new Random();
120             int n1 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
121             int n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
122             if (n1 < 0) textBox1.Text += "(" + n1 + ")";
123             else textBox1.Text += n1;
124             textBox1.Text += "*";
125             if (n2 < 0) textBox1.Text += "(" + n2 + ")";
126             else textBox1.Text += n2;
127         }
128 
129         private void RandomNumchu()
130         {
131             textBox1.Clear();
132             textBox3.Clear();
133             if (textBox4.Text == "" && textBox5.Text == "")
134             {
135                 MessageBox.Show("请输入取值范围!");
136                 return;
137             }
138 
139             Random ran = new Random();
140             int n1 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
141             int n2 = ran.Next(int.Parse(textBox4.Text), int.Parse(textBox5.Text));
142             if (n1 < 0) textBox1.Text += "(" + n1 + ")";
143             else textBox1.Text += n1;
144             textBox1.Text += "/";
145             if (n2 < 0) textBox1.Text += "(" + n2 + ")";
146             else textBox1.Text += n2;
147         }
148 
149         private void timer1_Tick(object sender, EventArgs e)
150         {
151             if (t <= 0)
152             {
153                 timer1.Enabled = false;
154                 textBox3.Enabled = false;
155                 MessageBox.Show("时间到!");
156                 textBox3.Enabled = false;
157                 Form2 frm2 = new Form2();
158                 frm2.ShowDialog();
159             }
160             t = t - 1;
161             label2.Text = t.ToString();
162         }
163 
164         private void button2_Click(object sender, EventArgs e)
165         {
166             timer1.Stop();
167             Form2 frm2 = new Form2();
168             frm2.ShowDialog();
169         }
170 
171         private void button3_Click(object sender, EventArgs e)
172         {
173             RandomNumjia();
174         }
175 
176         private void button4_Click(object sender, EventArgs e)
177         {
178             RandomNumjian();
179         }
180 
181         private void button5_Click(object sender, EventArgs e)
182         {
183             RandomNumcheng();
184         }
185 
186         private void button6_Click(object sender, EventArgs e)
187         {
188             RandomNumchu();
189         }
190 
191         private void button7_Click(object sender, EventArgs e)
192         {
193             if (textBox4.Text == "" && textBox5.Text == "")
194             {
195                 MessageBox.Show("请输入取值范围!");
196                 return;
197             }
198             else
199             {
200                 for (int i = 0; i < int.Parse(textBox6.Text);i++)
201                 {
202                     RDN();
203                 }
204             }
205         }
206 
207         private void textBox3_KeyDown(object sender, KeyEventArgs e)
208         {
209             string result = textBox1.Text;
210 
211             if (Count == int.Parse(textBox6.Text))
212             {
213                 Form2 frm2 = new Form2();
214                 frm2.ShowDialog();
215             }
216 
217             if (e.KeyCode == Keys.Enter)
218             {
219                 if (textBox3.Text == Calucate(result).ToString())   //直接调用Calucate这个方法计算result的值并与输入的值进行比较
220                 {
221                     right++;
222                     Count++;
223                     MessageBox.Show("回答正确!");
224                 }
225 
226                 else
227                 {
228                     MessageBox.Show("答题错误!");
229                     Count++;
230                     string s = textBox1.Text;
231                     if (s.Substring(textBox4.TextLength, 1) == "+")
232                     {
233                         RandomNumjia();
234                     }
235                     else if (s.Substring(textBox4.TextLength, 1) == "-")
236                     {
237                         RandomNumjian();
238                     }
239                     else if (s.Substring(textBox4.TextLength, 1) == "*")
240                     {
241                         RandomNumcheng();
242                     }
243                     else if (s.Substring(textBox4.TextLength, 1) == "/")
244                     {
245                         RandomNumchu();
246                     }
247                     else if (s.Length > 5)
248                     {
249                         RDN();
250                     }
251                 }
252 
253                 string m = textBox1.Text;
254                 if (m.Substring(textBox4.TextLength, 1) == "+")
255                 {
256                     RandomNumjia();
257                 }
258                 else if (m.Substring(1, 1) == "-")
259                 {
260                     RandomNumjian();
261                 }
262                 else if (m.Substring(1, 1) == "*")
263                 {
264                     RandomNumcheng();
265                 }
266                 else if (m.Substring(1, 1) == "/")
267                 {
268                     RandomNumchu();
269                 }
270                 else if (m.Length > 5)
271                 {
272                     RDN();
273                 }
274             }
275         }
276 
277         static Dictionary<char, int> priorities = null;  //优先级
278 
279         static void Calculator()                         //添加了四种运算符以及四种运算符的优先级
280         {
281             priorities = new Dictionary<char, int>();    
282             priorities.Add('#', -1);                     
283             priorities.Add('+', 0);
284             priorities.Add('-', 0);
285             priorities.Add('*', 1);
286             priorities.Add('/', 1);
287         } 
288 
289         const string operators = "+-*/";                 //运算符
290         static double Compute(double leftNum, double rightNum, char op)  //这是一种方法,用来计算左右两个数的静态方法!
291         {
292             switch (op)
293             {
294                 case '+': return leftNum + rightNum;
295                 case '-': return leftNum - rightNum;
296                 case '*': return leftNum * rightNum;
297                 case '/': return leftNum / rightNum;
298                 default: return 0;
299             }
300         }
301 
302         static bool IsOperator(char op)                  //每次判断这个字符是否是运算符?
303         {
304             return operators.IndexOf(op) >= 0;
305         }
306 
307             static bool IsAssoc(char op)                //返回一个关联符号
308         {
309             return op == '+' || op == '-' || op == '*' || op == '/';
310         }
311 
312         static Queue<object> QueueSort (string expression)           // 队列排序   
313         {
314             Queue<object> result = new Queue<object>();             
315             Stack<char> operatorStack = new Stack<char>();           //运算符栈
316             operatorStack.Push('#');
317             char top, cur, tempChar;                                          //top栈顶,current最近的;
318             string tempNum;
319             for (int i = 0, j; i < expression.Length; )                 //取出表达式
320             {
321                 cur = expression[i++];                                  //取出表达式的每个字符赋给cur
322                 top = operatorStack.Peek();                             //栈顶元素赋给top此时为"#"
323 
324                 if (cur == '(')                                         //将左括号压栈,此时栈顶元素为"("
325                 {
326                     operatorStack.Push(cur);
327                 }
328                 else
329                 {
330                     if (IsOperator(cur))                             //如果是运算符的话
331                     {
332                         while (IsOperator(top) && ((IsAssoc(cur) && priorities[cur] <= priorities[top])) || (!IsAssoc(cur) && priorities[cur] < priorities[top]))
333                         {
334                             result.Enqueue(operatorStack.Pop());     //如果元素为运算符并且优先级小于栈顶元素优先级,出栈
335                             top = operatorStack.Peek();              //继续把栈顶元素赋给top
336                         }
337                         operatorStack.Push(cur);                     //把数字压栈
338                     }
339                     else if (cur == ')')                           //将右括号添加到结尾
340                     {
341                         while (operatorStack.Count > 0 && (tempChar = operatorStack.Pop()) != '(')
342                         {
343                             result.Enqueue(tempChar);
344                         }
345                     }
346                     else
347                     {
348                         tempNum = "" + cur;
349                         j = i;
350                         while (j < expression.Length && (expression[j] == '.' || (expression[j] >= '0' && expression[j] <= '9')))
351                         {
352                             tempNum += expression[j++];
353                         }
354                         i = j;
355                         result.Enqueue(tempNum);
356                     }
357                 }
358             }
359             while (operatorStack.Count > 0)
360             {
361                 cur = operatorStack.Pop();
362                 if (cur == '#') continue;
363                 if (operatorStack.Count > 0)
364                 {
365                     top = operatorStack.Peek();
366                 }
367 
368                 result.Enqueue(cur);
369             }
370 
371             return result;
372         }
373 
374         static double Calucate(string expression)
375         {
376             try
377             {
378                 var rpn = QueueSort(expression);                   //rpn逆波兰表达式reverse polish notation 
379                 Stack<double> operandStack = new Stack<double>();
380                 double left, right;
381                 object cur;
382                 while (rpn.Count > 0)
383                 {
384                     cur = rpn.Dequeue();                           //出列
385                     if (cur is char)                               //如果cur为字符的话
386                     {
387                         right = operandStack.Pop();                //右边的数字出栈
388                         left = operandStack.Pop();                 //左边的数字出栈
389                         operandStack.Push(Compute(left, right, (char)cur));    //此时调用compute方法
390                     }
391                     else
392                     {
393                         operandStack.Push(double.Parse(cur.ToString()));            //是数字就压栈
394                     }
395                 }
396                 return operandStack.Pop();
397             }
398             catch
399             {
400                 throw new Exception("表达式格式不正确!");
401             }
402         }
403 
404     }
405 }

 

代码编写过程:

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 }

 

运行过程:

答题的时候给出取值范围,点击随机,程序会给出一个运算式,在等号后面的文本框
里面输入答案,这是会调用计算方法,并判断对错。

时间到了会给出测试结果。并提示时间到!

PSP耗时分析:

 

结对编程总结:

说明:我们一起做的是第五个增量。

  这次结对编程依然是我的老搭档张宇,总的来说他是一个比较优秀的搭档,对我的辅导也非常尽心,通过这两次结对编程我学到了很多知识,也了解了自己以前不知道的逆波兰表达式,这些多亏我的搭档给我讲解。说实话看到老师发的作业增量一次比一次难我都有放弃的想法,多亏搭档坚持。当看到增量作业一点头绪都没后来我们在网上查了一些资料才有所了解。通过这次编程我明白了所谓的迭代,代码永远都敲不完,客户的每一次需求,我们都要进行一次代码修改,还要保证之前的功能依然可以实现,所以程序员真的不是一个轻松的活。但是我想只要努力就可以成功!

转载于:https://www.cnblogs.com/thinking-star/p/4917349.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30325071/article/details/99993427

智能推荐

2024最新计算机毕业设计选题大全-程序员宅基地

文章浏览阅读1.6k次,点赞12次,收藏7次。大家好!大四的同学们毕业设计即将开始了,你们做好准备了吗?学长给大家精心整理了最新的计算机毕业设计选题,希望能为你们提供帮助。如果在选题过程中有任何疑问,都可以随时问我,我会尽力帮助大家。在选择毕业设计选题时,有几个要点需要考虑。首先,选题应与计算机专业密切相关,并且符合当前行业的发展趋势。选择与专业紧密结合的选题,可以使你们更好地运用所学知识,并为未来的职业发展奠定基础。要考虑选题的实际可行性和创新性。选题应具备一定的实践意义和应用前景,能够解决实际问题或改善现有技术。

dcn网络与公网_电信运营商DCN网络的演变与规划方法(The evolution and plan method of DCN)...-程序员宅基地

文章浏览阅读3.4k次。摘要:随着电信业务的发展和电信企业经营方式的转变,DCN网络的定位发生了重大的演变。本文基于这种变化,重点讨论DCN网络的规划方法和运维管理方法。Digest: With the development oftelecommunication bussiness and the change of management of telecomcarrier , DCN’s role will cha..._电信dcn

动手深度学习矩阵求导_向量变元是什么-程序员宅基地

文章浏览阅读442次。深度学习一部分矩阵求导知识的搬运总结_向量变元是什么

月薪已炒到15w?真心建议大家冲一冲数据新兴领域,人才缺口极大!-程序员宅基地

文章浏览阅读8次。近期,裁员的公司越来越多今天想和大家聊聊职场人的新出路。作为席卷全球的新概念ESG已然成为当前各个行业关注的最热风口目前,国内官方发布了一项ESG新证书含金量五颗星、中文ESG证书、完整ESG考试体系、名师主讲...而ESG又是与人力资源直接相关甚至在行业圈内成为大佬们的热门话题...当前行业下行,裁员的公司也越来越多大家还是冲一冲这个新兴领域01 ESG为什么重要?在双碳的大背景下,ESG已然成...

对比传统运营模式,为什么越拉越多的企业选择上云?_系统上云的前后对比-程序员宅基地

文章浏览阅读356次。云计算快速渗透到众多的行业,使中小企业受益于技术变革。最近微软SMB的一项研究发现,到今年年底,78%的中小企业将以某种方式使用云。企业希望投入少、收益高,来取得更大的发展机会。云计算将中小企业信息化的成本大幅降低,它们不必再建本地互联网基础设施,节省时间和资金,降低了企业经营风险。科技创新已成时代的潮流,中小企业上云是创新前提。云平台稳定、安全、便捷的IT环境,提升企业经营效率的同时,也为企业..._系统上云的前后对比

esxi网卡直通后虚拟机无网_esxi虚拟机无法联网-程序员宅基地

文章浏览阅读899次。出现选网卡的时候无法选中,这里应该是一个bug。3.保存退出,重启虚拟机即可。1.先随便选择一个网卡。2.勾先取消再重新勾选。_esxi虚拟机无法联网

随便推点

在LaTeX中使用.bib文件统一管理参考文献_egbib-程序员宅基地

文章浏览阅读913次。在LaTeX中,可在.tex文件的同一级目录下创建egbib.bib文件,所有的参考文件信息可以统一写在egbib.bib文件中,然后在.tex文件的\end{document}前加入如下几行代码:{\small\bibliographystyle{IEEEtran}\bibliography{egbib}}即可在文章中用~\cite{}宏命令便捷的插入文内引用,且文章的Reference部分会自动排序、编号。..._egbib

Unity Shader - Predefined Shader preprocessor macros 着色器预处理宏-程序员宅基地

文章浏览阅读950次。目录:Unity Shader - 知识点目录(先占位,后续持续更新)原文:Predefined Shader preprocessor macros版本:2019.1Predefined Shader preprocessor macros着色器预处理宏Unity 编译 shader programs 期间的一些预处理宏。(本篇的宏介绍随便看看就好,要想深入了解,还是直接看Unity...

大数据平台,从“治理”数据谈起-程序员宅基地

文章浏览阅读195次。本文目录:一、大数据时代还需要数据治理吗?二、如何面向用户开展大数据治理?三、面向用户的自服务大数据治理架构四、总结一、大数据时代还需要数据治理吗?数据平台发展过程中随处可见的数据问题大数据不是凭空而来,1981年第一个数据仓库诞生,到现在已经有了近40年的历史,相对数据仓库来说我还是个年轻人。而国内企业数据平台的建设大概从90年代末就开始了,从第一代架构出现到..._数据治理从0搭建

大学抢课python脚本_用彪悍的Python写了一个自动选课的脚本 | 学步园-程序员宅基地

文章浏览阅读2.2k次,点赞4次,收藏12次。高手请一笑而过。物理实验课别人已经做过3、4个了,自己一个还没做呢。不是咱不想做,而是咱不想起那么早,并且仅有的一次起得早,但是哈工大的服务器竟然超负荷,不停刷新还是不行,不禁感慨这才是真正的“万马争过独木桥“啊!服务器不给力啊……好了,废话少说。其实,我的想法很简单。写一个三重循环,不停地提交,直到所有的数据都accepted。其中最关键的是提交最后一个页面,因为提交用户名和密码后不需要再访问其..._哈尔滨工业大学抢课脚本

english_html_study english html-程序员宅基地

文章浏览阅读4.9k次。一些别人收集的英文站点 http://www.lifeinchina.cn (nice) http://www.huaren.us/ (nice) http://www.hindu.com (okay) http://www.italki.com www.talkdatalk.com (transfer)http://www.en8848.com.cn/yingyu/index._study english html

Cortex-M3双堆栈MSP和PSP_stm32 msp psp-程序员宅基地

文章浏览阅读5.5k次,点赞19次,收藏78次。什么是栈?在谈M3堆栈之前我们先回忆一下数据结构中的栈。栈是一种先进后出的数据结构(类似于枪支的弹夹,先放入的子弹最后打出,后放入的子弹先打出)。M3内核的堆栈也不例外,也是先进后出的。栈的作用?局部变量内存的开销,函数的调用都离不开栈。了解了栈的概念和基本作用后我们来看M3的双堆栈栈cortex-M3内核使用了双堆栈,即MSP和PSP,这极大的方便了OS的设计。MSP的含义是Main..._stm32 msp psp

推荐文章

热门文章

相关标签