C++學習筆記(四)--編程練習第七章__Linux

來源:互聯網
上載者:User

1、編寫一個程式,不斷要求使用者輸入兩個數直到其中的一個為0.對於每兩個數,程式將使用一個函數來計算他們的調和平均數,並將結果返回給main(),而後者報告結果。
程式如下:

#include<iostream>#include<cmath>double Aver(double x, double y);int main(){    using namespace std;    double x,y,A_s;    while(1)    {        cout<<"Please enter a number: ";        cin>>x;        cout<<"Please enter another number: ";        cin>>y;        if(fabs(x)<1e-6||fabs(y)<1e-6)            break;        A_s=Aver(x,y);        cout<<"The average is: "<<A_s<<endl;    }    system("pause");    return 0;}double Aver(double x, double y){    return 2.0*x*y/(x+y);}

2、編寫一個程式,要求使用者輸入最多10個高爾夫成績,並將其儲存在一個數組中。程式允許使用者提早結束輸入,並在一行上顯示所有成績,然後報告平均成績。請使用3個數組處理函數來分別進行輸入、顯示和計算平均成績。
程式如下:

#include<iostream>#include<iomanip>int in_put(double *);double compute(double *, int);void display(double);using namespace std;int main(){    double golf_grades[10]={0};    int num;    num= in_put(golf_grades);    double A_grade;    A_grade= compute(golf_grades, num);    int i=0;    for(;i<num;i++)    {        display(golf_grades[i]);    }    cout<<"\nThe Average grade is: "<<endl;        display(A_grade);    cout<<endl<<num;    system("pause");    return 0;}int in_put(double * arry){    int j=0;    for(j=0;j<10;j++)    {        cin>>arry[j];        if(cin.fail())            break;    }    return j;}double compute(double * arry, int num){    double sum=0;    for(int i=0;i<num;i++)    sum+=arry[i];    return sum/num;}void display(double a){    cout<<a<<"\t";}

3、下面是一個結構聲明:

struct box{    char maker[40];    float height;    float width;    float length;    float volume;};

a、編寫一個函數,按值傳遞box結構,並顯示每個成員的值。
b、編寫一個函數,傳遞box結構的地址,並將volume成員設定為其他三維長度的乘積。
c、編寫一個使用這兩個函數的簡單程式。
程式如下:

#include<iostream>struct box{    char maker[40];    float height;    float width;    float length;    float volume;};using namespace std;void show_box(box a){cout<<a.maker<<endl<<a.height<<"\t"<<a.width<<"\t"<<a.length<<"\t\n";}void transform(box * pbb){    pbb->volume=pbb->height*pbb->length*pbb->width;}int main(){    box a;    cout<<"Please input the Maker: "<<endl;    cin.get(a.maker,40);    cout<<"Please Enter the height: "<<endl;    cin>>a.height;    cout<<"Please Enter the width: "<<endl;    cin>>a.width;    cout<<"Please Enter the length: "<<endl;    cin>>a.length;    show_box(a);    transform(&a);    cout<<"The volume of the box is: "<<a.volume<<endl;    system("pause");    return 0;}

5、定義一個遞迴函式,接受一個整數參數,並返回該參數的階乘。0。倍定義為1.通用的公式是,如果n大於零,則n。=n*(n-1)。。在程式中對該函數進行測試,程式使用迴圈讓使用者輸入不同的值,程式將報告這些值得階乘。
程式如下:

#include<iostream>using namespace std;int mat(int n){    int result;    if(n==0)        return result=1;        result=n*mat(n-1);    return result;}int main(){    cout<<"Please Enter a number: "<<endl;    int n;    cin>>n;    int res;    res=mat(n);    cout<<n<<"!="<<res<<endl;    system("pause");    return 0;}

一開始寫的程式時錯誤的錯誤程式如下:
在遞迴調用中語句:result=n*mat(n-1);寫成了result=n*mat(–n);不需要使用自減或者自加類運算子號,因為在函數的遞迴調用中,每次調用利用的n值都是該次調用的形參–參數已經發生了想要的變化。

6、編寫一個程式,它使用下列函數:
Fill_array()將一個double數組的名稱和長度作為參數。它提示使用者輸入
double值,並將這些值儲存到數組中。當數組被填滿或使用者輸入了非數字時,輸入將停止,並返回實際輸入了多少個數字。
Show_array()將一個double數組的名稱和長度作為參數,並顯示數組的內容。
Reverse_array()將一個double數組的名稱和長度作為參數,並將儲存在數組中的值反轉。
程式將使用這些函數來填充數組,然後顯示數組;反轉數組,然後顯示數組;反轉數組中除第一個和最後一個元素之外的所有元素,然後顯示數組。
程式如下:

#include<iostream>using namespace std;int Fill_array(double a[], int len){    for(int i=0;i<len;i++)    {        cout<<"Please enter a number: "<<endl;        cin>>a[i];        if(cin.fail())            return i;    }    return len;}void Show_array(double a[], int len){    int i=0;    for(;i<len;i++)        cout<<a[i]<<"\t";    cout<<endl;}void Reverse_array(double a[], int len){    double kill;    int i=0;    for(;i<len/2;i++)    {        kill=a[i];        a[i]=a[len-i-1];        a[len-i-1]=kill;    }}int main(){    int num;    double a[10];    num=Fill_array(a,10);    Show_array(a,num);    Reverse_array(a,num);    Show_array(a,num);    double b[10];    b[0]=a[num-1];    b[num-1]=a[0];    for(int i=1;i<num-1;i++)        b[i]=a[i];    Show_array(b,num);    system("pause");    return 0;}

10、設計一個名為calculate()的函數,它接受兩個double值和一個指向函數的指標,而被指向的函數接受兩個double參數,並返回一個double值。
calculate()函數的類型也是double,並返回被指向的函數使用calculate()
的兩個double參數計算得到的值。例如,假設add()的函數的定義如下:

double add(double x,double y){    return x+y;} double q=calculate(2.5,10.4,add);

則上述代碼中的函數調用將導致calculate()把2.5和10.4傳遞給add()函數,並返回add()的傳回值(12.9)。
請編寫一個程式,它調用上述兩個函數和至少另一個與add()類似的函數。該程式使用迴圈來讓使用者成對地輸入數字。對於每對數字,程式都是用calculate()來調用add()和至少一個其他的函數。如果讀者愛冒險,可以嘗試建立一個指標數組,其中的指標指向add()樣式的函數,並編寫一個迴圈,使用這些指標連續讓calculate()調用這些函數。提示:下面是聲明這種指標數組的方式,其中包含三個指標:
double(*pf[3])(double,double);
可以採用數組初始化文法,並將函數名作為地址來初始化這樣的數組。
滿足要求的第一個程式如下:

#include<iostream>using namespace std;double add(double x,double y){    return x+y;}double calculate(double a, double b, double(* pf) (double , double)){    double s;    s=(*pf)(a,b);    return s;}int main(){    double a,b,sum;    cout<<"Please enter a number: "<<endl;    cin>>a;    cout<<"Please enter another number: "<<endl;    cin>>b;    sum=calculate(a,b,add);    cout<<sum<<endl;    system("pause");    return 0;}

滿足第二個要求的程式如下:

#include<iostream>using namespace std;double add(double x,double y){    return x+y;}double sub(double x, double y){    return x-y;}double calculate(double a, double b, double(* pf) (double , double)){    double s;    s=(*pf)(a,b);    return s;}int main(){    double a,b,sum;    cout<<"Please enter a number: "<<endl;    cin>>a;    cout<<"Please enter another number: "<<endl;    cin>>b;    double (*ss[2]) (double, double)={add,sub};    for(int i=0;i<2;i++)    {        sum=calculate(a,b,ss[i]);        cout<<sum<<endl;    }    system("pause");    return 0;}

使用函數指標,必須完成下面的步驟:
1、擷取函數的地址
擷取函數地址只需要使用函數名,這裡需要區分一下擷取函數值得方式。
例如:

process(think);//調用的是函數的地址

process(think());//調用的是函數的值

2、聲明一個函數指標
聲明指向某種資料類型的指標時,必須指定指標指向的類型。同樣,聲明指向函數的指標時,也要指定指標指向的函數類型。
其格式如下:

double (*pf)(int);//pf指向了一個函數,它的輸入時int型,輸出是double型

注意一下兩種聲明的區別:
double (*pf)(int);//聲明了一個函數指標,該函數傳回值是double
double *pf (int);//聲明了一個函數,其傳回值是指向double的指標。
在函數指標的賦值中,特徵標與傳回型別必須是相同的。

3、使用函數指標來調用函數
使用解除取址運算子*就可以,例如

pf=pam;//其中pam是函數名,pf是函數指標

則以下兩個等價:

pam(a);(*pf)(a);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.