Arithmetic Improved version

Source: Internet
Author: User

Requirements Analysis:

Randomly generate arithmetic formula, including parentheses and can answer the question, in the answer to the question to judge right and wrong, if the answer is wrong to show the correct answer.

Code Design:

Suffix expression:

  

voidHouzhuibds (Charstr[ -])//suffix expression{Stack<Char>S1; Stack<Number>S2; intI=0, j=0;  for(i=0; str[i]!=' /'; i++)    {        if(str[i]>='0'&&str[i]<='9') {Number num; NUM.A=0; Num.b=0;  while(str[i]<='9'&&str[i]>='0') NUM.A= (str[i++]-'0') +num.a*Ten;                S2.push (num); I--; }        Else        {            if(str[i]==')')            {                 while(S1.top ()! ='(') {Number num; Num.b=1; NUM.A=S1.top ();                    S2.push (num);                S1.pop ();            } s1.pop (); }            Else if(S1.empty () | | S1.top () = ='('|| str[i]=='(') {S1.push (str[i]); }            Else            {                if((str[i]=='*'|| str[i]=='/') && (s1.top () = ='+'|| S1.top () = ='-') ) S1.push (Str[i]); Else{number num; Num.b=1; NUM.A=S1.top ();                    S2.push (num);                    S1.pop (); I--; }            }                    }    }     while(!S1.empty ())        {Number num; Num.b=1; NUM.A=S1.top ();        S2.push (num);    S1.pop (); }     while(!S2.empty ())        {S3.push (S2.top ());    S2.pop (); }}

Evaluation operation:

DoubleQiuzhi () {stack<Double>S4;  while(!S3.empty ()) {Number C1=S3.top ();        S3.pop (); if(c1.b==0) S4.push (c1.a); Else        {            DoubleC2=S4.top ();            S4.pop (); Doublec3=S4.top ();            S4.pop (); DoubleC4; Switch((int) {c1.a) { Case '+': C4=C3+C2; Break;  Case '-': C4=C3-C2; Break;  Case '*': C4=C3*C2; Break;  Case '/': C4=C3/C2; Break;        } s4.push (C4); }    }    returns4.top ();}

After determining the number of elements:

voidAftersz (Charstr[ -],intTintN//the number is followed by a +-/* (0-4) closing parenthesis (5-9){    intI=rand ()%Ten; if(i<=4)    {        intJ=rand ()%4; Switch(j) { Case 0: str[t]='+'; Break;  Case 1: str[t]='-'; Break;  Case 2: str[t]='*'; Break;  Case 3: str[t]='/'; Break; } afterfh (str,++t,n); }    Else//closing parenthesis    {        if(y>0&& (str[t-2]!='('|| str[t-3]!='('&& (str[t-2]<='0'&&str[t-2]>='9')) {str[t]=')'; Y--; Aftersz (str,++t,n); }        ElseAftersz (str,t,n); }        }

To determine the post-symbol element:

voidAFTERFH (Charstr[ -],intTintN//after +-*/{        intP=rand ()%Ten; if(p>=3)//Digital        {            intNum=rand ()% -; if(num>=Ten)            {                inta=num%Ten; Str[t++]=num/Ten+'0'; Str[t]=a+'0'; }            ElseStr[t]='0'+num; N++; if(n==4) {str[++t]=' /'; return ; } aftersz (str,++t,n); }        Else//opening parenthesis{Str[t]='('; Y++; AFTERFH (str,++t,n); }}

Main function:

intMain () {Charstr[ -]; ints=0;  for(intI=0;i< -; i++)    {        intt=0; intn=0;         AFTERFH (Str,t,n); intlen=strlen (str);  while(y) {if(str[len-2]=='(') {Str[len-2]=str[len-1]; Len--; }            Else if(str[len-3]=='(') {Str[len-3]=str[len-2]; Str[len-2]=str[len-1]; Len--; }            ElseStr[len++]=')'; Y--; } Str[len]=' /';        Houzhuibds (str); Doubleans=Qiuzhi ();  for(intI=0; str[i]!=' /'; i++) printf ("%c", Str[i]); printf ("=\n?"); Doubles; scanf ("%LF",&s); if(ABS (S-ans) <0.01) {s++; printf ("you're a genius."); }        Elseprintf ("think again, the answer seems to be%.2lf oh!\n", ans); } printf ("you have a total of 20 answers to%d questions. \ n", s); return 0;}

Operation Result:

Ssh:[email Protected]:ziyoujay/sizeyunsuan1.git

Arithmetic Improved version

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.