大二写的作业,大三还有用,在这里记录一下。
#include#include #include #include using namespace std; class Stack { public: Stack(); ~Stack() { MakeEmpty(); } double numin(const char ch, double s); void charin(const char ch); void pushnum(double); void pushchar(int, const char); double popnum(); char popchar(); double calculate(); void MakeEmpty(); double GetAns() { return numtop->data; } char GetCharTopch() { return chartop->ch; } private: class NumNode //用来存数 { public: double data; NumNode *next; NumNode(double d, NumNode *p) :data(d), next(p) {} NumNode() {} }; class CharNode //用来存符号 { public: int data; char ch; CharNode *next; CharNode(int d, char c, CharNode* p) :data(d), ch(c), next(p) {} CharNode() {} }; NumNode* numtop; CharNode* chartop; }; Stack::Stack() { chartop = new CharNode; numtop = new NumNode; if (!chartop) { cerr << "内存分配错误!" << endl; exit(1); } chartop->ch = '@'; chartop->data = -1; chartop->next = 0; numtop = 0; } double Stack::numin(const char ch, double s) { s = (ch - 48) + s * 10; //原来的结果乘10加上新的数字 return s; } void Stack::charin(const char ch) { if (!(ch >= '0'&&ch <= '9')) { int outvalue = -1; //对除了)的符号赋运算优先值 if (ch == '+' || ch == '-') outvalue = 2; else if (ch == '*' || ch == '/' || ch == '%') outvalue = 4; else if (ch == '(') outvalue = 6; //运算时,先前符号优先于当前符号的情况 CharNode* t = chartop; while (outvalue <= t->data&&t->ch != '@') { //如果遇到的是')',之前有'('就把两个符号 if (t->ch == '('&&ch == ')') { popchar(); break; } t = t->next; pushnum(this->calculate()); } if (ch != '('&&ch != ')') this->pushchar(outvalue, ch); else if (ch == '(') this->pushchar(0, ch); } } void Stack::pushnum(double x) { numtop = new NumNode(x, numtop); } void Stack::pushchar(int x, const char ch) { chartop = new CharNode(x + 1, ch, chartop); } double Stack::popnum() { if (numtop == NULL) return 0; NumNode *temp = numtop; numtop = numtop->next; double x = temp->data; delete temp; return x; } char Stack::popchar() { if (chartop == NULL) return ' '; CharNode *temp = chartop; chartop = chartop->next; char ch = temp->ch; delete temp; return ch; } double Stack::calculate() { double res = 0.0; CharNode* t = chartop; t = t->next; char ch = popchar(); while (ch == '(') { t = t->next; ch = popchar(); } if (ch == '@') return popnum(); double a = popnum(); double b = popnum(); switch (ch) { case '+': res = a + b; break; case '-': res = b - a; break; case '*': res = a * b; break; case '/': res = b / a; break; case '%': res = b - int(b / a)*a; break; default: break; } return res; } void Stack::MakeEmpty() { NumNode* t1 = numtop; CharNode* t2 = chartop; while (numtop) { t1 = numtop; numtop = numtop->next; delete t1; } while (chartop) { t2 = chartop; chartop = chartop->next; delete t2; } } int main() { int i = 0, mark = 0, mode = 1; Stack OpStack; double ans = 0.0, s = 0.0, s1 = 0.0; char ch[20]; cout << "请输入一个表达式:"; cin >> ch; while (ch[i] != ' ') { if (ch[i] == '.') { mode = 2; mark = 0; //初始化mark } else if (isdigit(ch[i])) { s1 = OpStack.numin(ch[i], s1); //原来的结果乘10加上新的数字 if(mode==2) ++mark; //记录有多少位小数 if (!(isdigit(ch[i + 1])) && ch[i + 1] != '.') { s = s1/ pow(10.0, mark);//得到最终入栈的数 //计算完成,复位 OpStack.pushnum(s); mode = 1; s = 0.0; s1 = 0; mark = 0; } } else OpStack.charin(ch[i]); ++i; } while (OpStack.GetCharTopch() != '@') { OpStack.pushnum(OpStack.calculate()); } cout << OpStack.GetAns() << endl; return 0; }



