標籤:比較 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#實現