Programming Projects 3.4

来源:互联网 发布:Javascript alter 编辑:程序博客网 时间:2024/05/18 00:58


"queue.h"

 

#pragma once
const int maxqueue=10;
typedef int queue_entry;
enum Error_code{success,overflow,underflow};
class queue
{
public:
 queue(void);
 bool empty() const;
 Error_code serve();
 Error_code append(const queue_entry &item);
 Error_code retrieve(queue_entry &item)const;
protected:
 int count;
 int front,rear;
 queue_entry entry[maxqueue]; 
};


 


"queue.cpp"

 

#include "queue.h"


queue::queue(void)
{
 count = 0;
 rear = maxqueue-1;
 front=0;
}


bool queue::empty() const{
 return count==0;
 }


Error_code queue::append(const queue_entry &item){
  if(count>=maxqueue)
   return overflow;
  count++;
  rear=((rear+1)==maxqueue)?0:(rear+1);
  entry[rear]=item;
  return success;}


Error_code queue::serve(){
  if(count<=0)
   return underflow;
  count--;
  front=((front+1)==maxqueue)?0:(front+1);
  return success;}


Error_code queue::retrieve(queue_entry &item)const{
  if(count<=0)
   return underflow;
  item=entry[front];
  return success;}

 

 

 

"Extended_queue.h"

 

#pragma once
#include "queue.h"
class Extended_queue :
 public queue
{
public:
 int size()const;
 void clear();
 void print();

};

 

 

"Extended_queue.cpp"

 

#include<iostream>
using namespace std;
#include "Extended_queue.h"


int Extended_queue::size()const{
 cout<<"the size is:"<<count<<endl;
 return 0;}

 

void  Extended_queue::clear(){
    count=0;
    rear=front;
 cout<<"the queue is clear"<<endl;
}


void Extended_queue::print(){
 int i;
 {
  if(front<rear)
   for(i=0;i<=rear&&i>=front;i++)
    cout<<entry[i]<<endl;
  else
   for(i=0;i!=(i>=rear&&i<=front);i++)
    cout<<entry[i]<<endl;
  };
}

 

 

"test.cpp"

 

#include<iostream>
#include "queue.h"
#include "Extended_queue.h"
using namespace std;
void help();
void introduction();
char get_command();
bool do_command(char c,Extended_queue &test_queue);
int main(){
 Extended_queue test_queue;
 introduction();
 while(do_command(get_command(),test_queue));}


void help(){
 cout<<endl
 <<"a - Append the next input character to the extended queue"<<endl
 <<"s - Serve the front of the extended queue"<<endl
 <<"r - Retrieve and print the front entry"<<endl
 <<"# - The current size of the extended queue"<<endl
 <<"c - Clear the extended queue (same as delete)"<<endl
 <<"p - Print the extended queue"<<endl
 <<"h - This help screen"<<endl
 <<"q - Quit"<<endl

 <<"Press <Enter> to continue"<<endl<<flush;
 
 
 char c;
 do{
  cin.get(c);
 }while (c!='\n');}

 

void introduction(){
 cout<<"press <h> for help"<<endl;

}

char get_command(){
 char c;
 cout<<"select command and press <enter>"<<endl;
 cin>>c;
 if(c=='a'||c=='s'||c=='r'||c=='#'||c=='c'||c=='p'||c=='h'||c=='q')
  return c;
 else
  cout<<"please enter a valid command or <h> for help"<<endl;
}

bool do_command(char c,Extended_queue &test_queue){
 bool continue_input=true;
 queue_entry x;
 switch(c){
 case'a':
  cout<<"enter new data to insert"<<endl;
  cin>>x;
  if(test_queue.append(x)==overflow)
   cout<<"queue is full"<<endl;
  break;

 case's':
  if(test_queue.serve()==underflow)
   cout<<"queue is empty"<<endl;
  break;

 case'r':
  if(test_queue.retrieve(x)==underflow)
   cout<<"queue is empty"<<endl;
  else
   cout<<endl
   <<"the first entry is:"<<x
   <<endl;
  break;

 case'#':
  test_queue.size();
  break;

 case'c':
  test_queue.clear();
  break;

 case'p':
  test_queue.print();
  break;

 case'h':
  help();
  break;

 case'q':
  cout<<"extended queue demonstration finished."<<endl;
  continue_input=false;
  break;


 }
 return continue_input;
}

 

 

 

 

原创粉丝点击