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