1

I am trying to make a reverse Polish printer which can perform the following operation-

Inputs:

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

Outputs:

  1. abc*+
  2. ab+zx+*
  3. at+bac++cd+^*

This is my code:

#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';
}

But it is not even inputting properly and I am not able to figure out what are the mistakes in this code.Please help to figure out what are the problems.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
Alpa8
  • 470
  • 4
  • 12
  • What do you mean by "not inputting properly"? What's the input? What's the output? What's the desired output? – Tim Čas Feb 11 '15 at 16:23
  • BTW `a[i++] =v;` always last `v`. – BLUEPIXY Feb 11 '15 at 16:37
  • `printf("%s\n",*a[p]);` : `*a[p]` is `a char`. and `v` is out of scope. – BLUEPIXY Feb 11 '15 at 16:38
  • `for (m=0; m != k; m++, k--)` wrong. – BLUEPIXY Feb 11 '15 at 16:47
  • That 2nd error pointed out by @BLUEPIXY would've been caught by most modern compilers, if warnings were enabled. I **strongly** recommend that you enable warnings (`-Wall` in GCC and CLang; check your compiler's manual if you have some other compiler). – Tim Čas Feb 11 '15 at 16:48
  • @Tim Čas From not inputting properly I mean after inputing the no of test cases i.e.int z and first line if input it crashes – Alpa8 Feb 11 '15 at 17:13
  • 1
    @BLUEPIXY I couldn't understand what you mean by-printf("%s\n",*a[p]); : *a[p] is a char. and v is out of scope. – Alpa8 Feb 11 '15 at 17:16
  • `char *a[100];`: `a` is array of pointer to `char`. `a[p]` is pointer to `char`. `*a[p]` pointer dereference is `char`. – BLUEPIXY Feb 11 '15 at 17:29
  • `while (q-- > 0) { char v[400];` : `v` is an effective area in the while block. If you see outside of the block is not valid. – BLUEPIXY Feb 11 '15 at 17:35

1 Answers1

0

after inputing the no of test cases i.e.int z and first line if input it crashes

This is due to the

        printf("%s\n",*a[p]);

as BLUEPIXY noticed, *a[p] is a char; but %s expects a char *, thus you need

        printf("%s\n", a[p]);

and regarding v is out of scope, the crucial factor is not the scope (visibility), but the storage duration (lifetime) of v - its lifetime ends when execution of the block with which it is associated ends, and the value of a pointer a[i] to it becomes indeterminate; by changing

        a[i++] =v;

to

        a[i++] = strdup(v);

you can remedy that.

Armali
  • 18,255
  • 14
  • 57
  • 171