简体   繁体   中英

Char array in multiple test cases

#include<stdio.h>
#include<stdlib.h>     
#include<ctype.h>     
#include<string.h>

#define SIZE 100
char stack[SIZE];
int top = -1;

void push(char item)
{
    if(top >= SIZE-1)
    {
        printf("\nStack Overflow.");
    }
    else
    {
        top = top+1;
        stack[top] = item;
    }
}

char pop()
{
    char item;

    if(top <0)
    {
        printf("stack under flow: invalid infix expression");
    }
    else
    {
        item = stack[top];
        top = top-1;
        return(item);
    }
}

int is_operator(char symbol)
{
    if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int precedence(char symbol)
{
    if(symbol == '^')
    {
        return(5);
    }
    else if(symbol == '/')
    {
        return(4);
    }
    else if(symbol == '*')
    {
        return(3);
    }
    else if(symbol == '+' )         
    {
        return(2);
    }
    else if(symbol == '-' )         
    {
        return(1);
    }
    else
    {
        return(0);
    }
}

void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
    int i, j;
    char item;
    char x;

    push('(');                             
    strcat(infix_exp,")");                  

    i=0;
    j=0;
    item=infix_exp[i];         

    while(item != '\0')        
    {
        if(item == '(')
        {
            push(item);
        }
        else if( isdigit(item) || isalpha(item))
        {
            postfix_exp[j] = item;              /* add operand symbol to postfix expr */
            j++;
        }
        else if(is_operator(item) == 1)        /* means symbol is operator */
        {
            x=pop();
            while(is_operator(x) == 1 && precedence(x)>= precedence(item))
            {
                postfix_exp[j] = x;                  /* so pop all higher precendence operator and */
                j++;
                x = pop();                       /* add them to postfix expresion */
            }
            push(x);
            push(item);                 
        }
        else if(item == ')')        
        {
            x = pop();                  
            while(x != '(')              
            {
                postfix_exp[j] = x;
                j++;
                x = pop();
            }
        }
        else
        { 
            printf("\nInvalid infix Expression.\n");       
        }
        i++;
        item = infix_exp[i]; 
    } 
    if(top>0)
    {
        printf("\nInvalid infix Expression.\n");        

    }
    if(top>0)
    {
        printf("\nInvalid infix Expression.\n");        
    }
}

int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
    char infix[SIZE], postfix[SIZE];         /* declare infix string and postfix string */
    scanf("\n%[^\n]s",infix);
    InfixToPostfix(infix,postfix);                   /* call to convert */
    //printf("Postfix Expression: ");
    printf("%s\n",postfix);
    /* print postfix expression */
  }
    return 0;
}

This is the code to convert infix to postfix expression I am using multiple test cases the problem is the second time(second test case the postfix_exp is printing more than it should) Input 2 a+b*(c^de)^(f+g h)-ia (b+c)/d Output abcd^e-fgh*+^*+i- abc+d/ fgh +^*+i-

Can you tell me what is wrong because of which " fgh +^*+i-" the extra expression from the previous expression is being concatenated at the end of the output?

The postfix_exp seems to be overwritten but I was expecting a new assigned postfix expression. Why is it that earlier data(data from previous test case) is saved?

Please ask any further query in the comments section

You did not closed postfix_exp with the terminating '\\0' . After the first iteration postfix will look like something like this

XXXXXXXXXXX0

After the shorter 2nd input it will look something like

YYYYYYYXXXX0

Where X represent the characters written in the first iteration and Y the ones which in the second. So when you print it it will contain the last iteration results too.

Add this postfix_exp[j] = '\\0'; before the if (top>0) line.

Note that this is still undefined behavior since postfix array is unitialized and there is no guarantee that it will be zeroed out.

you should clos postfix_exp with the terminating '\\0' by adding

Add this postfix_exp[j] = '\0'; 

before the if (top>0) line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM