函数和结构
代码部分:
-
#include "stdafx.h"
-
#include "iostream"
-
struct travel_time
-
{int hours;int mins;};
-
const int Mins_per_hr=60;
-
travel_time sum( travel_time t1,travel_time t2 );
-
void show_time(travel_time t);
-
using namespace std;
-
int main(){
-
travel_time day1={5,45};//5 hrs,45 mins
-
travel_time day2={4,55};//5 hrs,45 mins
-
travel_time trip=sum(day1,day2);
-
cout<<"TWO-day total:";
-
show_time(trip);
-
travel_time day3={4,32};
-
cout<<"THREE-day total:";
-
show_time(sum(trip,day3));
-
return 0;
-
}
-
travel_time sum(travel_time t1,travel_time t2)
-
{ travel_time total;
-
total.mins=(t1.mins+t2.mins)% Mins_per_hr;
-
total.hours=t1.hours+t2.hours+(t1.mins+t2.mins)/ Mins_per_hr;
-
return total;
-
}
-
void show_time(travel_time t)
-
{cout<<t.hours<<" hours, "
-
<<t.mins<<" minutes\n";
-
}
执行结果:
这是一道函数+结构题,结构(struct)可以像变量一下应用于函数。