求大佬帮忙看一下我的代码哪里错了,谢谢!

P1957 口算练习题

LanPeilin @ 2022-01-26 11:09:58

求大佬帮忙看一下我的代码哪里错了,谢谢!(C++)

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>

    using namespace std;

    int main()
    {
        int n,a,b,c;
        char last,s[20],ans[20];
        scanf("%d\n",&n);
        while(n--)
        {
            fgets(s,sizeof(s),stdin);
            if(s[0]=='a' || s[0]=='b' || s[0]=='c') last==s[0],s[0]==' ';
            sscanf(s,"%d %d",&a,&b);
            switch(last)
            {
                case'a':c=a+b;sprintf(ans,"%d+%d=%d",a,b,c);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));
        }
    }

by dxy2020 @ 2022-01-26 11:20:26

case后加大括号?


by HarunluoON @ 2022-01-26 11:22:21

@小小蒟蒻一只 不用


by HarunluoON @ 2022-01-26 11:23:23

last == s[0],s[0] == ' ';


by Tx_Lcy @ 2022-01-26 11:28:07

事实上,AC代码应该是这样的

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath> 
using namespace std;
int main(){
    int n,a,b,c;
    char last,s[20],ans[20];
    scanf("%d\n",&n);
    while(n--)
    {
        fgets(s,sizeof(s),stdin);
        if (s[0]=='a' || s[0]=='b' || s[0]=='c') last=s[0],s[0]=' ';
        sscanf(s,"%d %d",&a,&b);
        switch(last)
        {
            case'a':c=a+b;sprintf(ans,"%d+%d=%d",a,b,c);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));
    }
    return 0;
}

@LanPeilin


by dxy2020 @ 2022-01-26 11:31:27

@HarunluoON 是在下才疏学浅了


by LanPeilin @ 2022-01-26 11:31:41

谢谢大佬!!!!!!!!!!!!!!!!


by lianggeer @ 2022-02-10 22:15:10

哈哈哈哈哈


|