C++ Primer Plus 第六版 第七章課後編程練習答案

1.

#include <iostream>
using namespace std;
double f(int x, int y);

int main()
{
    int x,y;
    double ave;
    while (1)
    {
        cout << "Enter 2 num: ";
        cin >> x >> y;
        if(x==0 || y==0)
        {
            cout << "Error.\n";
            break;
        }
        else
        {
            ave = f(x, y);
            cout << "Average is: " << ave << endl;
        }
    }

    return 0;
}

double f(int x, int y)
{
    double ave;
    ave  = 2 * double (x * y)/(x + y);
    return ave;
}

2.

#include <iostream>
using namespace std;
int input(double arr[], int limit);
void dispaly(double arr[], int i);
double cal(double arr[], int i);
const int MAX = 10;

int main()
{
    double scores[MAX];
    int size = input(scores, MAX);
    dispaly(scores, size);
    double ave;
    ave = cal(scores, size);
    cout << "The average score: " << ave << endl;

    return 0;
}

int input(double arr[], int limit)
{
    int i;
    double score;
    cout << "Enter scores, 'q' to quit.\n";
    for(i=0; i<limit; i++)
    {
        cout << "Enter scores#" << i << ": ";
        cin >> score;
        if(!cin)  //允許用戶提早結束輸入,比如輸入'q'
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Error\n";
            break;
        }
        arr[i] = score;
    }
    return i; //輸入的個數
}

void dispaly(double arr[], int i)
{
    cout << "You entered " << i << " scores.\n";
    cout << "The scores are:\n";
    for(int j=0; j<i; j++)
        cout << arr[j] << "\t"; //在一行上顯示所有成績
    cout << endl;
}

double cal(double arr[], int i)
{
    double ave;
    double sum = 0;
    for(int j=0; j<i; j++)
        sum += arr[j];
    ave = sum / i;
    return ave;
}

3.

#include <iostream>
using namespace std;

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

void display(box tmp);
float cal(box * tmp);

int main()
{
    cout << "Here is the example: \n";
    box tmp;
    cout << "Enter maker: ";
    cin >> tmp.maker;
    cout << "Enter height: ";
    cin >> tmp.height;
    cout << "Enter width: ";
    cin >> tmp.width;
    cout << "Enter length: ";
    cin >> tmp.length;
    cout << "Enter volume: ";
    cin >> tmp.volume;
    cout << endl;
    display(tmp);
    float val;
    val = cal(&tmp);
    cout << "The calculation: " << val <<endl;

    return 0;
}

void display(box tmp) //按值傳遞結構,顯示數據
{
    cout << "Maker: " << tmp.maker << endl;
    cout << "Height: " << tmp.height << endl;
    cout << "Width: " << tmp.width << endl;
    cout << "Length: " << tmp.length << endl;
    cout << "Volume: " << tmp.volume << endl;
}

float cal(box * tmp) //按地址傳遞結構,並計算數據
{
    float val;
    val = tmp->length * tmp->width * tmp->height;
    return val;
}

4.

#include <iostream>
using namespace std;
double cal(int num1, int num2, int bingo1, int bingo2);

int main()
{
    int num1, num2, bingo1, bingo2;
    double v;
    cout << "Enter Num1: ";
    cin >> num1;
    cout << "Enter Num2: ";
    cin >> num2;
    cout << "Enter Bingo1: ";
    cin >> bingo1;
    cout << "Enter Bingo2: ";
    cin >> bingo2;
    v = cal(num1, num2, bingo1, bingo2);
    cout << "Probability: " << v << endl;

    return 0;
}

double cal(int num1, int num2, int bingo1, int bingo2)
{
    double v1, v2, v3;
    v1 = double (bingo1)/num1;
    v2 = double (bingo2)/num2;
    v3 = v1 * v2;
    return v3;
}

5.

#include <iostream>
using namespace std;
int cal(int n);

int main()
{
    int n, result;
    while (1)
    {
        cout << "Enter num: ";
        cin >> n;
        if(!cin) //非數字輸入則直接終止
        {
            cout << "Quit!";
            break;
        }
        else if (n<0) //當輸入小於0,請重新輸入正值
        {
            cout << "Error. Please enter positive num!";
            continue;
        }
        else if (n>=0)
        {
            result = cal(n);
            cout << "Calcvulation: " << result << endl;
        }
    }

    return 0;
}

int cal(int n)
{
    int result;
    if(n==0)
        result = 1;
    else
        result = n * cal(n-1);
    return result;
}

6.

#include <iostream>
using namespace std;
int Fill_array(double arr[], int size);
void Show_array(double arr[], int num);
void Reverse_array(double arr[], int num);
const int MAX=30;

int main()
{
    double arr[MAX];
    int num = Fill_array(arr, MAX); //填充數組,然後顯示數組
    Show_array(arr, num);

    Reverse_array(arr, num); //反轉數組,然後顯示數組
    cout << "After reverse:\n";
    Show_array(arr, num);

    Reverse_array(arr, num); //先還原數組排序
    Reverse_array(arr+1, num-2); //反轉數組中除第一個和最後一個元素之外的所有元素,然後顯示數組
    //不能動首尾元素,故個數num-2。要從第二個元素開始,故地址是arr+1
    cout << "After reverse:\n";
    Show_array(arr, num);

    return 0;
}

int Fill_array(double arr[], int size) //將一個double數組的名稱和長度作爲參數
{
    int i;
    double j;
    for(i=0; i<size; i++)
    {
        cout << "Enter data:";
        cin >> j;
        if(!cin) //當輸入了非數字時,停止
        {
            cout << "Error. Quit!\n";
            break;
        }
        else
            arr[i] = j;
    }
    return i; //返回實際輸入了多少個數字
}

void Show_array(double arr[], int num)
{
    cout << "The array is:\n";
    for(int j=0; j<num; j++)
        cout << arr[j] << "\t";
    cout << "\n";
}

void Reverse_array(double arr[], int num)
{
    double tmp;
    for (int j = 0; j < (num / 2); j++)
    {
        tmp = arr[j];
        arr[j] = arr[num - 1 - j];
        arr[num - 1 - j] = tmp;
    }
}

7.

#include <iostream>
using namespace std;
double * fill_array(double * arr, double * limit);
void show_array(const double * arr, double * n);
void revalue(double r, double * arr, double * n);
const int MAX = 5;

int main()
{
    double properties[MAX];
    double * n = fill_array(properties,  properties+MAX);//注意形參limit對應的是properties+MAX
    show_array(properties, n);
    if(*n > 0)
    {
        cout << "Enter revaluatiojn factor: ";
        double factor;
        while (!(cin >> factor))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; Please enter a num: ";
        }
        revalue(factor, properties, n);
        show_array(properties, n);
    }
    cout << "Done.\n";
    return 0;
}

double * fill_array(double * arr, double * limit) //使用兩個指針參數來表示區間,將按值傳遞數組改爲按地址傳遞數組;返回值是指針,故fill_array類型是int*而非int
{
    double tmp;
    double * i;
    int j=0;
    for(i=arr; i<limit; i++, j++)  //從首地址起
    {
        cout << "Enter value#" << (j + 1) << ": ";
        cin >> tmp;
        if(!cin)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input. Quit!"<<endl;
            break;
        }
        else if (tmp<0)
            break;
        *i = tmp;
    }
    return i;  //指針傳遞
}

void show_array(const double * arr, double * n)
{
    int j=0;
    const double * i;
    for(i = arr; i<n; i++,j++)
    {
        cout << "Property #" << (j + 1) << ": $";
        cout << *i << endl;
    }
}

void revalue(double r, double * arr, double * n)
{
    for(double * i = arr; i<n; i++)
        *i *= r;
}

8.

#include <iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = {"Spring", "Summer", "Fall", "Winter"}; //用const char*數組存儲表示季度名稱的字符串
void fill(double * pa);
void show(double * da);

int main()
{
    double expenses[Seasons];
    fill(expenses);
    show(expenses);

    return 0;
}

void fill(double * pa) //用double數組存儲開支; double * pa,同 double pa[]
{
    for(int i=0; i<Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> pa[i];
    }
}

void show(double * da)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for(int i=0; i<Seasons; i++)
    {
        cout << Snames[i] << ": $" << da[i] << endl;
        total += da[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

9.

#include <iostream>
using namespace std;
const int SLEN = 30;
struct student {
        char fullname[SLEN];
        char hobby[SLEN];
        int ooplevel;
};
int getinfo(student pa[], int n);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int n);

int main()
{
        cout << "Enter class size: ";
        int class_size;
        cin >> class_size;
        while (cin.get() != '\n')
            continue;

        student * ptr_stu = new student[class_size];
        int entered = getinfo(ptr_stu, class_size);
        for (int i = 0; i < entered; i++)
        {
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
        }
        display3(ptr_stu, entered);
        delete[] ptr_stu;
        cout << "Done\n";
    return 0;
}

int getinfo(student pa[], int n) //輸入結構體信息,返回實際輸入個數
{
    int count=0;
    for(int i=0; i<n; i++)
    {
        cout << "Enter fullname: ";
        cin >> pa[i].fullname;
        cout << "Enter hobby: ";
        cin >> pa[i].hobby;
        cout << "Enter ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get(); //不能省,否則輸入有問題!!!
        count++;
    }
    cout << "Enter over!\n";
    return count;
}

void display1(student st) //顯示結構體信息
{
    cout << "Fullname: " << st.fullname << "\n";
    cout << "Hobby: " << st.hobby << "\n";
    cout << "Ooplevel: " << st.ooplevel << "\n";
}

void display2(const student *ps) //按指針顯示結構體信息
{
    cout << "Fullname: " << ps->fullname << "\n";
    cout << "Hobby: " << ps->hobby << "\n";
    cout << "Ooplevel: " << ps->ooplevel << "\n";
}

void display3(const student pa[], int n) //指針指向首地址,顯示結構體信息
{
    for(int i=0; i<n; i++)
    {
        cout << "Fullname: " << pa[i].fullname << "\n";
        cout << "Hobby: " << pa[i].hobby << "\n";
        cout << "Ooplevel: " << pa[i].ooplevel << "\n";
    }
}

10.

#include <iostream>
using namespace std;
double add(double x, double y);
double sub(double x, double y);
double calculate(double x, double y, double(*pf)(double x, double y) );

int main()
{
    while (1)
    {
        cout << "Enter 2 num ('q' to quit!): ";
        double x, y;
        cin >> x >> y;
        if(!cin)
        {
            cout << "Quit!\n";
            break;
        }
        double result1 = calculate(x, y, add);
        double result2 = calculate(x, y, sub);
        cout << "x" << "+" << "y" << "=" << result1 << endl;
        cout << "x" << "-" << "y" << "=" << result2 << endl;
    }

    return 0;
}

double add(double x, double y)
{
    return x+y;
}

double sub(double x, double y)
{
    return x-y;
}

double calculate(double x, double y, double(*pf)(double x, double y) ) //指針數組
{
    double result;
    result = (*pf)(x, y);
    return result;
}

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