今日做题,遇一难关,恳请指教

P1957 口算练习题

ShouZheng_Observer @ 2022-07-06 12:17:26

#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
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;
}

by Hisaishi_Kanade @ 2022-07-06 12:20:02

首先考虑fgets有没有把\r\n之类的读进来


by m256i @ 2022-07-06 12:22:32

您的问题是?


by ShouZheng_Observer @ 2022-07-06 12:24:54

@该名称已占用 求调,输出错误,第一个一直输出0+0=0


by m256i @ 2022-07-06 12:31:33

s[0]==' ';

by m256i @ 2022-07-06 12:33:06

@PhantomKnight


by m256i @ 2022-07-06 12:35:48

但凡开个 -Wall.jpg


by ShouZheng_Observer @ 2022-07-06 12:39:58

@该名称已占用 哦哦哦哦哦,我真是个**


|