いくつかのパラメータ伝達問題(c++primer plusを読む)

8031 ワード

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:    :
#include <iostream>
  • 
    
  • double betsy(int);
    
  • double pam(int);
    
  • void estimate (int lines,double (*pf)(int));
    
  • int main()
    
  • {
    
  •     using namespace std;
    
  •     int code;
    
  •     cin >> code;
    
  •     estimate (code,betsy);
    
  •     estimate(code,pam);
    
  •     return 0;
    
  • }
    
  • double betsy(int lns)
    
  • {
    
  •     return 0.05*lns;
    
  • }
    
  • double pam (int lns)
    
  • {
    
  •     return 0.03*lns + 0.0004*lns*lns;
    
  • }
    
  • void estimate (int lines,double(*pf)(int))
    
  • {
    
  •     using namespace std;
    
  •     cout << (*pf)(lines) ;
    
  • }
    

  • .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
    .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
    .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
    .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
    .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
    .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
    .number_show{ padding-left:52px !important; list-style:decimal outside !important}
    .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
    .number_hide{ padding-left:0px !important; list-style-type:none !important}
    .number_hide li{ list-style-type:none !important; border-left:0px}
    ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
    ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
    .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
    ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

    function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){var content=codeElement.innerText;if(window.clipboardData==null){window.alert(「ブラウザではスクリプトコピーはサポートされていません.手動でコピーしてみてください.」)Else{window.clipboardData.setData("Text",content);window.alert("ソースコードはクリップボードにコピーされました")}}}}function LineNumberVisible(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){if(codeElement.className=="mainarea number_hide"){codeElement.className="mainarea number_show";key.innerText="行番号を隠す"}else{codeElement.className="mainarea number_hide";key.innerText="行番号を表示する"}}function ChangeIcon(key,isHover){if(isHover)key.style.cor='red';else key.style.cor='blue'}function CopyCode_CheckKey(key){if(window.event.keyCode==13)CopyCode(key)}function AboutMe(){window.alert("このコードボックスはCodePaste for Windows Live Writerによって生成されます.r
    r
    Author:範伝根r
    Email:[email protected]\r
    Website: http://chuangen.name\r
    Blog: http://blog.csdn.net/chuangen");}
    2回目にestimate()関数を呼び出すと関数pam()のアドレスが渡されます.つまり*pfポインタです.pf=pam