1 Replies - 366 Views - Last Post: 08 September 2011 - 10:08 PM Rate Topic: -----

#1 idkerr231  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 08-September 11

InfixPostfix evaluation.error in the evaluation.Stacks in c to be used

Posted 08 September 2011 - 09:02 PM

I got the infix to postfix already. but i don't know what went wrong during the evaluation. please help me with this.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
 
#define MAX 10
#define EMPTY -1
 
struct stack
{
    int data[MAX];
    int top;
};

int isempty(struct stack *s);
void emptystack(struct stack* s);
void push(struct stack* s,int item);
char pop(struct stack* s);
void display(struct stack s);
int isoperator(char e);
int priority(char e);
void infix2postfix(char* infix, char * postfix, int insertspace);
int evaluate(float a, float b, char op);

void main()
{
    char in[50],post[50];

	struct stack X;
	int op1, op2, result;
	emptystack(&X);

    strcpy(&post[0],"");
    printf("Infix:   \t");
    fflush(stdin);
	gets(in);

    infix2postfix(&in[0],&post[0],1); 
    printf("Postfix: \t%s",&post[0]);

	while (post[0]!='\0')
	{
		while (post[0]==' '||post[0]=='\t')
		{
			post[0]++;
		}
		if(isdigit(post[0]))
		{
			push(&X, post[0]-48);
		}
		else
		{
			op1=pop(&X);
			op2=pop(&X);

			switch(post[0])
			{
			case '+': result=op2+op1;
				break;
			case '-': result=op2-op1;
				break;
			case '*': result=op2*op1;
				break;
			case '/': result=op2/op1;
				break;
			case '%': result=op2%op1;
				break;

			default: printf("\nInvalid Operator.");
				return;
			}
			push(&X, result);
		}
		post[0]++;
	}
	result=pop(&X);
}

int isempty(struct stack* s)
{
    return (s->top==EMPTY) ? 1 : 0;
}
 
void emptystack(struct stack* s)
{
    s->top=EMPTY;
}
 
void push(struct stack* s,int item)
{
    if(s->top==(MAX-1))
    {
        printf("\nSTACK FULL");
    }
    else
    {
        ++s->top;
        s->data[s->top]=item;
    }
}
 
char pop(struct stack* s)
{
    int ret=(char)EMPTY;
    if(!isempty(s))
    {
        ret=s->data[s->top];
        --s->top;
    }
    return ret;
}
 
void display(struct stack s)
{
    while(s.top!=EMPTY)
    {
        printf("\n%d",s.data[s.top]);
        s.top--;
    }
}
 
int isoperator(char e)
{
    if(e=='+'||e=='-'||e=='*'||e=='/'||e=='%')
        return 1;
    else
        return 0;
}
 
 
int priority(char e)
{
    int pri = 0;
 
    if(e=='*'||e=='/'||e=='%')
        pri = 2;
    else
    {
        if(e=='+'||e=='-')
            pri=1;
    }
    return pri;
}
 
void infix2postfix(char* infix, char* postfix, int insertspace)
{
    char *i,*p;
    struct stack X;
    char n1;
    emptystack(&X);
    i=&infix[0];
    p=&postfix[0];
 
    while(*i)
    {
        while(*i==' '||*i=='\t')
        {
            i++;
        }
 
        if(isdigit(*i)||isalpha(*i))
        {
            while(isdigit(*i)||isalpha(*i))
            {
                *p = *i;
                p++;
                i++;
            }
            if(insertspace)
            {
                *p=' ';
                p++;
            }
		}
 
        if(*i=='(' )
        {
            push(&X,*i);
            i++;
        }
 
        if(*i==')')
        {
            n1=pop(&X);
            while(n1!='(' )
            {
                *p=n1;
                p++;
                if(insertspace)
                {
                    *p=' ';
                    p++;
                }
                n1=pop(&X);
            }
            i++;
        }
 
        if(isoperator(*i))
        {
            if(isempty(&X))
                push(&X,*i);
            else
            {
                n1=pop(&X);
                while(priority(n1) >= priority(*i))
                {
                    *p=n1;
                    p++;
                    if(insertspace)
                    {
                        *p=' ';
                        p++;
                    }
                    n1=pop(&X);
                }
                push(&X,n1);
                push(&X,*i);
            }
            i++;
        }
    }
    while(!isempty(&X))
    {
        n1=pop(&X);
        *p=n1;
        p++;
        if(insertspace)
        {
            *p=' ';
            p++;
        }
	}
    *p='\0';
}



Is This A Good Question/Topic? 1
  • +

Replies To: InfixPostfix evaluation.error in the evaluation.Stacks in c to be used

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1345
  • View blog
  • Posts: 4,631
  • Joined: 19-February 09

Re: InfixPostfix evaluation.error in the evaluation.Stacks in c to be used

Posted 08 September 2011 - 10:08 PM

Hi, post[0]++ just increments the value of the first element of the array.


while (post[0]!='\0') checks the first element of the array every time.


You want a pointer most likely as i in your infix2postfix() function.

/* create pointer */
char * post_ptr;
/* initalise pointer */
post_ptr = &post[0];
/* or post_ptr = post; */

while (*post_ptr != '\0')  /* check current character */
{
  ...

  post_ptr++; /* move pointer to point to next character */
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1