九度oj题目1019:简单计算器
题目1019:简单计算器
1 #include <stdio.h> 2 #include <cstring> 3 #include <queue> 4 #include <iostream> 5 using namespace std; 6 double stack[110]; 7 int main(){ 8 //freopen("D:\\INPUT.txt","r",stdin); 9 int tail; 10 double num; 11 while(cin>>num&&num!=0){ 12 //cout<<num<<endl; 13 tail=0; 14 memset(stack,0,sizeof(stack)); 15 stack[tail++]=num; 16 //cout<<stack[tail-1]<<endl; 17 char c1,cal; 18 //cout<<"1"<<endl; 19 while(scanf("%c",&c1)){ 20 //cout<<c1<<endl; 21 if(c1=='\n'){ 22 //cout<<1<<endl; 23 break; 24 } 25 //cout<<2<<endl; 26 scanf("%c %lf",&cal,&num); 27 //cout<<num<<endl; 28 //cout<<cal<<endl; 29 switch(cal){ 30 case '+':{ 31 stack[tail++]=num; 32 //cout<<stack[tail-1]<<endl; 33 break; 34 } 35 case '-':{ 36 stack[tail++]=-num; 37 //cout<<stack[tail-1]<<endl; 38 break; 39 } 40 case '*':{ 41 tail--; 42 stack[tail]=stack[tail]*num; 43 tail++; 44 //cout<<stack[tail]<<endl; 45 break; 46 } 47 case '/':{ 48 tail--; 49 stack[tail]=stack[tail]/num; 50 tail++; 51 //cout<<stack[tail]<<endl; 52 break; 53 } 54 } 55 } 56 int i; 57 double sum=0; 58 for(i=0;i<tail;i++){ 59 sum+=stack[i]; 60 //cout<<stack[i]<<endl; 61 } 62 printf("%.2lf\n",sum); 63 } 64 return 0; 65 }