I was implementing this algorithm of parsing a mathematical expression....but dint meet the
required result........i have restricted the string to take one digit numeral only
i think i am suffering because of this type conversions between int and char...but don't know what to do...
here's the code..

//parsing.cpp
#include<iostream>
#include<conio.h>
using namespace std;
class stack
{
      private:
              char str[40];
              int top;
              public:
                     stack()
                     {top=0;
                            }
                            void push(char s)
                            {
                                 str[top]=s;
                                 top++;
                                 }
                                 char pop()
                                 {
                                      return str[top--];
                                      }
                                      int gettop()
                                      {
                                          return top;
                                          }
                                          void show()
                                          {
                                               cout<<str[top];
                                           }
};
class expression
{
      private:
              stack s1;
              char pstr[40];
              public:
                    void get()
                    {
                         cout<<"enter expression:";
                         cin>>pstr;
                    }
                    int solve();
                    void parse();
};
void expression::parse()
{
     char ch;
     int len;
     char lastval;
     char lasttop;
     len=strlen(pstr);
     for(int i=0;i<len;i++)
     {
             ch==pstr[i];
             if(ch>='0'&&ch<='9')
             s1.push(ch-'0');
             else if((ch=='+'||ch=='-'||ch=='*'||ch=='/')&&(s1.gettop()==1))
             s1.push(ch);
             else if((ch=='+'||ch=='-'||ch=='*'||ch=='/')&&(s1.gettop()>1))
             {
                  lastval=s1.pop();
                  lasttop=s1.pop();
                  if(ch=='*'||ch=='/'&&lasttop=='+'||lasttop=='-')
                  {
                                                                  s1.push(lasttop);
                                                                  s1.push(lastval);
                  }
                  else
                  {
                      switch(lasttop)
                      {
                                     case '+':
                                     s1.push(s1.pop()+lastval);
                                     break;
                                     case '-':
                                     s1.push(s1.pop()-lastval);
                                     break;
                                     case '*':
                                     s1.push(s1.pop()*lastval);
                                     break;
                                     case '/':
                                     s1.push(s1.pop()/lastval);
                                     break;
                                     default:
                                             cout<<"invalid operator error.";
                      }
                      s1.push(ch);
                  }
                  s1.push(ch);
                  }
     }
     s1.show();
}
int expression::solve()
{
    char lastval;
    while(s1.gettop()>1)
    {
                        lastval=s1.pop();
                        switch(s1.pop())
                        {
                                        case '+':
                                     s1.push(s1.pop()+lastval);
                                     break;
                                     case '-':
                                     s1.push(s1.pop()-lastval);
                                     break;
                                     case '*':
                                     s1.push(s1.pop()*lastval);
                                     break;
                                     case '/':
                                     s1.push(s1.pop()/lastval);
                                     break;
                                     default:
                                             cout<<"invalid operator error.";
                        }
    }
    return int(s1.pop());
}
int main()
{
    expression e1;
    char che='y';
    cout<<"enter an expression of the form:"<<endl;
    cout<<"3+7/9*6"<<endl;
    cout<<"note that expression should contain only single digit numeral and...."<<endl;
    cout<<"only '+','-','*' and '/' arithmetic operators should be there..."<<endl;
    do
    {
                expression* epr=new expression;
                epr->get();
                epr->parse();
                cout<<endl<<"the answer is "<<epr->solve()<<endl;
                cout<<"do another(y/n):";
                cin>>che;
                delete epr;
    }while(che!='n');
    getch();
    return 0;
}

the code gets compiled but doesnt show the required result...i m sure it must be the
problem with the int and char thing...bt wat can i do in order to run this program well
just after a subtle change..
thanx in advance

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I included string.h and modified 2 lines

char pop()
{
    return str[--top];
}

and in function parse

ch=pstr[i];

and managed to get 3+7=10
but 4+5+7 throws error "invalid operator error." and still prints the wrong answer.

Debugging can help solve the problem. Trace through each line.

Posting partially corrected code properly

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class stack
{
private:
    char str[40];
    int top;
public:
    stack()
    {
        top=0;
    }
    void push(char s)
    {
        str[top]=s;
        top++;
    }
    char pop()
    {
        return str[--top];
    }
    int gettop()
    {
        return top;
    }
    void show()
    {
        cout<<str[top];
    }
};
class expression
{
private:
    stack s1;
    char pstr[40];
public:
    void get()
    {
        cout<<"enter expression:";
        cin>>pstr;
    }
    int solve();
    void parse();
};
void expression::parse()
{
    char ch;
    int len;
    char lastval;
    char lasttop;
    len=strlen(pstr);
    for(int i=0; i<len; i++)
    {
        ch=pstr[i];
        if(ch>='0'&&ch<='9')
            s1.push(ch-'0');
        else if((ch=='+'||ch=='-'||ch=='*'||ch=='/')&&(s1.gettop()==1))
            s1.push(ch);
        else if((ch=='+'||ch=='-'||ch=='*'||ch=='/')&&(s1.gettop()>1))
        {
            lastval=s1.pop();
            lasttop=s1.pop();
            if(ch=='*'||ch=='/'&&lasttop=='+'||lasttop=='-')
            {
                s1.push(lasttop);
                s1.push(lastval);
            }
            else
            {
                switch(lasttop)
                {
                case '+':
                    s1.push(s1.pop()+lastval);
                    break;
                case '-':
                    s1.push(s1.pop()-lastval);
                    break;
                case '*':
                    s1.push(s1.pop()*lastval);
                    break;
                case '/':
                    s1.push(s1.pop()/lastval);
                    break;
                default:
                    cout<<"invalid operator error.";
                }
                s1.push(ch);
            }
            s1.push(ch);
        }
    }
    s1.show();
}
int expression::solve()
{
    char lastval;
    while(s1.gettop()>1)
    {
        lastval=s1.pop();
        switch(s1.pop())
        {
        case '+':
            s1.push(s1.pop()+lastval);
            break;
        case '-':
            s1.push(s1.pop()-lastval);
            break;
        case '*':
            s1.push(s1.pop()*lastval);
            break;
        case '/':
            s1.push(s1.pop()/lastval);
            break;
        default:
            cout<<"invalid operator error.";
        }
    }
    return int(s1.pop());
}
int main()
{
    expression e1;
    char che='y';
    cout<<"enter an expression of the form:"<<endl;
    cout<<"3+7/9*6"<<endl;
    cout<<"note that expression should contain only single digit numeral and...."<<endl;
    cout<<"only '+','-','*' and '/' arithmetic operators should be there..."<<endl;
    do
    {
        expression* epr=new expression;
        epr->get();
        epr->parse();
        cout<<endl<<"the answer is "<<epr->solve()<<endl;
        cout<<"do another(y/n):";
        cin>>che;
        delete epr;
    }
    while(che!='n');
//getch();
    return 0;
}

Vinayak

This article has been dead for over six months. Start a new discussion instead.