C 反向抛光转换器

C 反向抛光转换器,c,reverse,postfix-notation,rpn,C,Reverse,Postfix Notation,Rpn,我正在尝试制作一台可执行以下操作的反向抛光打印机- 投入: (a+(b*c)) ((a+b)*(z+x)) ((a+t)*((b+(a+c))^(c+d))) 产出: abc*+ ab+zx+* at+bac++cd+^* 这是我的代码: #include <stdio.h> #include <string.h> char pop(int t); void push(int c, int t); int main() { int z; scanf("%d

我正在尝试制作一台可执行以下操作的反向抛光打印机-

投入:

  • (a+(b*c))
  • ((a+b)*(z+x))
  • ((a+t)*((b+(a+c))^(c+d)))
  • 产出:

  • abc*+
  • ab+zx+*
  • at+bac++cd+^*
  • 这是我的代码:

    #include <stdio.h>
    #include <string.h>
    char pop(int t);
    void push(int c, int t);
    int main()
    {
        int z;
        scanf("%d", &z);
        char *a[100];
        int i = 0;
        int q = z;
        while (q-- > 0)
        {
            char v[400];
            scanf("%s", &v);
            int t;
            for (t = 0; t < strlen(v); t++)    //loop to put the values and signs in the 2 stacks
            {
                if ((v[t] == '*') || (v[t] == '+') || (v[t] == '-') || (v[t] == '^'))
                {
                    push(v[t], 2);
    
    
                }
    
                else if (v[t] == ')')
                {
                    int y = pop(2);
                    push(y, 1);
    
                }
    
                else
                {
                    push(v[t], 1);
    
                }
            }
            int k = 0;
            char c;
            while ((c = pop(1)) !='\0')    //loop to put elements in the array v
            {
    
                if (c != '(')
                {
    
                    v[k++] = c;
    
                }
            }
            v[k--] = '\0';
            int m;
            for (m=0; m != k; m++, k--)     //for reversing the string
            {
                char t = v[m];
                v[m] = v[k];
                v[k] = t;
            }
    
            a[i++] =v;
            printf("%s",a[i - 1]);
        }
        int p;
        for (p = 0; p <z ; p++)   //printing the elements
            printf("%s\n",*a[p]);
        return 0;
    }
    char ac[400];
    char as[400];
    int ic = 0;
    int is = 0;
    void push(int c,int t)
    {
        if (t == 1 && ic != 400)
            ac[ic++] = c;
        else if (t == 2 && is != 400)
            as[is++] = c;
    }
    char pop(int t)
    {
        if (t == 1 && ic != 0)
            return ac[--ic];
        if (t == 2 && is != 0)
            return as[--is];
        return '\0';
    }
    
    #包括
    #包括
    字符pop(int-t);
    无效推力(int c,int t);
    int main()
    {
    intz;
    scanf(“%d”、&z);
    char*a[100];
    int i=0;
    int q=z;
    而(q-->0)
    {
    charv[400];
    scanf(“%s”、&v);
    int t;
    for(t=0;t
    这是由于

            printf("%s\n",*a[p]);
    
    正如BLUEPIXY所注意到的,
    *a[p]
    是一个
    char
    ;但是
    %s
    需要一个
    char*
    ,因此您需要

            printf("%s\n", a[p]);
    
    关于
    v
    超出范围,关键因素不是范围(可见性),而是
    v
    的存储持续时间(生存期)-其生存期在与其关联的块的执行结束时结束,指向它的指针
    a[i]
    的值变得不确定;通过改变

            a[i++] =v;
    


    你可以纠正这个问题。

    你说的“输入不正确”是什么意思?输入是什么?输出是什么?期望的输出是什么?顺便说一下
    a[i++]=v;
    总是最后一个
    v
    printf(“%s\n”、*a[p]);
    *a[p]
    a字符
    v
    超出范围。
    错。@BLUEPIXY我不理解你所说的printf(“%s\n”,*a[p]);:*a[p]是字符,而v超出了范围。
            a[i++] = strdup(v);