四則運算-C#實現

來源:互聯網
上載者:User

標籤:比較   wrong   ntp   console   pre   補充   express   題目   產生   

拿到題目的時候,快放假了,也沒心思做。十月七號的一下午大概從兩點做到八點半,加上十月八號的十二點半到兩點半,做了一共八個半小時,去掉吃飯半個小時那麼一共做了八個小時。

逆波蘭運算式我是扒的別人代碼,沒有自己寫一遍。

記得有一位老師曾經在課上講過,每個人按照約定完成自己的工作,這是現代工業的基礎。

學習了一些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地址

結對程式設計爭論的點、體會稍後補充

四則運算-C#實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.