p1957为啥测试数据能过,但是爆零

P1957 口算练习题

chenrushi @ 2023-09-19 10:43:14


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    int num,num1,num2,i;//输入个数,第一个数,第二个数
    char a; 
    char tmp[20],str[50];

    scanf("%d",&num);//输入计算式个数 
    scanf("%c",&a);//防止回车影响 
    for(i=0;i<num;i++) {
        fgets(tmp,10,stdin);//得到字符串 
        /*输入一行计算式*/ 
        if('z'>=tmp[0]&&tmp[0]>='a')
            sscanf(tmp,"%c %d %d",&a,&num1,&num2);
        else
            sscanf(tmp,"%d %d",&num1,&num2);
        /*输出结果*/ 
        memset(tmp,'\0',sizeof(tmp));//清空字符串 
        memset(str,'\0',sizeof(str));//清空字符串 
        if(a=='a'){
            sprintf(str,"%d+%d=%d",num1,num2,num1+num2);//将计算式放入str 
            printf("%s\n",str);
            printf("%d\n",strlen(str));
        }
        else if(a=='b'){
            sprintf(str,"%d-%d=%d",num1,num2,num1-num2);
            printf("%s\n",str);
            printf("%d\n",strlen(str));
        }
        else if(a=='c'){
            sprintf(str,"%d*%d=%d",num1,num2,num1*num2);
            printf("%s\n",str);
            printf("%d\n",strlen(str));
        }
    }
    return 0;
}

|