1. Requirement Analysis
Design a program to demonstrate the process of using the operator priority method to evaluate an arithmetic expression. The operator precedence is used to evaluate the arithmetic expression.
(1) Input Format: expression, for example, 2*(3 + 4)
The contained operators can only include '+ ','-','*','/','(',')';
(2) form of output: calculation result, for example, 2*(3 + 4) = 14;
(3) functions that the program can achieve: evaluate the expression and Output
2. System Design
1. Stack abstract data type definition:
ADT Stack {
Data Object: D = {ai | ai ε ElemSet, I = ,..., N, n ≥ 0}
Data Relationship: R1 = {<ai-1, ai> | ai-1, ai, D, I = 2 ,..., N}
It is agreed that an end is the top of the stack, and ai end is the bottom of the stack.
Basic operations:
Push (& S, e)
Initial Condition: Stack S already exists
Operation Result: Element e is inserted as the new top element of the stack.
Pop (& S, & e)
Initial Condition: Stack S already exists and is not empty
Operation Result: Delete the stack top element of S and return its value with e.
} ADT Stack
3. Main functions of each module:
* Push (SC * s, char c): press the character to stack
* Push (SF * s, float f): pushes the value to the stack.
* Pop (SC * s): unstacks characters
* Pop (SF * s): returns the value to the stack.
Operate (a, theta, B): Perform '+', '-', '*', '/', and '^' operations on a and B Based on theta.
In (Test, * TestOp): If Test is an operator, true is returned. Otherwise, false is returned.
ReturnOpOrd (op, * TestOp): If Test is an operator, the subscript of this operator in the array is returned.
Precede (Aop, Bop): returns the priority between Aop and Bop Based on the operator priority table.
EvaluateExpression (* MyExpression): Evaluate the arithmetic expression using the operator Priority Method
The complete program code is as follows:
Copy codeThe Code is as follows: # include "stdio. h"
# Include "stdlib. h"
# Include "string. h"
# Include "math. h"
# Define true 1
# Define false 0
# Define OPSETSIZE 8
Typedef int Status;
Unsigned char Prior [8] [8] =
{// Operator priority table
// '+ ''-''' * '''/'' ('')'' # ''^'
/* '+' */'>', '>', '<', '>', '>', '<',
/* '-' */'>', '>', '<', '>', '>', '<',
/* '*/'> ','> ',' <','> ','> ',' <',
/* '/' */'>', '<', '>', '>', '<',
/* '(' */'<', '=', ',' <',
/* ')' */'>', ','> ',
/* '#' */'<', ',' = ',' <',
/* '^' */'>', '<', '>'
};
Typedef struct StackChar
{
Char c;
Struct StackChar * next;
} SC; // node SC of StackChar type
Typedef struct StackFloat
{
Float f;
Struct StackFloat * next;
} SF; // node SF of StackFloat type
SC * Push (SC * s, char c) // SC pointer Push, returns p
{
SC * p = (SC *) malloc (sizeof (SC ));
P-> c = c;
P-> next = s;
Return p;
}
SF * Push (SF * s, float f) // SF-type pointer Push, returns p
{
SF * p = (SF *) malloc (sizeof (SF ));
P-> f = f;
P-> next = s;
Return p;
}
SC * Pop (SC * s) // SC-type pointer Pop
{
SC * q = s;
S = s-> next;
Free (q );
Return s;
}
SF * Pop (SF * s) // SF type pointer Pop
{
SF * q = s;
S = s-> next;
Free (q );
Return s;
}
Float Operate (float a, unsigned char theta, float B) // calculate the function Operate
{
Switch (theta)
{
Case '+': return a + B;
Case '-': return a-B;
Case '*': return a * B;
Case '/': return a/B;
Case '^': return pow (a, B );
Default: return 0;
}
}
Char OPSET [OPSETSIZE] = {'+', '-', '*', '/', '(', ')', '#', '^ '};
Status In (char Test, char * TestOp)
{
Int Find = false;
For (int I = 0; I <OPSETSIZE; I ++)
{
If (Test = TestOp [I])
Find = true;
}
Return Find;
}
Status ReturnOpOrd (char op, char * TestOp)
{
For (int I = 0; I <OPSETSIZE; I ++)
{
If (op = TestOp [I])
Return I;
}
}
Char precede (char Aop, char Bop)
{
Return Prior [ReturnOpOrd (Aop, OPSET)] [ReturnOpOrd (Bop, OPSET)];
}
Float EvaluateExpression (char * MyExpression)
{
// Operator Priority Algorithm for arithmetic expression value calculation
// Set OPTR and OPND as the operator stack and arithmetic stack respectively, and OP as the operator set
SC * OPTR = NULL; // operator stack, character element
SF * OPND = NULL; // Operation Number stack, real number Element
Char TempData [20];
Float Data, a, B;
Char theta, * c, Dr [] = {'#', '\ 0 '};
OPTR = Push (OPTR ,'#');
C = strcat (MyExpression, Dr );
Strcpy (TempData, "\ 0"); // string copy Function
While (* c! = '#' | OPTR-> c! = '#')
{
If (! In (* c, OPSET ))
{
Dr [0] = * c;
Strcat (TempData, Dr); // String concatenation Function
C ++;
If (In (* c, OPSET ))
{
Data = atof (TempData); // String Conversion Function (double)
OPND = Push (OPND, Data );
Strcpy (TempData, "\ 0 ");
}
}
Else // stack if it is not an operator
{
Switch (precede (OPTR-> c, * c ))
{
Case '<': // The top element of the stack has a low priority.
OPTR = Push (OPTR, * c );
C ++;
Break;
Case '=': // parentheses and one character
OPTR = Pop (OPTR );
C ++;
Break;
Case '>': // return the stack and import the calculation result to the stack.
Theta = OPTR-> c; OPTR = Pop (OPTR );
B = OPND-> f; OPND = Pop (OPND );
A = OPND-> f; OPND = Pop (OPND );
OPND = Push (OPND, Operate (a, theta, B ));
Break;
} // Switch
}
} // While
Return OPND-> f;
} // EvaluateExpression
Int main (void)
{
Char s [128];
Puts ("Enter the expression :");
Gets (s );
Puts ("the expression value is :");
Printf ("% s \ B = % g \ n", s, EvaluateExpression (s ));
System ("pause ");
Return 0;
}
The test results are as follows: