|
From: senem b. <sbe...@ya...> - 2002-10-27 16:27:32
|
thanks a lot, i'll try it in a few minutes...
got one more question to ask:
i wrote a program using a few stacks, i compiled it and it seems to be working... however, it doesn't do what i intend to do... in the program, there are two stacks called garage1 and garage2 and there's another stack called temporary which i use to hold the popped elements values. but i strangely can't pop the value i'm looking for, only the last element is popped... the code is huge,i'll mark the part which doesn't work and i'll be glad if anyone would look at it and tell me where i'm going wrong...
kind regards...
senem
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
#define maximum 1
typedef struct garage{
int elements;
int top;
int array[MAX];
}garage;
typedef struct temp_storage{
int tempelements;
int temptop;
int temparray[maximum]; // has to be replaced with a string array
}temp_storage;
garage garage1,garage2,temporary;// temporary to store while
temp_storage garage3; // removing a car from the stacks
// 1 and 2
int addcar(int &,int &); // i don't know if it makes any sense
int removecar(int,int &); // but i have to use these 2 functions
// the teacher said
void create(garage &);
void push (int,garage&);
int pop(garage&);
int isfull(garage&);
int isempty(garage&);
void display(garage &);
void temp_create(temp_storage&);
void temp_push(int,temp_storage&);
int temp_pop(temp_storage&);
int temp_empty(temp_storage&);
int temp_full(temp_storage&);
void temp_display(temp_storage&);
int findel(garage &,int); file://reference??
void error(char *);
int temp_findel(temp_storage&,int); file://reference??
int main(){
char answer;
int garage_no=0;
int num;
create(garage1);
create(garage2);
create(temporary);
temp_create(garage3);
while(true){
cout<<"park or remove the car(p/r)";
cin>>answer;
if(answer=='p'){
cout<<"plaka no:";
cin>>num;
if(addcar(garage_no,num)){
cout<<"successfully placed"<<endl;
cout<<"car parked in the "<<garage_no
<<"th garage"<<endl;
}
else
cout<<"car not placed"<<endl;
display(garage1);
display(garage2);
temp_display(garage3);
}
else if(answer=='r'){
cout<<"number:";
cin>>num;
cout<<"garage no:";
cin>>garage_no;
if(removecar(garage_no,num))
cout<<"car found and successfully delivered"<<endl;
else
cout<<"car not found"<<endl;
display(garage1);
display(garage2);
temp_display(garage3);
}
else
error("check the entered letter");
cout<<"continue(y/n)??";
cin>>answer;
if(answer=='n')
break;
}
return 0;
}
void create(garage &line){
for(int i=0;i<MAX;i++)
line.array[i]=0;
line.elements=0;
line.top=0;
}
void temp_create(temp_storage &line){
for(int j=0;j<maximum;j++)
line.temparray[j]=0;
line.tempelements=0;
line.temptop=0;
}
void display(garage &line){
for(int k=0;k<line.top;k++)
cout<<line.array[k]<<" ";
cout<<endl;
}
void temp_display(temp_storage &line){
for(int a=0;a<line.temptop;a++)
cout<<line.temparray[a]<<" ";
cout<<endl;
}
int isfull(garage &line){
return(line.elements==MAX);
}
int temp_isfull(temp_storage &line){
return(line.tempelements==maximum);
}
int isempty(garage & line){
return(line.elements==0);
}
int temp_isempty(temp_storage &line){
return(line.tempelements==0);
}
void push(int element,garage& line){
if(!isfull(line)){
line.array[line.top]=element;
line.top++;
line.elements++;
}
else{
cout<<"error,cannot push,stack is full"<<endl;
exit(1);
}
}
void temp_push(int element,temp_storage& line){
if(!temp_isfull(line)){
line.temparray[line.temptop]=element;
line.temptop++;
line.tempelements++;
}
else{
cout<<"error,cannot push,stack is full"<<endl;
exit(1);
}
}
int pop(garage &line){
int r;
if(isempty(line)){
cout<<"error,cannot pop,stack is empty"<<endl;
exit(1);
}
else{
line.top--;
r=line.array[line.top];
line.elements--;
return r;
}
}
int temp_pop(temp_storage &line){
int result;
if(temp_isempty(line)){
cout<<"error,cannot pop,stack is empty"<<endl;
exit(1);
}
else{
line.temptop--;
result=line.temparray[line.temptop];
line.tempelements--;
return result;
}
}
int addcar(int &garagenum,int &carnum){
if(garage1.elements<MAX){
garagenum=1;
push(carnum,garage1);
return 1;
}
else if(garage2.elements<MAX){
cout<<"garage 1 is full"<<endl;
garagenum=2;
push(carnum,garage2);
return 1;
}
else if(garage3.tempelements<maximum){
cout<<"garage 1 and garage 2 are full"<<endl;
garagenum=3;
temp_push(carnum,garage3);
return 1;
}
else{
cout<<"all places reserved"<<endl;
return 0;
}
}
void error(char *sentence){
cout<<sentence<<endl;
}
/*int removecar(int garagenum,int &carnum){
int popped_num1,popped_num2,popped_num3;
int i=0;
if(garagenum==1){
while(garage1.array[i]!=carnum){
popped_num1=pop(garage1);
push(popped_num1,temporary);
i++;
}
popped_num1=pop(garage1);
while(!isempty(temporary)){
popped_num2=pop(temporary);
push(popped_num2,garage1);
}
popped_num3=temp_pop(garage3);
push(popped_num3,garage1);
return 1;
}
if(garagenum==2){
i=0;
while(garage2.array[i]!=carnum){
popped_num1=pop(garage2);
push(popped_num1,temporary);
i++;
}
popped_num1=pop(garage2);
while(!isempty(temporary)){
popped_num2=pop(temporary);
push(popped_num2,garage2);
}
popped_num3=temp_pop(garage3);
push(popped_num3,garage2);
return 1;
}
if(garagenum==3){
if(findel(garage1,carnum)){
i=0;
while(garage1.array[i]!=carnum){
popped_num1=pop(garage1);
push(popped_num1,temporary);
i++;
}
popped_num1=pop(garage1);
while(!isempty(temporary)){
popped_num2=pop(temporary);
push(popped_num2,garage1);
}
return 1;
}
if(findel(garage2,carnum)){
i=0;
while(garage2.array[i]!=carnum){
popped_num1=pop(garage2);
push(popped_num1,temporary);
i++;
}
popped_num1=pop(garage2);
while(!isempty(temporary)){
popped_num2=pop(temporary);
push(popped_num2,garage2);
}
popped_num3=temp_pop(garage3);
push(popped_num3,garage2);
return 1;
}
if(temp_findel(garage3,carnum)){
popped_num1=temp_pop(garage3);
return 1;
}
else
return 0;
}
}
*/
int findel(garage & line,int element){
for(int i=0;i<line.top;i++){
if(line.array[i]==element)
return 1;
else
return 0;
}
}
int temp_findel(temp_storage &line,int element){
for(int i=0;i<line.temptop;i++){
if(line.temparray[i]==element)
return 1;
else
return 0;
}
}
---------------------------------
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site |