面向?qū)ο笊蠙C(jī)考試題—關(guān)于隊(duì)列
請(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;
}
}
}
- 熱門課程
- 報(bào)名咨詢
- 2022年10月自考西方政治制度知識(shí)點(diǎn):憲政
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):唯心主義和存在的根源
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):馬克思主義哲學(xué)的產(chǎn)生是哲學(xué)發(fā)展中的偉大變革
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):唯物主義
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):哲學(xué)與科學(xué)的分化
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):馬克思主義哲學(xué)的歷史發(fā)展
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):馬克思主義哲學(xué)與中國(guó)的社會(huì)主義事業(yè)
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):對(duì)世界統(tǒng)一性的不同認(rèn)識(shí)
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):意識(shí)是物質(zhì)的產(chǎn)物
- 2022年10月自考馬克思主義哲學(xué)原理知識(shí)點(diǎn):意識(shí)的能動(dòng)作用



