RBN

I need your help in RBN in stack with class my code:
int calculator::compute()
{
for(i=0;i<8;i++){
if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
{x.pop();
switch (expr[i]) {
case'+':
x.push(expr[i-2]+expr[i-1]);
break;
case'-':
x.push(expr[i]-expr[i-1]);
break;
case'*':
x.push(expr[i-2]*expr[i-1]);
break;
case'/':
x.push(expr[i]/ expr[i-1]);
break; }
}
else
{
x.push(expr[i]);
x.top();
cout<<"|"<<x.top()<<"|"<<endl;
x.pop();}


}}
but it's giving me
int calculator::compute()
{
for(i=0;i<8;i++){
if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
{x.pop();
switch (expr[i]) {
case'+':
x.push(expr[i-2]+expr[i-1]);
break;
case'-':
x.push(expr[i]-expr[i-1]);
break;
case'*':
x.push(expr[i-2]*expr[i-1]);
break;
case'/':
x.push(expr[i]/ expr[i-1]);
break; }
}
else
{
x.push(expr[i]);
x.top();
cout<<"|"<<x.top()<<"|"<<endl;
x.pop();}


}}int calculator::compute()
{
for(i=0;i<8;i++){
if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
{x.pop();
switch (expr[i]) {
case'+':
x.push(expr[i-2]+expr[i-1]);
break;
case'-':
x.push(expr[i]-expr[i-1]);
break;
case'*':
x.push(expr[i-2]*expr[i-1]);
break;
case'/':
x.push(expr[i]/ expr[i-1]);
break; }
}
else
{
x.push(expr[i]);
x.top();
cout<<"|"<<x.top()<<"|"<<endl;
x.pop();}


}}
but it is giving me:
|56
|49
|50
|52
|0
and my string="812+4*+"
so i did 3 files: one is for the funktion and constructor of class:
#include<iostream>
#include<stack>
#include<string>
#include"class1.h"
using namespace std;
calculator::calculator(string f,stack<int> n)
{expr=f;
x=n;}
int calculator::compute()
{
for(i=0;i<8;i++){
if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
{
switch (expr[i]) {
case'+':
expr[i-2]+expr[i-1];
x.top();
x.pop();
break;
case'-':
x.push(expr[i-1]-expr[i-2]);
break;
case'*':
x.push(expr[i-2]*expr[i-1]);
break;
case'/':
x.push(expr[i-1]/ expr[i-2]);
break; }
}
else
{
x.push(expr[i]);
x.top();
cout<<"|"<<x.top()<<"|"<<endl;
x.pop();}


}}
and one for main:
#include<iostream>
#include<stack>
#include<string>
#include"class1.h"
using namespace std;
int main()
{
stack<int> n;
string d="812+4*+";
calculator calc(d,n);
calc.compute();
return 0;
}
and one for the class:
#include<iostream>
#include<stack>
#include<string>
using namespace std;
class calculator{
private:
int i;
stack<int>x;
string expr;
public:
calculator(string,stack<int>);
int compute();
};
i don't know where the problem is :(
These values:
|56
|49
|50
|52
are the ASCII equivalents of the characters '8', '1', '2' and '4'.

To convert a single character digit to an integer, you can do this:
1
2
char digit = '8';
int n = digit - '0';

In your code it means this line
 
    x.push(expr[i]);
should be
 
    x.push(expr[i]-'0');  // convert char to int 


I was puzzled by this for loop:
 
    for (i=0; i<8; i++)

What is this mysterious number 8?
It should in fact be more like this:
 
    for (int i=0; i<expr.size(); i++)


One more thing, this line:
1
2
    case '+':
        expr[i-2] + expr[i-1];

calculates a result, but doesn't store it anywhere - there should also be a push()
Maybe I misunderstood the intention, but perhaps something like this:
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
#include <iostream>
#include <stack>
#include <string>

class calculator {
private:
    std::stack<int> x;
    std::string     expr;
    
public:
    calculator(std::string);
    int compute();
};

calculator::calculator(std::string f)
{
    expr = f;
}

int calculator::compute()
{
    for (size_t i=0; i<expr.size(); i++)
    {          
        int result;
        switch (expr[i]) 
        {
            case '+':
                result = x.top();
                x.pop();
                result += x.top();
                x.pop(); 
                x.push(result);
                break;
                
            case '-':
                
                break;
            case '*':
                
                break;
            case '/':
                
                break; 
                
            case '0': case '1': case '2': case '3': case '4': 
            case '5': case '6': case '7': case '8': case '9': 
                x.push(expr[i]-'0');  // convert char to int
                std::cout << "|" << x.top() << "|" << std::endl;
                break;
                
            default:
                std::cout << "Unhandled operator: " << expr[i] << '\n';
        }
    }
    
    return x.top();
}

int main()
{
    calculator calc("32+");
    std::cout << "result = " << calc.compute() << '\n';
}

Output:
|3|
|2|
result = 5

Last edited on
thanks for your response i have changed my code:

#include<iostream>
#include<stack>
#include<string>
#include"class.h"
using namespace std;
calculator::calculator(string f,stack<int> n)
{expr=f;
x=n;}
int calculator::compute()
{
for (i=0;i<expr.size();i++)
{
if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
{int result;
switch (expr[i]) {
case'+':
result = x.top();
x.pop();
result += x.top();
x.pop();
x.push(result);
break;
case'-':
result = x.top();
x.pop();
result -= x.top();
x.pop();
x.push(result);
break;
case'*':
result = x.top();
x.pop();
result *= x.top();
x.pop();
x.push(result);
break;
case'/':
result = x.top();
x.pop();
result /= x.top();
x.pop();
x.push(result);
break; }}
else{
x.push(expr[i]-'0');
cout<<"|"<<x.top()<<"|"<<endl;
}
return x.top();
}}
and the main
#include<iostream>
#include<stack>
#include<string>
#include"class.h"
using namespace std;
int main()
{
stack<int> n;
string d="812+4*+";
calculator calc(d,n);
cout<<calc.compute()<<endl;
return 0;
}
but im getting
|8|
8 :(
The issue with the code giving incorrect output - this line
return x.top(); is inside the for-loop, so the loop will execute only once.

Please use code tags to help the code to display properly,
[code]your code here[/code]
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
#include<iostream>
#include<stack>
#include<string>
#include"class.h"
using namespace std;
calculator::calculator(string f,stack<int> n)
	{expr=f;
	x=n;}
int calculator::compute()
{
    for (i=0;i<expr.size();i++)
{
	if ((expr[i]=='+')||(expr[i]=='-')||(expr[i]=='*')||(expr[i]=='/'))
	{int result;
	switch (expr[i]) {                     
   	case'+': 
	result = x.top();
         x.pop();
        result += x.top();
	x.pop();
	x.push(result);
	break;                    
	case'-':
	result = x.top();
        x.pop();
        result -= x.top();
	x.pop();
	x.push(result);
	break;          
	case'*':
	result = x.top();
         x.pop();
        result *= x.top();
	x.pop();
	x.push(result);
	break;                     
	case'/':
	result = x.top();
         x.pop();
        result /= x.top();
	x.pop();
	x.push(result);
	break; }}
	else{                
	x.push(expr[i]-'0'); 
        cout<<"|"<<x.top()<<"|"<<endl;
        }
      	return x.top();	
}}

and the main
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<stack>
#include<string>
#include"class.h"
using namespace std;
int main()
{
stack<int> n; 
string d="812+4*+";
calculator calc(d,n);
cout<<calc.compute()<<endl;
return 0;
}

but im getting
|8|
8 :(
The program should give me the result of every operation
|1| |3| |4| |12| |20|
|2| |8| |3| |8|
|8| |8|
Last edited on
Thanks for the code formatting - it helps.
kais2 wrote:
but im getting
|8|
8 :(
As I mentioned previously,
Chervil wrote:
The issue with the code giving incorrect output - this line
return x.top(); is inside the for-loop, so the loop will execute only once.



kais2 wrote:
The program should give me the result of every operation
|1| |3| |4| |12| |20|
|2| |8| |3| |8|
|8| |8|

To achieve that, take line 46 and move it.
 
        cout<<"|"<<x.top()<<"|"<<endl;
It is currently inside the last part of the if-else statement. It needs to be exactly where you currently have the return x.top(); at line 48, and the latter needs to be moved to line 49.5 - that is to say, between the two closing braces at line }}.
something like this:
42
43
44
45
46
47
48
49
50
51
            }
        }
        else
        {                
            x.push(expr[i]-'0'); 
        }
        cout<<"|"<<x.top()<<"|"<<endl;
    }
    return x.top(); 
}


That gives me an output like this:
|8|
|1|
|2|
|3|
|4|
|12|
|20|
20


(Note: for your intended output you may need the cout positioned somewhat differently - try it yourself). )
Last edited on
thank you alot ,the program is working very well :)
Topic archived. No new replies allowed.