Parsing problem

I don't see any problem.but it's dont working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstdlib>
#include <iostream>
#include <cstring>
//Parsing
using namespace std;

const int LEN = 80;
const int MAX = 40;
////////////////////////////////////////
class Stack
{
      private:
              char st[MAX];
              int top;
      public:
             Stack()
             { top = 0; }
             
             void push(char var)
             { st[++top] = var; }
             
             char pop()
             { return st[top--]; }
             
             int gettop()
             { return top; }
};
//////////////////////////////////////////////////
class express
{
      private:
              Stack s;
              char* pStr;
              int len;
      public:
             express(char* ptr)
             {
                           pStr = ptr;
                           len = strlen(pStr);
             }
             
             void parse();
             int solve();
};
////--------------------------------------------
void express::parse()
 {
     char ch;
     char lastval;
     char lasttop;
     
     for(int j = 0; j<LEN; j++)
       {
             ch = pStr[j];
             
             if(ch >= '0' && ch <= '9')
               s.push(ch-'0');
               
             else if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
               {
                  if(s.gettop() == 1)
                    s.push(ch);
                  else
                  {
                      lastval = s.pop();
                      lasttop = s.pop();
                   
                      if( (ch == '*' || ch == '/' && lasttop == '+' || lasttop == '-') )
                        {
                              s.push(lasttop);
                              s.push(lastval);
                        }
                        else
                          {
                              switch(lasttop)
                                {
                                     case '+': s.push(s.pop() + lastval); break;
                                     case '-': s.push(s.pop() - lastval); break;
                                     case '*': s.push(s.pop() * lastval); break;
                                     case '/': s.push(s.pop() / lastval); break;
                                     default: cout<<"\nUnkown oper"; exit(1);
                                }
                          }
                          s.push(ch);
                           }
                      }
                      else
                      { cout<<"\nUnkown input character"; exit(1); }
                    }
                  } 
//----------------------------------------------------
int express::solve()
  {
    char lastval;
    
    while(s.gettop() > 1) 
      {
        lastval = s.pop();
        switch( s.pop() )
         {
           case '+': s.push( s.pop() + lastval); break; 
           case '-': s.push( s.pop() - lastval); break;
           case '*': s.push( s.pop() * lastval); break;
           case '/': s.push( s.pop() / lastval); break;
           default: cout<<"\nUnkown operator"; exit(1); 
           }
        }
           return int ( s.pop() );
}

/////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
    char ans;
    char string[LEN];
    
    cout << "\nEnter an arithmetic expression"
          "\nof the form 2+3*4/2."
          "\nNo number may have more than one digit."
          "\nDon's use any spaces or parentheses.";
          
          do
           {
              cout << "Enter expresssion: ";
              cin >> string;
              
              express* eptr = new express(string);
              eptr -> parse();
              cout << "\nThe numerical value is: " << eptr->solve();
              delete eptr;
              
              cout << "\nDo another(Enter y or n)?";
              cin >> ans;
              } while(ans =='y');
              
    
    

    system("PAUSE");
    return EXIT_SUCCESS;
}

http://www.cplusplus.com/forum/articles/1295/

specifically:

When asking about code
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.
I revise again this code.but I'dont see any problem.I think have a problem on my computer or compiler. please Will you compile or try on your computer ?. thanks
Not without you telling me what the problem is. Are you getting a compiler error? Does the program crash? Or is it just not behaving as expected?

Be specific about what the problem is. I don't know what this program is supposed to do or how it's supposed to do it. I'm not about to invest my time trying to figure out what the problem is on top of trying to figure out how to solve that problem.
thanks for helping.take it easy
Perusing your code I see two things wrong. Fix those two things and it will work.
thank u.I will examine. ;)
oh yes.I saw problem. "LEN" at line 52.I changed LEN with len :)
Hammurabi this is working now.but I'dont see any problem where is second problem?
The other problem is that in your Stack, push should be st[top++] = var; and pop should be return st[--top];. Also you should test for overflow/underflow.
it's not problem but thanks for helping again
Topic archived. No new replies allowed.