结对编程 四则运算

 

 

 

 

设计思路:

不是这么久不做作业,而是确实水平有限,但是出了结对编程,又实在不想去无聊的浪费大把时间抄写陈老师的作业,便与小伙伴一起再次打起代码,再加上其他好朋友的帮助就做了这个简陋的程序,用了大约一个星期。

总结:

还是觉得编程不简单,也许是练得少吧,还是要提高自己的能力,陈老师课上推荐了一些书,下课要找时间看看,大一C#没有认真学导致大二跟不上,好后悔。我想我会赶上的。特别要感谢我的同伴和我的室友们。

伙伴:康贺

 

代码实现:

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace ConsoleApplication2
  7 {
  8     class Program
  9     {
 10         public static void Ni()
 11         {
 12             Console.WriteLine("请输入你想做的运算+,-,*,/ ");
 13             string m = Console.ReadLine();
 14             switch (m)
 15             {
 16                 case "+":
 17                     Shu.Wo();
 18                     break;
 19                 case "-":
 20                     Shu.Sub();
 21                     break;
 22                 case "*":
 23                     Shu.Sheng();
 24                     break;
 25                 case "/":
 26                     Shu.Chu();
 27                     break;
 28 
 29             }
 30             Console.ReadLine();
 31         }
 32         static void Main(string[] args)
 33         {
 34             Ni();
 35         }
 36     }
 37 
 38     //定义委托
 39     public delegate double Num(double x, double y);
 40     public class Math
 41     {
 42         public static double Add(double x, double y)
 43         { return x + y; }
 44         public static double Sub(double x, double y)
 45         { return x - y; }
 46         public static double sub(double x, double y)
 47         { return y - x; }
 48         public static double Sheng(double x, double y)
 49         { return x * y; }
 50         public static double Chu(double x, double y)
 51         { return x / y; }
 52     }
 53     public class Shu
 54     {
 55         public static int o = 0;//获取做错的题数
 56         public static int i = 0;//获取做题总数
 57         public static int right = 0;//获取做错的题数
 58         public static void Wo()
 59         {
 60 
 61 
 62             double sum, n;
 63             Random r = new Random();
 64             double x = r.Next(0, 10);
 65             double y = r.Next(0, 10);
 66             sum = x + y;
 67             Console.WriteLine("{0}+{1}=", x, y);
 68             Console.WriteLine("结果:");
 69             n = double.Parse(Console.ReadLine());
 70             Num nu = new Num(Math.Add);
 71             Console.WriteLine("正确结果是:");
 72             Console.WriteLine("{0}", nu(x, y));
 73             if (sum == n)
 74             {
 75                 Console.WriteLine("正确。");
 76                 i++;
 77                 right++;
 78                 Wo();
 79 
 80 
 81 
 82             }
 83             else if (n == 99)//提示输入99表示退出运算,不做其它运算
 84             {
 85                 Jieshu();
 86 
 87             }
 88 
 89             else if (n == 00)//提示输入00退出本次运算可以继续做其它运算
 90             {
 91                 Jieshu();
 92 
 93                 Program.Ni();
 94 
 95             }
 96             else
 97             {
 98                 Console.WriteLine("你的结果不正确");
 99                 i++;
100                 o++;
101                 Wo();
102 
103 
104 
105             }
106 
107 
108         }
109         public static void Jieshu()
110         {
111             Console.WriteLine("你已退出计算。你共做{0}道题,答对{1},答错{2}\n", i, right, o);
112 
113         }
114         public static void Sub()
115         {
116 
117 
118             double sum, n;
119             Random r = new Random();
120             double x = r.Next(0, 10);
121             double y = r.Next(0, 10);
122             if (x > y)
123             {
124                 sum = x - y;
125                 Console.WriteLine("{0}-{1}=", x, y);
126                 Console.WriteLine("结果:");
127                 n = double.Parse(Console.ReadLine());
128                 Num nu = new Num(Math.Sub);
129                 Console.WriteLine("正确结果是:");
130                 Console.WriteLine("{0}", nu(x, y));
131                 if (sum == n)
132                 {
133                     Console.WriteLine("正确!");
134                     i++;
135                     right++;
136                     Wo();
137                 }
138                 else if (n == 99)
139                 {
140                     Jieshu();
141 
142                 }
143 
144                 else if (n == 00)
145                 {
146                     Jieshu();
147 
148                     Program.Ni();
149 
150 
151 
152                 }
153                 else
154                 {
155                     Console.WriteLine("你的结果不正确");
156                     i++;
157                     o++;
158                     Sub();
159                 }
160 
161             }
162             else if (x < y)
163             {
164                 sum = y - x;
165                 Console.WriteLine("{0}-{1}=", y, x);
166                 Console.WriteLine("结果:");
167                 n = double.Parse(Console.ReadLine());
168                 Num nu = new Num(Math.sub);
169                 Console.WriteLine("正确结果是:");
170                 Console.WriteLine("{0}", nu(x, y));
171                 if (sum == n)
172                 {
173                     Console.WriteLine("正确!");
174                     i++;
175                     right++;
176                     Wo();
177                 }
178                 else if (n == 99)
179                 {
180                     Jieshu();
181 
182                 }
183 
184                 else if (n == 00)
185                 {
186                     Jieshu();
187 
188                     Program.Ni();
189 
190 
191                 }
192                 else
193                 {
194                     Console.WriteLine("你的结果不正确");
195                     i++;
196                     o++;
197                     Sub();
198                 }
199             }
200         }
201         public static void Sheng()
202         {
203 
204 
205             double sum, n;
206             Random r = new Random();
207             double x = r.Next(0, 10);
208             double y = r.Next(0, 10);
209             sum = x * y;
210             Console.WriteLine("{0}*{1}=", x, y);
211             Console.WriteLine("结果:");
212             n = double.Parse(Console.ReadLine());
213             Num nu = new Num(Math.Sheng);
214             Console.WriteLine("正确结果是:");
215             Console.WriteLine("{0}", nu(x, y));
216             if (sum == n)
217             {
218                 Console.WriteLine("正确!");
219                 i++;
220                 right++;
221                 Wo();
222             }
223             else if (n == 99)
224             {
225                 Jieshu();
226 
227             }
228 
229             else if (n == 00)
230             {
231                 Jieshu();
232 
233                 Program.Ni();
234 
235 
236 
237             }
238             else
239             {
240                 Console.WriteLine("你的结果不正确");
241                 i++;
242                 o++;
243                 Sheng();
244             }
245         }
246         public static void Chu()
247         {
248 
249 
250             double sum, n;
251             Random r = new Random();
252             double x = r.Next(0, 10);
253             double y = r.Next(0, 10);
254             if (y != 0)
255             {
256                 sum = x / y;
257                 Console.WriteLine("{0}/{1}=", x, y);
258                 Console.WriteLine("结果:");
259                 n = double.Parse(Console.ReadLine());
260                 Num nu = new Num(Math.Chu);
261                 Console.WriteLine("正确结果是:");
262                 Console.WriteLine("{0}", nu(x, y));
263                 if (sum == n)
264                 {
265                     Console.WriteLine("正确!");
266                     i++;
267                     right++;
268                     Wo();
269                 }
270                 else if (n == 99)
271                 {
272                     Jieshu();
273 
274                 }
275 
276                 else if (n == 00)
277                 {
278                     Jieshu();
279 
280                     Program.Ni();
281 
282 
283 
284                 }
285                 else
286                 {
287                     Console.WriteLine("你的结果不正确");
288                     i++;
289                     o++;
290                     Chu();
291                 }
292             }
293             else
294             {
295                 sum = y / x;
296                 Console.WriteLine("{0}/{1}=", y, x);
297                 Console.WriteLine("结果:");
298                 n = double.Parse(Console.ReadLine());
299                 Num nu = new Num(Math.Chu);
300                 Console.WriteLine("正确结果是:");
301                 Console.WriteLine("{0}", nu(x, y));
302                 if (sum == n)
303                 {
304                     Console.WriteLine("正确!");
305                     i++;
306                     right++;
307                     Wo();
308                 }
309                 else if (n == 99)
310                 {
311                     Jieshu();
312 
313                 }
314                 else if (n == 00)
315                 {
316                     Jieshu();
317 
318                     Program.Ni();
319 
320 
321 
322                 }
323                 else
324                 {
325                     Console.WriteLine("你的结果不正确");
326                     i++;
327                     o++;
328                     Chu();
329                 }
330             }
331         }
332 
333     }
334 }

 

posted @ 2015-10-29 14:36  侯77、  阅读(167)  评论(2编辑  收藏  举报