C++ Primer第七章課後編程題

1、編寫一個程序,不斷要求用戶輸入兩個數,直到其中的一個爲0.對於每兩個數,程序將使用一個函數來計算它們的調和平均數,並將結果返回給main(),而後者將報告結果。調和平均數指的是倒數平均值的倒數,計算公式如下:
代碼
#include<iostream>
double avg(int x, int y);
int main()
{
    using namespace std;
    int a;
    int b;
    cout << "Enter two number:\n";
    while((cin >> a >> b) && a != 0 && b != 0)
    {
        cout << "result: " << avg(a, b) << endl;
        cout << "Enter next two number:\n";
    }
    cout << "Bey!\n";
    return 0;
}
double avg(int x, int y)
{
    double value = 2.0*x*y/(x+y);
    return value;
}
運行結果

2、編寫一個以,要求用戶輸入最多10個高爾夫成績,並將其存儲在一個數組中。程序允許用戶提早結束輸入,並在一行上顯示所有成績,然後報告平均成績。請合3個數組處理函數來分別進行輸入,顯示和計算平均成績。
代碼
#include<iostream>
const int limit = 10;
int fill_array(double ar[], int limit);
double show_array(double ar[], int size);
void avg_array(double sum, int size);
int main()
{
    using namespace std;
    double ar[limit];
    int size = fill_array(ar, limit);
    double sum = show_array(ar, size);
    avg_array(sum,size);
    return 0;
}
int fill_array(double ar[], int limit)
{
    using namespace std;
    double temp;
    int i;
    for (i=0; i<limit; i++)
    {
        cout << "Enter value #" << (i+1) << ": ";
        cin >> temp;
        if(!cin)
        {
            cin.clear();
            while(cin.get() != '\n');
                continue;
            cout << "Bad input:\n";
            break;
        } else if(temp < 0)
            break;
        ar[i] = temp;
    }
    return i;
}
         
double show_array(double ar[], int size)
{
    using namespace std;
    int i;
    double sum;
    cout << "所有成績: ";
    for(i=0; i< size; i++)
    {
        sum += ar[i];
        cout << ar[i] << " ";
    }
    return sum;
}
   
void avg_array(double sum, int size)
{
    using namespace std;
    cout << "平均值爲: " << sum/size <<endl;
}
運行結果


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;
};
void show_box(box b);
void set_box(box *pb);
int main()
{
    box b = {"Guugle Gu", 2, 3,5, 1};
    show_box(b);
    set_box(&b);
    show_box(b);
    return 0;
}
void show_box(box b)
{
    using namespace std;
    cout << "Box maker: " << b.maker <<endl;
    cout << "Box height: " << b.height <<endl;
    cout << "Box width: " << b.width <<endl;
    cout << "Box length: " << b.length <<endl;
    cout << "Box volume: " << b.volume <<endl;
}
void set_box(box *pb)
{
    using namespace std;
    pb->volume = pb->height * pb->width * pb->length;
}
運行結果


4、許多州的彩票發行機構都使用如程序清單7.4所示的簡單彩票玩法的變體。在這些玩法中,玩家從一組被稱爲域號碼(field number)的號碼中選擇幾個。例如,可以從域號碼1~47中選擇5個號碼:還可以從第二個間(如1~27)中選擇一個號碼(稱爲特選號碼)。要贏得頭等獎,必須正確猜中所有的號碼。中頭獎的機率是選中所有域號碼機率與選中特選號碼機率的乘積。請修改程序清單7.4,以計算中得這種彩票頭獎的贓證。
代碼
#include<iostream>
const int n1=5;
const int num1 = 47;
const int n2=1;
const int num2 = 27;
long double odds(unsigned num, unsigned n);
int main()
{
    using namespace std;
    double odds1 = odds(num1, n1);
    double odds2 = odds(num2, n2);
    cout << "中頭獎的機率爲: " << odds1*odds2 <<endl;
    return 0;
}
long double odds(unsigned num, unsigned n)
{
    using namespace std;
    long double res=1;
    for(num, n; n>0; n--,num--)
       res = res * n/num;
    return res;
}
運行結果


5、定義一個遞歸函數,接受一個整數參數,並返回該參數的階乘。在程序中對該函數進行測試,程序使用循環讓用戶輸入不同的值,程序員將報告這些值的階乘。
代碼
#include<iostream>
int factorial(int n);
int main()
{
    using namespace std;
    int n;
    cout << "Enter a number:";
    cin >> n;
    cout << factorial(n) <<endl ;
    return 0;
}
int factorial(int n)
{
    int res = 1;
    if (n > 1)
        res = n * factorial(n-1);
    else
        res = res * 1;
    return res;
}
運行結果


6.編寫一個程序,它使用下列函數:
Fill_array()將一個double數組的名稱和長度作爲參數,它提示用戶輸入double值,並將這些值存儲到數組中。當數組被填滿或用戶輸入了非數字時,輸入將停止,並返回實際輸入了多少個數字。
Show_array()將一個double數組 的名稱和長度作爲參數,並顯示該數組的內容。
Reverse-array()將一個double數組的名稱和長度作爲參數,並將存儲在數組中的值的順序反轉。程序將使用這些函數來填充數組,然後顯示數組;反轉數組,然後示數組;反轉數組中除第一個和最後一個元素之外的所有元素,然後顯示數組。
代碼
#include<iostream>
int fill_array(double ar[], int limit);
void show_array(double ar[], int size);
void reverse_array(double ar[], int size);
using namespace std;
int main()
{
    int n;
    cout << "輸入數組的元素個數:";
    cin >> n;
    double ar[n];
    int size = fill_array(ar, n);
    show_array(ar, size);
    reverse_array(ar, size);
    cout << "---------反轉後結果----------\n";
    show_array(ar, size);
    return 0;
}
int fill_array(double ar[], int limit)
{
    double temp;
    int i;
    for (i=0; i<limit; i++)
    {
        cout << "Enter value#" << (i+1) << " \n";
        cin >> temp;
        if (!cin)
        {
            cin.clear();
            cout << "bad input:\n";
            break;
        }else
            ar[i] = temp;
    }
    return i;
}
void show_array(double ar[], int size)
{
    for(int i=0; i<size; i++)
        cout << "ARR_RES" << (i+1) << ": " << ar[i] <<endl;;
}
void reverse_array(double ar[], int size)
{
    int i,j;
    for (i=1, j=size-i-1; i<j; i++, j--)
    {
        double temp = ar[i];
        ar[i] = ar[j];
        ar[j] = temp;
    }
}
運行結果


9、設計一個名爲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()類似的數。如果讀者愛冒險,可以嘗試創建一個指針數組,其中的指針指向add()樣式的函數,並編寫一個循環,使用這些指針連續讓calculate()調用這些函數。
提示:下面是聲明這種指針數組的方式,其中包含3個指針:double (*pf[3]) (double,double);可以採用數組初始化句法,並將函數名作爲地址來初始化這樣的數組。
代碼
#include<iostream>
double add(double x, double y);
double sub(double x, double y);
double mean(double x, double y);
double calculate(double x, double y, double (*pf)(double, double));
int main()
{
    using namespace std;
    double (*pf[3])(double, double) = {add, sub, mean};
    const char (*guugle[3]) = {"sum", "difference", "mean"};
    double a,b;
    cout << "Enter pairs of numbers (q to quit):";
    int i;
    while (cin >> a >> b)
    {
        cout << calculate(a, b, add) << "= sum\n";
        cout << calculate(a, b, mean) << "= mean\n";
        for(i=0; i<3 ; i++)
            cout << calculate(a, b, pf[i]) << " = " << guugle[i] << "\n";
    }
    return 0;
}
double calculate(double x, double y, double (*pf)(double, double))
{
    return (*pf)(x, y);
}
double add(double x, double y)
{
    return x + y;
}
double sub(double x, double y)
{
    return x - y;
}
double mean(double x, double y)
{
    return (x+y)/2.0;
}
運行結果



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章