(C++对象数组)旅行团问题

题目:

(C++对象数组)旅行团问题_第1张图片

(C++对象数组)旅行团问题_第2张图片

(C++对象数组)旅行团问题_第3张图片

//说明:
// OUC_DMR  2019/05/05
//题目说可以最多生成62个旅客
//本程序默认生成26个旅客,为什么不是62呢?
//太麻烦了我懒得改


#include
#include
#include

#define random(x) (rand()%x)

//const int  MAX_luggage = 3;
//const int MAX_howToTravel = 2;
//const int MAX_destination = 3;

using namespace std;

//乘客类
class Passenger
{
public:
	char name;
	int luggage;      //行李数量值0,1,2代表实际1,2,3
	int howToTravel;  //自由行或旅游社,0,1
	int destination;	//国内短途,国内长途,国际游,0,1,2
	int ticketPrice;	//个人的票价
	bool isApplied=false;  //是否报了旅行社(有可能团满而报不上)
	Passenger() {}
	Passenger(Passenger& a)
	{
		a.name = this->name;
		a.luggage = this->luggage;
		a.howToTravel = this->howToTravel;
		a.destination = this->destination;
		a.ticketPrice = this->ticketPrice;
	}
};
//旅行团类
class travelGroup
{
public:
	int numPassenger=0;   //团内人数
	int destination;      //国内短途,国内长途,国际游,0,1,2
	int groupPrice=0;		//团一共需付的票价
	Passenger people[6];	//顶多6个人
};
//旅行社类
class travelAgency
{
public:
	travelGroup groups[5];	//5个团
	travelAgency() {};

};
//随机生成旅客,这里限制只生成26位旅客 'A','B','C'...
void random_people(Passenger passengers[])
{
	char name = 'A';

	srand(static_cast (time(NULL)));  //以当前时间设置随机种子
	for (int i = 0; i < 26; i++)
	{
		cout << "我是乘客";
		passengers[i].name = name;
		cout << passengers[i].name;
		cout << ",选择";

		//随机了行李数,如何去,目的地
		int remainder1 = random(3);
		int remainder2 = random(2);
		int remainder3 = random(3);

		passengers[i].luggage = remainder1;
		passengers[i].howToTravel = remainder2;
		passengers[i].destination = remainder3;

		switch (passengers[i].howToTravel)
		{
		case 0:cout << "自由出行"; break;
		case 1:cout << "报名旅行社"; break;
		default:
			break;
		}
		cout << ",";
		switch (passengers[i].destination)
		{
		case 0:cout << "国内短途"; break;
		case 1:cout << "国内长途"; break;
		case 2:cout << "国际游"; break;
		default:
			break;
		}
		cout << ",携带";
		switch (passengers[i].luggage)
		{
		case 0:cout << "1件行李"; break;
		case 1:cout << "2件行李"; break;
		case 2:cout << "3件行李"; break;
		default:
			break;
		}
		cout << "行李编号是";
		for (int j = 0; j < passengers[i].luggage+1; j++)
		{
			cout << name << "0" << j + 1 << ",";
		}
		cout << "买票需付";
		switch (passengers[i].destination)
		{
		        case 0:passengers[i].ticketPrice = 10;
					cout << passengers[i].ticketPrice<<"元。"; break;
	        	case 1:passengers[i].ticketPrice = 100; 
					cout << passengers[i].ticketPrice << "元。"; break;
		        case 2:passengers[i].ticketPrice = 500; 
					cout << passengers[i].ticketPrice << "元。"; 
					if (passengers[i].luggage == 2)
					{

						cout  << "行李托运费100元";
					}
						break;
		        default:
			    break;

		}
		cout << endl;
		name++;

	}
	cout << endl;
}
//旅行社生成5个随机目的地的旅行团
//注意形参需要是引用,之后有注释提醒为什么用引用
void random_agency(travelAgency& agency,Passenger passengers[])  //引用,为什么passenger不用引用呢?他是数组
{																//传的是地址,可以直接修改
	srand(static_cast (time(NULL)));
	for (int i = 0; i < 5; i++)
	{
		int remainder3 = random(3);
		agency.groups[i].destination = remainder3;
	}			//随机生成5个旅行团
	for (int i = 0; i < 26; i++)		//遍历乘客,如果该乘客报团										
	{									//与旅行团目的地一样,则加进旅行社
		for (int j = 0; j < 5; j++)
		{
			
			if (passengers[i].howToTravel == 1) //我这里没写好,不用那么多if,用&&的
			{
				if (passengers[i].destination ==agency.groups[j].destination)
				{
					if (!passengers[i].isApplied)
					{
						if (agency.groups[j].numPassenger < 6)
						{
							agency.groups[j].people[agency.groups[j].numPassenger] = passengers[i];
							
							passengers[i].isApplied = true;		// !!!
							agency.groups[j].numPassenger++;    //!!!注意了,这里就是为什么要用引用的地方,你要去修改对象的属性了!
						}
					}
					
				}
			}
		}
	}
}
//旅行社报名
void travelApply(travelAgency& agency)     //也是用引用,原因与上同理
{
	cout << "旅行社开始报名" << endl;
	cout << "旅行社共计安排了5个旅行团,共计26人报名。" << endl;
	cout << "出行的目的地分别是:";
	for(int i=0;i<5;i++)
	{
		switch (agency.groups[i].destination)
		{
		case 0:cout << "国内短途"; break;
		case 1:cout << "国内长途"; break;
		case 2:cout << "国际游"; break;
		default:
			break;
		}
		cout << ",";
	}
	cout <

 

你可能感兴趣的:(C/C++)