题目
试题五(共15分)
阅读以下说明和C++代码,填充代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
下面的程序用来计算并寻找平面坐标系中给定点中最近的点对(若存在多对,则输出其中的一对即可)。程序运行时,先输入点的个数和一组互异的点的坐标,通过计算每对点之间的距离,从而确定出距离最近的点对。例如,在图5-1所示的8个点中,点(1,1)与(2,0.5)是间距最近的点对。
【C++代码】
include <iostream>
include <cmath>
using namespace std;
class GPoint {
private:
double x, y;
public:
void setX(double x) { this->x = x; }
void setY(double y) { this->y = y; }
double getX() { return this->x; }
double getY() { return this->y; }
};
class ComputeDistance {
public:
double distance(GPoint a,GPoint b) {
return sqrt《a.getX() - b.getX())*(a.getX() - b.getX())
+ (a.getY() - b.getY())*(a.getY() - b.getY()));
}
};
int main()
{
int i,j, numberOfPoints=0;
cout<<"输入点的个数:";
cin>>numberOfPoints;
(1) points= neW GPoint[numberOfPoints];//创建保存点坐标的数组
memset(points,0,sizeof(points));
cout <<"输入"<< numberOfPoints<<"个点的坐标:";
for(i=0;i<numberOfPoints; i++){
double tmpx, tmpy;
cin>>tmpx>>tmpy;
points[i].setX(tmpx);
points[i].setY(tmpy);
}
(2) computeDistance= new ComputeDistance();
int p1=0,p2=1;//p1和p2用于表示距离最近的点对在数组中的下标
double shortestDistance= computeDistance->distance(points[p1], points[p2]);
//计算每一对点之间的距离
for(i=0;i<numberOfPoints; i++){
for(j=i+1;j< (3) ;j++){
double tmpDistance=computeDistance-> (4) ;
if ((5) ) {
p1=i; p2 =j;
shortestDistance= tmpDistance;
}
}
}
cout<<"距离最近的点对是:(";
cout"points[p1].getX()<<","<<points[pl].getY()<<")和(";
cout<<points[p2].getX()<<","<<points[p2].getY()<<")"<<endl;
delete computeDistance;
return 0:
}
第1题
试题五(共 15分)
阅读以下说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:
void addElement(Object): 在列表尾部添加一个对象;
Object lastElement(): 返回列表尾部对象;
int numberOfElement(): 返回列表中对象个数;
void removeLastElement(): 删除列表尾部的对象。
现需要借助LinkedList来实现一个Stack栈类,C++代码1和C++代码2分别采用继承和组合的方式实现。
【C++代码 1】
class Stack :public LinkedList{
public:
void push(Object o){ addElement(o); }; //压栈
Object peek(){ return (1) ; }; //获取栈顶元素
bool isEmpty(){ //判断栈是否为空
return numberOfElement() == 0;
};
Object pop(){ //弹栈
Object o = lastElement();
(2) ;
return o;
};
};
【C++代码 2】
class Stack {
private:
(3) ;
public:
void push(Object o){ //压栈
list.addElement(o);
};
Object peek(){ //获取栈顶元素
return list. (4) ;
};
bool isEmpty(){ //判断栈是否为空
return list.numberOfElement() == 0;
};
Object pop(){//弹栈
Object o = list.lastElement();
list.removeLastElement();
return o;
};
};
【问题】
若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)
第2题
试题五(共15分)
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
现欲开发一个软件系统,要求能够同时支持多种不同的数据库,为此采用抽象工厂模式设计该系统。以SQL Server和Access两种数据库以及系统中的数据库表Department为例,其类图如图5-1所示。
【C++代码】
include <iostream>
using namespace std;
class Department{/*代码省略*/};
class IDepartment{
public:
(1) =0;
(2) =0;
};
class SqlserverDepartment:(3){
public:
void Insert(Department* department){
cout <<"Insert a record into Department in SQL Server!\n";
∥其余代码省略
}
Department GetDepartment(int id){
/*代码省略*/
}
};
class AccessDepartment: (4) {
public:
void Insert(Department* department){
cout <<"Insert a record into Department in ACCESS!\n";
∥其余代码省略
}
Department GetDepartment(int id){
/*代码省略*/
}
};
(5){
public:
(6)=0;
};
class SqlServerFactory:public IFactory{
public:
IDepartment*CreateDepartment(){ return new SqlserverDepartment(); }
∥其余代码省略
};
class AccessFactory:public IFactory{
public:
IDepartment* CreateDepartment(){ return new AccessDepartment();}
∥其余代码省略
};
第3题
试题五(共15分)
阅读以下说明和 C++代码,将应填入 (n) 处的语句或语句成分写在答题纸的对应栏内。
【说明】
某数据文件students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的数据全部读入对象数组,按分数从高到低进行排序后选出排名前 30%的学生。
【C++代码】
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
string sNO; //学号
int credit; //分数
public:
Student(string a,int b) { sNO = a; credit = b;}
Student(){}
int getCredit();
void out();
};
(1) ::getCredit() {
return credit;
}
(2) ::out() {
cout << "SNO: " << sNO << ", Credit=" << credit << endl;
}
class SortStudent {
public:
void sort(Student *s, int n);
SortStudent(){}
};
void SortStudent::sort(Student *s,int n) {
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(s[i]. (3) < s[j]. (4) ) {
Student temp = s[i]; s[i] = s[j]; s[j] = temp;
}
}
}
}
int main(int argc, char* argv[])
{
const int number = 100; //学生总数
ifstream students;
students.open("students.txt");
if(!students.is_open()) {
throw 0;
}
Student *testStudent = (5) [number];
int k = 0;
string s;
while (getline(students,s,'\n')) { //每次读取一个学生的学号和成绩
Student student(s.substr(0,s.find(',')), atoi(s.substr(s.find(',')+1).c_str()));
testStudent[k++] = student;
}
students.close();
(6) ;
ss.sort(testStudent,k);
cout <<"top 30%: "<<endl;
for(k = 0; k < number * 0.3; k++) {
testStudent[k].out();
}
delete []testStudent;
return 0;
}
第4题
试题五(共15分)
阅读下列说明和C++-代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。
【C++代码】
include <iostream>
using namespace std;
class invoice{
public:
(1) {
cout《 "This is the content of the invoice!"《 endl;
}
};
class Decorator : public invoice {
Invoice *ticket;
public:
Decorator(lnvoice *t) { ticket = t; }
void printinvoice(){
if(ticket != NULL)
(2);
}
};
class HeadDecorator : public Decorator{
public:
HeadDecorator(lnvoice*t): Decorator(t) { }
void printinvoice0 {
cout《 "This is the header of the invoice! "<< endl;
(3) ;
}
};
class FootDecorator : public Decorator{
public:
FootDecorator(invoice *t): Decorator(t) { }
void printlnvoice() {
(4) ;
cout《 "This is the footnote of the invoice!"《 endl;
}
};
int main(void) {
Invoice t;
FootDecorator f(&t);
HeadDecorator h(&f);
H.printlnvoice();
cout< < “_____”< < endl;
FootDecorator a(NULL);
HeadDecorator b((5) );
B.printinvoice();
return 0;
}
程序的输出结果为:
This is the header of the invoice!
This is the content of the invoice!
This is the footnote of the invoice!
----------------------------
This is the header of the invoice!
This is the footnote of the invoice!
第5题
试题五(共15分)
阅读以下说明,回答问题1至问题3,将解答填入答题纸的对应栏内。
【说明】
小明在一家超市工作,该超市将进行整体改造,小明负责信息系统软硬件升级,他制定的工作计划如表5.1所示。
每项任务的逻辑关系和部分时间信息如图5-1所示。
【问题1】 (10分)
请根据表5-1和图5-1,计算各项任务的最迟开始时间和最迟结束时间,填充图5-1中的空(1)~(10)。
第6题
试题五(共15分)
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某咖啡店当卖咖啡时,可以根据顾客的要求在其中加入各种配料,咖啡店会根据所加入的配料来计算费用。咖啡店所供应的咖啡及配料的种类和价格如下表所示。
【C++代码】
include <iostream>
include <string>
using namespace std;
const int ESPRESSO_PRICE = 25;
const int DRAKROAST_PRICE = 20;
const int MOCHA_PRICE = 10;
const int WHIP_PRICE = 8;
class Beverage { //饮料
(1) :string description;
public:
(2) (){ return description; }
(3) ;
};
class CondimentDecorator : public Beverage { //配料
protected:
(4) ;
};
class Espresso : public Beverage { //蒸馏咖啡
public:
Espresso () {description="Espresso"; }
int cost(){return ESPRESSO_PRICE; }
};
class DarkRoast : public Beverage { //深度烘焙咖啡
public:
DarkRoast(){ description = "DardRoast"; }
int cost(){ return DRAKROAST_PRICE; }
};
class Mocha : public CondimentDecorator { //摩卡
public:
Mocha(Beverage*beverage){ this->beverage=beverage; }
string getDescription(){ return beverage->getDescription()+",Mocha"; }
int cost(){ return MOCHA_PRICE+beverage->cost(); }
};
class Whip :public CondimentDecorator { //奶泡
public:
Whip(Beverage*beverage) { this->beverage=beverage; }
string getDescription() {return beverage->getDescription()+",Whip"; }
int cost() { return WHIP_PRICE+beverage->cost(); }
};
int main() {
Beverage* beverage = new DarkRoast();
beverage=new Mocha((5) );
beverage=new Whip((6) );
cout<<beverage->getDescription()<<"¥"<<beverage->cost() endl;
return 0;
}
编译运行上述程序,其输出结果为:
DarkRoast, Mocha, Whip ¥38
第7题
试题五(共15分)
阅读下列说明和C++代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
某大型商场内安装了多个简易的纸巾售卖机,自动出售2元钱一包的纸巾,且每次仅售出一包纸巾。纸巾售卖机的状态图如图5-1所示。
采用状态(State)模式来实现该纸巾售卖机,得到如图5-2所示的类图。其中类State为抽象类,定义了投币、退币、出纸巾等方法接口。类SoldState、SoldOutState、NoQuarterState和HasQuarterState分别对应图5-1中纸巾售卖机的4种状态:售出纸巾、纸巾售完、没有投币、有2元钱。
【C++代码】
include<iostream>
using namespace std;
∥以下为类的定义部分
class TissueMachine; //类的提前引用
class State{
public:
virtual void insertQuarter()=0; //投币
virtual void ejectQuarter()=0; //退币
virtual void turnCrank()=0; //按下“出纸巾”按钮
virtual void dispense()=0; //出纸巾
};
/*类SoldOutState. NoQuarterState. HasQuarterState. SoldState的定义省略,每个类中均定义了私有数据成员TissueMachine* tissueMachine;*/
class TissueMachine{
private:
(1) *soldOutState, *noQuarterState, *hasQuarterState,*soldState, *state;
int count, //纸巾数
public:
TissueMachine(int numbers);
void setState(State* state);
State* getHasQuarterState();
State* getNoQuarterState();
State* getSoldState();
State* getSoldOutState();
int getCount();
//其余代码省略
};
//以下为类的实现部分
void NoQuarterState::insertQuarter(){
tissueMachine->setState((2) );
}
void HasQuarterState::ejectQuarter(){
tissueMachine->setState((3) );
}
void SoldState::dispense(){
if(tissueMachine->getCount()>0){
tissueMachine->setState ((4) );
}
else{
tissueMachine->setState((5) );
}
}//其余代码省略
第8题
试题五(共15分)
阅读以下说明、图和C++代码,填补C++代码中的空缺(1)~(5),将解答写在答题纸的对应栏内。
【说明】
已知对某几何图形绘制工具进行类建模的结果如图5.1所示,其中Shape为抽象类(应至少包含一个纯虚拟(virtual)函数),表示通用图形,Box表示矩形,Ellipse表示椭圆,Circle表示圆(即特殊的椭圆),Line表示线条。
下面的C++代码用于实现图5-1所给出的设计思路,将其空缺处填充完整并编译运行,输
出结果为:
Ellipse
Circle
Ellipse
C
E
【C++代码】
include <string>
include <iostream>
using namespace std;
class Shape{
public:
Shape(const string& name){
m_name= name;
}
~Shape(){}
(1) void paint() = 0;
stringgetName()const {
retumm name;
}
Private:
string m_name;
};
//Box和 Line类的定义与 Ellipse类似,其代码略
classEllipse (2) {
public:
Ellipse(const string& name) : Shape(name){ cout<<"Ellipse" <<endl; }
Voidpaint() { cout<<getName()<<endl;}
};
classCircle (3) {
public:
Circle(const string& name) : Ellipse(name){ cout<<"Circl"<<endl; }
};
class Diagram {
public:
void drawAShap(Shape* shape){ shape->paint(); }
void drawShapes() {
shapes[0] = new Circle("C");
shapes[l] = new Ellipse("E");
for (int i=O;i<2; ++1) {
drawAShap(shapes[i]);
}
}
void close (){ /*删除形状,代码略 */ }
private:
Shape* shapes[2];
};
int main()
{
Diagram* diagram = (4)
diagram->drawShapes();
diagram->close ();
(5) diagram;
}
第9题
试题五(共15分)
阅读以下说明和C++代码,填补C++代码中的空缺(1)~(6),将解答写在答题纸的对应栏内。
【说明】
已知某公司按周给员工发放工资,其工资系统需记录每名员工的员工号、姓名、工资等信息。其中一些员工是正式的,按年薪分周发放(每年按52周计算);另一些员工是计时工,以小时工资为基准,按每周工作小时数核算发放。
下面是实现该工资系统的C++代码,其中定义了四个类:工资系统类PayRoll,员工类Employee,正式工类Salaried和计时工类Hourly,Salaried和Hourly是Employee的子类。
【C++代码】
//头文件和域名空间略
const int EMPLOYEE_NUM=5;
class Employee{
protected:
int empCode; ∥员工号
string name; ∥员工姓名
double salary; ∥周发放工资
public:
Employee(const int empCode, const string &name){
this->empCode= empCode; this->name= name;
}
virtual~Employee(){}
virtual void pay()=0;
double getSalary(){ return this->salary; }
};
class Salaried (1){
private: double payRate; //年薪
public:
Salaried(const int empCode,const string &name,double payRate)
:Employee(empCode,name){
this->payRate= payRate;
}
void pay(){
this->salary=(2) ;//计算正式员工的周发放工资数
cout<<this->name<<":"<<this->salary<<endl;
}
};
class Hourly (3) {
private:
double payRate; //小时工资数
int hours; //周工作小时数
public:
Hourly(const int empCode, const string &name, int hours, double payRate)
:Employee(empCode,name){
this->payRate= payRate; this->hours= hours,
}
void pay(){
this->salary= (4) ;//计算计时工的周发放工资数
cout<<this->name<<":"<<this->salary<<endl;
}
};
class PayRoll{
public:
void pay(Employee* e[]){
for (int i=0;i<EMPLOYEE_ NUM; i++){
e[i]->pay();
}
}
};
int main(){
PayRoll* payRoll= new PayRoll;
(5)employees[EMPLOYEE_ NUM]={
new Salaried(l00l,"Zhang San", 58000.00),
//此处省略对其他职工对象的生成
new Hourly(1005,"L1", 12, 50.00),
};
payRoll->pay ((6) );
double total= 0.0;
for (int i=0;i< EMPLOYEE_NUM; i++)
{ total+=employees[il->getSalary(); } //统计周发放工资总额
cout<<"总发放额="<<total<<endl;
delete payRoll; retum 0;
}
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!