黄色三级不卡在线观看-日本免费视频中文字幕-久久精品一区二区三区91-丝袜视频日本成人午夜视频-少妇的玉足让我爽翻天-亚洲av中文字字幕乱码综合-在线免费亚洲精品视频-九九精品在线一区区-欧美日韩一区二区三区内射

學(xué)歷改變命運(yùn)
24小時(shí)客服:4008135555/010-82335555
當(dāng)前位置:首頁(yè) > 筆記串講 > 面向?qū)ο笊蠙C(jī)考試題—關(guān)于隊(duì)列

面向?qū)ο笊蠙C(jī)考試題—關(guān)于隊(duì)列

2006年06月08日    來(lái)源:北京自考熱線   字體:   打印

  請(qǐng)實(shí)現(xiàn)一個(gè)隊(duì)列,既可以存放整數(shù),又可以存放字符串。簡(jiǎn)單的說(shuō),隊(duì)列是一種數(shù)據(jù)結(jié)構(gòu),按照先進(jìn)先出的順序管理進(jìn)、出隊(duì)列的元素。本題要求完成:

  (1) 實(shí)現(xiàn)描述隊(duì)列的類Queue,其中定義了隊(duì)列的大小Size(即隊(duì)列中可以存放的元素個(gè)數(shù)),并包括進(jìn)隊(duì)列函數(shù)Add,出隊(duì)列函數(shù)Delete、顯示隊(duì)列頭部元素的函數(shù)Head和顯示隊(duì)列尾部元素的函數(shù)Tail.

  (2) 定義基類Element,至少包含純虛函數(shù)ShowMe.

  (3) 從基類Element中派生整數(shù)類MyInteger和字符串類MyString, 具體實(shí)現(xiàn)上述純虛函數(shù)ShowMe,顯示該元素的類型和相應(yīng)的值。

 ?。?) 重載輸入“>>”*作符,使得可以通過(guò)cin直接讀入上述整數(shù)類和字符串類的對(duì)象值。

  (5) 編寫main函數(shù),測(cè)試上述所要求的各種功能,即可以根據(jù)菜單命令增加隊(duì)列元素、刪除隊(duì)列元素、顯示隊(duì)列頭部元素和隊(duì)列尾部元素,其中的元素可以是整數(shù)和/或字符串。

  提示:

  虛擬基類Element的定義至少包括以下純虛函數(shù)ShowMe.

class Element
{
 // …… 
public:
  virtual void ShowMe () = 0;
 // …… 
};
*/

#include "stdafx.h"
#include "iostream.h"
#include "string.h"
const max=1000;
#define NULL 0
class Element
{ public:
  virtual void ShowMe () = 0;
};
class MyInteger:public Element
{ int a;
public:
 MyInteger(){a=0;}
 friend istream &operator>>(istream &is, MyInteger &MyI);
 int Get() {return a;};
 void ShowMe()
 { cout<<"整數(shù):"<<a<<endl; }
};
istream &operator>>(istream &is, MyInteger &MyI)
{
 cout<<" 請(qǐng)輸入整數(shù):";
 is>>MyI.a;
 return is; }
class MyString:public Element
{ char s[100];
public:
 friend istream &operator>>(istream &is, MyString &MyS);
 void ShowMe()
 { cout<<"字符串:"<<s<<endl; }
};
istream &operator>>(istream &is, MyString &MyS)
{
 cout<<" 請(qǐng)輸入字符串:";
 is>>MyS.s;
 return is;
}
class Queue
{ int size;
 Element *Elm[max];
 MyInteger MyI[max];
 MyString MyS[max];
public:
 Queue(){
  for (int i=0; i<max; i++)
   Elm[i]=NULL;
  size=-1;
 }
 void add(MyInteger &My)
 {
  if (full()) cout<<"隊(duì)列已滿"<<endl;
  else {
   MyI[++size]=My;
   Elm[size]=new MyInteger;
   Elm[size]=&MyI[size];
  }
 }
 void add(MyString &My)
 {
  if (full()) cout<<"隊(duì)列已滿"<<endl;
  else {
   MyS[++size]=My;
   Elm[size]=new MyString;
   Elm[size]=&MyS[size];
  }
 }
  
 void tail()
 { if(empty()) cout<<"隊(duì)列為空"<<endl; 
  else{
   cout<<" 隊(duì)列的尾元素為";
   Elm[size]->ShowMe();
  }
 }
 void head()
 { 
  if(empty()) cout<<"隊(duì)列為空"<<endl;
  else{
   cout<<" 隊(duì)列的頭元素為";
   Elm[0]->ShowMe();
  }
 }
 void del()
 {
  if(empty()) cout<<"隊(duì)列為空"<<endl; 
  else{
   cout<<" 出隊(duì)列的元素為";
   Elm[size--]->ShowMe(); 
  }
 }
 bool empty()
 
 {
  return (bool)(size==-1); 
 }
 bool full()
 {
  return (bool)(size==max-1);
 }
};
void main()
{ MyInteger my1;
 MyString my2;
 Queue queue;
 int s=1;
 while(s)
 {
  cout<<"Please select 1-6 "<<endl;
  cout<<" 1: 整數(shù)進(jìn)隊(duì)列;"<<endl;
  cout<<" 2: 字符串進(jìn)隊(duì)列;"<<endl;
  cout<<" 3: 顯示隊(duì)列頭元素;"<<endl;
  cout<<" 4: 顯示隊(duì)列尾元素"<<endl;
  cout<<" 5: 出隊(duì)列;"<<endl;
  cout<<" 6: 退出程序"<<endl;
  cout<<"--------------------------------------"<<endl;
  cout<<"請(qǐng)選擇您的*作:";
  cin>>s;
  switch(s)
  {
   case 1: cin>>my1; queue.add(my1); break;
   case 2: cin>>my2; queue.add(my2); break;
   case 3: queue.head(); break;
   case 4: queue.tail(); break;
   case 5: queue.del(); break;
   default: s=0; break;
  }
 }
}