1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cctype> 6 #include <ctime> 7 8 using namespace std; 9 10 template <class T> 11 class stack 12 { 13 private: 14 int sum=0; 15 struct node{ 16 T data;struct node *link; 17 }; 18 public: 19 struct node *ttop=NULL; 20 stack(){ttop=NULL;sum=0;} 21 ~stack() 22 { 23 struct node *temp; 24 while (ttop) 25 { 26 temp=ttop;ttop=ttop->link; 27 delete temp; 28 } 29 } 30 inline bool empty() 31 { 32 if (ttop)return false; 33 else return true; 34 } 35 bool push(const T item) 36 { 37 struct node *temp=new node; 38 if (temp){sum++;temp->data=item;temp->link=ttop; 39 ttop=temp;return true;} 40 else return false; 41 } 42 bool pop(void) 43 { 44 if (empty())return false; 45 else { 46 sum--; 47 struct node *temp; 48 temp=ttop; 49 ttop=ttop->link; 50 delete temp;return true; 51 } 52 } 53 T top(void) 54 { 55 if (sum>0) 56 return ttop->data; 57 else return 0x7fffffff; 58 } 59 int size(void){return sum;} 60 }; 61 62 class expression{ 63 private:int Numlen(char *s) 64 { 65 int i=0;int f=0; 66 while (s[i]=='.'||isdigit(s[i])){ 67 if (s[i]=='.')f++; 68 i++;} 69 if (f>1)return -1; 70 return i; 71 } 72 double Read(char *s) 73 { 74 double num; 75 sscanf(s,"%lf",&num); 76 return num; 77 } 78 public:char s[1000]; 79 double Calc(void) 80 { 81 double a,b,sum; 82 int l; 83 stack <double> Num; 84 stack <char> Op; 85 while (!Num.empty())Num.pop(); 86 while (!Op.empty())Op.pop(); 87 if (s[0]=='+'||s[0]=='-')Num.push(0); 88 int flag=0; 89 while (s[flag]!='