大佬帮忙看看,那里错了

P1957 口算练习题

Bary @ 2022-05-11 18:26:17

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n,a,b,c;
    char last,s[20],ans[20];
    cin>>n;
    getchar();
    while(n--)
    {
        fgets(s,sizeof(s),stdin);//从stdin文件里输入s-1个字符到s字符串里 
        if(s[0]=='a'||s[0]=='b'||s[0]=='c')
        {
            last=s[0];
            s[0]=' ';
        }
        sscanf(s,"%d %d", &a, &b);//从s中读取信息 
        switch(last)
        {
            case 'a':
                c=a+b;
                sprintf(ans,"%d+%d=%d",a,b,c);//输出到ans字符串里 
                break;
            case 'b':
                c=a-b;
                sprintf(ans,"%d-%d=%d",a,b,c);
                break;
            case 'c':
                c=a*b;
                sprintf(ans,"%d*%d=%d",a,b,c);
                break;
        }
        printf("%s\n%d\n",ans,strlen(ans));
    }
}

|