为什么测试对了,提交全WA?求个大佬看看!!!

P1957 口算练习题

ssx666 @ 2022-11-30 21:49:27

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
    int n,a,b;
    char str[100];
    char s[100];
    char m;
    scanf("%d",&n);
    getchar();//吸收数字后的回车
    while (n--)
    {
        memset(s,0,sizeof(s));//重置为0;
        gets(str);//读入
        if (isalpha(str[0]))//通过第一个字符判断是那种情况
        {
            m=str[0];//存入符号
            switch (str[0])
            {
                case ('a'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d+%d=%d",a,b,a+b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('b'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d-%d=%d",a,b,a-b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('c'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d*%d=%d",a,b,a*b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;  
            }
        }
        else 
        {
            switch (m)//上一次存入的符号
            {
                case ('a'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d+%d=%d",a,b,a+b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('b'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d-%d=%d",a,b,a-b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('c'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d*%d=%d",a,b,a*b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;

            }

        }
    }
    return 0;
}

by ssx666 @ 2022-11-30 21:50:09

有哪个大佬帮忙看看嘛?孩子快疯了


by muyangli @ 2022-11-30 21:53:58

你试试改成fgets试一下


by Jorylee @ 2022-12-02 14:16:13

AC代码

#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
    int n,a,b;
    char str[100];
    char s[100];
    char m;
    scanf("%d",&n);
    // getchar();//吸收数字后的回车
    while (n--)
    {
        memset(s,0,sizeof(s));//重置为0;
        scanf(" %[^\n]",str);
        if (isalpha(str[0]))//通过第一个字符判断是那种情况
        {
            m=str[0];//存入符号
            switch (str[0])
            {
                case ('a'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d+%d=%d",a,b,a+b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('b'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d-%d=%d",a,b,a-b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('c'):
                        sscanf(str,"%*s%d%d",&a,&b);
                        sprintf(s,"%d*%d=%d",a,b,a*b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;  
            }
        }
        else 
        {
            switch (m)//上一次存入的符号
            {
                case ('a'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d+%d=%d",a,b,a+b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('b'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d-%d=%d",a,b,a-b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;
                case ('c'):
                        sscanf(str,"%d%d",&a,&b);
                        sprintf(s,"%d*%d=%d",a,b,a*b);
                        puts(s);
                        printf("%d",strlen(s));
                        break;

            }

        }
        putchar('\n');
    }
    return 0;
}

首先是getchar()的问题, 比较特殊的是在 windows 中换行是\r\n是两个字符, 但题主只吸收了一个.

如何修改? 为了避免更多的麻烦, 我使用scanf(" %[^\n]")来读入一行, 其中`(空格) 表示忽略不可见字符,%[^\n]表示一直读入只到遇到\n`, 从而完美解决这个问题.

另外一个问题是在printf("%d",strlen(s))中没有输出换行, 为了方便修改我在最后添加了putchar('\n')


by suchachaos @ 2022-12-23 10:53:39

@Jorylee 跪谢


by syzd @ 2023-01-25 18:47:19

@Jorylee 跪谢大佬,学到了


|