zoukankan      html  css  js  c++  java
  • 函数和结构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. }

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

  • 相关阅读:
    04-JQuery
    03-JavaScript
    02-CSS&JS
    01-HTML
    [LeetCode]Insert Interval
    [shell编程]正则表达式
    [LeetCode]Jump Game II
    [LeetCode]Jump Game
    [LeetCode]Wildcard Matching
    [shell编程]初识sed和gawk
  • 原文地址:https://www.cnblogs.com/whcsrj/p/12928889.html
Copyright © 2011-2022 走看看