hey guys... Thanks for your help in my previous post. Havin another problem in my stack assignment this time.. Have done how much i could but there is a lil error in it.. Possible in the convertip function... Hope i can get the help needed.. Thanks in advance...=)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTACK 20

typedef struct {
	char data[MAXSTACK];
	int top;
}STACK;
void Initialize(STACK* myStack)
{
	myStack->top = -1;
}

int stackEmpty(STACK *myStack)
{
	if(myStack->top == -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int stackFull(STACK *myStack)
{
	if(myStack->top == MAXSTACK -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
void push (STACK* myStack, char item)
{
	if (stackFull(myStack) !=1)
	{
		myStack->data[++myStack->top] = item;
	}
	else
	{
		printf("The Stack is full\n");
	}

}
char pop (STACK* myStack)
{
	if(stackEmpty(myStack) != 1)
	{
		return myStack->data[myStack->top--];
	}
	else
	{
		printf("The Stack is empty\n");
		return NULL;
	}
}

int prcd(char symbol)
{
	if (symbol == '+' || symbol == '-')
		return 2;
	else if (symbol == '*' || symbol == '/')
		return 4;
	else if (symbol == '^' || symbol == '$')
		return 6;
	else if (symbol == '(' || symbol == ')' || symbol == '#')
		return 1;
}

int isoperator(char symbol)
{
	switch(symbol)
	{
		case '+':
		case '-':
		case '*':
		case '/':
		case '^':
		case '$':
		case '(':
		case ')':
			return 1;
		break;

		default:
			return 0;
	}
}

void convertip(char infix[],char postfix[], STACK* myStack)
{
	int i, symbol, j=0;

	myStack->data[++top]='#';

	for(i=0;i < strlen(infix); i++)
	{
		symbol=infix[i];
		
		if(isoperator(symbol)==0)
		{
			postfix[j]=symbol;
			j++;
		}
		else
		{
			if(symbol=='(')
				push(&myStack, symbol);

			else if(symbol==')')
			{
				while(myStack->data[top]!='(')
				{
					postfix[j]=pop(&myStack);
					j++;
				}
				
				pop(&myStack);//pop out (.
			}
			
			else
			{
				if(prcd(symbol) > prcd(stack[top]))
					push(&myStack,symbol);
				
				else
				{
					while(prcd(symbol) <= prcd(stack[top]))
					{
						postfix[j]=pop(&myStack);
						j++;
					}
					
					push(&myStack,symbol);
				}
			}
		}
	}

	while(myStack->data[top]!='#')
	{
		postfix[j]=pop(&myStack);
		j++;
	}
	postfix[j]='\0';//null terminate string.
}

int main (void)
{
	STACK myStack;
	Initialize(&myStack);

	char infix[30], postfix[30];
	clrscr();

	printf("Enter the valid infix string:\n");
	scanf("%s", infix);
	
	convertip(infix, postfix, &myStack);

	printf("The corresponding postfix string is: %s\n", postfix);
}

You have to be careful of lines like this: while(myStack->data[top]!='(')

top is not a variable, but a member of the STACK structure. So you need to write it like this: myStack->top. You have that same error several times.

>>push(&myStack, symbol);
variable myStack is already a pointer, so remove the & address operator.

Thanks for the tips ancient dragon..=)
solved my big problem in an instance..=)
But there is still another problem...
Compilation ahs no error in it..
But, when i run the code, it has runtime error saying that stack arond the variable myStack is coruppted...
Tried finding the placing..

Could you guys help me out?..=)
Thanks you guys..=)

post current code

Ancient dragion... managed to solve the problem...=)
Now, a new thing arise where i am suppose to evaluate the postfic expression... But,,, error accours as usual... Here is my development....

int operation(int p1,int p2,char op)
{
	switch(op)
		{
			case '+':
				return p1+p2;
			case '*':
				return p1*p2;
			case '-':
				return p1-p2;
			case '/':
				return p1/p2;
		}
}

int evaluate(char pos[50])
{
	STACK s1;
	int p1,p2,result,i;
	s1.top=-1;
	for(i=0;pos[i]!='\0';i++)
	{
		if(isdigit(pos[i]))
		{
			push(&s1,pos[i]-'0'
		}
		else
		{
			p2=pop(&s1);
			p1=pop(&s1);
			result=operation(p1,p2,pos[i]);
			push(&s1,result);
		}/*end of for loop*/
	return pop(&s1);
}

really wishing you a big thanks for helping me out bro..=)

Here is the updated code... Compiled no error... But, i can't run... It says it has debug error... and the stack around variable 'eval' has problems... something like that...

I THink the problem is at the evaluate function...
Please try to help me out..=)
Thanks you guys...=)

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
 
#define MAXSTACK 40 
 
typedef struct 
{
	char data[MAXSTACK];
	int top;

}STACK;

void Initialize(STACK* myStack)
{
	myStack->top = -1;
}
 
int stackEmpty(STACK *myStack)
{
	if(myStack->top == -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int stackFull(STACK *myStack)
{
	if(myStack->top == MAXSTACK -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
 
void push (STACK* myStack, int item)
{
	if (stackFull(myStack) != 1)
	{
		++myStack->top; 
        myStack->data[myStack->top]=item; 
	}
	else
	{
		printf("The Stack is full\n");
	}

} 

char pop (STACK* myStack)
{
	if(stackEmpty(myStack) != -1)
	{
		return myStack->data[myStack->top--];
	}
	else
	{
		printf("The Stack is empty\n");
		return NULL;
	}
}
 
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 infix_to_postfix(char* infix, char * postfix, int insertspace) 
{ 
        char *p_infix,*p_postfix; 
        STACK X; 
        char n1; 
		Initialize(&X); 
        p_infix = &infix[0]; 
        p_postfix = &postfix[0]; 
 
        while(*p_infix) 
        { 
                while(*p_infix == ' ' || *p_infix == '\t') 
                { 
                        p_infix++; 
                } 
 
                if( isdigit(*p_infix) || isalpha(*p_infix) ) 
                { 
                        while( isdigit(*p_infix) || isalpha(*p_infix)) 
                        { 
                                *p_postfix = *p_infix; 
                                p_postfix++; 
                                p_infix++; 
                        } 
                        /*SPACE CODE*/ 
                        if(insertspace) 
                        { 
                                *p_postfix = ' '; 
                                p_postfix++; 
                        } 
                        /*END SPACE CODE*/ 
                } 
 
                if( *p_infix == '(' ) 
                { 
                        push(&X,*p_infix); 
                        p_infix++; 
                } 
 
                if( *p_infix == ')') 
                { 
                        n1 = pop(&X); 
                        while( n1 != '(' ) 
                        { 
                           *p_postfix = n1; 
                           p_postfix++; 
                           /*SPACE CODE*/ 
                           if(insertspace) 
                           {
							   *p_postfix = ' '; 
                               p_postfix++; 
                           } 
                          /*END SPACE CODE*/ 
                          n1 = pop(&X); 
                        } 
                        p_infix++; 
                } 
 
                if( isoperator(*p_infix) ) 
                { 
                        if(stackEmpty(&X)) 
                                push(&X,*p_infix); 
                        else 
                        { 
                                n1 = pop(&X); 
                                while(priority(n1) >= priority(*p_infix)) 
                                { 
                                        *p_postfix = n1; 
                                        p_postfix++; 
                                        /*SPACE CODE*/ 
                                        if(insertspace) 
                                        { 
                                                *p_postfix = ' '; 
                                                p_postfix++; 
                                        } 
                                        /*END SPACE CODE*/ 
                                        n1 = pop(&X); 
                                } 
                                push(&X,n1); 
                                push(&X,*p_infix); 
                        } 
                        p_infix++; 
                } 
        } 
        while(!stackEmpty(&X)) 
        { 
                n1 = pop(&X); 
                *p_postfix = n1; 
                p_postfix++; 
                /*SPACE CODE*/ 
                if(insertspace) 
                { 
                        *p_postfix = ' '; 
                        p_postfix++; 
                } 
                /*END SPACE CODE*/ 
        } 
        *p_postfix = '\0'; 
}

int operation(int p1,int p2,char op)
{
	switch(op)
		{
			case '+':
				return p1+p2;
			case '*':
				return p1*p2;
			case '-':
				return p1-p2;
			case '/':
				return p1/p2;
		}
}

int evaluate(char pos[])
{
	STACK eval;
	int p1,p2,result,i;
	eval.top = -1;
	for(i=0;pos[i]!='\0';i++)
	{
		if(isdigit(pos[i]))
		{
			push(&eval,pos[i]-'0');/*use to find the integer value of it*/
		}
		else
		{
			p2=pop(&eval);
			p1=pop(&eval);
			result=operation(p1,p2,pos[i]);
			push(&eval,result);
		}
	}/*end of for loop*/
	return pop(&eval);
} 

int main(void) 
{ 
        char infix[50],postfix[50];
		int m;
			
        strcpy(&postfix[0],""); 
        printf("Enter Infix Expression : "); 
		fflush(stdin); 
		gets(infix);
		printf("\n\n");

        infix_to_postfix(&infix[0],&postfix[0],1); 

		printf("Postfix Expression is : %s\n",&postfix[0]); 

		m = evaluate(postfix);

		printf("%d\n",m);
		
		return 0; 
}

The problem is in the evaluate() function. The content of the parameter contains spaces, which should not be evaluated in that function.

if(isdigit(pos[i]))
		{
			push(&eval,pos[i]-'0');/*use to find the integer value of it*/
		}
		else if( !isspace(pos[i]) )
		{
			p2=pop(&eval);
			p1=pop(&eval);
			result=operation(p1,p2,pos[i]);
			push(&eval,result);
		}

That gets rid of the program crash, but it still doesn't work when I enter a little more complex expression.

Enter Infix Expression : 1+2+10+234


Postfix Expression is : 1 2 + 10 + 234 +
7
Press any key to continue . . .

A little more testing indicates that in the evaluate() function, when you find a digit it needs to convert everything up to the first non-digit to an integer and then push that result onto the stack. For example if I enter "10+20+30" the function should push the values 10, 20 and 30 onto the stack. What it is now doing is pushing the individual digits, 1, 0, 2, 0, 3, 0. You could fix that something like this:

for(i=0;pos[i]!='\0';i++)
    {
        if(isdigit(pos[i]))
        {
            int x = 0;
            while( pos[i] != '\0' && isdigit(pos[i]) )
            {
                x = (x * 10) + pos[i] - '0';
                i++;
            }
            push(&eval,x);/*use to find the integer value of it*/
        }

But that too only works for values less than 126 because you are using char as the data type of the stack. Use long long and you could get much larger values and would require some additional recoding.

Thanks for the ancient dragon..=)
Now, its showin up a values at least..=)
will keep testing it..=)
The postfix expression doesn't sum up...
Anyway..=)
If you have the solution, then please post it..=)
ur help is very much appreciated bro..=)
PS: i only need the postfix to read numbers from 1 to 9...=)
But, yeah.. a little extra would be an advantage to0..=)

Ancient dragon... Problem solved...=)
Just one minor part...
I want my answer for the evaluate to give the float type..=)
Any idea where to change it?
Tried changing result in the evaluate function.. But, it doesn't sum up to the answer..=)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.