那么為什么要用后綴表達式求值?根據我自己的理解,后綴表達式求值不必判斷運算符的優先級,只要按順序執行。每遇到一個操作符就處理一次,便于計算機處理。
那么下面我就分享一下我的中綴表達式轉后綴的代碼:
也許大家在考試時轉化一定會:
中綴表達式:(8+9*10)-4/2+3
其轉化思路:
1、將每個操作符對應的兩個操作數用括號括上(((8+(9*10))-(4/2))+3)
2、將操作符移到對應的括號外(((8(910*)+)(42)/)-3)+
3、去掉括號即可 8910*+42/-3+
如果要用數據結構中的棧和隊列實現
1、用一個字符串存儲表達式
2、掃描字符串。當其為0--9時直接入隊列,遇到左括號入棧,操作符級別 #:0用于最后判斷,+ -:1,* /:2
3、首先向堆中放一個#,當進入堆的操作符的級別不小于棧頂操作符的優先級,則入棧,否則彈出棧頂元素并進入隊列,將下一個操作符壓入棧。
4、一直循環直到將所有字符處理完
5、最后將所有元素出隊列并打印就可得到后綴表達式
1
#include<stdio.h>
2
#define Max 20
3
//自定義棧
4
template<class Elem>
5
struct Stack
{
6
int top;
7
Elem *p;
8
int size;
9
};
10
template<class Elem>
11
void Set_Ssize(Stack<Elem> &sta,int n)
{
12
sta.size=n;
13
sta.p=new Elem[sta.size];
14
sta.top=0;
15
}
16
template<class Elem>
17
void Push(Stack<Elem> &sta,Elem item)
{
18
sta.p[sta.top++]=item;
19
}
20
template<class Elem>
21
void Pop(Stack<Elem> &sta,Elem &e)
{
22
e=sta.p[--sta.top];
23
}
24
template<class Elem>
25
bool IsEmpty(Stack<Elem> &sta)
{
26
return sta.top==0;
27
}
28
template<class Elem>
29
bool IsFull(Stack<Elem> &sta)
{
30
return sta.top==sta.size;
31
}
32
//自定義隊列
33
template<class Elem>
34
struct MyQuene
{
35
int front;
36
int rear;
37
Elem *p;
38
int size;
39
};
40
template<class Elem>
41
void Set_Qsize(MyQuene<Elem> &Q,int n)
{
42
Q.size=n;
43
Q.front=0;
44
Q.rear=0;
45
Q.p=new Elem[Q.size];
46
}
47
template<class Elem>
48
void AddQuene(MyQuene<Elem> &Q,Elem item)
{
49
Q.p[Q.rear++]=item;
50
if(Q.rear==Q.size)
51
Q.rear=0;
52
}
53
template<class Elem>
54
void DeleteQuene(MyQuene<Elem> &Q,Elem& e)
{
55
e=Q.p[Q.front++];
56
if(Q.front==Q.size)
57
Q.front=0;
58
}
59
template<class Elem>
60
Elem GetTop(Stack<Elem> &sta)
{
61
return sta.p[sta.top-1];
62
}
63
template<class Elem>
64
bool IsEmpty(MyQuene<Elem> &Q)
{
65
return Q.front==Q.rear;
66
}
67
template<class Elem>
68
bool IsFull(MyQuene<Elem> &Q)
{
69
int len=Q.front<Q.rear?Q.rear-Q.front:Q.rear-Q.front+Q.size;
70
return len==Q.size-1;
71
}
72
//定義運算符的優先級
73
int GetPrecedence(char a)
{
74
switch(a)
{
75
case '#':return 0;
76
case '+':
77
case '-':return 1;
78
case '*':
79
case '/':return 2;
80
case '^':return 3;
81
default:break;
82
}
83
}
84
template<class Elem>
85
void Middle_Bhind(Stack<Elem> &st,MyQuene<Elem>&Mq,char*A,int n)
{//中綴表達式轉為后綴表達式(自己的實驗需求)
86
Set_Ssize(st,n);
87
Set_Qsize(Mq,n+1);
88
char tem;
89
int i=0;
90
Push(st,'#');
91
do
{
92
if((A[i]>='0'&&A[i]<='9')||(A[i]>='A'&&A[i]<='Z')||(A[i]>='a'&&A[i]<='z'))
93
AddQuene(Mq,A[i]);
94
else if(A[i]=='(')
95
Push(st,A[i]);
96
else if(A[i]==')')
{
97
while(GetTop(st)!='(')
{
98
Pop(st,tem);
99
AddQuene(Mq,tem);
100
}
101
Pop(st,tem);
102
}
103
else if(GetTop(st)=='(')
104
Push(st,A[i]);
105
else
{
106
while(GetPrecedence(A[i])<=GetPrecedence(GetTop(st)))
{
107
Pop(st,tem);
108
AddQuene(Mq,tem);
109
}
110
Push(st,A[i]);
111
}
112
i++;
113
}while(A[i]!='\0');
114
while(GetTop(st)!='#')
{
115
Pop(st,tem);
116
AddQuene(Mq,tem);
117
}
118
while(!IsEmpty(Mq))
{
119
DeleteQuene(Mq,tem);
120
printf("%c",tem);
121
}
122
putchar('\n');
123
}
124
void main()
{
125
char str[Max];
126
Stack<char> st;
127
MyQuene<char>Mq;
128
printf("請輸入中綴表達式:");
129
gets(str);
130
printf("后綴表達式:");
131
Middle_Bhind(st,Mq,str,Max);
132
}
133
134
135
136
137

2

3

4

5



6

7

8

9

10

11



12

13

14

15

16

17



18

19

20

21



22

23

24

25



26

27

28

29



30

31

32

33

34



35

36

37

38

39

40

41



42

43

44

45

46

47

48



49

50

51

52

53

54



55

56

57

58

59

60



61

62

63

64



65

66

67

68



69

70

71

72

73



74



75

76

77

78

79

80

81

82

83

84

85



86

87

88

89

90

91



92

93

94

95

96



97



98

99

100

101

102

103

104

105



106



107

108

109

110

111

112

113

114



115

116

117

118



119

120

121

122

123

124



125

126

127

128

129

130

131

132

133

134

135

136

137
