第十三行不理解,每行可能有三个数据或两个数据,为什么只读入两个数据?

P1957 口算练习题

NGC5055 @ 2022-08-17 22:20:04

#include<iostream>
#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);//这是第13行
        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;
}

AC的代码,但我不理解

谢谢大佬


by C201529 @ 2022-08-17 22:26:51

@Nanine


by NGC5055 @ 2022-08-17 22:38:21

@C201529

好的,谢谢你,明白了。


by fish_love_cat @ 2022-08-18 14:44:08

为啥是sscanf


by NGC5055 @ 2022-08-19 16:53:23

@fish_love_cat

sscanf(s,"%d %d",&a,&b)

是指 从字符串s中“拿出”(大概就这个意思两个整数,并赋值给变量a,b,不同于scanf

然后按照题目的意思,把这两个数+ 或 - 或 *


by fish_love_cat @ 2022-09-12 19:34:14

@Nanine 栓Q~


|