Pair programming Implementation arithmetic

Source: Internet
Author: User

Due to the time rush, the program is still some distance from the complete body, but the completion degree has been 90%. The rest is to optimize the user-friendliness and the like.

Main completion function: 1, realize the random generation of arithmetic formula.

2, the result is calculated by the four synthesis calculation formula which is generated randomly.

To complete the function: 1, to increase the integration system, generated by the random formula is a score, for example, 1+2+3+4 this expression and 99*98*97*96 this type of operation is not the same, the score is determined by two aspects, one is the size of the number, on the other hand is an operator. For example, the number and Mahayana division and so on more points high.

2, add bonus points, such as the score to say a word of encouragement or a pattern.

Here is the program section:

The 1,itos function converts the shape to a string.

1 string itos (int2{3     stringstream s; 4      s << i; 5      return s.str (); 6  }

The 2,num function generates a random number within 100.

int num () {    return rand ()%;    }

The 3,FH function, which generates a random number of 4 or less, is used for random operators.

int FH () {    return rand ()%4;}

4,convert2rpn function, the character of the formula has infix expression conversion to the suffix expression, used for subsequent calculations, is a very important function, carrying a great deal of work.

voidCONVERT2RPN (string&s) {StringStream ss; Stack<Char>Stk;  for(size_t i =0; I < s.length (); i++) {        if(IsDigit (s.at (i))) {SS<<s.at (i); //If the next one is not a number, or is already the last, add an overhead grid            if((I < s.length ()-1&&!isdigit (s.at (i +1)))                    || i = = S.length ()-1) {SS<<' '; }        }        Else {            if(Stk.empty ()) {Stk.push (s.at (i)); }            Else {                Switch(s.at (i)) { Case '(': Stk.push (s.at (i));  Break;  Case ')':                    //put the matching ' (' and above symbols out of the stack                     while(Stk.top ()! ='(') {SS<<Stk.top ();                    Stk.pop ();                    } stk.pop ();  Break;  Case '+':                 Case '-':                    //' + '-' * '/' Both out of stack                     while(!stk.empty () && stk.top ()! ='(') {SS<<Stk.top ();                    Stk.pop ();                    } stk.push (s.at (i));  Break;  Case '*':                 Case '/':                    //' * ' and '/' out of the stack                     while(!stk.empty () && (stk.top () = ='*'|| Stk.top () = ='/') ) {SS<<Stk.top ();                    Stk.pop ();                    } stk.push (s.at (i));  Break; }            }        }    }    //when the operation is finished, the elements of the stack are pop out .     while(!Stk.empty ()) {SS<<Stk.top ();    Stk.pop (); } s=ss.str ();}

The 5,CALCULATERPN function is used to evaluate the suffix expression.

floatCALCULATERPN (Const string&s) {stack<float>Stk;  for(size_t i =0; I < s.length (); i++) {        //if it's a number, it's combined with the previous numbers .        if(IsDigit (s.at (i))) {intE = atoi (&s.at (i)); intT = E/Ten;  while(T >0) {i++; T/=Ten; } I++;        Stk.push (e); }        Else {            floatR =Stk.top ();            Stk.pop (); floatL =Stk.top ();            Stk.pop (); floatresult; Switch(s.at (i)) { Case '+': Result= L +R;  Break;  Case '-': Result= L-R;  Break;  Case '*': Result= L *R;  Break;  Case '/': Result= L/R;  Break;        } stk.push (Result); }    }    returnstk.top ();} 


6, Main function

intMain () {Charope[4]={'+','-','*','/'};    Srand (Time (NULL)); strings; S= ITOs (num ()) +OPE[FH ()]+itos (num ()) +OPE[FH ()]+itos (num ()) +OPE[FH ()]+ITOs (num ()); cout<< s+" "+"=";    CONVERT2RPN (s); cout<< CALCULATERPN (s) <<Endl; return 0;}

The results of the program, although relatively simple, but there are some technical content, after the update will be more user-friendly.

Only do a little work, ask teachers and classmates to criticize, thank you.

Pair programming Implementation arithmetic

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.