0

I have created a program that loads an expression from an input file and converts the expression from infix to postfix form.

So in the file I have expressions. Each expression is placed on a separate line in the text file. I read from the file line by line and form a array of strings that make up the expressions from the text file. I take each string from the array and convert it to postfix form.

The problem is that when converting from infix to postfix, I print out the characters. The array of strings from the input is not changed.

Here' s code:

for(i = 0; i < strlen(c); i++)
    {
        if(c[i] == '(')
            stek.arr[++stek.tos] = '(';

        else if(c[i] == ')')
        {
            while(stek.arr[stek.tos] != '(')
            {
                printf("%c", stek.arr[stek.tos--]);
              
            }

            stek.tos--; 
        }
        // if Operator
        else if(isOperator(c[i]))
        {
            while(stek.tos != -1 && !isHigherPrecedence(c[i], stek.arr[stek.tos]))
            {
                printf("%c", stek.arr[stek.tos--]);
               
            }

            stek.arr[++stek.tos] = c[i];
        }
        // if operand
        else
        {
            printf("%c", c[i]);
        }

    }
    while(stek.tos != -1)
    {
        printf("%c", stek.arr[stek.tos--]);
       
    }

This is just the conversion code from infix to postfix form.

c-represents a string of expressions loaded from a file, it is at the beginning a string of the first line, a string of the second line, etc.

My question is : When a string is converted from infix to postfix form, it is printed character by character. How to create a new array of strings that will contain only the postfix form?

I want these characters that are printed individually in this code to be put into an array, and it becomes an array of strings.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • If you have created the array that holds the input, you can create an array that holds the output. – n. m. could be an AI Nov 15 '22 at 10:21
  • To create array of strings, you will need to start with a int8_t ** (a 2d array). Given, you know how many strings you are dealing with, you can malloc accordingly. Then have a temporary int8_t array in which you store the postfix form. Once you know the length of this string then you can allocate memory for the first available index of outer array. Then copy the content from the temporary array to this newly allocated memory. – NeilB Nov 15 '22 at 10:30

0 Answers0