Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Realization of expression evaluation with Python3
Realization of expression evaluation with Python3
Include & lt malloc.h & gt # include & lt stdio.h & gt # include & ltctype.h & gt//the header file of the function that determines whether it is a character # definemaxsize100typedef intelementtype; Typedef structure sqstack sqstack// Since SQL stack is not a type, struct sqstack is charch [7] =; //Convert the symbol into a character array int f1[7] =; //In-stack element priority IntF2 [7] =; //Priority of elements other than stack structsqstack {elementtypestack [maxsize]; int top}; void init stack(sq stack * s){ s-& gt; top = 0; } void Push(sqstack *s,elem type x){ if(s-& gt; top = = maxsize- 1)printf(" Overflow \ n "); else { s-& gt; top++; s-& gt; Stack [s->; top]= x; } } void Pop(sqstack *s,elem type * x){ if(s-& gt; Top==0) printf ("underflow \ n"); else { * x = s-& gt; Stack [s->; top]; s-& gt; top-; }} Elem type gettop (sqstacks) {if (s.top = = 0) {printf ("underflow \ n"); Returns 0; } else returns s.stack [s.top]; } element type f (char c) {switch (c) {case'+':returns 0; Case'-':returns1; Case "*": Return 2; Case'/':Return 3; Case' (':returns 4; Case')': returns 5; Default: 6 is returned; } } char precend(char c 1,char C2){ int I 1 = f(c 1); int I2 = f(C2); //Convert characters into numbers if (f1[i1] >; F2[i2])// Find the priority to return through the original setting' >'; else if(f 1[I 1]& lt; F2[i2]) returns' : pop (&; OPTR & amp; θ); Pop (& b); Pop (& a); //Note who pushed the stack first here (&; OPND,Operate(a,theta,b)); Break; }}}//It is wrong to judge whether to end with an operator here (gettop(opnd));); } main() {int result; Printf ("Enter your arithmetic expression: \ n"); result = evaluate expression(); Printf ("Result: %d\n ",Result); Returns 0; }: This calculator is implemented with a stack. 1, which defines the stack structure of suffix calculator. Because there are not many cells to be stored, here we use sequential stack, that is, we use one-dimensional array to simulate the stack: # definemax100 intstack [max]; int top = 0; So the program defines a one-dimensional array with the length of MAX, where MAX is defined as a constant 100 by macro. We can modify the macro definition and redefine the size of the stack. The top of that integer data represent the top of the stack. Since there are no data elements in the stack when the program starts, TOP is initialized to 0. 2. Store the operand of suffix calculator. After defining stack[MAX], we can use stack operation to store two operands that have been entered continuously. Let's see how it is implemented: int push(int i) /* store operand, stack operation */{if (top