求助,本人的解答为什么全部WA

P1957 口算练习题

leyouyuan @ 2024-08-22 21:50:59

#include<cstdio>
#include<iostream>
#include<cstring>
#define _ADD 1
#define _SUB 2
#define _MUL 3

using namespace std;
void mygetline(char *s)
{
    char c='0';
    while((c=getchar())!='\n'){
        *s=c;
        s++;
    }
    *s='\0';
}

int switchmode(char c)
{
    if(c=='a') return _ADD;
    if(c=='b') return _SUB;
    if(c=='c') return _MUL;
    return 0;
}

char opr(int m)
{
    if(m==_ADD)return '+';
    if(m==_SUB)return '-';
    if(m==_MUL)return '*';
    return 0;
}

int result(int m,int a,int b)
{
    if(m==_ADD)return a+b;
    if(m==_SUB)return a-b;
    if(m==_MUL)return a*b;
    return 0;
}

int main()
{
    int mode;
    char s [40];
    char res [40];
    char *t;
    int a,b;
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++)
    {
        mygetline(s);
        t=s;
        if(*t>='a'&&*t<'z')
        {
            mode=switchmode(*t);
            t+=2;
        }
        sscanf(t,"%d %d",&a,&b);
        sprintf(res,"%d%c%d=%d\n",a,opr(mode),b,result(mode,a,b));
        cout<<res<<endl;
        cout<<strlen(res)<<endl;
    }
}

如图所示,下载到的第一组数据中,输入为

1
a 99 999

输出是

99+999=1098
11

我看都是对的啊!


by Ke9_qux @ 2024-08-22 22:11:32

@leyouyuan 修改后已AC

#include<cstdio>
#include<iostream>
#include<cstring>
#define _ADD 1
#define _SUB 2
#define _MUL 3

using namespace std;
void mygetline(char *s)
{
    char c='0';
    while((c=getchar())!='\n'&&c!='\r'){
        *s=c;
        s++;
    }
    *s='\0';
}

int switchmode(char c)
{
    if(c=='a') return _ADD;
    if(c=='b') return _SUB;
    if(c=='c') return _MUL;
    return 0;
}

char opr(int m)
{
    if(m==_ADD)return '+';
    if(m==_SUB)return '-';
    if(m==_MUL)return '*';
    return 0;
}

int result(int m,int a,int b)
{
    if(m==_ADD)return a+b;
    if(m==_SUB)return a-b;
    if(m==_MUL)return a*b;
    return 0;
}

int main()
{
    int mode;
    char s [40];
    char res [40];
    char *t;
    int a,b;
    int n;
    scanf("%d\n",&n);
    for(int i = 1;i<=n;i++)
    {
        mygetline(s);
        cerr<<s<<endl;
        t=s;
        if(*t>='a'&&*t<'z')
        {
            mode=switchmode(*t);
            t+=2;
        }
        sscanf(t,"%d %d",&a,&b);
        sprintf(res,"%d%c%d=%d",a,opr(mode),b,result(mode,a,b));
        cout<<res<<endl;
        cout<<strlen(res)<<endl;
    }
}

上一页 |