C++ 结构和类的设计 [火星旅游票价]

结构和类的设计:
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;
}

最终结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值