zoukankan      html  css  js  c++  java
  • C艹函数与结构体

     传递指针

    代码:

    #include <iostream>
    #include <cmath>
    
    struct polar{
        double distance;
        double angle;
    };
    
    struct rect{
      double x;
      double y;
    };
    
    void rect_to_polar(const rect * pxy, polar * pda);
    void show_polar(const polar * pda);
    
    int main(int argc, char const *argv[]) {
      using namespace std;
      rect rplace;
      polar pplace;
      std::cout << "Enter the x and y values:";
      while (cin >> rplace.x >> rplace.y)
      {
        rect_to_polar(&rplace, &pplace);
        show_polar(&pplace);
        std::cout << "Next two numbers (q to quit)" << '
    ';
      }
      std::cout << "Done." << '
    ';
      return 0;
    }
    
    void show_polar(const polar * pda)
    {
      using namespace std;
      const double Rad_to_deg = 57.29577951;
    
      std::cout << "distance = " << pda->distance;
      std::cout << ", angle = " << pda->angle * Rad_to_deg;
      std::cout << " degrees" << '
    ';
    }
    
    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);
    }
    View Code

    传递结构体

    #include <iostream>
    #include <cmath>
    
    struct polar{
      double distance;
      double angle;
    };
    
    struct rect{
      double x;
      double y;
    };
    
    polar rect_to_polar(rect xypos);
    void show_polar(polar dapos);
    
    int main(int argc, char const *argv[]) {
      using namespace std;
    
      rect rplace;
      polar pplace;
    
      std::cout << "Enter the x and y values: " << '
    ';
    
      while (cin >> rplace.x >> rplace.y){
        pplace = rect_to_polar(rplace);
        show_polar(pplace);
        std::cout << "Next two numbers (q to quit) :" << '
    ';
      }
    
      return 0;
    }
    
    
    
    polar rect_to_polar(rect xypos)
    {
      using namespace std;
      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)
    {
      using namespace std;
      const double Rad_to_deg = 57.29577951;
    
      std::cout << "distance = " << dapos.distance << '
    ';
      std::cout << ", angle = " << dapos.angle * Rad_to_deg << '
    ';
    }
    View Code
  • 相关阅读:
    java中值传递和引用传递
    java中的XML
    java I/O流
    RandomAccessFile类
    java中File类
    Java 理论与实践: 正确使用 Volatile 变量
    eclipse 总弹出 secure storage的解决办法
    安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
    Android关联源码support-v4的问题解决
    关于spring framework最新发布压缩包的下载问题 【非常非常新手帖】
  • 原文地址:https://www.cnblogs.com/renfanzi/p/7531565.html
Copyright © 2011-2022 走看看