结构和类的设计:
C++实现
#include <iostream>
#include "world.h"
#include<ctime>
#include <vector>
#include<unordered_set>
#include <math.h>
using namespace std;
struct MyStruct
{
string companyNmae;
uint32_t Days;
string tripType;
uint32_t price;
uint32_t speed;
};
typedef std::vector<MyStruct> Ms;
class GoHx
{
public:
uint32_t CalArrivalTime(int ss);//最终到达火星天数
uint32_t CalRandomPrice(int ss);//最终最终票价
uint32_t Calwf();//最终单程还是往返
string wf(int ss);//返回单程还是往返字符串
uint32_t GetTripType() { return tripType; }
uint32_t GetSpeed() { return randomSpeed; }
private:
const uint32_t range = 62100000;//公里数
const uint32_t minPrice = 3000;//最低票价
const uint32_t maxPrice = 5000;//最高票价
const uint32_t minSpeed = 16;//最低速度
const uint32_t maxSpeed = 30;//最低速度
uint32_t randomPrice;//最终随机票价
uint32_t randomSpeed;//最终随机速度
uint32_t ArrivalDay;//最终到达时间
uint32_t tripType;//0=往返,1=单程
};
uint32_t GoHx::CalArrivalTime(int ss)//计算最终到达天数
{
randomSpeed = rand() % (maxSpeed - minSpeed + 1) + minSpeed;
ArrivalDay = range / (randomSpeed * 60 * 60 * 24);
if (ss == 1)
return ArrivalDay;
if (ss == 0)
return ArrivalDay * 2;
return 0;
}
uint32_t GoHx::CalRandomPrice(int ss)//最终票价
{
randomPrice = rand() % (maxPrice - minPrice + 1) + minPrice;
if (ss == 1)
return randomPrice;
if (ss == 0)
return randomPrice * 2;
return 0;
}
uint32_t GoHx::Calwf()//最终是否往返
{
tripType = rand() % (1 - 0 + 1) + 0;
return tripType;
}
string GoHx::wf(int ss)//单程还是往返返回字符串
{
switch (ss)
{
case 1:
return "单程";
case 0:
return "往返";
}
return "";
}
int main()
{
Ms vec;
MyStruct gs;
GoHx go;
gs.companyNmae = "公司1";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
gs.companyNmae = "公司2";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
gs.companyNmae = "公司3";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
gs.companyNmae = "公司4";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
gs.companyNmae = "公司5";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
gs.companyNmae = "公司6";
gs.tripType = go.wf(go.Calwf());
gs.Days = go.CalArrivalTime(go.GetTripType());
gs.price = go.CalRandomPrice(go.GetTripType());
gs.speed = go.GetSpeed();
vec.push_back(gs);
cout << "公司名称" << " " << "是否往返" << " " << "到达所需天数" << " " << "最终票价" <<" "<<"最终速度"<< endl;
for (size_t i = 0; i < vec.size(); i++)
{
cout << vec.at(i).companyNmae.c_str() << " " << vec.at(i).tripType.c_str() << " " << vec.at(i).Days << " $" << vec.at(i).price<<" " <<vec.at(i).speed<< endl;
}
return 0;
}
最终结果: