求助:vscode正常,但提交全RE
查看原帖
求助:vscode正常,但提交全RE
537302
Dr不能楼主2022/4/17 11:55

不知道为什么,交了就是报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;
}

2022/4/17 11:55
加载中...