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);
}