Pair programming: Arithmetic. Member: Hao Nan Yang Yuning Development language: C language

Source: Internet
Author: User

Demand analysis: 1. Ability to automatically question and answer

2. Contains the arithmetic of "+,-,*,/, ()".

3. Display the answer to the question

Structural design: 1. Automatic problem-solving using random number generation statement implementation: including randomly generated numbers, operation symbols and topic length

2. Use variables to constrain the order of left and right brackets and the characteristics of simultaneous existence.

3. The problem output is stored with a string.

4. Compute the string into the stack.

Some core code:

Randomly generated topic length:

A=rand ()%3+3;
for (i=0;i<a;i++)
{
......
}

Randomly generated numbers and symbols:

C=rand ()% ++1; Digital printf ("%d", c);
B=rand ()%4; Symbols
Switch (b)
{
Case 0:printf ("+"); math[m]= ' + '; m++;
if (i<a-3&&j==10) b=10;
else B=rand ()%4; Break
Case 1:printf ("-"); math[m]= '-'; m++;
if (i<a-3&&j==10) b=10;
else B=rand ()%4; Break
Case 2:printf ("*"); math[m]= ' * '; m++;
if (i<a-3&&j==10) b=10;
else B=rand ()%4; Break
Case 3:printf ("/"); math[m]= '/'; m++;
b=20; break;
}
if (b==10)
{
printf ("(");
Math[m]= ' ('; m++;
j=3;
B=rand ()%4;
}
if (j!=10)
j--;
if (j==0) {
printf (")");
Math[m]= ') ';
m++;
B=rand ()%3;
j=10;
}

To calculate an expression using a stack:

The symbol precedence value is used to control five operation symbols.

 while(c!='='|| x!='=')//Loop through each character in an expression    {        if(Isoperator (c))//if the operator        {            if(flag) {Seqstackpush (stackdata,q); //expression into the stackq=0;//operand Clear 0flag=0;//the flag is zeroed to indicate that the operand is already in the stack            }            Switch(PRI (X,C))//determining operator Precedence            {                 Case-1: Seqstackpush (STACKOPER,C); //operator Input Stackc=exp[i++];  Break;  Case 0: C=seqstackpop (Stackoper);//operator parentheses, equals sign out of stack, discardedC=exp[i++];//Remove a character                     Break;  Case 1: Oper=seqstackpop (Stackoper);//operator out of Stackb=Seqstackpop (Stackdata); A=seqstackpop (Stackdata);//two operand out of stackT=calc (A,OPER,B);//Calculation ResultsSeqstackpush (stackdata,t);//put the results of the operation into the stack                  Break; }         }Else if(c>='0'&&c<='9')//if the output character is between 0 and 9{C-='0';//convert a character to a numberq=q*Ten+c;//rounding of multi-digit numbersC=exp[i++];//Remove the next characterflag=1;//sets a flag indicating that the operand is not in the stack        }         Else{printf ("input error \ n");            Getch (); Exit (0); } x=seqstackpeek (Stackoper);//get top of stack operator} q=Seqstackpop (Stackdata);      Seqstackfree (Stackoper);  Seqstackfree (Stackdata); //free up memory footprint    returnQ//out of the stack, return the result         } 

The decision of the precedence value:

intPRI (CharOper1,CharOPER2){    intpri; Switch(Oper2)//judging the priority level    {         Case '+':         Case '-':            if(oper1=='('|| oper1=='=')//is an opening parenthesispri=-1;//Oper1<oper2            Elsepri=1;//Oper1>oper2             Break;  Case '*':         Case '/':            if(oper1=='*'|| oper1=='/'|| oper1==')') PRI=1;//Oper1>oper2            Elsepri=-1;//Oper1<oper2               Break;  Case '(':            if(oper1==')')//left parenthesis cannot appear immediately to right parenthesis{printf ("syntax error \ n"); Exit (0); } Elsepri=-1;//Oper1<oper2               Break;  Case ')':            if(oper1=='(') PRI=0; Else if(oper1=='=') {printf ("parentheses do not match \ n"); Exit (0); }Elsepri=1;  Break;  Case '=':            if(oper1=='(') {printf ("parentheses do not match \ n"); Exit (0); }Else if(oper1=='=') PRI=0;//equal sign match, return 0             Elsepri=1;//Oper1>oper2                Break; }    returnpri;} 

Main function Control problem number:

Charans[]=" /"; intB,i; Srand (Time (0));  while(1) {printf ("Current function: Generate a question bank \ n"); printf ("Please enter the required number of topics:"); scanf ("%d",&b);  for(i=0; i<b;i++) {exp (); printf ("%d\n", Calcexp (math)); }    }


Operation Result:

Pair programming: Arithmetic. Member: Hao Nan Yang Yuning Development language: C language

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.