函数和结构2

  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "cmath"

  4. using namespace std;

  5. struct polar

  6. {double distance;double angle;};

  7. struct rect

  8. {double x;double y;};

  9. //函数原型,即告诉编译器下面函数的结构是怎样的,比如传递几个参数,每个参数的类型是int还是char等

  10. polar rect_to_polar(rect xypos);

  11. void show_polar(polar dapos);

  12. int main()

  13. {rect rplace;polar pplace;

  14. cout<<"Enter the x and y value:"<<endl;

  15. while(cin>>rplace.x>>rplace.y){

  16.     pplace=rect_to_polar(rplace);

  17.     show_polar(pplace);

  18.     cout<<"next two numbers(q to quit):";

  19. }

  20. cout<<"Done. ";

  21. return 0;}

  22.  

  23. polar  rect_to_polar(rect xypos)

  24. {polar answer;
  25.      answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
  26.      answer.angle=atan2(xypos.y,xypos.x);
  27.      return answer;
  28. void show_polar(polar dapos)
  29. {     const double Rad_to_deg=57.29577957;
  30.       cout<<"distance="<<dapos.distance<<endl;
  31.       cout<<"angle="<<dapos.angle*Rad_to_deg<<endl;
  32.       cout<<" degrees "<<endl;
  33. }

结构是多个变量的组合体,作用是增加代码的重用性,比如一个结构包含姓名,年龄,身高,它可以应用于每个学生,这就是重用性。

原文地址:https://www.cnblogs.com/whcsrj/p/12928889.html