#4#6两个测试点报错,求条

P1449 后缀表达式

KatsuraJiu @ 2024-11-29 02:15:15

第四和第六两个测试点报错,求大佬帮忙看一下是哪里的问题

#include <stdio.h>
#include <string.h>

int num[100]={0};
int h=0;

int cot (char c);

int main()
{
    char i[200];
    int n=0;
    int t=0;
    int s=0;
    int k=0;

    scanf("%s",i);

    n=strlen(i);

    for(t=0;t<n;t++){
        if(i[t]>='0'&&i[t]<='9'){
            if(i[t-1]=='+'||i[t-1]=='-'||i[t-1]=='*'||i[t-1]=='*'){
                h++;
            }
            num[h]=(num[h])*10+(i[t]-48);
        }else if(i[t]=='.'){
            h++;
        }else if(i[t]=='+'||i[t]=='-'||i[t]=='*'||i[t]=='*'){
            k=t-1;
            if(i[k]=='.'){
                h--;
                s=cot(i[t]);
            }else{
                s=cot(i[t]);
            }
        }else if(i[t]=='@'){
            break;
        }
    }

    printf("%d",s);

    return 0;
}

int cot (char c)
{
    int T=h;
    int a=0;
    int sum=0;

    a=num[T];
    sum=num[T-1];

    if(c=='+'){
        sum=sum+a;
    }else if(c=='-'){
        sum=sum-a;
    }else if(c=='*'){
        sum=sum*a;
    }else if(c=='/'){
        sum=sum/a;
    }

    num[h]=0;

    h=T-1;
    num[h]=sum;

    return sum;
}

by ymx2009 @ 2024-11-29 07:46:13

#include <stdio.h>
#include <string.h>

int num[100]={0};
int h=0;

int cot (char c);

int main()
{
    char i[200];
    int n=0;
    int t=0;
    int s=0;
    int k=0;

    scanf("%s",i);

    n=strlen(i);

    for(t=0;t<n;t++){
        if(i[t]>='0'&&i[t]<='9'){
            if(i[t-1]=='+'||i[t-1]=='-'||i[t-1]=='*'||i[t-1]=='/'){//除号写成乘号了
                h++;
            }
            num[h]=(num[h])*10+(i[t]-48);
        }else if(i[t]=='.'){
            h++;
        }else if(i[t]=='+'||i[t]=='-'||i[t]=='*'||i[t]=='/'){//除号写成乘号了+1
            k=t-1;
            if(i[k]=='.'){
                h--;
                s=cot(i[t]);
            }else{
                s=cot(i[t]);
            }
        }else if(i[t]=='@'){
            break;
        }
    }

    printf("%d",s);

    return 0;
}

int cot (char c)
{
    int T=h;
    int a=0;
    int sum=0;

    a=num[T];
    sum=num[T-1];

    if(c=='+'){
        sum=sum+a;
    }else if(c=='-'){
        sum=sum-a;
    }else if(c=='*'){
        sum=sum*a;
    }else if(c=='/'){
        sum=sum/a;
    }

    num[h]=0;

    h=T-1;
    num[h]=sum;

    return sum;
}

by ymx2009 @ 2024-11-29 07:49:17

@KatsuraJiu 哥们写代码细心一点


by ymx2009 @ 2024-11-29 11:22:57

@KatsuraJiu bro关注一下,thx


by KatsuraJiu @ 2024-11-30 02:16:17

@ymx2009 天啊,感谢大佬,已关注<3


|