你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。
以下只是把逻辑简单地写出来的而已。重构的话优先用工厂模式把那三种车型规划起来。另外其实这题就是一堆定义堆起来就可以写了。
#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
//Global
enum eTravelStrategy
{
E_TS_PREFERENCE_TIME,
E_TS_PREFERENCE_EXPENSE,
};
static eTravelStrategy gTravelStrategy=E_TS_PREFERENCE_TIME;//
#define RUSHHOURS_MORNING_START_HOUR 8
#define RUSHHOURS_MORNING_START_MIN 30
#define RUSHHOURS_MORNING_START (RUSHHOURS_MORNING_START_HOUR*60+RUSHHOURS_MORNING_START_MIN)
#define RUSHHOURS_MORNING_END_HOUR 10
#define RUSHHOURS_MORNING_END_MIN 0
#define RUSHHOURS_MORNING_END (RUSHHOURS_MORNING_END_HOUR*60+RUSHHOURS_MORNING_END_MIN)
#define RUSHHOURS_AFTERNOON_START_HOUR 17
#define RUSHHOURS_AFTERNOON_START_MIN 30
#define RUSHHOURS_AFTERNOON_START (RUSHHOURS_AFTERNOON_START_HOUR*60+RUSHHOURS_AFTERNOON_START_MIN)
#define RUSHHOURS_AFTERNOON_END_HOUR 20
#define RUSHHOURS_AFTERNOON_END_MIN 0
#define RUSHHOURS_AFTERNOON_END (RUSHHOURS_AFTERNOON_END_HOUR*60+RUSHHOURS_AFTERNOON_END_MIN)
bool isInRushhours(int hour,int min)
{
int mins=hour*60+min;
if( ((mins>=RUSHHOURS_MORNING_START)&&(mins<=RUSHHOURS_MORNING_END))
||((mins>=RUSHHOURS_AFTERNOON_START)&&(mins<=RUSHHOURS_AFTERNOON_END)))
{
return true;
}
else
{
return false;
}
}
//Bicycle
#define BICYCLE_COST_PER_HOUR 2
#define BICYCLE_AVGSPEED 18.f
#define BICYCLE_RUSHHOUR_IC 1.1f //influence coefficient
float getBicycleTimeSpent(int distance,bool bInRushHours)
{
if(distance<0)
return 0;
if(bInRushHours)
{
return distance/BICYCLE_AVGSPEED*BICYCLE_RUSHHOUR_IC;
}
else
{
return distance/BICYCLE_AVGSPEED;
}
}
float vehicle(float hours)
{
if(hours<0)
return 0;
return hours*BICYCLE_COST_PER_HOUR;
}
//Texi
#define TEXI_COST_STARTING 13
#define TEXI_COST_PER_KM 2.3f
#define TEXI_AVGSPEED 80.f
#define TEXI_RUSHHOUR_IC 2.0f //influence coefficient
float getTexiTimeSpent(int distance,bool bInRushHours)
{
if(distance<0)
return 0;
if(bInRushHours)
{
return distance/TEXI_AVGSPEED*TEXI_RUSHHOUR_IC;
}
else
{
return distance/TEXI_AVGSPEED;
}
}
float vehicle(int distance)
{
if(distance<=0)
return 0;
if(distance<=3)
{
return TEXI_COST_STARTING;
}
else
{
return TEXI_COST_STARTING+(distance-3)*TEXI_COST_PER_KM;
}
}
//Bus
#define BUS_STATION_DIST 2.f
#define BUS_AVGSPEED 60.f
#define BUS_STOP_TIMESPENT 0.017f// 1MIN
#define BUS_RUSHHOUR_IC 1.6f //influence coefficient
int getBusStationCount(int distance)
{
if(distance<0)
return 0;
return (int)(distance/BUS_STATION_DIST)+1;
}
float getBusTimeSpent(int distance,bool bInRushHours)
{
if(distance<0)
return 0;
if(bInRushHours)
{
return distance/BUS_AVGSPEED+getBusStationCount(distance)*BUS_STOP_TIMESPENT*BUS_RUSHHOUR_IC;
}
else
{
return distance/BUS_AVGSPEED+getBusStationCount(distance)*BUS_STOP_TIMESPENT;
}
}
float vehicle(int stationCount,float height)
{
if(stationCount<=0)
return 0;
if(height<1.2f)
{
return 0;
}
else if(height<1.4f)
{
return 2*0.25;
}
else
{
return 2;
}
}
bool floatCompare(float f0,float f1)
{
if(fabs((double)(f0-f1))<0.000001)
{
return true;
}
else
{
return false;
}
}
int main()
{
cout << "Hello Traveler" << endl;
char tmp[64];
char mostMatchWay[64];
while(1)
{
cout<<endl;
cout << "Distance(km):";
memset(tmp,0,64);
cin>>tmp;
int distance=atoi(tmp);
cout << "Height(m):";
memset(tmp,0,64);
cin>>tmp;
float height=(float)atof(tmp);
cout << "DepartureTime:hour=";
memset(tmp,0,64);
cin>>tmp;
int hour=atoi(tmp);
cout << "DepartureTime:min=";
memset(tmp,0,64);
cin>>tmp;
int min=atoi(tmp);
cout << "TravelStrategy:1=PreferenceTime;2=PreferenceExpanse";
cout << "Which your choose[num]:";
memset(tmp,0,64);
cin>>tmp;
int travelStrategy=atoi(tmp);
gTravelStrategy=(travelStrategy==1?E_TS_PREFERENCE_TIME:E_TS_PREFERENCE_EXPENSE);
bool bRushHour=isInRushhours(hour,min);
//Bicycle
float bicycleTimeSpentH=getBicycleTimeSpent(distance,bRushHour);
float bicycleCost=vehicle(bicycleTimeSpentH);
//Texi
float texiTimeSpentH=getTexiTimeSpent(distance,bRushHour);
float texiCost=vehicle(distance);
//bus
float busTimeSpentH=getBusTimeSpent(distance,bRushHour);
float busCost=vehicle(getBusStationCount(distance),height);
cout << "Bicycle:"<<bicycleTimeSpentH<<"h,"<<bicycleCost<<endl;
cout << "Texi:"<<texiTimeSpentH<<"h,"<<texiCost<<endl;
cout << "Bus:"<<busTimeSpentH<<"h,"<<busCost<<endl;
memset(mostMatchWay,0,64);
switch(gTravelStrategy)
{
case E_TS_PREFERENCE_TIME:
if(floatCompare(bicycleTimeSpentH,texiTimeSpentH))
{
if(floatCompare(texiTimeSpentH,busTimeSpentH)
|| (texiTimeSpentH<busTimeSpentH))
{
sprintf(mostMatchWay,"Texi");
}
else
{
sprintf(mostMatchWay,"Bus");
}
}
else if(bicycleTimeSpentH>texiTimeSpentH)
{
if(floatCompare(texiTimeSpentH,busTimeSpentH)
|| (texiTimeSpentH<busTimeSpentH))
{
sprintf(mostMatchWay,"Texi");
}
else
{
sprintf(mostMatchWay,"Bus");
}
}
else
{
if(floatCompare(bicycleTimeSpentH,busTimeSpentH)
|| (bicycleTimeSpentH>busTimeSpentH))
{
sprintf(mostMatchWay,"Bus");
}
else
{
sprintf(mostMatchWay,"Bicycle");
}
}
cout << "The most match TravelStrategy is:"<<mostMatchWay;
break;
default:
if(floatCompare(bicycleCost,texiCost))
{
if(floatCompare(texiCost,busCost)
|| (texiCost<busCost))
{
sprintf(mostMatchWay,"Texi");
}
else
{
sprintf(mostMatchWay,"Bus");
}
}
else if(bicycleCost>texiCost)
{
if(floatCompare(texiCost,busCost)
|| (texiCost<busCost))
{
sprintf(mostMatchWay,"Texi");
}
else
{
sprintf(mostMatchWay,"Bus");
}
}
else
{
if(floatCompare(bicycleCost,busCost)
|| (bicycleCost>busCost))
{
sprintf(mostMatchWay,"Bus");
}
else
{
sprintf(mostMatchWay,"Bicycle");
}
}
cout << "The most match TravelStrategy is:"<<mostMatchWay;
break;
}
cout<<endl;
}
return 0;
}