|
From: chromatonus 7. <chr...@ho...> - 2001-12-15 01:57:48
|
hi, I was given the following header (can't change it, sorry) to write a
calculator prgm using linked lists as stack (reverse polish notation or
postfix)
#include <iostream.h>
#include <conio.h>
struct node
{
int data;
node *next;
};
class stacktype
{
public:
// initial-value constructor
stacktype( int );
// postcondition: initialize list as STACK = NULL
// top of stack will be the lead node
// and will set screen row to initial value
void initialize();
// postcondition: object stack list is set to NULL
// the screen display is cleared
bool is_empty();
// postcondition: return FALSE if stack = NULL otherwise
// return TRUE
void push( int );
// postcondition: the entered data value will be placed at the HEAD
// of the list, making it the TOP.
// The second parameter is for the output to the screen
int pop();
// postcondition: the data at the HEAD (top) will be returned, and the
// head will be deleted from the list.
private:
node *mystack;
int mytop, my_first_top;
};
//=============================================================================
//============================= Free functions
================================
//=============================================================================
void draw_stack( int );
// postcondition: draws stack using value from constructor as base value
void add( stacktype & );
// precondition: stack must have at least two elements.
// postcondition: top two elements of stack are popped, added then pushed
// back onto the stack
// result will be displayed in output screen
void subt( stacktype & );
// precondition: stack must have at least two elements.
// postcondition: top two elements of stack are popped, first one
// subtracted from the second and the result puched
back
// onto the stack
// result will be displayed in output screen
void mult( stacktype & );
// precondition: stack must have at least two elements.
// postcondition: top two elements of stack are popped, multiplied
// together, and the product pushed back onto the stack
// result will be displayed in output screen
void display( stacktype );
// postcondition: the top of the stack will be displayed in the output
// screen. This wil be performed after each keystroke.
:::::::This was part of what I wrote:
//=============================================================================
//============================== Implementation
===============================
//=============================================================================
stacktype::stacktype( int )
{
mystack = NULL;
mytop = 0;
my_first_top = 0;
}
void stacktype::initialize()
{
mystack = NULL;
mytop = 0;
my_first_top = 0;
clrscr();
//then it just displays a pretty picture
int h, k;
gotoxy(3,2);
cout<< "> ";
for(k=2; k<=22; k++)
{
gotoxy(60,k);
cout<< "||";
gotoxy(70,k);
cout<< "||";
}
for(h=60; h<=71; h++)
{
gotoxy(h,21);
cout<< "-";
gotoxy(h,22);
cout<< "-";
}
gotoxy(59,22);
cout<< "-";
gotoxy(72,22);
cout<< "-";
for(k=3; k<=20; k++)
{
for(h=62; h<=69; h++)
{
gotoxy(h,k);
cout<< " ";
}
}
gotoxy(50,10);
cout<< "OUTPUT:";
for(h=50; h<=56; h++)
{
gotoxy(h,11);
cout<< "-";
gotoxy(h,14);
cout<< "-";
}
for(k=12; k<=13; k++)
{
gotoxy(49,k);
cout<< "|";
gotoxy(57,k);
cout<< "|";
}
}
bool stacktype::is_empty()
{
return (mystack == NULL);
}
void stacktype::push( int x )
{
mytop++;
node *mm = new node;
mm -> data = x;
mm -> next = mystack;
mystack = mm;
gotoxy(62,21-mytop);
cout<< x;
gotoxy(50,13);
cout<< x << " ";
}
int stacktype::pop()
{
gotoxy(62,20-mytop);
cout<< " ";
if( !is_empty() )
{
int x = mystack -> data;
mytop--;
node *j;
j = mystack;
mystack = mystack -> next;
delete *j;
gotoxy(50,13);
cout<< x << " ";
return x;
}
}
my pop is wrong because i used nodes, but i don't know how to avoid the
error, and how would you declare a stacktype using this header? thank you
very much for your help :)
_________________________________________________________________
Join the worlds largest e-mail service with MSN Hotmail.
http://www.hotmail.com
|