码迷,mamicode.com
首页 > 其他好文 > 详细

简易四则运算

时间:2015-10-02 22:22:22      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

        题目:编写一个能对0--10之间的整数进行四则运算的“软件”
              程序能接收用户输入的整数答案,并判断对错
              程序结束时,统计出答对、答错的题目数量。

using System; 

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Simple_arithmetic //简易四则运算

{    

class Program   

  {        

       static void Main(string[] args)

        {      

             Console.WriteLine("请输入您想做的运算: ");

             Console.WriteLine("输入 【+】 表示加法");

             Console.WriteLine("输入 【-】 表示减法");

             Console.WriteLine("输入 【*】 表示乘法");  

             Console.WriteLine("输入 【/】 表示除法");  

             Console.WriteLine("输入【 -1】表示结束");

             Console.WriteLine("====================");

         

            //首先声明

            string opt = Console.ReadLine();

            switch (opt)   //使用switch语句  

           {      

                  case "+": 

                      Opp.Add(); 

                      break;

                   case "-":

                       Opp.Sub(); 

                        break;

                   case "*":

                      Opp.Mul();

                      break;  

                   case "/":  

                     Opp.Div();

                     break;

            }  

           Console.Read();     

    }   

  }

   public delegate double Num(double x, double y);//定义委托
    public class SimpleMath
    {
        public static double Add(double x, double y)
        { return x + y; }
        public static double Sub(double x, double y)
        { return x - y; }
        public static double Mul(double x, double y)
        { return x * y; }
        public static double Div(double x, double y)
        { return x / y; }
    }

     public class Opp

    {

          public static int fault = 0;

          public static int right = 0;

          public static void Add()//加法,实现加法功能

        {

              double sum, op;

            Random p = new Random();

            double x = p.Next(0, 10);

            double y =p.Next(0, 10);

            sum = x + y;

            Console.WriteLine("{0}+{1}=", x, y);

            Console.WriteLine("请输入结果:");

            op = double.Parse(Console.ReadLine());

            Num um = new Num(SimpleMath.Add);

            Console.WriteLine("正确结果是:");  

           Console.WriteLine("{0}+{1}={2}", x, y, um(x, y));

            if (sum == op)  

           {   

              Console.WriteLine("您的结果正确!");  

                   fault++;                                                                                                                        
                   right++;

                    Add();

            }

            else if (op == -1)

            {

                end();

            }

             else

            {

                Console.WriteLine("您的结果错误!");  

                    fault++;

                    Add();

            }

        }

        public static void end()

        {

            Console.WriteLine("您已退出计算!您共做{0}道题,答对{1}", fault, right);

        }

        public static void Sub()//减法,实现减法功能

        {

            double jan, op;

            Random p = new Random();

            double x =p.Next(0, 10);

            double y = p.Next(0, 10);

            jan = x - y;

            Console.WriteLine("{0}-{1}=", x, y);

            Console.WriteLine("请输入结果:");

            op = double.Parse(Console.ReadLine());

            Num um = new Num(SimpleMath.Sub);

            Console.WriteLine("正确结果是:");

            Console.WriteLine("{0}-{1}={2}", x, y, um(x, y));

            if (jan == op)

            {

                Console.WriteLine("您的结果正确!");

                fault++;

                right++;

                Add();

            }

            else if (op == -1)

            {

                end();

            }

            else

            {

                Console.WriteLine("您的结果错误!");

                   fault++;

                    Sub();

            }

     }

            public static void Mul()//乘法,实现乘法功能

        {

            double cen, op;

            Random p = new Random();

            double x = p.Next(0, 10);

            double y = p.Next(0, 10);

            cen = x * y;

            Console.WriteLine("{0}*{1}=", x, y);

            Console.WriteLine("请输入结果:");

            op = double.Parse(Console.ReadLine());

            Num um = new Num(SimpleMath.Mul);

            Console.WriteLine("正确结果是:");

            Console.WriteLine("{0}*{1}={2}", x, y, um(x, y));

            if (cen == op)

            {

                Console.WriteLine("您的结果正确!");

                fault++;

                right++;

                Add();

             }

               else if (op == -1)

            {

                end();

            }

              else

            {

                Console.WriteLine("您的结果错误!");

                fault++;

                Mul();

            }

        }

             public static void Div()//除法,实现除法功能
        {
            double chu, op;
            Random p = new Random();
            double x = p.Next(0, 10);
            double y = p.Next(0, 10);
            chu = x / y;
            Console.WriteLine("{0}/{1}=", x, y);
            Console.WriteLine("请输入结果:");
            op = double.Parse(Console.ReadLine());
            Num um = new Num(SimpleMath.Div);
            Console.WriteLine("正确结果是:");
            Console.WriteLine("{0}/{1}={2}", x, y, um(x, y));
            if (chu == op)
            {
                Console.WriteLine("您的结果正确!");
                fault++;
                right++;
                Add();
            }
            else if (op == -1)
            {
                end();
            }
            else
            {
                Console.WriteLine("您的结果错误!");
                fault++;
                Div();
            }
        }
    }
}

       技术分享

       技术分享

       PSP分析表

       技术分享

   《1》 需求分析:

      刚开始设计时,按照要求来做的,没有考虑太多。结果总是出错,并且测试起来 ,不像是一个“智能化”的小程序。

      后来回想,老师以前说过,做程序要站在用户角度考虑,以用户的身份去设计、分析。这样才能做得相对比较完整。

   《2》 具体设计思路:

      这个程序做了两遍,刚开始是用窗体做的。先前老师让我们做过类似的例子。结果越做越糟,总是出错。然后我就用

      控制台来做的。通过翻查教材,运用了一直不太能熟练掌握的委托和一些循环语句。中间还有我朋友的指导(他学的比我好

     通过和他的讨论,到代码的具体实现。感觉自己进步了许多

   《3》思考:

         通过这次的测试,感觉自己的逻辑思维仍有欠缺。一些简单的语句,需要仔细思考。才能够想明白,与其他同学的差距

         仍需要努力才能拉低。不过这次还是有不小的收获的!

 

 

      

 

  
        

 

简易四则运算

标签:

原文地址:http://www.cnblogs.com/harlem/p/4852521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!