11 Replies - 1243 Views - Last Post: 20 November 2011 - 08:11 PM Rate Topic: -----

#1 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Problem with Postfix

Posted 18 November 2011 - 08:09 PM

Long story short

I get these errors:
- "Unhandled exception at 0x00ee1576 in Stack.exe: 0xC0000005: Access violation reading location 0x33532ebc."
- >Stack.exe!pop(stack * s) Line 47 + 0xc bytes C++


When debugging this code after getting the postfix to display this code

FYI my professor gave permission to use code we find as long as we give credit, implement a particular piece of code, and write a document explaining how it works according to each line of code. Yes I know weird and that it's probably more work then doing it by myself but she's new at teaching this subject and I'd have to make the document regardless.

//Source Code from user born2c0de of http://www.dreamincode.net 
#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

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

//Evaluates the stack
int evaluate(char *postfix)
{
    char *p;
    struct stack X;
    int op1,op2,result;
	result = pop(&X);

    emptystack(&X);
    p = &postfix[0];
 
    while(*p != '\0')
    {
       /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
      /* if is digit */
        if(isdigit(*p))
        {
            push(&X,*p - 48);
        }
        else
        {
           //it is an operator
            op1 = pop(&X);
            op2 = pop(&X);

            push(&X,result);
        }
        p++;
    }

    return result;
}//End of the evaluation code




int isempty(struct stack *s)
{
    return (s->top == EMPTY) ? 1 : 0;
}
 

//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],post[50];
	char exp[MAX];
 
    strcpy(&post[0],"");
    printf("Enter Infix Expression: ");
   

	gets(in);
	infix2postfix(&in[0],&post[0],1);
    printf("Postfix Expression is: %s\n",&post[0]);//Prints Infix expression
	
	gets(exp);
	infix2postfix(&in[0],&exp[0],1);
	printf("The Evaluation of the expression is %d\n",exp,evaluate(&exp[0]));
	

	std::getchar();
    
	return 0;

	
}




Sorry wrong section, can someone move this?


If not moved in 2 hours I'll post it in the proper section

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Postfix

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Problem with Postfix

Posted 18 November 2011 - 09:54 PM

Looks like the correct section to me, this is C code which is appropriate for the C/C++ forum.

Could you please post a link to where you found this code. If this is the exact code you found without any changes it has several things wrong with it, starting with the use of gets().


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 18 November 2011 - 10:11 PM

View Postjimblumberg, on 18 November 2011 - 09:54 PM, said:

Looks like the correct section to me, this is C code which is appropriate for the C/C++ forum.

Could you please post a link to where you found this code. If this is the exact code you found without any changes it has several things wrong with it, starting with the use of gets().


Jim


Here you are
http://www.dreaminco...pressions-in-c/
It's the stack conversion and string evaluation combined into 1
Was This Post Helpful? 0
  • +
  • -

#4 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 19 November 2011 - 12:27 AM

As an FYI my initial plan was to make it so I'd be able to write an infix, have it be converted into a postfix (and printed/displayed) and that postfix be evaluated (and displayed.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Problem with Postfix

Posted 19 November 2011 - 06:25 AM

Not the issue, but that code generates a warning:

b2cstack.c: In function ‘main’:
b2cstack.c:250: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
b2cstack.c:250: warning: too many arguments for format


which is this line:

printf("The Evaluation of the exp<b></b>ression is %d\n",exp,evaluate(&exp[0]));


DON'T USE gets()!!!

Your use of things like &exp[0] implies you don't have a real understanding of arrays in C; perhaps reviewing the link in my signature, Arrays in C/C++, might be helpful as well.

What data are you entering to test this?

EDIT: I want to apologize...the god-awful use of gets() was actually in the tutorial. I have rectified that.

This post has been edited by JackOfAllTrades: 19 November 2011 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Problem with Postfix

Posted 19 November 2011 - 06:41 AM

@JackOfAllTrades The code generating the warning messages you mentioned, along with the use of the gets() function, are contained in the original tutorial. However OP's problem may be the way they combined several of the snippets presented in this tutorial. But this tutorial should possibly be modified to remove the some of the bad concepts, such as gets().

Jim
Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Problem with Postfix

Posted 19 November 2011 - 07:04 AM

I changed gets() to fgets in the tutorial and removed the fflush(stdin) call.
Was This Post Helpful? 1
  • +
  • -

#8 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 19 November 2011 - 12:57 PM

View PostJackOfAllTrades, on 19 November 2011 - 07:04 AM, said:

I changed gets() to fgets in the tutorial and removed the fflush(stdin) call.


I added in what you suggested and I have isolated the error to be when I try and combine the "pop" sections of code from the Postfix and Evaluation section of code.

Pop Section combination attempt:
char pop(struct stack* s)
{
    char ret=(char)EMPTY;
    if(s->top == EMPTY)
        printf("\nSTACK EMPTY");
    else
    {
        ret= s->data[s->top];
        --s->top;
    }
    return ret;
}




Updated code in it's entirety
//Source Code from user born2c0de of http://www.dreamincode.net 
#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

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)
{
    char ret=(char)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--;
    }
}

//Evaluates the stack
int evaluate(char *postfix)
{
    char *p;
    struct stack X;
    int op1,op2,result;
	result = pop(&X);

    emptystack(&X);
    p = &postfix[0];
 
    while(*p != '\0')
    {
       /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
      /* if is digit */
        if(isdigit(*p))
        {
            push(&X,*p - 48);
        }
        else
        {
           //it is an operator
            op1 = pop(&X);
            op2 = pop(&X);

            push(&X,result);
        }
        p++;
    }

    return result;
}//End of the evaluation code




int isempty(struct stack *s)
{
    return (s->top == EMPTY) ? 1 : 0;
}
 

//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 };
	char exp[MAX];
 
    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
	
	fgets(exp, sizeof(exp), stdin);
        exp[strlen(exp) - 1] = '\0';
    printf("%s EQUALS %d\n",exp,evaluate(&exp[0]));	

	std::getchar();
    
	return 0;

	
}




The input I use for testing is "1 + 2 - 3" and it's the same error.

If you could help in anyway (either code or a tutorial/reading material) I'd be grateful.
Was This Post Helpful? 0
  • +
  • -

#9 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 19 November 2011 - 05:56 PM

View PostJackOfAllTrades, on 19 November 2011 - 07:04 AM, said:

I changed gets() to fgets in the tutorial and removed the fflush(stdin) call.


Oh as an FYI the code you changed in the evaluate string section of the tutorial doesn't work and produces an error with the line of code
 exp[strlen(exp) - 1] = '\0';



Because you left out
#include isostream



Though considering you're an expert and I'm a novice the fault more then likely lands on my end...
Was This Post Helpful? 0
  • +
  • -

#10 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 20 November 2011 - 01:40 PM

Okay I fixed the old problem but now I have a new one. No mater what I enter for the infix it converts to postfix (it's suppose to do that) but it always produces an answer of "-1" for the evaluation.

"New" code below
//Source Code from user born2c0de of http://www.dreamincode.net 
#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;
    }
}
 
char pop(struct stack* s)
{
    char 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--;
    }
}

//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';
}

//Evaluates the stack
 
int evaluate(char *postfix)
{
    char *p;
    struct stack X;
    int op1,op2,result;
 
    emptystack(&X);
    p = &postfix[0];
 
    while(*p != '\0')
    {
       /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
      /* if is digit */
        if(isdigit(*p))
        {
            push(&X,*p - 48);
        }
        else
        {
           /* it is an operator */
            op1 = pop(&X);
            op2 = pop(&X);
 
            switch(*p)
            {
                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 0;
            }
            push(&X,result);
        }
        p++;
    }
    result = pop(&X);
    return result;
}


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
	
	fgets(post, sizeof(post), stdin);
        post[strlen(post) - 1] = '\0';
    printf("%s EQUALS %d\n",post,evaluate(&post[0]));	

	std::getchar();
    
	return 0;

	
}



This post has been edited by jimblumberg: 20 November 2011 - 02:38 PM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#11 Thatguywithsomecode  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 26-October 11

Re: Problem with Postfix

Posted 20 November 2011 - 07:56 PM

Fixed everything. "Fused" version of the original tutorial is posted below.
//Source Code from user born2c0de of http://www.dreamincode.net 
#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;
    }
}
 
char pop(struct stack* s)
{
    char 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--;
    }
}

//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';
}

//Evaluates the stack
 
int evaluate(char *postfix)
{
    char *p;
    struct stack X;
    int op1,op2,result;
 
    emptystack(&X);
    p = &postfix[0];
 
    while(*p != '\0')
    {
       /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
      /* if is digit */
        if(isdigit(*p))
        {
            push(&X,*p - 48);
        }
        else
        {
           /* it is an operator */
            op1 = pop(&X);
            op2 = pop(&X);
 
				switch(*p)
            {
                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:
                    return result;
            }
            push(&X,result);
        }
        p++;
    }
    result = pop(&X);
    return result;
}


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]);
	
	fgets(in, sizeof(in), stdin);
        post[strlen(post) - 1] = '\0';
    printf(" %s EQUALS %d\n",post,evaluate(&post[0]));	

	std::getchar();
    
	return 0;

	
}



Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6074
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: Problem with Postfix

Posted 20 November 2011 - 08:11 PM

The problem you noted is that the tutorial you used as the basis for this is written for C, and you want to use C++, at least in some ways.

As a result your headers are redundant. stdio.h and cstdio and really the same. Here's what your headers should look like for C++:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>


In fact, you can get rid of the C stuff altogether by changing the top of the file to:

#include <iostream>
#pragma warning(disable: 4996)

static const int MAX = 50;
static const int EMPTY = -1;



and your main to:

int main()
{
   char in[50] = { 0 },post[50] = { 0 };

   std::cout << "Enter Infix Expression: ";
   std::cin.getline(in, sizeof(in));

   infix2postfix(in, post, 1);

   std::cout << "Postfix Expression is: " << post << std::endl;

   std::cin.getline(in, sizeof(in));
   std::cout << post << " EQUALS " << evaluate(post) << std::endl;

   getchar();

   return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1