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 253 254
| #include "Rp.h" #include <iostream> #include <cstring> using namespace std;
void Rp::stack_pop(char *ch) { fake_stack *p; p = sp; *ch = p->value; sp = sp->fd; sp->bk = NULL; p->fd = NULL; delete p; }
void Rp::stack_push(char ch) { fake_stack *q; q = new fake_stack; q->value = ch; q->bk = NULL; q->fd = sp; sp->bk = q; sp = q; }
bool Rp::isNum(char ch) { if(ch < 'z' && ch >= 'a') return true; return false; }
int Rp::getNum(char ch, int value) { if(ch == 'T') return value; values *p; p = head; while(p!=NULL){ if(p->v_c == ch) return p->v; p = p->next; } cout<<"get num failed!"<<endl; return 0; } void Rp::show(char ch, char *in, char *out) { fake_stack *p; string stack; p = bp; do{ stack += p->value; p = p->bk; }while(p != NULL); cout<<ch; cout.width(20); cout<<in; cout.width(20); cout<<stack; cout.width(20); cout<<output; cout<<endl; }
int Rp::compare1(char ch) { switch (ch) { case '+': return 1; case '-': return 1; case '*': return 2; case '/': return 2; default: return -1; } } int Rp::compare(char arg1, char arg2) { int t1,t2; if(t1 = compare1(arg1)) if(t2 = compare1(arg2)) if(t1<=t2) return 0; return 1; }
void Rp::Calculate() { char *p = output; char value1,value2; char res = 'T'; int c_res; while(*p != '#'){ if(isNum(*p)){ stack_push(*p); p++; }else{ stack_pop(&value2); stack_pop(&value1); switch (*p) { case '+':{ stack_push(res); c_res = getNum(value1, c_res) + getNum(value2, c_res); p++; break; } case '-':{ stack_push(res); c_res = getNum(value1, c_res) - getNum(value2, c_res); p++; break; } case '*':{ stack_push(res); c_res = getNum(value1, c_res) * getNum(value2, c_res); p++; break; } case '/':{ stack_push(res); c_res = getNum(value1, c_res) / getNum(value2, c_res); p++; break; } default:{ cout<<"Erro!"; break; } } } } cout<<"\nCalculate result: "<<c_res<<endl; }
void Rp::Rpn() { char *in(input),*out(output); // 输入、输出缓冲区指针 cout<<"current\t\t"<<"input\t\t"<<"sym stack\t\t"<<"output"<<endl; char ch; ch = *in; in++; while (ch != '#'){ if(isNum(ch)){ *out = ch; out++; ch = *in; in++; show(ch,in,output); }else if(ch != '(' && ch!= ')'){ if(sp->value == '#'){ stack_push(ch); ch = *in; in++; show(ch,in,output); }else{ L: int t = compare(ch,sp->value); switch (t) { case 1:{ stack_push(ch); ch = *in; in++; show(ch,in,output); break; } default:{ stack_pop(out); show(ch,in,output); out++; goto L; } } } }else if(ch == '('){ stack_push(ch); show(ch,in,output); ch = *in; in++; }else if(ch == ')'){ L1: if(sp->value == '('){ char trash; stack_pop(&trash); show(ch,in,output); ch = *in; in++; }else if(sp->value == '#') cout<<"Erro!"<<endl; else{ stack_pop(out++); show(ch,in,output); goto L1; } }else{ while(sp->value == '#'){ if(sp->value != '('){ stack_pop(out++); show(ch,in,output); }else cout<<"Erro!"<<endl; } } } if(sp->value != '#'){ stack_pop(out++); show(ch,in,output); } *out = '#'; while(sp!=bp){ char temp; stack_pop(&temp); } }
Rp::Rp(char inputs[]) { strcpy(input,inputs); bp = new fake_stack; bp->value = '#'; sp = bp; bp->fd = NULL; bp->bk = NULL; head = new values; values *p; p = head; char temp; cout<<"\nPlease input sym and value(press E to end input): "<<endl; start: cout<<"\nsym: "; cin>> temp; if(temp == 'E') goto run; p->v_c = temp; cout<<"value: "; cin>> p->v; p->next = new values; p = p->next; goto start; run: fflush(stdin); fflush(stdout); Rpn(); Calculate(); }
|