Thread: [Dev-C++] reverse polish notation
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: chromatonus 7. <chr...@ho...> - 2001-12-08 22:52:08
|
hi, my stack works, (thanks to Mr. Hardman,) but i am now trying to create a
calculator: type in digits 0~9 in one line, like this: 123, for example.
Then if you type in '+', then the prgm pushes the top two # out of stack,
add them, and pop it back in. So 2 and 3 would be added and 5 is popped in
on top of 1. same for '*' and '-'. if '=' is typed then the top element of
the stack is displayed on the screen (like the answer).
i have to make sure that all the input chars be on one line but not
overlapping, and that my add, subtract and mult. functions work (they
don't.)
notes: 'q' quits the program and 'i' clears everything on the screen except
the > prompt.
thank you for your help : )
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int const max_stack = 100;
class stacktype
{
public:
stacktype();
// default constructor
// postcondition: initializes stack by setting TOP = 0
void initialize();
// postcondition: erase stack by setting TOP = 0
void push( int );
// postcondition: If the stack is not full,
// increase TOP by 1 , then
// put info into DATA[TOP]
int pop();
// postcondition: If the stack is not empty,
// put DATA[TOP] into x,
// decrease TOP by 1,
// return x
bool is_empty();
// postcondition: Return true if stack is empty, false otherwise.
bool is_full();
// postcondition: Return true if stack is full, false otherwise.
private:
int mydata[max_stack];
int mytop; // subscript of the top of the stack
};
//==============================================================================
//============================== Free functions
================================
//==============================================================================
void a_two( stacktype );
// precondition: send in stack
// postcondition: new stack formed with top element = sum of top 2
elements
// of old stack
void s_two( stacktype );
// precondition: send in stack
// postcondition: new stack formed with top element = difference of top 2
// elements of old stack
void m_two( stacktype );
// precondition:
// postcondition:
void display_top( stacktype );
// precondition: send in stack
// postcondition: display, without destruction, the value in data[top]
//==============================================================================
//============================== Implementation
================================
//==============================================================================
stacktype::stacktype()
{
mytop = 0;
}
void stacktype::initialize()
{
mytop = 0;
clrscr();
gotoxy(3,3);
cout<< "> ";
}
bool stacktype::is_empty()
{
return (mytop == 0);
}
bool stacktype::is_full()
{
return (mytop == max_stack-1);
}
void stacktype::push( int x )
{
if( !is_full() )
{
mytop++;
mydata[mytop] = x;
}
}
int stacktype::pop()
{
if( !is_empty() )
{
int x = mydata[mytop];
mytop--;
return x;
}
}
//==============================================================================
//============================== Client Program
================================
//==============================================================================
int main()
{
int c = 3;
stacktype S;
char z;
gotoxy(3,3);
cout<< "> ";
c = 5;
while( z != 'Q' && z != 'q' )
{
gotoxy(c,3);
cin>> z;
switch( z )
{
case 'I':
case 'i': S.initialize();
break;
case '+': a_two(S);
break;
case '-': s_two(S);
break;
case '*': m_two(S);
break;
case '=': display_top(S);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': z = getche();
int j = z;
S.push( j );
break;
}
c++;
z = getche();
}
}
void a_two( stacktype S )
{
int p = S.pop();
int q = S.pop();
S.push( p+q );
}
void s_two( stacktype S )
{
int p = S.pop();
int q = S.pop();
S.push( p-q );
}
void m_two( stacktype S )
{
int p = S.pop();
int q = S.pop();
S.push( p*q );
}
void display_top( stacktype S )
{
int t = S.pop();
cout<< t;
S.push(t);
}
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|
|
From: Jason H. <jas...@bt...> - 2001-12-09 01:50:59
|
Hi again.
I've had a quick look at what you are doing and have found what I think is
the source of your main problem although there are a few other things that
probably need tweeking, but I think you can do that for yourself. Do you
remember what I said about passing an object as a parameter makes a copy of
that object and doesn't affect the original? That is what is stopping your
function from working. In this case the value that you want to push back
onto the stack needs to be done to the original, but you are putting it in a
copy that is destroyed at the end of the function call. There are two ways
around it, pass the stack as a reference, or pass a pointer to the stack and
work with that.
As you can guess I'm working on a rewritten version (just for fun), but it's
not finished yet.
Keep on trying. You may want to look at the way you are entering your data
as you seem to be mangleing the values a little, although I'm not sure,
maybe it works?
I hope that this helps. I'll try to help you any way I can if you need it in
future.
Jason.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, December 08, 2001 10:51 PM
Subject: [Dev-C++] reverse polish notation
> hi, my stack works, (thanks to Mr. Hardman,) but i am now trying to create
a
> calculator: type in digits 0~9 in one line, like this: 123, for example.
> Then if you type in '+', then the prgm pushes the top two # out of stack,
> add them, and pop it back in. So 2 and 3 would be added and 5 is popped in
> on top of 1. same for '*' and '-'. if '=' is typed then the top element of
> the stack is displayed on the screen (like the answer).
>
> i have to make sure that all the input chars be on one line but not
> overlapping, and that my add, subtract and mult. functions work (they
> don't.)
>
> notes: 'q' quits the program and 'i' clears everything on the screen
except
> the > prompt.
>
> thank you for your help : )
>
> #include <iostream.h>
> #include <conio.h>
> #include <stdlib.h>
>
> int const max_stack = 100;
>
> class stacktype
> {
> public:
> stacktype();
> // default constructor
> // postcondition: initializes stack by setting TOP = 0
> void initialize();
> // postcondition: erase stack by setting TOP = 0
> void push( int );
> // postcondition: If the stack is not full,
> // increase TOP by 1 , then
> // put info into DATA[TOP]
> int pop();
> // postcondition: If the stack is not empty,
> // put DATA[TOP] into x,
> // decrease TOP by 1,
> // return x
> bool is_empty();
> // postcondition: Return true if stack is empty, false otherwise.
> bool is_full();
> // postcondition: Return true if stack is full, false otherwise.
> private:
> int mydata[max_stack];
> int mytop; // subscript of the top of the stack
> };
>
>
>
//==========================================================================
====
> //============================== Free functions
> ================================
>
//==========================================================================
====
>
> void a_two( stacktype );
> // precondition: send in stack
> // postcondition: new stack formed with top element = sum of top 2
> elements
> // of old stack
>
> void s_two( stacktype );
> // precondition: send in stack
> // postcondition: new stack formed with top element = difference of top
2
> // elements of old stack
>
> void m_two( stacktype );
> // precondition:
> // postcondition:
>
> void display_top( stacktype );
> // precondition: send in stack
> // postcondition: display, without destruction, the value in data[top]
>
>
//==========================================================================
====
> //============================== Implementation
> ================================
>
//==========================================================================
====
>
> stacktype::stacktype()
> {
> mytop = 0;
> }
> void stacktype::initialize()
> {
> mytop = 0;
> clrscr();
> gotoxy(3,3);
> cout<< "> ";
> }
> bool stacktype::is_empty()
> {
> return (mytop == 0);
> }
> bool stacktype::is_full()
> {
> return (mytop == max_stack-1);
> }
> void stacktype::push( int x )
> {
> if( !is_full() )
> {
> mytop++;
> mydata[mytop] = x;
> }
> }
> int stacktype::pop()
> {
> if( !is_empty() )
> {
> int x = mydata[mytop];
> mytop--;
> return x;
> }
> }
>
>
>
//==========================================================================
====
> //============================== Client Program
> ================================
>
//==========================================================================
====
>
> int main()
> {
> int c = 3;
> stacktype S;
> char z;
> gotoxy(3,3);
> cout<< "> ";
> c = 5;
> while( z != 'Q' && z != 'q' )
> {
> gotoxy(c,3);
> cin>> z;
> switch( z )
> {
> case 'I':
> case 'i': S.initialize();
> break;
>
> case '+': a_two(S);
>
> break;
>
> case '-': s_two(S);
>
> break;
>
> case '*': m_two(S);
>
> break;
>
> case '=': display_top(S);
>
> break;
>
> case '0':
> case '1':
> case '2':
> case '3':
> case '4':
> case '5':
> case '6':
> case '7':
> case '8':
> case '9': z = getche();
> int j = z;
> S.push( j );
> break;
>
> }
> c++;
> z = getche();
> }
> }
>
> void a_two( stacktype S )
> {
> int p = S.pop();
> int q = S.pop();
> S.push( p+q );
> }
>
> void s_two( stacktype S )
> {
> int p = S.pop();
> int q = S.pop();
> S.push( p-q );
> }
>
> void m_two( stacktype S )
> {
> int p = S.pop();
> int q = S.pop();
> S.push( p*q );
> }
>
> void display_top( stacktype S )
> {
> int t = S.pop();
> cout<< t;
> S.push(t);
> }
>
>
>
>
>
>
>
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|