C语言,求大佬

P1449 后缀表达式

wanan2203ouchengli @ 2023-07-29 14:49:56

C语言,求大佬

本地都过了,不知道为什么洛谷上过不了

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct N {
    char* top, * base;
    int size;
}N;

void CSH( N & l) {
    l.size = 10000;
    l.top = new char[10000];
    l.base = l.top;
}

int RZ( N & l,int e) {
    if (l.top - l.base == l.size) {
        printf("满了");
        return 0;
    }
    *l.top++= e;
}

int CZ(N& l,int a,int b) {
    if (l.top == l.base) {
        printf("空的");
        return 0;
    }
    else {
        a = *--l.top;
        b = *--l.top;
    }
}

int main() {
    N l;
    CSH(l);
    char a[10000];
    int b[100],i=-1,h=0,e=0,s=0,f=0;
    while (a[i]!= '@') {
        i++;
        scanf("%c",&a[i]);
    }
    for (int j = 0; j <= i; j++) {
        if (a[j] >= '0' && a[j] <= '9') {
            b[h]= 'a[j]' - 48;
            h++;
        }
        else {
            int r = h;
            if (a[j] == '.') {
                for (int u = 0; u <= r; u++) {
                    e += b[i] * pow(10, h-u);
                }
                RZ(l, e);
            }
            else {
                CZ(l, s, f);
                switch (a[j]) {
                case '+':RZ(l, f + s); break;
                case '-':RZ(l, f - s); break;
                case '*':RZ(l, f * s); break;
                case '/':RZ(l, f / s); break;
                }
            }
        }
    }
    printf("%d", *l.top);
}

by Wanxue @ 2023-07-29 14:59:20

你要不把两个测试用的printf(满了和空的)删了试试


by winner_0207_AFO @ 2023-07-29 15:21:16

#include<bits/stdc++.h>
using namespace std;
string s;
long long a[505], top = -1,res;
void calc(char op) {
    long long x = a[top --];
    long long y = a[top --];
    if(op == '+'){
        a[++ top] = y + x;return;
    }else if(op == '-'){
        a[++ top] = y - x;return;
    }else if(op == '*'){
        a[++ top] = y * x;return;
    }else if(op == '/'){
        a[++ top] = y / x;return;
    } 
}
int main() {
    getline(cin, s);
    for(int i = 0; s[i] != '@'; i ++) {
        long long x = 0;
        while(s[i] >= '0' && s[i] <= '9') x = x * 10 + s[i ++] - '0';
        if(x) a[++ top] = x;
        else if(s[i] != ' ') calc(s[i]);
    }
    cout << a[top];
    return 0;
}

或吧满了,空的删了逝逝


by wanan2203ouchengli @ 2023-07-29 15:42:46

@Wanxue 把测试用的删了也还是一样的错误


by wanan2203ouchengli @ 2023-07-29 15:45:20

@NBxzz2012 感谢分享 不过我还是想练习顺序表,我这错误原因似乎是我的语法问题


|