标签:namespace stack temp ace tor sem ret clu har
1 #include<iostream> 2 #include<vector> 3 #include<stdlib.h> 4 using namespace std; 5 class node{ 6 public: 7 virtual ~node(){ 8 } 9 }; 10 class odnode : public node{ 11 public: 12 double val; 13 odnode(){ 14 15 } 16 odnode(string s){ 17 val = atof(s.c_str()); 18 } 19 }; 20 class opnode :public node{ 21 public: 22 char op; 23 int level; 24 opnode(){ 25 26 } 27 opnode(char op, int level){ 28 this->op = op; 29 this->level = level; 30 } 31 }; 32 template <class T> 33 class mystack{ 34 public: 35 int topindex; 36 T array[100]; 37 mystack(){ 38 topindex = 0; 39 } 40 bool isempty(){ 41 return topindex == 0; 42 } 43 void push(T n){ 44 array[topindex++] = n; 45 } 46 T pop(){ 47 if (isempty()) 48 return NULL; 49 return array[--topindex]; 50 } 51 T top(){ 52 if (isempty()) 53 return NULL; 54 return array[topindex - 1]; 55 } 56 }; 57 double myabs(double a){ 58 if (a>0) 59 return a; 60 else 61 return -a; 62 } 63 double mod(double a, double b){ 64 double c = myabs(a); 65 double d = myabs(b); 66 while (c >= d){ 67 c = c - d; 68 } 69 if (a*b>0) 70 return c; 71 else 72 return -c; 73 } 74 double evaluateExpr(string expr){ 75 int index = 0; 76 vector<node*> nodelist; 77 mystack<opnode*> ms; 78 while (index<expr.length()){ 79 char c = expr.at(index); 80 if (c >= ‘0‘&&c <= ‘9‘){ 81 int tempindex = index + 1; 82 while ((tempindex<expr.length()) && (expr.at(tempindex) >= ‘0‘&& 83 expr.at(tempindex) <= ‘9‘ || 84 expr.at(tempindex) == ‘.‘ 85 )){ 86 tempindex++; 87 } 88 string temps; 89 if (tempindex >= expr.length()){ 90 temps = expr.substr(index); 91 } 92 else{ 93 temps = expr.substr(index, tempindex - index); 94 } 95 index = tempindex; 96 node* tempn = new odnode(temps); 97 nodelist.push_back(tempn); 98 } 99 if (c == ‘+‘ || c == ‘-‘){ 100 opnode* tempn = new opnode(c, 1); 101 opnode* templn = ms.top(); 102 while (templn != NULL&&templn->level >= tempn->level){ 103 opnode* templln = ms.pop(); 104 nodelist.push_back(templln); 105 templn = ms.top(); 106 } 107 ms.push(tempn); 108 index++; 109 } 110 if (c == ‘*‘ || c == ‘/‘ || c == ‘%‘){ 111 opnode* tempn = new opnode(c, 2); 112 opnode* templn = ms.top(); 113 while (templn != NULL&&templn->level >= tempn->level){ 114 opnode* templln = ms.pop(); 115 nodelist.push_back(templln); 116 templn = ms.top(); 117 } 118 ms.push(tempn); 119 index++; 120 } 121 if (c == ‘(‘){ 122 opnode* tempn = new opnode(c, -1); 123 ms.push(tempn); 124 index++; 125 } 126 if (c == ‘)‘){ 127 opnode* tempn = ms.top(); 128 while (tempn->op != ‘(‘){ 129 nodelist.push_back(ms.pop()); 130 tempn = ms.top(); 131 } 132 ms.pop(); 133 index++; 134 } 135 } 136 while (ms.top() != NULL){ 137 node* tempn = ms.pop(); 138 nodelist.push_back(tempn); 139 } 140 for (int i = 0; i<nodelist.size(); i++){ 141 node* tempn = nodelist[i]; 142 odnode* odp = dynamic_cast<odnode*>(tempn); 143 if (odp){ 144 double d = ((odnode*)tempn)->val; 145 cout << d << ‘ ‘; 146 } 147 else{ 148 cout << ((opnode*)tempn)->op << ‘ ‘; 149 } 150 } 151 cout << endl; 152 mystack<double*> mns; 153 int cindex = 0; 154 while (cindex<nodelist.size()){ 155 node* tempn = nodelist[cindex]; 156 opnode* opp = dynamic_cast<opnode*>(tempn); 157 if (opp){ 158 double od2 = *mns.pop(); 159 double od1 = *mns.pop(); 160 double re = 0; 161 char op = opp->op; 162 switch (op){ 163 case ‘+‘: 164 re = od1 + od2; break; 165 case ‘-‘: 166 re = od1 - od2; break; 167 case ‘*‘: 168 re = od1*od2; break; 169 case ‘/‘: 170 re = od1 / od2; break; 171 case ‘%‘: 172 re = mod(od1, od2); break; 173 } 174 double *d = new double(re); 175 mns.push(d); 176 } 177 else{ 178 double *d = new double(((odnode*)tempn)->val); 179 mns.push(d); 180 } 181 cindex++; 182 } 183 return *mns.pop(); 184 } 185 int main(){ 186 string expr = "1.2*2"; 187 cout << expr << endl; 188 double result = evaluateExpr(expr); 189 cout << "result:" << result << endl; 190 return 0; 191 }
标签:namespace stack temp ace tor sem ret clu har
原文地址:https://www.cnblogs.com/fate-/p/12583857.html