直接放代码
在做这个实验的时候用到了随机数
但是我的随机数都是一样,除非运行时间长
虽然是伪随机数,但是这个情况有点奇怪
难到我的种子比别人大?
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<string>
#include <windows.h>
using namespace std;
static enum Destination { DomesticShort, DomesticLong, International };
static string goal[] = { "国内短途","国内长途","国际游" };
static string type_go[] = { "自由行","报团" };
static enum TravelType { single, team };
class Luggage;
class Passenger;
class TravelAgency;
class Team;
class TicketOffice;
static const int fare[3] = { 10,100,500 };
class Luggage
{
public:
Luggage() {
this->number = 0;
};
~Luggage() {};
private:
int number;
};
class Passenger
{
private:
string name;
static const int MaxNums = 62;
static const int LugsNums = 4;
Luggage lugs[LugsNums];
Destination de;
TravelType tt;
int BoxNums;
string type;
string where;
bool success;
int fare;
int things_fare;
public:
Passenger() {
name = '0';
de = DomesticShort;
tt = single;
BoxNums = 0;
type = "自由出行";
where = "国内短途";
success = false;
fare = 0;
things_fare = 0;
};
Passenger(int order, int boxs, int travel, int destination)
{
fare = 0;
things_fare = 0;
srand(static_cast<unsigned>(time(NULL)));
success = false;
this->BoxNums = boxs;
if (order < 26)
{
this->name = 'A' + order;
}
else if (order >= 26 && order < 52)
{
this->name = 'a' + order - 26;
}
else
{
this->name = '0' + order - 52;
}
setTravelType(travel);
setDestination(destination);
switch (de)
{
case DomesticShort:
fare = 10;
break;
case DomesticLong:
fare = 100;
break;
case International:
fare = 500;
break;
default:
break;
}
if (de == International && BoxNums == 3)
things_fare = 100;
}
string getName()
{
return this->name;
}
void setSuccess(bool k)
{
this->success = k;
}
bool getSuccess()
{
return success;
}
Destination getDe()
{
return this->de;
}
TravelType getType()
{
return this->tt;
}
void setDestination(int i)
{
switch (i)
{
case 0:this->de = DomesticShort;
this->where = ",国内短途";
break;
case 1:this->de = DomesticLong;
this->where = ",国内长途";
break;
case 2:this->de = International;
this->where = ",国际游";
break;
}
}
void setTravelType(int i)
{
switch (i)
{
case 0:this->tt = single;
this->type = ",选择自由出行";
break;
case 1:this->tt = team;
this->type = ",报名旅行社";
break;
}
}
void show()
{
cout << "我是乘客" + name << type << where << ",携带" << BoxNums << "件行李 ";
if (BoxNums > 0)
{
cout << "行李编号是";
for (int i = 0; i < BoxNums; i++)
cout << name << 0 << i << "、";
cout << "买票需付" << fare << "元";
if (things_fare == 100)
{
cout << "行李托运费100元。";
}
}
cout << endl;
}
int getFare()
{
return this->fare + this->things_fare;
}
};
class Team
{
public:
Team() {};
Team(int k) {
de = DomesticShort;
member = 0;
cost = 0;
switch (k)
{
case 0:this->de = DomesticShort;
break;
case 1:this->de = DomesticLong;
break;
case 2:this->de = International;
break;
}
};
~Team() {};
void setMember(int i)
{
this->member = i;
}
void setCost(Passenger& passenger)
{
this->cost += passenger.getFare();
}
Destination getDe()
{
return this->de;
}
void add()
{
this->member++;
}
int getMember()
{
return this->member;
}
Passenger getPas(int i)
{
return this->passenger[i];
}
int getCost()
{
return this->cost;
}
string getDeName()
{
return goal[this->de];
}
private:
int member;
Destination de;
int cost;
Passenger passenger[6];
};
class TravelAgency
{
public:
TravelAgency()
{
all_member = 0;
srand(time(0));
for (int i = 0; i < 5; i++)
{
int k = rand() % 3;
team[i] = Team(k);
}
}
void signup(Passenger& passenger)
{
for (int i = 0; i < 5; i++)
{
if (passenger.getDe() == this->team[i].getDe() && !passenger.getType() == single && passenger.getSuccess() == false)
{
if (team[i].getMember() != 6)
{
all_member++;
team[i].getPas(i) = passenger;
team[i].add();
team[i].setCost(passenger);
passenger.setSuccess(1);
}
}
}
}
void show()
{
cout << "旅行社开始报名。" << endl;
cout << "旅行社共计安排5个旅行团,共计" << all_member << "人报名。" << endl;
cout << "出行目的地分别是:";
for (int i = 0; i < 5; i++)
{
cout << team[i].getDeName() << ",";
}
cout << "每个旅行团的人数分别是:";
for (int i = 0; i < 5; i++)
{
cout << team[i].getMember() << ",";
}
cout << "每个旅行团需要支付的票价是:";
for (int i = 0; i < 5; i++)
{
cout << team[i].getCost() << ",";
}
}
private:
static const int all_team = 5;
Team team[5];
int all_member;
};
class TicketOffice
{
public:
TicketOffice();
void buyTicket(Passenger& passenger)
{
switch (passenger.getDe())
{
case DomesticShort:
if (passenger.getSuccess())
{
bill[0] += 10;
names[0] += "," + passenger.getName();
}
else
{
bill[3] += 10;
names[3] += "," + passenger.getName();
}
break;
case DomesticLong:
if (passenger.getSuccess())
{
bill[1] += 100;
names[1] += "," + passenger.getName();
}
else
{
bill[4] += 100;
names[4] += "," + passenger.getName();
}
break;
case International:
if (passenger.getSuccess())
{
bill[2] += 500;
names[2] += "," + passenger.getName();
}
else
{
bill[5] += 500;
names[5] += "," + passenger.getName();
}
break;
}
}
~TicketOffice();
void show()
{
cout << "售票处开始售票" << endl;
cout << "售票结束,营业额统计信息如下:" << endl;
for (int i = 0; i < 6; i++)
{
cout << office[i] << bill[i] << "元 ," << "乘客姓名:" << names[i] << endl;
}
}
private:
static const string office[6];
int bill[6];
string names[6];
};
const string TicketOffice::office[6] = { "国内短途团购:","国内长途团购:","国际游团购 :","国内短途自由行:","国内长途自由行 :","国际游自由行:" };
TicketOffice::TicketOffice()
{
for (int i = 0; i < 6; i++)
{
bill[i] = 0;
}
}
TicketOffice::~TicketOffice()
{
}
int main()
{
int boxs=0, travel=0, destination=0;
// srand(static_cast<unsigned>(time(NULL)));
srand(time(0));
int number = rand() % 62;
Passenger* passenger = new Passenger [number];
TravelAgency agency;
TicketOffice to;
for (int i = 0; i < number; i++)
{
boxs = rand() % 4;
travel = rand() % 2;
destination = rand() % 3;
passenger[i] = Passenger(i, boxs, travel, destination);
agency.signup(passenger[i]);
Sleep(500);
}
for (int i = 0; i < number; i++)
{
passenger[i].show();
to.buyTicket(passenger[i]);
}
agency.show();
to.show();
return 0;
}