求助:vscode正常,但提交全RE

P1957 口算练习题

Dr不能 @ 2022-04-17 11:55:14

不知道为什么,交了就是报re(signal 11,内存)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int num1 = 0,num2 = 0;

void getnum(char* str,int typ)
{
    char *p =NULL;
    p = NULL;
    if(typ == 0)
    {
        p = &str[2];
    }
    else
    {
        p = &str[0];
    }

    p = strtok(p," ");
    num1 = atoi(p);
    p = strtok(NULL," ");
    num2 = atoi(p);

}

int main()
{
    int n,res;
    static char chr[120],typ,str[120];
    int i =  0;
    scanf("%d",&n);
    getchar();
    for(i = 1;i<=n;i++)
    {
        gets(chr);
        if(chr[0] == 'a')
        {
            typ = 'a';
            getnum(chr,0);
        }
        else if(chr[0] == 'b')
        {
            typ = 'b';
            getnum(chr,0);
        }
        else if(chr[0] == 'c')
        {
            typ = 'c';
            getnum(chr,0);
        }
        else
        {
            getnum(chr,1);
        }
//---------------------------------------------
        if(typ == 'a')
        {
            res = num1 + num2;
            sprintf(str,"%d+%d=%d",num1,num2,res);
            puts(str);
            printf("%d\n",strlen(str));
        }
        if(typ == 'b')
        {
            res = num1 - num2;
            sprintf(str,"%d-%d=%d",num1,num2,res);
            puts(str);
            printf("%d\n",strlen(str));
        }
        if(typ == 'c')
        {
            res = num1 * num2;
            sprintf(str,"%d*%d=%d",num1,num2,res);
            puts(str);
            printf("%d\n",strlen(str));
        }
    }

    return 0;
}

by Engulf @ 2022-04-17 12:15:05

尽量别用 gets()

但不知道具体错误


|