Arithmetic-c# implementation

Source: Internet
Author: User

When you get the title, it's time for a holiday, and no mind to do it. The afternoon of October 7, probably from two o ' 8:30, plus October 8 12:30 to two and a half, did a total of 8.5 hours, cut off to eat half an hour so altogether did eight hours.

Inverse Polish expression I am the code of the other people, did not write it myself again.

Remember that a teacher once said in class, everyone in accordance with the agreement to complete their work, which is the basis of modern industry.

Learned some C # languages.

I wrote the code to generate a arithmetic expression with parentheses. The numbers are random, the parentheses are violent, and the extra violence.

Rational number calculation lazy write, goddess favor worthless.

How do you avoid generating infinite decimals after parentheses? This I did not avoid, still sometimes calculated with a decimal, less than.

It is better to handle the parentheses before it is added.

Since the first number must not be divisor (appearing in the denominator)

I directly construct the number of 2,3,4 into 2^m*5^n, so that you can get a finite fraction, of course, there may be a better and more beautiful way.

Here are some key codes to stick with.

  Static voidMain (string[] args) {   intNum//The number of expressions            intCNT =0; inttoken =0; vararguments = Commandlineargumentparser.parse (args);//command line parameter            intAnslen =1;//The number of the answers of expressions that has been used.            Double[] ansrep;//array to maintain answers that has been usedAnsrep =New Double[ +]; ansrep[0] = -23579; if(!arguments. Has ("- C"))//if the command line argument does not have the-cnum = -; Else{num=int. Parse (arguments. Get ("- C"). Next);//the number of expressions to be output in the command-line argumentstoken =1; }            if(token==0)            {                 for(inti =0; i < num; i++) {Test T=NewTest ();//New TestT.opinit ();                    T.getbracketexp (); //T.getexp (); //T.norepeatedans (Anslen, ansrep);ansrep[(anslen++)-1] =T.ANSR; T.dispexp ();//Display ExpressionConsole.Write ("?"); DoubleAns =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                        {                            stringFloatvalue =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(inti =0; i < num; i++) {Test T=NewTest ();//New TestT.opinit ();                    T.getbracketexp (); //T.getexp (); //T.norepeatedans (Anslen, ansrep);ansrep[(anslen++)-1] =T.ANSR; T.dispexpans (t);//display expression and answer}} Console.WriteLine ("Total problems,you has solved {0}problem (s)", CNT); }    }

This is my main function.

It is worth mentioning that reading command-line parameters is also used by other people's code, these two add up to nearly 300 lines of more.

Pair programming Photos, finally modify some code format, find some bugs, my partner wangyuling classmate patiently give me help and guidance, as a pilot to give some direction of opinion.

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

This is the git address of my code.

The point of contention in pairing programming, and the experience later added

Arithmetic-c# implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.