Connecting Tech Pros Worldwide Help | Site Map

int to char problem ?

bushido
Guest
 
Posts: n/a
#1: Nov 9 '07
#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::output()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector[i]<<endl;
}
}

int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str[i] != '+') && (_str[i] != '*'))
{
_stack.push(_str[i]);
}
else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
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[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
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.

Ron Natalie
Guest
 
Posts: n/a
#2: Nov 9 '07

re: int to char problem ?


bushido wrote:
Quote:
>
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 '+':
...


robin
Guest
 
Posts: n/a
#3: Nov 9 '07

re: int to char problem ?


>
Quote:
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'); ".

Victor Bazarov
Guest
 
Posts: n/a
#4: Nov 9 '07

re: int to char problem ?


bushido wrote:
Quote:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Stack
{
public :
Stack();
void push(char&);
void push(const char&);
Quote:
void pop();
char getpop();
A better name would be 'top'.
Quote:
void output();
>
private:
vector<char_vector;
int topOfStack;
Why is it 'int'?
Quote:
};
Stack::Stack():topOfStack(-1){}
void Stack::push(char &x)
{
topOfStack++;
Why do you need this?
Quote:
_vector.push_back(x);
}
void Stack::pop()
{
topOfStack--;
Why do you need this? You can change the size of the vector.
Quote:
}
char Stack::getpop()
{
return _vector[topOfStack--];
You can simply do _vector.back();
Quote:
}
void Stack::output()
{
for(int i = topOfStack-1; i != -1 ; i--)
{
cout<<"item "<<i<<"= "<<_vector[i]<<endl;
}
Use _vector.size() instead of 'topOfStack'.
Quote:
}
>
int main()
{
string _str;
cin>>_str;
Stack _stack;
for(int i=0 ;i != _str.size(); i++)
{
if((_str[i] != '+') && (_str[i] != '*'))
{
_stack.push(_str[i]);
So, you push every character from the string into the stack until
you meet a plus or an asterisk.
Quote:
}
else if(_str[i] == '+')
{
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?
Quote:
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
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?
Quote:
>
else if(_str[i] == '+')
{
int temp;
temp = _stack.getpop() + _stack.getpop();
char ctemp = (char)temp;
_stack.push(ctemp);
}
else if(_str[i] == '*')
{
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


bushido
Guest
 
Posts: n/a
#5: Nov 9 '07

re: int to char problem ?


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

not work 56+ b'coz is ';'

Closed Thread


Similar C / C++ bytes