
-----------------------------------
alpesh
Sat Oct 02, 2004 10:24 am

postfix / infix notation
-----------------------------------
// Turbo c++

#include
#include
#include
#include
#include

char stack[50],y;
float stack1[50],z;
int top=-1,top1=-1,prec1=0,prec2=0;

int check(char value)      /* sets precedence for the operator */
 {
  int prec=0;
  switch(value)
	{
	 case '+' : prec=1;break;
	 case '-' : prec=1;break;
	 case '*' : prec=2;break;
	 case '/' : prec=2;break;
	 case '^' : prec=3;break;
	 default  : prec=0;
	}
  return(prec);
 }
void push(char x)
 {
  top++;
  stack[top]=x;
 }
char pop()
 {
   y=stack[top];
   top--;
   return(y);
 }
void push1(float x)
 {
  top1++;
  stack1[top1]=x;
 }
float pop1()
 {
   z=stack1[top1];
   top1--;
   return(z);
 }
void main(void)
 {
	char tempa[1],infix[50],postfix[50];
	int i=0,j=0,n,b=0,postfix1[50];
	float af,bf,answer;
	clrscr();
/* reading in the expression */
	printf("ENTER THE EXPRESSION TO BE EVALUATED : ");
	scanf("%s",infix);
	i=strlen(infix);
	push('(');
	infix[i]=')';
	n=i;
/* conversion to postfix */
	for (i=0;i='0') && (infix[i]=prec1)
		  {
			prec2=check(stack[top]);
			if (prec2>=prec1)
			 {
			  postfix[j]=pop();
			  j++;
			 }
		  }
		 push(infix[i]);
		}
	}
  n=n-b+1;
  printf("\n%s","THE EQUIVALENT POSTFIX EXPRESSION IS : ");
  for (i=0;i= STACK_DEPTH - 1;
}

/* We alo need to know if it's empty
    If it is, we can't pop values from it.
 */
bool char_stack_empty(int top)
{
   return top < 0;
}

/* Push a character onto a stac, but only it the stack can accept
    more.  If it can, increment top.  Otherwise return false.
 */
bool char_stack_push(char stack[], int * top, char newItem)
{
   if (!char_stack_full(*top))
   {
      stack[*top++] = newItem;
      return true;
   }
   else
   {
      return false;
   }
}

/* Pop a value, if that's possible.  If it is, decrement
    top and return true.  Otherwise return false.
 */
bool char_stack_pop(char stack[], int * top, char * output)
{
   if (!char_stack_empty(*top))
   {
      *output = stack[*top--];
      return true;
   }
   else
   {
      return false;
   }
}

/* FUNCTIONS FOR FLOAT STACK HANDLING */

/* Initialize the variables for a char stack */
void float_stack_init(float stack[], int * top)
{
   *top = 0;
   for (int i = 0; i < STACK_DEPTH; i++)
   {
      stack[i] = 0;
   }
}

/* We need to know if the stack can accept more values
    If it is, we can't push values onto it.
 */
bool float_stack_full(int top)
{
   return top >= STACK_DEPTH - 1;
}

/* We alo need to know if it's empty
    If it is, we can't pop values from it.
 */
bool float_stack_empty(int top)
{
   return top < 0;
}

/* Push a character onto a stack, but only it the stack can accept
    more.  If it can, increment top.  Otherwise return false.
 */
bool float_stack_push(float stack[], int * top, float newItem)
{
   if (!float_stack_full(*top))
   {
      stack[*top++] = newItem;
      return true;
   }
   else
   {
      return false;
   }
}

/* Pop a value, if that's possible.  If it is, decrement
    top and return true.  Otherwise return false.
 */
bool float_stack_pop(char stack[], int * top, float * output)
{
   if (!float_stack_empty(*top))
   {
      *output = stack[*top--];
      return true;
   }
   else
   {
      return false;
   }
}


int main()
{
   char stack[STACK_DEPTH];
   int top;
   char value;

   char_stack_init(stack, &top);
   char_stack_push(stack, &top, 'A');
   char_stack_pop(stack, &top, &value);

   printf("%c", value);

   return 0;
}


Now, let's turn our attention to your function to check the precendence level of an operator.  First off, it's wonderful that you've broken this out into a function!

However, you're doing a bit too much work in the switch.  


If two cases have the same result, we can rewrite the switch thusly:

int check(char operator)
{
	int prec = 0;
		
	switch (operator)
	{
		case '+': case '-':
			prec = 1;
			break;
		case '*': case '/':
			prec = 2;
			break;
		case '^': 
			prec = 3;
			break;
		default : 
			prec = 0;
	}
		
	return(prec);
}
Further, assigning to the variable, then returning its value is redundant.  We can return from inside the switch.  This also means we can remove all of those breaks, as well as the variable declaration, the assignment, and the return at the end of the function.

int check(char operator)
{
	switch (operator)
	{
		case '+': case '-':
			return 1;
		case '*': case '/':
			return 2;
		case '^': 
			return 3;
		default : 
			return 0;
	}
}


One last thing, "check" isn't a very descriptive name.  Try changing it to something like "evaluate_operator_precendence".
