So basically I need to find a way to evaluate an infix that's been converted into a postfix. The code below takes an entered infix and converts it into a postfix but I don't know how to take that postfix and evaluate it. Every single tutorial I've seen evaluates a postfix entered directly instead of a infix, so I have no clue what to do. So i decided to ask for help in doing this.
Though to be honest, I'm more interested in reading material then direct code so I can understand it. But if you explain the code, that'd work too.
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string.h>
#include <ctype.h>
#pragma warning(disable: 4996)
#define MAX 50
#define EMPTY -1
//Construct the Stack that is to be manipulated by the code
struct stack
{
int data[MAX];// Allow for "max" chracter input
int top;// To be used when "moving" a part of the stack
};//End of stack contruction
// Determine if a stack is empty when manipulating the stack
void emptystack(struct stack* s)
{
s->top=EMPTY;
}//End of Empty Determin-er
int isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}
void push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
printf("\nSTACK FULL");
}
else
{
++s->top;
s->data[s->top]=item;
}
}
int pop(struct stack* s)
{
int ret=EMPTY;
if(s->top == EMPTY)
printf("\nSTACK EMPTY");
else
{
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--;
}
}
//Determines what operators can be used in accordence to the code
char isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
return 1;
else
return 0;
}//END of operator determin-er
//Determines the priority of the operators in relation to each other
char priority(char e)
{
int pri = 0;
if(e == '*' || e == '/' || e =='%')
pri = 2;
else
{
if(e == '+' || e == '-')
pri = 1;
}
return pri;
}//End of priority Determin-er
//Changes Infix notation into prefix notation
void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p; //Varibles for the infix and postfix of the stack
struct stack X; // Variable for the precontructed stack
char n1;
emptystack(&X);
i = &infix[0]; //Determines the use of the "i variable"
p = &postfix[0]; //Determines the use of the "i variable"
//While loop for when a person uses a space when entering the stack
while(*i)
{
while(*i == ' ' || *i == '\t')
{
i++;
}
if( isdigit(*i) || isalpha(*i) )
{
while( isdigit(*i) || isalpha(*i))
{
*p = *i;
p++;
i++;
}
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
if( *i == '(' )
{
push(&X,*i);
i++;
}
if( *i == ')')
{
n1 = pop(&X);
while( n1 != '(' )
{
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
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++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
n1 = pop(&X);
}
push(&X,n1);
push(&X,*i);
}
i++;
}
}
while(!isempty(&X))
{
n1 = pop(&X);
*p = n1;
p++;
/*SPACE CODE*/
if(insertspace)
{
*p = ' ';
p++;
}
/*END SPACE CODE*/
}
*p = '\0';
}
int main()
{
char in[50] = { 0 },post[50] = { 0 };
strcpy(&post[0],"");
printf("Enter Infix Expression: ");
fgets(in, sizeof(in), stdin);
in[strlen(in) - 1] = '\0';
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is: %s\n",&post[0]);//Prints Infix expression
std::getchar();
return 0;
}