实验六 对象数组
1 实验目的
(1)学习如何使用数组名作为函数的参数,通过传数组的方式,在被调用函数中访问主调函数中的多个对象的方法。
(2)学习如何在类中内嵌对象数组成员变量。
(3)学习对象数组的初始化。
2 实验内容
2.1 根据题目要求编写模拟的程序
(1)五一小假期,许多人选择外出旅游。每位乘客(Passenger)最多可以携带3件行李(Luggage)。并且乘客可以选择个人自由行;或者参加旅行社(TravelAgency)组织的旅行团。
(2)自由行的目的地、旅行团的目的地都相同,都是3个,分别是国内短途(DomesticShort)、国内长途(DomesticLong)和国际游(International)。用枚举类型表示。
(3)旅行社总共安排了5个旅行团(Team),旅行团的目的地类型不限。每个团最多允许6位游客报名。超过6位,就不再加入新的游客了。
(4)自由行的乘客需要到售票处(TicketOffice)亲自买票(Ticket);参加旅行团的乘客,由旅行团到售票处团购。国内短途票10元/位,国内长途100元/位,国际票500元/位。购买国际票时,每位乘客可以免费托运2件行李。超过2件,每件行李多收100元托运费。
(5)模拟乘客到旅行社报名,自由行乘客到售票处买票,以及旅行社统一到售票处团购票的过程。
3 题目要求
(1)创建新的工程项目HolidayTravel;
(2)主程序对应的源文件名为main.cpp,里面包含main()函数。
(3)乘客名字的合法命名规则是“乘客A”,……,“乘客Z”;“乘客a”,……,“乘客z”;“乘客0”,……,“乘客9”。最多可以自动生成62位乘客。
4 编写程序并测试
可以自由定义类的成员,可以自由扩展程序,只要实现题目要求即可。
5 程序执行完毕后,命令行窗口显示的结果可能是:
我是乘客A,选择自由出行,国内长途,携带2件行李,行李编号是A01、A02,买票需付100元。
我是乘客B,报名旅行社,国内短途,携带3件行李,行李编号是B01、B02、B03,买票需付10元。
我是乘客C,报名旅行社,国际游,携带3件行李,行李编号是C01、C02、C03,买票需付500元,行李托运费100元。
……
……
旅行社开始报名。
旅行社共计安排了5个旅行团,共计××人报名。
出行的目的地分别是:…,…,…,…,…;每个旅行团的人数分别是:…,…,…,…,…;每个旅行团需要支付的票价是:…,…,…,…,…。
售票处开始售票。
售票结束,营业额统计信息如下:
国内短途自由行:……元,乘客姓名:………………
国内长途自由行:……元,乘客姓名:………………
国际游自由行: ……元,乘客姓名:………………
国内短途团购: ……元,乘客姓名:………………
国内长途团购: ……元;乘客姓名:………………
国际游团购: ……元;乘客姓名:………………
提示:采用下面的方法产生随机数,并根据随机数的不同,生成不同类型的乘客(出行的目的地、携带的行李、个人自由行,或跟随旅行社出游)。
#include // For time()
#include // For srand() and rand()
const int MAX_TYPE = ***;
srand(static_cast (time(NULL))); // 初始化随机数发生器
int number = rand();
int remainder = number % MAX_TYPE;
6 代码
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <map>
using namespace std;
class Luggage;
class Passenger;
class TravelAgency;
class TicketOffice;
class Team;
enum Destination {
DomesticShort, DomesticLong, International
};
enum Ways {
self, agency
};
string Destination_name[3] = {"国内短途", "国内长途", "国际游"};
string price_name[2][3] = {"国内短途自由行", "国内长途自由行", "国际游自由行", "国内短途团 购", "国内长途团购", "国际游团购"};
class Luggage {
public:
Luggage(string name) : name(name) {}
Luggage() {};
void setName(string code) { this->name = name; }
string getName() { return name; }
private:
string name;
};
class Ticket {
public:
Ticket() {
ticket_types.insert(make_pair(DomesticShort, 10));
ticket_types.insert(make_pair(DomesticLong, 100));
ticket_types.insert(make_pair(International, 500));
}
int get_price(Destination d) { return ticket_types[d]; }
private:
map<Destination, int> ticket_types;
};
class TicketOffice {
public:
TicketOffice() {
for (int i = 0; i <= 1; i++) {
for (
int j = 0;
j <= 2; j++) {
price[i][j] = 0;
}
}
};
int buyTicket(Destination destination, int tpye, string newname) {
int d = destination;
name[tpye][d].push_back(newname);
price[tpye][d] += ticket.get_price(destination);
return ticket.get_price(destination);
}
void sp() {
cout << "售票处开始售票。\n售票结束,营业额统计信息如下:\n";
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 2; j++) {
cout << price_name[i][j] << ":\t";
cout << price[i][j] << "元,乘客姓名: ";
for (int k = 0; k < name[i][j].size(); k++) { cout << name[i][j][k] << " "; }
cout << endl;
}
}
}
private :
int price[2][3];
vector<string> name[2][3];
Ticket ticket;
};
class Team {
public:
Team(Destination destination, string name, TicketOffice &ticketOffice) : destination(destination), all_price(0),
peo_num(1) {
all_price += ticketOffice.buyTicket(destination, 1, name);
};
int getPeo() { return peo_num; }
Destination getDestination() { return destination; }
int getPrice() { return all_price; }
int addpeople(string name, TicketOffice &ticketOffice) {
if (peo_num <= 6) {
all_price += ticketOffice.buyTicket(destination, 1, name);
peo_num++;
return 1;
} else { return 0; }
}
private:
int peo_num;
Destination destination;
int all_price;
};
class TravelAgency {
public:
TravelAgency() : team_num(0) {};
void add_people(Destination newDestination, string name, TicketOffice &ticketOffice) {
for (int i = 1; i <= team_num; i++) {
if (team[i]->getPeo() == 6) continue;
if (team[i]->getDestination() == newDestination) {
team[i]->addpeople(name, ticketOffice);
return;
}
}
if (team_num < 5) {
team_num++;
team[team_num] = new Team(newDestination, name, ticketOffice);
}
}
void baoming() {
int num = 0;
cout << "旅行社开始报名。\n";
cout << "旅行社共计安排了 " << team_num << "个旅行团,共计";
for (int i = 1; i <= team_num; i++) { num += team[i]->getPeo(); }
cout << num << "人报名。\n出行的目的地分别是:";
for (int i = 1; i <= team_num; i++) {
cout << Destination_name[team[i]->getDestination()];
cout << ",";
}
cout << "每个旅行团需要支付的票价是:";
for (int i = 1; i <= team_num; i++) {
cout << team[i]->getPrice();
cout << ",";
}
cout << endl;
}
private:
Team *team[6];
int team_num;
};
class Passenger {
public:
Passenger(string name, int des, int wy) : luggage_num(0), name(name) {
switch (des) {
case 0:
destination = DomesticShort;
break;
case 1:
destination = DomesticLong;
break;
case 2:
destination = International;
break;
}
switch (wy) {
case 0:
way = self;
break;
case 1:
way = agency;
break;
}
}
void addLuggages(Luggage &lg) { if (luggage_num <= 2) { luggages[++luggage_num] = lg; }};
Destination getDestination() { return destination; }
Ways getWays() { return way; }
int getLuggageNum() { return luggage_num; }
string getName() { return name; }
void printAndBuy(TravelAgency &travelAgency, TicketOffice &ticketOffice) {
cout << "我是" << name << ",";
if (way == self) {
ticketOffice.buyTicket(destination, 0, name);
cout << "选择自由出行,";
} else {
cout << "报名旅行社,";
travelAgency.add_people(destination, name, ticketOffice);
}
cout << Destination_name[destination] << ",";
cout << "携带 " << luggage_num << " 件行李,行李编号是:";
for (int i = 1; i <= luggage_num; i++) { cout << luggages[luggage_num].getName() << "、"; }
cout << "买票需付" << ticket.get_price(destination) << "元";
if (luggage_num == 3 && destination == 2) { cout << ",行李托运费100元。"; } else { cout << "。"; }
cout << endl;
}
private:
int luggage_num;
Luggage luggages[4];
string name;
Destination destination;
Ways way;
Ticket ticket;
};
int main() {
TravelAgency travelAgency;
TicketOffice ticketOffice;
srand(static_cast<unsigned> (time(NULL)));
int number = rand();
int peoNum = number % 62;
for (int i = 0; i <= peoNum; i++) {
string nm;
char a;
if (i <= 25) { a = (i + 'A'); } else if (i <= 51) { a = (i - 26 + 'a'); } else { a = (i - 52 + '0'); }
nm = "乘客";
nm.push_back(a);
int des = rand() % 3;
int wy = rand() % 2;
Passenger passenger(nm, des, wy);
nm.clear();
nm.push_back(a);
int lug_num = rand() % 3 + 1;
for (int i = 0; i < lug_num; i++) {
Luggage luggage(nm + to_string(i));
passenger.addLuggages(luggage);
}
passenger.printAndBuy(travelAgency, ticketOffice);
}
cout << endl << endl;
travelAgency.baoming();
cout << endl << endl;
ticketOffice.sp();
return 0;
}