• 几种参数传递问题(读c++ primer plus)


    1:用数组做参数传递

    #include   <iostream>
    
    #include <cmath>
    
    
    
    struct polar
    {
    
    double distance;:
    
    double angle;
    
    };
    struct rect
    {
    double x;
    double y;
    }
    
    void rect_to_polar(const *pxy,polar *pda);
    void show_polar(const polar *pda);
    
    int main()
    {
    
    rect rplace;
    polar pplace;
    
    while (cin >> rplace.x >> rplace.y)
    {
    rect_to_polar (&rplace,&pplace);
    show_polar(&pplace)
    }
    return 0;
    }
    
    void show_polar(const polar* pda)
    {
        using namespace std;
        cout <<pda->distance;
        cout << pda->angle;
    }
    
    void rect_to_polar (const rect *pxy,polar *pda)
    
    {
    using namespace std;
    pda->distance =
    sqrt(pxy ->x+pda->y*pda->y);
    }
    调用函数时候,将结构的指针的地址而不是结构本身传递给他,这一点很重要。
    3:函数指针:
    1. #include <iostream>
      
    2. 
      
    3. double betsy(int);
      
    4. double pam(int);
      
    5. void estimate (int lines,double (*pf)(int));
      
    6. int main()
      
    7. {
      
    8.     using namespace std;
      
    9.     int code;
      
    10.     cin >> code;
      
    11.     estimate (code,betsy);
      
    12.     estimate(code,pam);
      
    13.     return 0;
      
    14. }
      
    15. double betsy(int lns)
      
    16. {
      
    17.     return 0.05*lns;
      
    18. }
      
    19. double pam (int lns)
      
    20. {
      
    21.     return 0.03*lns + 0.0004*lns*lns;
      
    22. }
      
    23. void estimate (int lines,double(*pf)(int))
      
    24. {
      
    25.     using namespace std;
      
    26.     cout << (*pf)(lines) ;
      
    27. }
      

    第二次调用estimate()函数时候传递了函数pam()的地址。也就是*pf指针。pf=pam

  • 相关阅读:
    HDOJ 1202 The calculation of GPA
    HDOJ 1197 Specialized Four-Digit Numbers
    HDOJ 1196 Lowest Bit(二进制相关的简单题)
    HDOJ 1194 Beat the Spread!(简单题)
    NOIP2018游记
    CF1043
    洛谷P1280 尼克的任务
    洛谷P1155 双栈排序
    SPOJ6340 ZUMA
    chessboard
  • 原文地址:https://www.cnblogs.com/sunliming/p/1837739.html
Copyright © 2020-2023  润新知