}
四、编程题
1、在三角形类triangle实现两个函数,功能是输入三个顶点坐标判断是否构成三角形。
要求使用下方point类来存储三个顶点坐标。
classpoint{
private:
floatx,y;
public:
point(floata,floatb){x=a;y=b;}
point(){x=0;y=0;},
voidset(floata,floatb){x=a;y=b;}
floatgetx(){returnx;}
floatgety(){returny;}
};
(判断三条边能构成三角形的条件:
任意两边之和大于第三边或任意两边之差小于第3边。
)
答案:
#include
#include
#include“point.h”
classtriangle{
private:
pointx,y,z;
floats1,s2,s3;
public:
triangle(floatx1,floaty1,floatx2,floaty2,floatx3,floaty3);//用于输入三个顶点坐标
booltest();//用于判断是否构成三角形
};
triangle:
:
triangle(floatx1,floaty1,floatx2,floaty2,floatx3,floaty3)
{
x.set(x1,y1);
y.set(x2,y2);
z.set(x3,y3);
}
voidtriangle:
:
test(){
s1=sqrt((x.getx()-y.getx())*(x.getx()-y.getx())+(x.gety()-y.gety())*(x.gety()-y.gety()));
s2=sqrt((x.getx()-z.getx())*(x.getx()-z.getx())+(x.gety()-z.gety())*(x.gety()-z.gety()));
s3=sqrt((y.getx()-z.getx())*(y.getx()-z.getx())+(y.gety()-z.gety())*(y.gety()-z.gety()));
if(((s1+s2>s3)&&(s1+s3>s2)&&(s2+s3>s1))||
((abs(s1-s2)cout<<“三个顶点能构成三角形”;
returntrue;
}
else{
cout<<“三个顶点坐标不能构成三角形”;
returnfalse;
}
2、若链表结点结构如下:
structnode
{
intnum;
structnode*next;
}
请你完成链表逆置函数structnode*reverse(structnode*head)。
答案:
structnode*reverse(structnode*head){//head链表头结点
structnode*p,*temp1,*temp2;
if(head==NULL||head->next==NULL)returnhead;
p=head->next;
head->next=NULL;
while(p!
=NULL)
{
temp1=head;
head=p;
temp2=p;
p=p->next;
temp2->next=temp1;
}
returnhead;
}
3、定义并实现一个字符栈类Stack,数据成员包括一个存放字符的数组stck[]和一个栈指针tos,栈数组的大小由常量SIZE确定。
要求:
1)实现栈的基本操作Push()和Pop(),注意判断栈满和栈空的情况
2)实现c_count()方法计算字符ch出现的次数,如果ch不出现,则返回0
3)实现inverse()方法使数组元素按逆序排列
答案:
constintSIZE=100;
classStack
{
private:
charstck[SIZE];
inttos;
public:
Stack():
tos(0){};
voidPush(charch);
charPop();
intc_count(charch);
voidinverse();
};
voidStack:
:
Push(charch)
{
if(tos==SIZE)
cout<<”\nStackisfull\n”;
else{
stck[tos]=ch;
tos++;
}
}
charStack:
:
Pop(){
if(tos==0){
cout<<”\nStackisempty\n”;
return0;
}
tos--;
returnstck[tos];
}
intStack:
:
c_count(charch){
inttemp=tos;
intcount=0;
while(temp>0){
temp--;
if(stck[temp]==ch)
count++;
}
returncount;
}
voidStack:
:
inverse(){
char*p=newchar[tos];
for(inti=0;i<=tos-1;i++)
p[i]=a[i];
for(inti=0;i<=tos-1;i++)
a[i]=a[tos-i-1];
delete[]p;
}
4、小明开了一家面馆,菜单包括:
牛肉面、羊肉面、臊子面、西红柿鸡蛋面等。
请你设计服务员类、厨房类、面条类、牛肉面、羊肉面等,模拟点单制作面条的全过程。
因为菜单上面条种类一直在变化更改,因此要求使用设计模式——工厂模式,来减少后期代码修改的工作量。
要求:
1)要求使用继承实现
2)要求使用工厂设计模式
3)编写main()方法进行测试
答案:
#pragmaonce
#include
usingnamespacestd;
classNoodle
{
private:
stringn_type;
public:
Noodle(string_n_type):
n_type(_n_type){};
virtual~Noodle();
virtualvoidmake();
virtualvoidcooking();
virtualvoidsauce();
};
#include"Noodle.h"
Noodle:
:
~Noodle()
{
}
voidNoodle:
:
make()
{
cout<}
voidNoodle:
:
cooking()
{
cout<}
voidNoodle:
:
sauce()
{
cout<}
#pragmaonce
#include"Noodle.h"
classBeefNoodle:
publicNoodle
{
public:
BeefNoodle():
Noodle("牛肉面"){};
~BeefNoodle();
};
#include"BeefNoodle.h"
BeefNoodle:
:
~BeefNoodle()
{
}
#pragmaonce
#include"Noodle.h"
classMuttonNoodle:
publicNoodle
{
public:
MuttonNoodle():
Noodle("羊肉面"){};
~MuttonNoodle();
};
#include"pch.h"
#include"MuttonNoodle.h"
MuttonNoodle:
:
~MuttonNoodle()
{
}
#pragmaonce
#include"Noodle.h"
classSaoziNoodle:
publicNoodle
{
public:
SaoziNoodle():
Noodle("臊子面"){};
~SaoziNoodle();
};
#include"SaoziNoodle.h"
SaoziNoodle:
:
~SaoziNoodle()
{
}
#pragmaonce
#include"Noodle.h"
classTomatoandEggNoodle:
publicNoodle
{
public:
TomatoandEggNoodle():
Noodle("西红柿鸡蛋面"){};
~TomatoandEggNoodle();
};
#include"TomatoandEggNoodle.h"
TomatoandEggNoodle:
:
~TomatoandEggNoodle()
{
}
#pragmaonce
#include"Noodle.h"
classCook
{
public:
Cook();
~Cook();
Noodle*createNoodles(conststd:
:
string&type);
};
#include"Cook.h"
#include"BeefNoodle.h"
#include"MuttonNoodle.h"
#include"SaoziNoodle.h"
#include"TomatoandEggNoodle.h"
Cook:
:
Cook()
{
}
Cook:
:
~Cook()
{
}
Noodle*Cook:
:
createNoodles(conststd:
:
string&type)
{
if(type=="牛肉面"){
returnnewBeefNoodle();
}
elseif(type=="羊肉面"){
returnnewMuttonNoodle();
}
elseif(type=="臊子面"){
returnnewSaoziNoodle();
}
elseif(type=="西红柿鸡蛋面"){
returnnewTomatoandEggNoodle();
}
else{
returnNULL;
}
}
#pragmaonce
#include"Cook.h"
#include"Noodle.h"
classWaiter
{
private:
Cook&cook;
public:
Waiter(Cook&_cook):
cook(_cook){};
~Waiter();
Noodle*orderNoodles(conststd:
:
string&type);
};
#include"pch.h"
#include"Waiter.h"
Waiter:
:
~Waiter()
{
}
Noodle*Waiter:
:
orderNoodles(conststd:
:
string&type)
{
Noodle*noodle=cook.createNoodles(type);
if(noodle){
noodle->make();
noodle->cooking();
noodle->sauce();
}
returnnoodle;
}
#include
#include"Waiter.h"
#include"Cook.h"
intmain(){
Cookcook;
Waiterwaiter(cook);
Noodle*noodle1=waiter.orderNoodles("牛肉面");
if(noodle1){
deletenoodle1;
}
Noodle*noodle2=waiter.orderNoodles("羊肉面");
if(noodle2){
deletenoodle2;
}
Noodle*noodle3=waiter.orderNoodles("臊子面");
if(noodle3){
deletenoodle3;
}
Noodle*noodle4=waiter.orderNoodles("西红柿鸡蛋面");
if(noodle4){
deletenoodle4;
}
}