用c++的STL中stack实现10以内的整数运算
如题:
代码结构:
主程序:calc.cpp
辅助类:Comp 用于比较计算顺序
Isop 用于判断是否是操作符
Comp.h、Comp.cpp 代码如下:
1 #ifndef COMP_H 2 #define COMP_H 3 //定义函数对象,用于确定什么时候开始计算 4 class Comp 5 { 6 public: //a是栈上面的元素、b是栈下面的元素 7 bool operator()(char a,char b); 8 }; 9 10 #endif
1 #include "Comp.h" 2 3 //格式:返回值 operator()(参数列表){} 4 //a代表先出栈的元素,b代表后出栈的元素 5 6 //测试无误 7 bool Comp::operator()(char a,char b) 8 { 9 if((a=='*'&&b=='*')||(a=='+'&&b=='+')) 10 return true; 11 if(((a=='*')&&(b=='+'||b=='-'))||(a=='/'&&(b=='+'||b=='-'))||(b=='(')||(b=='[')) //只有以上情况能计算 12 { 13 return true; 14 } 15 return false; 16 }
Isop.h、Isop.cpp 代码如下:
1 #ifndef ISOP_H 2 #define ISOP_H 3 4 //函数对象,判断是否是操作符 5 class Isop 6 { 7 public: 8 //如果截断之后刚好是符号?? 9 bool operator()(char c); 10 }; 11 12 #endif
1 #include "Isop.h" 2 3 //测试无误 4 bool Isop::operator ()(char c) 5 { 6 if(c=='-'||c=='+'||c=='*'||c=='/'||c=='('||c==')'||c=='['||c==']') 7 return true; 8 return false; 9 }
主程序calc.cpp 代码如下:
1 #include <iostream> 2 #include "Comp.h" 3 #include "Isop.h" 4 #include <stack> 5 #include <string> 6 using namespace std; 7 /** 8 1. 接收用户输入//没有判断是否有非法字符 9 2. 处理 10 3. 输出 11 */ 12 13 14 /** 15 stl中的栈pop()返回空值,top()返回引用(左值,可改)和常引用(做右值,不可改) 16 top的定义: 17 T& top(void); 18 const T& top(void) const; 19 20 为什么pop()没有返回值? 21 为了防止复制失败,栈顶的值也消失 22 T t = s.pop();//pop()如果返回值,取出值(栈中无该值),然后赋值失败,值消失 23 */ 24 25 26 /* 27 要自己写一个类,分别来存放float和char 28 用string的话,float用不了,并且不能双位数 29 */ 30 31 32 int calc(int num1,int num2,char c); //做计算的函数 33 int main() 34 { 35 stack<int> num; //数据 36 stack<char> op; //操作符 37 stack<int> fz; //注意类型要转换回去 38 string exp; //保存表达式 39 Comp com; //作比较,决定什么时候计算 40 Isop isop; //判断是否是操作符 41 char oper1; 42 char oper2; 43 int num1; 44 int num2; 45 int result; 46 int flag = 0; 47 48 //1. 49 cout << "welcome to MyCalculator!\n"; 50 cout << "please input your expression:\n"; 51 cin>>exp; 52 cin.ignore(); 53 //string 自己的函数,为什么要两次回车 54 //getline(cin,exp); 55 56 57 //2.边入栈边处理 58 for(int i=0;i<exp.length();i++) 59 { 60 if(isop((char)exp[i])) //操作符 61 { 62 if(exp[i]==')') //如果是反括号, 63 { 64 65 if(!num.empty()) 66 { 67 num1 = (int)num.top(); 68 num.pop(); 69 } 70 else 71 { 72 cout << "please input something!" << endl; 73 exit(1); 74 } 75 if(!op.empty()) 76 { 77 oper1 = op.top(); 78 op.pop(); 79 } 80 else 81 { 82 cout << "please input something!" << endl; 83 exit(1); 84 } 85 while(oper1!='(') //出栈是(,丢弃,一次计算完成 86 { 87 if(op.empty()) //小括号不成对 88 { 89 cout << "input errorn"; 90 exit(0); 91 } 92 93 if(!op.empty()) 94 { 95 oper2 = op.top(); 96 op.pop(); 97 if(com(oper1,oper2)) 98 { 99 if(!num.empty()){ 100 num2 = (int)num.top(); 101 num.pop(); 102 } 103 else 104 { 105 cout << "no elements1!" << endl; 106 exit(1); 107 } 108 //做计算 109 result = calc(num2,num1,oper1); 110 num.push(result); 111 //1种做法是把辅助栈弹回,2是把辅助栈中的计算出来,实现第二种 112 while(!fz.empty()) 113 { 114 oper1 = (char)fz.top(); 115 fz.pop(); 116 if(!fz.empty()){ 117 num1 = (int)fz.top(); 118 fz.pop(); 119 } 120 else 121 { 122 cout << "no element2!" <<endl; 123 exit(1); 124 } 125 if(!num.empty()) 126 { 127 num2 = (int)num.top(); 128 num.pop(); 129 } 130 else 131 { 132 cout << "no element3!" << endl; 133 exit(1); 134 } 135 result = calc(num1,num2,oper1); 136 num.push(result); 137 } 138 oper1 = oper2; 139 if(!num.empty()) 140 { 141 num1 = (int)num.top(); 142 num.pop(); 143 } 144 else 145 { 146 cout << "no element3!" << endl; 147 } 148 } 149 else 150 { 151 fz.push(num1); 152 fz.push(oper1); 153 oper1 = oper2; 154 if(!num.empty()) 155 { 156 num1 = (int)num.top(); 157 num.pop(); 158 } 159 else 160 { 161 cout << "no element3!" << endl; 162 exit(1); 163 } 164 } 165 } 166 }//直到取出小括号 167 num.push(num1); 168 } 169 else if(exp[i]==']') //如果是反大括号 170 { 171 172 } 173 else //以上都不是,就入栈 174 { 175 op.push((char)exp[i]); 176 } 177 } 178 else //数据入栈 179 { 180 num.push((int)exp[i]-48); 181 } 182 } 183 //入栈完毕,做处理,没有括号的情况 184 185 186 if(!num.empty()) 187 { 188 num1 = (int)num.top(); 189 num.pop(); 190 } 191 else 192 { 193 cout << "no element4!" << endl; 194 exit(1); 195 } 196 if(!op.empty()){ 197 oper1 = op.top(); 198 op.pop(); 199 } 200 else 201 { 202 cout << "no element5!" << endl; 203 exit(1); 204 } 205 while(!op.empty()) 206 { 207 oper2 = op.top(); 208 op.pop(); 209 if(com(oper1,oper2)) 210 { 211 if(!num.empty()) 212 { 213 num2 = (int)num.top(); 214 num.pop(); 215 } 216 else 217 { 218 cout << "no elementt6!" << endl; 219 exit(1); 220 } 221 //做计算 222 result = calc(num1,num2,oper1); 223 num.push(result); 224 //1种做法是把辅助栈弹回,2是把辅助栈中的计算出来,实现第二种 225 //处理逻辑与之前一样,可写成函数 226 //判断是否可以取值,栈是否为空,可以写成函数 227 while(!fz.empty()) 228 { 229 oper1 = (char)fz.top(); 230 fz.pop(); 231 if(!fz.empty()) //第一个数 232 { 233 num1 = (int)fz.top(); 234 fz.pop(); 235 } 236 else 237 { 238 cout << "no element7!" << endl; 239 exit(1); 240 } 241 if(!num.empty()) //第二个数 242 { 243 num2 = (int)num.top(); 244 num.pop(); 245 } 246 else 247 { 248 cout << "no element8!" << endl; 249 exit(1); 250 } 251 result = calc(num1,num2,oper1); 252 num.push(result); 253 } 254 } 255 else 256 { 257 fz.push(num1); 258 fz.push(oper1); 259 } 260 261 oper1 = oper2; //回到最初状态 262 if(!num.empty()) 263 { 264 num1 = (int)num.top(); 265 num.pop(); 266 } 267 else 268 { 269 cout << "no element9!" << endl; 270 } 271 } 272 //计算op栈中最后一个元素 273 if(!num.empty()) 274 { 275 num2 = (int)num.top(); 276 num.pop(); 277 } 278 else 279 { 280 cout << "no element10!" << endl; 281 exit(1); 282 } 283 result = calc(num1,num2,oper1); 284 num.push(result); 285 286 //op栈空,但是fz没空的情况 287 while(!fz.empty()) 288 { 289 oper1 = (char)fz.top(); 290 fz.pop(); 291 if(!fz.empty()) //第一个数 292 { 293 num1 = (int)fz.top(); 294 fz.pop(); 295 } 296 else 297 { 298 cout << "no element7!" << endl; 299 exit(1); 300 } 301 if(!num.empty()) //第二个数 302 { 303 num2 = (int)num.top(); 304 num.pop(); 305 } 306 else 307 { 308 cout << "no element8!" << endl; 309 exit(1); 310 } 311 result = calc(num1,num2,oper1); 312 num.push(result); 313 } 314 315 cout << "the expression equals to "<< result << endl; 316 return 0; 317 } 318 319 320 int calc(int num1,int num2,char c) 321 { 322 int r=0; 323 switch(c) 324 { 325 case '+': 326 r = num2+num1; 327 break; 328 case '-': 329 r = num2-num1; 330 break; 331 case '*': 332 r = num2*num1; 333 break; 334 case '/': 335 if(num1==0) 336 { 337 cout << "error:divede 0!\n"; 338 exit(0); 339 } 340 r = num2/num1; 341 break; 342 default: 343 cout << "there are some bugs!" <<endl; 344 345 } 346 return r; 347 }
自己随便测试了下,都可以过。
有问题,请指出!!