zoukankan      html  css  js  c++  java
  • 软件工程第四周作业之四则运算-C#实现

    拿到题目的时候,快放假了,也没心思做。十月七号的一下午大概从两点做到八点半,加上十月八号的十二点半到两点半,做了一共八个半小时,去掉吃饭半个小时那么一共做了八个小时。

    逆波兰表达式我是扒的别人代码,没有自己写一遍。

    记得有一位老师曾经在课上讲过,每个人按照约定完成自己的工作,这是现代工业的基础。

    学习了一些C#语言。

    我写代码生成了带括号的四则运算表达式。数是随机的,括号是暴力生成的,特暴力。

    有理数计算懒得写了,女神青睐不值钱。

    加上括号之后怎么避免产生无限小数呢?这个我没有避免,仍然有时候会算出带小数的,比较少。

    加上括号之前的比较好处理。

    由于第一个数一定不会做被除数(出现在分母里)

    我直接把第2,3,4个数都构造成2^m*5^n,这样一定可以得到一个有限小数,当然,可能存在更好更漂亮的方法。

    下面贴一些关键代码。

      static void Main(string[] args)
            {   int num;//The number of expressions
                int cnt = 0;
                int token = 0;
                var arguments = CommandLineArgumentParser.Parse(args);//command line parameter
                int anslen = 1;//The number of the answers of expressions that have been used.
                double[] ansrep;//array to maintain answers that have been used
                ansrep = new double[1000];
                ansrep[0] = -23579;
                if (!arguments.Has("-c"))//如果命令行参数没有-c
                    num = 20;
                else
                { 
                num = int.Parse(arguments.Get("-c").Next);//命令行参数中得到要输出的表达式数量
                    token = 1;
                }
                if (token==0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        Test t = new Test();//new Test
                        t.opinit();
                        t.GetBracketExp();
                        // t.GetExp();
                        //t.NoRepeatedAns(anslen, ansrep);
                        ansrep[(anslen++) - 1] = t.ansr;
                        t.DispExp();//display expression
                        Console.Write("?");
                        double ans = double.Parse(Console.ReadLine());
                        // Console.WriteLine("{0},{1}",ans,t.ansr);
                        if (Math.Abs(ans - t.ansr) < 1e-7)//answer is right
                        {
                            Console.WriteLine("You are very clever!!!");
                            cnt++;
                        }
                        else//answer is wrong
                        {
                            if (Math.Abs((double)Math.Round(t.ansr) - t.ansr) < 1e-7)//Intenger
                            {
                                Console.WriteLine("Sorry,the answer is {0}", (int)t.ansr);
                            }
                            else//decimal
                            {
                                string floatValue = t.ansr.ToString();
                                floatValue = floatValue.TrimEnd('.', '0');
                                Console.WriteLine("Sorry,the answer is {0}", floatValue);
    
                            }
                        }
                        /* for (int j = 0; j < anslen; j++)
                         {
                             Console.WriteLine("{0:f5}", ansrep[j]);
                         }*/
                    }
                }
                else
                {
                    for (int i = 0; i < num; i++)
                    {
                        Test t = new Test();//new Test
                        t.opinit();
                        t.GetBracketExp();
                        // t.GetExp();
                        //t.NoRepeatedAns(anslen, ansrep);
                        ansrep[(anslen++) - 1] = t.ansr;
                        t.DispExpAns(t);//display expression and answer
               
                    }
                }
                Console.WriteLine("Total 20 Problems,you have solved {0}problem(s)", cnt);
            }
        }

    这是我的主函数。

    值得一提的是,读取命令行参数也是用的别人代码,这两个加起来有接近三百行之多。

    结对编程的照片,最后修改一些代码的格式,找一些bug,我的搭档王玉玲同学耐心的给予我帮助和指导,作为一个领航者给出一些方向上的意见。

    https://git.coding.net/Rainbows/F4.git

    这是我代码的git地址

    结对编程争论的点、体会稍后补充

  • 相关阅读:
    EXCEL每次打开文件都会出现一个空白sheet1窗口
    Python基础知识之面向对象编程
    Python基础知识之模块详解
    Python基础知识之正则表达式re模块
    Python基础知识之xml模块
    Python基础知识之json&pickle模块
    Python基础知识之装饰器
    VUE-02
    VUE
    虚拟环境的搭建
  • 原文地址:https://www.cnblogs.com/gaoyb348/p/7638242.html
Copyright © 2011-2022 走看看