弟弟的作业

描述

你的弟弟刚做完了“100以内数的加减法”这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中ab是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。

输入
输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
输出
输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
样例输入
1+2=3
3-1=5
6+7=?
99-0=99
样例输出
2

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 
 5 int main(){
 6     int a;
 7     int b;
 8     char sign;
 9     char result[10];
10     int length;
11     int sum;
12     int i;
13     int amount;
14     
15     amount=0;
16     while(scanf("%d%c%d=%s",&a,&sign,&b,&result)!=EOF){
17         if(strcmp(result,"?")==0)
18             continue;
19             
20         length=strlen(result);
21         
22         sum=0;
23         for(i=0;i<length;i++){
24             sum+=(result[i]-'0')*pow(10,length-1-i);
25         }
26         
27         if(sign=='+'){
28             if(a+b==sum)
29                 amount++;
30         }
31         
32         else if(sign=='-'){
33             if(a-b==sum)
34                 amount++;
35         }    
36     }
37     printf("%d\n",amount);
38     return 0;
39 }

 

 
posted @ 2014-11-16 03:20  zqxLonely  阅读(505)  评论(0编辑  收藏  举报