1

次の操作を実行できる逆ポーランドプリンターを作成しようとしています-

入力:

  1. (a+(b*c))
  2. ((a+b)*(z+x))
  3. ((a+t)*((b+(a+c))^(c+d)))

出力:

  1. abc*+
  2. ab+zx+*
  3. 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';
}

しかし、正しく入力さえされておらず、このコードの何が間違っているのかわかりません.何が問題なのかを理解するのを手伝ってください.

4

1 に答える 1