1 #include <iostream> 2 #include<sstream> 3 using namespace std; 4 template<typename T> 5 class stack 6 { 7 T p[40]; 8 int toop; 9 public: 10 stack() { toop = -1; } 11 void push(T t) { toop++; p[toop] = t; } 12 T top() { return p[toop]; } 13 bool empty() { if (toop == -1)return true; return false; } 14 void pop() { toop--; } 15 }; 16 class caculator 17 { 18 string s;//原波兰式的容器 19 stack<char>op; 20 stack<float>num; 21 stringstream ss;//用于转换的流 22 stringstream sb;//插入逆波兰式的流 23 string str;//存放数字的容器,每次更新 24 string strs;//存放逆波兰式的容器 25 float x, y; 26 public: 27 caculator(char *p) { s = p; } 28 float trans(const char *p); 29 float antipoland(); 30 void show() { cout << strs; } 31 void readnum(); 32 void caucEveTime(); 33 void shownum() { while (!num.empty()) { cout << num.top() << endl; num.pop(); } } 34 void showop() { while (!op.empty()) { cout << op.top() << endl; op.pop(); } } 35 bool checkpoint(const char *p); 36 }; 37 bool caculator::checkpoint(const char *p) 38 { 39 int i = strlen(p); 40 while (i--) 41 { 42 if (*p == '.') 43 return true; 44 *p++; 45 } 46 return false; 47 } 48 float caculator::trans(const char *p)//底层const,对象为常量 49 { 50 float n = 0; float m = 0; 51 int i = strlen(p); int j;//记录小数点后有几位 52 if (checkpoint(p)) 53 { 54 while (--i && (*p != '.')) 55 { 56 n = n * 10 + (*p - '