int to char problem ?

B

bushido

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Stack
{
public :
Stack();
void push(char&);
void pop();
char getpop();
void output();

private:
vector<char> _vector;
int topOfStack;
};
Stack::Stack():topOfStack(-1){}
void Stack::push(char &x)
{
topOfStack++;
_vector.push_back(x);
}
void Stack::pop()
{
topOfStack--;
}
char Stack::getpop()
{
return _vector[topOfStack--];
}
void Stack::eek:utput()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector<<endl;
}
}

int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str != '+') && (_str != '*'))
{
_stack.push(_str);
}
else if(_str == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
}
cout<<endl;
_stack.output();
return 0;
}

I have problem in line

else if(_str == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}

b'coz ex. 56+
5 => 53
6 => 54
sum is 107 => k but i want 11 not k and push in vector is 11 plese
help.
 
R

Ron Natalie

bushido said:
b'coz ex. 56+
5 => 53
6 => 54
sum is 107 => k but i want 11 not k and push in vector is 11 plese
help.
Huh? Could you use sentences. Do you know the difference between
a character and it's numeric representation? Your "cast" doesn't
make this conversion, the numeric representation in char converts
with no modification to other integral times.

Since you're using single chars, how do you think a single push is
going to push two characters?

How are you going to distinguish 56 as two tokens 5 and 6 from
56? Me thinks you should introduce some punctuation into your
program and use a non-char integral type. for example:

5 6 +

could be parsed with

int operand;
if(cin >> operand) {
// converted to int
_stack.push(operand);
} else {
cin.clear(); // clear error
string operator;
cin >> operator;
switch(operator[0]) {
case '+':
...
 
R

robin

b'coz ex. 56+
5 => 53
6 => 54
sum is 107 => k but i want 11 not k and push in vector is 11 plese
help.

Note one fact that:

'0' -> 48
....
'5' -> 53
'6' -> 54

therefore:
'6' - '0' -> 6
'5' - '0' -> 5

You need to write "temp = (_stack.getpop() - '0') + (_stack.getpop() -
'0'); ".
 
V

Victor Bazarov

bushido said:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Stack
{
public :
Stack();
void push(char&);

void push(const char&);
void pop();
char getpop();

A better name would be 'top'.
void output();

private:
vector<char> _vector;
int topOfStack;

Why is it 'int'?
};
Stack::Stack():topOfStack(-1){}
void Stack::push(char &x)
{
topOfStack++;

Why do you need this?
_vector.push_back(x);
}
void Stack::pop()
{
topOfStack--;

Why do you need this? You can change the size of the vector.
}
char Stack::getpop()
{
return _vector[topOfStack--];

You can simply do _vector.back();
}
void Stack::eek:utput()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector<<endl;
}


Use _vector.size() instead of 'topOfStack'.
}

int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str != '+') && (_str != '*'))
{
_stack.push(_str);


So, you push every character from the string into the stack until
you meet a plus or an asterisk.
}
else if(_str == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();


When you add '5' and '6', what are you going to get? Write a simple
program:

#include <iostream>
int main() {
std::cout << "'5' + '6' is " << '5'+'6' << std::endl;
}

what do you get? Why?
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
}
cout<<endl;
_stack.output();
return 0;
}

I have problem in line
Huh?


else if(_str == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str == '*')
{
int temp;
temp = _stack.getpop() * _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}

b'coz ex. 56+
5 => 53
6 => 54
sum is 107 => k but i want 11 not k and push in vector is 11 plese
help.


You probably need to convert the char you're about to insert into its
"meaning". '5' you need to convert to value 5. '6' to value 6. YOu
can do it by subtracting '0' from the char.

V
 
B

bushido

temp = (_stack.getpop()-'0') + (_stack.getpop()-'0');

not work 56+ b'coz is ';'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,279
Latest member
LaRoseDermaBottle

Latest Threads

Top