|
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
|