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
|
#include <iostream> #include <fstream> #include <cstring> #include <cstdlib>
using namespace std;
const int maxLength = 100;
#include "listStack.h"
void computerExpression(char* expr) { listStack val; T x, y, v; char p; int op[] = { 32,14,20,5,70,2 }; int i, len;
len = strlen(expr);
for (i = 1; i <= len; i++) { if (expr[i - 1] >= 'A' && expr[i - 1] <= 'Z') { v = op[expr[i - 1] - 'A']; cout << expr[i - 1] << '=' << v << " "; val.push(v); } else if (expr[i - 1] == '+' || expr[i - 1] == '-' || expr[i - 1] == '*' || expr[i - 1] == '/') { p = expr[i - 1]; val.pop(x); val.pop(y); if (p == '+') { v = x + y; val.push(v); } else if (p == '-') { v = y - x; val.push(v); } else if (p == '*') { v = y * x; val.push(v); } else if (p == '/') { if (x == 0) { cout << "n除数不能为零" << endl; exit(1); } else { v = y / x; val.push(v); } } } else { break; } } val.pop(x); if (val.isStackEmpty()) { cout << "n计算结果:" << x << endl; } else { cout << "n表达式中缺少操作数" << endl; } }
void postfixExpression() { char* strExpre; int len;
fstream inf("postfixFile.txt", ios::in); if (!inf) { cerr << "无法打开文件" << endl; exit(1); } strExpre = new char[maxLength]; inf >> strExpre;
len = strlen(strExpre); strExpre[len] = '#'; strExpre[len + 1] = '
|
近期评论