1 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| #include <iostream>
using namespace std;
template<class T> struct StackNode //栈结点 { T data; StackNode<T> *next; };
template<class T> struct LinkStack // 链栈 { StackNode<T> *top; int count; };
int priority[7][7] = { {1, 1, -1, -1, -1, 1, 1}, {1, 1, -1, -1, -1, 1, 1}, {1, 1, 1, 1, -1, 1, 1}, {1, 1, 1, 1, -1, 1, 1}, {-1, -1, -1, -1, -1, 0, -2}, {1, 1, 1, 1, -2, 1, 1}, {-1, -1, -1, -1, -1, -2, 0} };
template<class T> bool InitStack(LinkStack<T> *s);
template<class T> bool push(LinkStack<T> *s, T data);
template<class T> bool pop(LinkStack<T> *s);
template<class T> bool StackEmpty(LinkStack<T> *s);
template<class T> T GetTop(LinkStack<T> *s);
int GetPriority(char a, char b); int Operate(int a, char theta, int b);
template<class T1, class T2> void GetExpressionValue(LinkStack<T1> *OPNd, LinkStack<T2> *OPTr);
int main() {
LinkStack<int> *OPNd = new LinkStack<int>; LinkStack<char> *OPTr = new LinkStack<char>; InitStack(OPNd); InitStack(OPTr);
push(OPTr, '#'); cout << "请输入你要计算的表达式,记得在表达式的末尾加上'#'! " << endl; GetExpressionValue(OPNd, OPTr); cout << "答案为:" << GetTop(OPNd); return 0; }
template<class T> bool InitStack(LinkStack<T> *s) { s->top = NULL; s->count = 0; return true; }
template<class T> bool push(LinkStack<T> *s, T data) { StackNode<T> *p = new StackNode<T>; p->data = data; p->next = s->top; s->top = p; s->count++; return true; }
template<class T> bool pop(LinkStack<T> *s) { if (StackEmpty(s)) { cout << "栈为空,无元素可弹出!" << endl; return false; } StackNode<T> *p = s->top; s->top = p->next; s->count--; delete p; return true; }
template<class T> bool StackEmpty(LinkStack<T> *s) { return s->top == NULL; }
template<class T> T GetTop(LinkStack<T> *s) { if (s->top == NULL) { cout << "栈为空,无栈顶元素!" << endl; } return s->top->data; }
template<class T1, class T2> void GetExpressionValue(LinkStack<T1> *OPNd, LinkStack<T2> *OPTr) { char theta; char ch;
cin >> ch; while (ch != '#' || GetTop(OPTr) != '#') {
if (isdigit(ch)) { int data = 0; while (isdigit(ch)) { data = data * 10 + ch - '0'; cin >> ch; }
push(OPNd, data); } else {
switch (GetPriority(GetTop(OPTr), ch)) { case -1: { push(OPTr, ch); cin >> ch; break; } case 0: { pop(OPTr); cin >> ch; break; } case 1: { int a, b; theta = GetTop(OPTr); pop(OPTr); b = GetTop(OPNd); pop(OPNd); a = GetTop(OPNd); pop(OPNd); push(OPNd, Operate(a, theta, b)); break; } default: break; } } } }
int GetPriority(char a, char b) { int i = -1; int j = -1; switch (a) { case '#': i = 6; break; case ')': i = 5; break; case '(': i = 4; break; case '/': i = 3; break; case '*': i = 2; break; case '-': i = 1; break; case '+': i = 0; break; default: break; }
switch (b) { case '#': j = 6; break; case ')': j = 5; break; case '(': j = 4; break; case '/': j = 3; break; case '*': j = 2; break; case '-': j = 1; break; case '+': j = 0; break; default: break; }
if (i >= 0 && j >= 0) { return priority[i][j]; } else { return -2; } }
int Operate(int a, char theta, int b) { int res = 0; switch (theta) { case '+': res = a + b; break; case '-': res = a - b; break; case '*': res = a * b; break; case '/': if (b == 0) { cout << "分母为零,运算出错!" << endl; res = 0; } else { res = a / b; } break; default: break; } return res; }
|