To compute an expression, a key issue is the matching of parentheses. Now, using the stack to calculate the expression value can be used as the core part of a simple arithmetic calculator. According to the stack features (Advanced and later), it is decided to convert the input expression into a suffix expression and use the suffix expression for calculation.
Implementation Method:
1. Define two stacks, one for storing operators and the other for storing operands.
1 # include <stdio. h>
2 # include <string>
3 # include <conio. h>
4 # define MAXSIZE 100
5 typedef float datatype;
6
7 typedef struct
8 {
9 datatype a [MAXSIZE];
10 int top;
11} sequence_stack;
12
13 typedef struct
14 {
15 char B [MAXSIZE];
16 int top;
17} SeqStack;
2. Two arrays are required. One is used to store the input infix expression, and the other is used to store the suffix expression after converting the infix expression.
Major code for converting an infix expression into a suffix expression:
1 int operation (char op) // determines whether it is an operator
2 {
3 switch (op)
4 {
5 case '+ ':
6 case '-':
7 case '*':
8 case '/': return 1;
9 default: return 0;
10}
11}
12 int priority (char op) // judge the operator priority
13 {
14 switch (op)
15 {
16 case '#': return-1;
17 case '(': return 0;
18 case '+ ':
19 case '-': return 1;
20 case '*':
21 case '/': return 2;
22 default: return-1;
23}
24}
25 // convert an infix expression to a suffix expression
26 void postfix (char e [], char f [], SeqStack * s, sequence_stack * s1)
27 {
28 int I = 0, j = 0;
29 int t;
30 push_SeqStack (s ,'#');
31 while (e [I]! = '#')
32 {
33 if (e [I]> = '0' & e [I] <= '9') | e [I] = '.')
34 f [j ++] = e [I];
35 else if (e [I] = '(')
36 {
37 push_SeqStack (s, e [I]);
38}
39 else if (e [I] = ')')
40 {
41 t = s-> top-1;
42 while (s-> B [t]! = '(')
43 {
44 f [j ++] = s-> B [-- s-> top];
45 t = s-> top-1;
46}
47 s-> top --;
48}
49 else if (operation (e [I])
50 {
51 f [j ++] = '';
52 while (priority (s-> B [s-> top-1])> = priority (e [I])
53 f [j ++] = s-> B [-- s-> top];
54 push_SeqStack (s, e [I]);
55}
56 I ++;
57}
58 while (s-> top) f [j ++] = s-> B [-- s-> top];
59 {}
60 evalpost (f, s1 );
61}
3. Pass the array containing the suffix expression to the function of the calculated expression;
Code used to calculate the value of a suffix expression:
1 float readnumber (char f [], int * I) // convert the number string
2 {
3 float x = 0.0;
4 int k = 0;
5 while (f [* I]> = '0' & f [* I] <= '9 ')
6 {
7 x = x * 10 + (f [* I]-'0 ');
8 (* I) ++;
9}
10 if (f [* I] = '.')
11 {
12 (* I) ++;
13 while (f [* I]> = '0' & f [* I] <= '9 ')
14 {
15 x = x * 10 + (f [* I]-'0 ');
16 (* I) ++;
17 k ++;
18}
19}
20 while (k! = 0)
21 {
22 x = x/10.0;
23 k = K-1;
24}
25 return (x );
26}
27 void evalpost (char f [], sequence_stack * s)
28 {
29 int I = 0;
30 float x1, x2;
31 while (f [I]! = '#')
32 {
33 if (f [I]> = '0' & f [I] <= '9 ')
34 {
35 push_sequence_stack (s, readnumber (f, & I ));
36}
37 else if (f [I] = '')
38 I ++;
39 else if (f [I] = '+ ')
40 {
41x2 = s-> a [-- s-> top];
42x1 = s-> a [-- s-> top];
43 push_sequence_stack (s, x1 + x2 );
44 I ++;
45}
46 else if (f [I] = '-')
47 {
48x2 = s-> a [-- s-> top];
49x1 = s-> a [-- s-> top];
50 push_sequence_stack (s, x1-x2 );
51 I ++;
52}
53 else if (f [I] = '*')
54 {
55x2 = s-> a [-- s-> top];
56x1 = s-> a [-- s-> top];
57 push_sequence_stack (s, x1 * x2 );
58 I ++;
59}
60 else if (f [I] = '/')
61 {
62x2 = s-> a [-- s-> top];
63x1 = s-> a [-- s-> top];
64 push_sequence_stack (s, x1/x2 );
65 I ++;
66}
67}
68}
Finally, as long as the result of the call computation is stored in the first position of the operand stack, and the result is passed to the desired place (you can put the result in your own program ), the result is displayed. Note that the expression to be calculated can be input in any way. Okay, that's it.