函数和结构2
-
#include "stdafx.h"
-
#include "iostream"
-
#include "cmath"
-
using namespace std;
-
struct polar
-
{double distance;double angle;};
-
struct rect
-
{double x;double y;};
-
//函数原型,即告诉编译器下面函数的结构是怎样的,比如传递几个参数,每个参数的类型是int还是char等
-
polar rect_to_polar(rect xypos);
-
void show_polar(polar dapos);
-
int main()
-
{rect rplace;polar pplace;
-
cout<<"Enter the x and y value:"<<endl;
-
while(cin>>rplace.x>>rplace.y){
-
pplace=rect_to_polar(rplace);
-
show_polar(pplace);
-
cout<<"next two numbers(q to quit):";
-
}
-
cout<<"Done.\n";
-
return 0;}
-
-
polar rect_to_polar(rect xypos)
- {polar answer;
- answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
- answer.angle=atan2(xypos.y,xypos.x);
- return answer;
- }
- void show_polar(polar dapos)
- { const double Rad_to_deg=57.29577957;
- cout<<"distance="<<dapos.distance<<endl;
- cout<<"angle="<<dapos.angle*Rad_to_deg<<endl;
- cout<<" degrees\n"<<endl;
- }
结构是多个变量的组合体,作用是增加代码的重用性,比如一个结构包含姓名,年龄,身高,它可以应用于每个学生,这就是重用性。