函数和结构
//传递返回结构
////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
struct str_times
{
int hours;
int mins;
};
const int mins_per_hr=60;
str_times sum(str_times t1,str_times t2);
void show_time(str_times t);
int main()
{
using namespace std;
str_times day1={5,45};
str_times day2={4,55};
str_times trip=sum(day1,day2);
cout<<"two-day total: ";
show_time(trip);
str_times day3={4,32};
cout<<"three-day total: ";
show_time(sum(trip,day3));
return 0;
}
str_times sum(str_times t1,str_times t2)
{
str_times 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(str_times t)
{
using namespace std;
cout<<t.hours<<" hours, "
<<t.mins<<" minutes\n";
}
//传递结构地址
////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cmath>
//structure templates
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
//prototypes
void rect_to_polar(const rect * pxy,polar * pda);
void show_polar(const polar * pda);
int main()
{
using namespace std;
rect rplace;
polar pplace;
cout<<"enter the x and y values: ";
while(cin>>rplace.x>>rplace.y)
{
rect_to_polar(&rplace,&pplace);
show_polar(&pplace);
cout<<"next two num(q to out): ";
}
cout<<"done!"<<endl;
return 0;
}
//show st
void rect_to_polar(const rect * pxy,polar * pda)
{
using namespace std;
pda->distance=
sqrt(pxy->x*pxy->x+pxy->y*pxy->y);
pda->angle=atan2(pxy->y,pxy->x);//atan2 根据 x y 值计算角度
}
void show_polar(const polar * pda)
{
using namespace std;
const rad_to_deg=57.29577951;
cout<<"distance= "<<pda->distance;
cout<<", angel="<<pda->angle*rad_to_deg;
cout<<"degrees\n";
}