problem description ]
Expression solving is one of the basic problems of implementing programming , and it is also a typical example of the application of stack . Design a program to evaluate an expression using operator precedence .
[ Basic Requirements ]
Input a grammatically correct, variable-free integer expression from the keyboard in the form of a sequence of characters, and use the operator precedence relationship given in Table 3.1 of the textbook to evaluate the four mixed arithmetic expressions, and imitate the textbook example 3 - 1 Demonstrate the change process of operator stack , operand stack , input characters and main operations during evaluation .
[implementation hints ]
( 1 ) Set the operator stack and the operand stack to assist in analyzing the operator precedence relationship;
( 2 ) while reading the sequence of expressions, complete the identification processing of the operand (integer) and the corresponding operation;
( 3 ) While identifying the operand, convert its character sequence form into integer form;
( 4 ) Output the operator stack, operand stack, input characters and the contents of the main operation at the appropriate position in the program
First understand the following arithmetic precedence:
designing process:
Data Type Definition:
1. Sequence stack definition
typedef struct sqstack {//Sequence stack structure elemtype stack[MAXSIZE]; int top; }sqstsck;
2. Global variable definition:
char ch[8]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'}; //Convert symbols to character arrays int f1[7]={3,3,5,5,1,6,0}; //Element priority in stack int f2[7]={2,2,4,4,6,1,0}; //Priority of elements outside the stack int n=0;
Source program:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<ctype.h> //Determine whether it is the header file of the character function #define MAXSIZE 100 typedef int elemtype; char ch[8]={'+' , '-' , '*' , '/' ,'(' , ')' , '#'}; //Convert symbols to character arrays int f1[7]={3,3,5,5,1,6,0}; //Element priority in stack int f2[7]={2,2,4,4,6,1,0}; //Priority of elements outside the stack int n=0; typedef struct sqstack {//Sequence stack structure elemtype stack[MAXSIZE]; int top; }sqstsck; //1. Convert the operator to the corresponding number elemtype cton(char c) { switch(c) { case '+': return 0; case '-': return 1; case '*': return 2; case '/': return 3; case '(': return 4; case ')': return 5; default: return 6; } } //2. Compare character precedence char Compare(char c1,char c2) { int i1=cton(c1); int i2=cton(c2); //turn characters into numbers if(f1[i1]>f2[i2]) return '>'; //find the priority through the original setting else if(f1[i1]<f2[i2]) return '<'; else return '='; } //3. Four arithmetic operations int Operate(elemtype a,elemtype t,elemtype b) { int sum switch(t) { case 0: sum=a+b; break; case 1: sum=a-b; break; case 2: sum=a*b; break; default: sum=a/b; } return sum; } //5. Take the top element of the stack elemtype Gettop(sqstack s) { if(s.top ==0) { printf("ERROR,underflow\n"); return 0; } else return s.stack[s.top]; } //6. Initialization void Initstack(sqstack *s) { s->top =0; } //7. Pop the stack void Pop(sqstack *s,elemtype *x) { if(s->top ==0) printf("ERROR,underflow!\n"); else { *x=s->stack [s->top]; s->top--; } } //8. Push the stack void Push(sqstack *s,elemtype x) { if(s->top ==MAXSIZE-1) printf("ERROR,Overflow!\n"); else { s->top++; s->stack[s->top]=x; } } //9. If the stack is empty, return TRUE, otherwise return FASLE bool StackEmpty(sqstack s) { if(s.top ==0) return true; else return false; } //4. Main expression function int EvaluateExpression() { char c; int i=0,sum=0; int k=1,j=1; //Set the switch variable elemtype x,t,a,b; sqstack OPTR,OPND; Initstack(&OPTR); Push(&OPTR,cton('#')); //'#' is pushed onto the stack Initstack(&OPND); c=getchar(); while(c!='#'||ch[Gettop(OPTR)]!='#') { if(isdigit(c)) //Determine whether c is a number { sum=0; while(isdigit(c)) { if(!j) //j is used for string conversion judgment, j is 0 conversion { sum = sum * 10 ( c - ' 0 ' ) ; } else sum=sum*10+(c-'0'); //The character c is converted into the corresponding number c=getchar(); } Push(&OPND,sum); //The current c is not a number, then convert the previous number string to a decimal number and then press j=1; } else if(k) { switch(Compare(ch[Gettop(OPTR)],c)) { case '<': Push(&OPTR,cton(c)); //Integer the character and push it into the operator stack c=getchar(); break; case '=': Pop(&OPTR,&x); //The top element of the operator stack is popped c=getchar(); break; case '>': Pop(&OPTR,&t); //The top element of the operator stack is popped Pop(&OPND,&b); //The top element of the operand stack is popped from the stack Pop(&OPND,&a); //The top element of the operand stack is popped from the stack Push(&OPND,Operate(a,t,b)); break; } } } return (Gettop(OPND)); } void main() { system("color a"); int result; printf("Please enter the expression '#' to end:\n"); result=EvaluateExpression(); printf("The result of the expression is: %d\n",result); system("pause"); }