做题太慌张了。总是出问题。感觉时间不够用,着急反而效率更低,没有之前不忙不急的时候做的事情更多。
这道题整半天也没做上来,
静下心来写了半小时就差不多了
自己新写的
1 #include<stdio.h> 2 int count(char d,int b,int sum){ 3 switch(d){ 4 case '+': 5 sum=sum+b; 6 break; 7 case '-': 8 sum=sum-b; 9 break; 10 case '*': 11 sum=sum*b; 12 break; 13 case '/': 14 sum=sum/b; 15 break; 16 default:{ 17 return sum; 18 break; 19 } 20 } 21 return sum; 22 } 23 int main(){ 24 char c; 25 char d='?'; 26 int a; 27 int b=0; 28 int sum; 29 scanf("%d",&sum); 30 //printf("sum==%d\n",sum); 31 c=getchar(); 32 33 while(c!='='){ //printf("c==%c\n",c); 34 if(c=='+'){ 35 sum=count(d,b,sum); 36 b=0; 37 d=c; 38 c=getchar(); 39 } 40 else if(c=='-'){ 41 sum=count(d,b,sum); 42 b=0; 43 d=c; 44 c=getchar(); 45 } 46 else if(c=='*'){ 47 sum=count(d,b,sum); 48 b=0; 49 d=c; 50 c=getchar(); 51 52 } 53 else if(c=='/'){ 54 sum=count(d,b,sum); 55 b=0; 56 d=c; 57 c=getchar(); 58 } 59 else { 60 61 //printf("c==%c\n",c); 62 a=c-'0'; 63 b=b*10+a;//收集数字 64 //printf("a==%d\n",a); 65 //printf("b==%d\n",b); 66 //printf("sum==%d\n",sum); 67 c=getchar(); 68 } 69 70 } 71 72 //printf("END sum==%d\n",sum); 73 sum=count(d,b,sum); 74 //printf("END c==%c\n",c); 75 //printf("END b==%c\n",b); 76 //printf("END sum==%d\n",sum); 77 sum=count(c,b,sum); 78 printf("%d",sum); 79 return 0; 80 81 82 }