0

Hello everyone!
I'm writing a program to evaluate an infix expression(for example: 1.5*(2-3)+(4.5-4)). To do that I'm using function void infix2postfix(char* infix, char * postfix, int insertspace) to convert an infix expression to the postfix one (for this purpose I'm using a stack to hold operators of infix expression);
then I'm using function float evaluate(char *postfix) to evaluate a postfix expression by using another stack to hold operands of postfix expression.
Each function works well. But when combining two functions together to evaluate an infix expression, I've got something like: debug assertion failed...(it's showed in the picture).
Here is my code:

/
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
using namespace std;

#define MAX 10
#define EMPTY -1

struct operatorStack //to hold operators of infix expresstion
{
    char data[MAX];
    int operatorTop;
};

int isemptyoperatorStack(struct operatorStack *s)
{
    return (s->operatorTop == EMPTY) ? 1 : 0;
}

void emptyoperatorStack(struct operatorStack* s)
{
    s->operatorTop=EMPTY;
}

void pushoperatorStack(struct operatorStack* s,int item)
{
    if(s->operatorTop == (MAX-1))
    {
        cout<<"\noperatorStack FULL"<<endl;
    }
    else
    {
        ++s->operatorTop;
        s->data[s->operatorTop]=item;
    }
}

char popoperatorStack(struct operatorStack* s)
{
    char ret=(char)EMPTY;
    if(!isemptyoperatorStack(s))
    {
        ret= s->data[s->operatorTop];
        --s->operatorTop;
    }
    return ret;
}

/*********************************************************************/
struct operandStack //to hold operands of a postfix expression
{
    float data[MAX];
    int operandTop;
};

int isemptyoperandStack(struct operandStack *s)
{
    return (s->operandTop == EMPTY) ? 1 : 0;
}

void emptyoperandStack(struct operandStack* s)
{
    s->operandTop=EMPTY;
}

void pushoperandStack(struct operandStack* s,float item)
{
    if(s->operandTop == (MAX-1))
    {
        cout<<"\noperandStack FULL"<<endl;
    }
    else
    {
        ++s->operandTop;
        s->data[s->operandTop]=item;
    }
}

float popoperandStack(struct operandStack* s)
{
    float ret;
    if(!isemptyoperandStack(s))
    {
        ret= s->data[s->operandTop];
        --s->operandTop;
    }
    return ret;
}

/*--------------------------------------------------------------------*/
int isoperator(char e)
{
    if(e == '+' || e == '-' || e == '*' || e == '/')
        return 1;
    else
        return 0;
}


int priority(char e)
{
    int pri = 0;

    if(e == '*' || e == '/')
        pri = 2;
    else
    {
        if(e == '+' || e == '-')
            pri = 1;
    }
    return pri;
}
/*********************************************************************/
void infix2postfix(char* infix, char * postfix, int insertspace)//convert infix to postfix
{
    char *i,*p;
    struct operatorStack X;
    char n1;
    emptyoperatorStack(&X);
    i = &infix[0];
    p = &postfix[0];

    while(*i)
    {
        while(*i == ' ' || *i == '\t')
        {
            i++;
        }

        if( isdigit(*i) || isalpha(*i)||*i=='.' )
        {
            while( isdigit(*i) || isalpha(*i)||*i=='.')
            {
                *p = *i;
                p++;
                i++;
            }
            /*SPACE CODE*/
            if(insertspace)
            {
                *p = ' ';
                p++;
            }
            /*END SPACE CODE*/
        }

        if( *i == '(' )
        {
            pushoperatorStack(&X,*i);
            i++;
        }

        if( *i == ')')
        {
            n1 = popoperatorStack(&X);
            while( n1 != '(' )
            {
                *p = n1;
                p++;
                /*SPACE CODE*/
                if(insertspace)
                {
                    *p = ' ';
                    p++;
                }
                /*END SPACE CODE*/
                n1 = popoperatorStack(&X);
            }
            i++;
        }

        if( isoperator(*i) )
        {
            if(isemptyoperatorStack(&X))
                pushoperatorStack(&X,*i);
            else
            {
                n1 = popoperatorStack(&X);
                while(priority(*i) <= priority(n1))
                {
                    *p = n1;
                    p++;
                    /*SPACE CODE*/
                    if(insertspace)
                    {
                        *p = ' ';
                        p++;
                    }
                    /*END SPACE CODE*/
                    n1 = popoperatorStack(&X);
                }
                pushoperatorStack(&X,n1);
                pushoperatorStack(&X,*i);
            }
            i++;
        }
    }
    while(!isemptyoperatorStack(&X))
    {
        n1 = popoperatorStack(&X);
        *p = n1;
        p++;
        /*SPACE CODE*/
        if(insertspace)
        {
            *p = ' ';
            p++;
        }
        /*END SPACE CODE*/
    }
    *p = '\0';
}


/***********************************************************************/
float evaluate(char *postfix)//to evaluate a postfix expression
{
    char *p;
    struct operandStack stk;
    float op1,op2,result;
    string buffer;
    buffer.clear();
    emptyoperandStack(&stk);
    p = &postfix[0];

    while(*p != '\0')
    {
       /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
      /* if is digit or a point (decimal separator) */
        while(isdigit(*p)||*p=='.')
        {
            buffer.push_back(*p);
            ++p;
            if(*p==' '||*p == '\t') 
            {
                pushoperandStack(&stk,atof(buffer.c_str()));
                buffer.clear();
                break;
            }
        }
        if(isoperator(*p))
        {
            /* it is an operator */
            op1 = popoperandStack(&stk);
            op2 = popoperandStack(&stk);
            
            switch(*p)
            {
                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;
            }
            pushoperandStack(&stk,result);
        }
        ++p;
    }
    result = popoperandStack(&stk);
    return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
    /*********************************
    //test function infix2postfix: (well done)
    char in[50]="1.5*(2-3)+(4.5-4)",post[50];
    cout<<"infix string: "<<"1.5*(2-3)+(4.5-4)"<<endl;
    infix2postfix(&in[0],&post[0],1);
    cout<<"postfix string: "<<post<<endl;
    *********************************/

    /*********************************
    //test function evaluate:(well done)
    cout<<"postfix string: "<<"1.5 2 3 - * 4.5 4 - +"<<endl;
    char post[50]="1.5 2 3 - * 4.5 4 - +";
    cout<<"Result:"<<evaluate(&post[0])<<endl;
    *********************************/

    /********************************
    //together: (doesn't work)
    char in[50]="1.5*(2-3)+(4.5-4)",post[50];
    cout<<"infix string: "<<"1.5*(2-3)+(4.5-4)"<<endl;
    infix2postfix(&in[0],&post[0],1);
    cout<<"postfix string: "<<post<<endl;
    cout<<"Result:"<<evaluate(&post[0])<<endl;
    ********************************/
    
    system("PAUSE");
    return 0;
}

Here are pictures:
http://i1013.photobucket.com/albums/af260/phabion/infix2postfix-1.png
http://i1013.photobucket.com/albums/af260/phabion/evaluatePostfix.png
http://i1013.photobucket.com/albums/af260/phabion/together.png
I'm not an English native person, so it's difficult to understand the situation here. So I really need your helps.
Thank you in advanced!

Edited by svatstika: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by VernonDozier
0

One of your lines of code calls a function that somewhere in the line of stack calls calls a function that has requirements for valid parameters passed to it and uses an "assert" function to validate those parameters. In your case a parameter flunked the test and the program aborted. Look at the stack trace to find out the exact sequence of calls. That will give you an exact line number in your own code that caused the problem.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.