概述
C++PrimerPlus 第七章 函数-C++的编程模块(编程练习含答案)
1、编写一个程序,不断要求用户输入两个数,直到其中的一个为0。对于每两个数,程序将使用一个函数来计算它们调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
答案:
#include<iostream> using namespace std; double average(double, double); int main() { double x, y; cout << "Enter two numbers: "; while (cin >> x >> y) { if (x == 0 || y == 0) break; double ave = average(x, y); cout << "Average: " << ave << endl; cout << "Enter two numbers: "; } return 0; } double average(double x, double y) { double ave; ave = 2.0 * x * y / (x + y); return ave; }
2、编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。
答案:
#include <iostream> using namespace std; int input(double grade[], int limit); void show(double grade[], int num); double compute(double grade[], int num); int main() { const int limit = 10; double socre[limit]; int num = input(socre, limit); show(socre, num); cout << "平均值为:" << compute(socre, num) << endl; system("pause"); return 0; } int input(double grade[], int limit) { int i; double sc; for (i = 0; i < limit; i++) { cout << "enter the scores of golf(q to quit!):"; cin >> sc; if (!cin) { cin.clear(); while (cin.get() != 'n') continue; cout << "end entering!n"; break; } grade[i] = sc; } return i; } void show(double grade[], int num) { for (int i = 0; i < num; i++) cout << grade[i] << endl; } double compute(double grade[], int num) { double sum = 0; for (int i = 0; i < num; i++) sum += grade[i]; return double(sum / num); }
3、下面是一个结构声明:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a. 编写一个函数,按值传递box结构,并显示每个成员的值。
b. 编写一个函数,传递box结构的地址,并将volume成员设置为其他三维长度的乘积。
c. 编写一个使用这两个函数的简单程序。
答案:
#include <iostream> using namespace std; struct box { char maker[40]; float height; float width; float length; float volume; }; void ShowBox(box); float VolumeBox(box*); int main() { cout << "Please Enter The Box Information:n"; box b; cout << "Enter the maker: "; cin >> b.maker; cout << "Enter the height: "; cin >> b.height; cout << "Enter the width: "; cin >> b.width; cout << "Enter the length: "; cin >> b.length; b.volume = VolumeBox(&b); ShowBox(b); return 0; } void ShowBox(box b) { cout << "maker: " << b.maker << endl; cout << "height: " << b.height << endl; cout << "width: " << b.width << endl; cout << "length: " << b.length << endl; cout << "volume: " << b.volume << endl; } float VolumeBox(box* b) { b->volume = b->height * b->width * b->length; return b->volume; }
4、许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码;还可以从第二区间(如1~27)选择一个号码(称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中正确选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。
答案:
#include<iostream> using namespace std; long double probability(unsigned number, unsigned numbers); int main() { double total, choices, temp; cout << "Enter the total number of choices on the game card andn"; cout << "the number of picks allowed and the second section(<= total number):" << endl; while ((cin >> total >> choices >> temp) && choices <= total && temp <= total) { cout << "you have one chance in "; cout << probability(total, choices) * probability(temp, 1); cout << " of winning.n"; cout << "Next two numbers (q to quit): "; } cout << "byen"; return 0; } long double probability(unsigned number, unsigned numbers) { long double result = 1.0; long double n; unsigned p; for (n = number, p = numbers; p > 0; n--, p--) result = result * n / p; return 1.0 / result; }
5、定义一个递归函数,接受一个整数参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3*2!,依此类推;而0!被定义为1。通用的计算公式是,如果n大于零,则n! = n*(n-1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
答案:
#include <iostream> using namespace std; int factorial(int x); int main() { int n; while (true) { cout << "Please Enter n: "; cin >> n; while (!cin) { cin.clear(); while (cin.get() != 'n') continue; } cout << "The factorial of " << n << " is: " << factorial(n) << endl; } return 0; } int factorial(int x) { int n; if (x == 0) { n = 1; } else { n = x * factorial(x - 1); } return n; }
6、编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。
Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。
Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。
程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素,然后显示数组。
答案:
#include<iostream> using namespace std; const int SIZE = 10; int Fill_array(double[], int); void Show_array(double[], int); void Reverse_array(double[], int); int main() { double ar[SIZE]; int i = Fill_array(ar, SIZE); cout << "Show Array:n"; Show_array(ar, i); cout << "Reverse Array:n"; Reverse_array(ar, i); Show_array(ar, i); return 0; } int Fill_array(double ar[], int n) { int i = 0; double number; cout << "Enter " << n << " numbers: " << endl; while (i<n && cin >> number) { ar[i++] = number; } return i; } void Show_array(double ar[], int n) { for (int i = 0; i < n; i++) { cout << ar[i] << " "; } cout << endl; } void Reverse_array(double ar[], int n) { double temp; for (int i = 0; i < n / 2; i++) { temp = ar[i]; ar[i] = ar[n - 1 - i]; ar[n - 1 - i] = temp; } }
7、修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。fill_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置;其他的函数可以将该指针作为第二个参数,以标识数据结尾。
答案:
#include<iostream> using namespace std; const int Max = 5; double* fill_array(double* ar, double* limit); void show_array(const double* ar, double* size); void revalue(double r, double* ar, double* size); int main() { double properties[Max]; double* size = fill_array(properties, properties + Max); show_array(properties, size); if (size > 0) { cout << "Enter revaluation factor: "; double factor; while (!(cin >> factor)) { cin.clear(); while (cin.get() != 'n') continue; cout << "Bad input; Please enter a number: "; } revalue(factor, properties, size); show_array(properties, size); } cout << "Done.n"; return 0; } double* fill_array(double* ar, double* limit) { double temp; int i = 0; double* p; for (p = ar; p < limit; p++,i++) { cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != 'n') continue; cout << "Bad input; input process terminated.n"; break; } else if (temp < 0) break; *p = temp; } return p; } void show_array(const double* ar, double* size) { for (int i = 0; ar < size; ar++,i++) { cout << "Property #" << (i + 1) << ": $"; cout << *ar << endl; } } void revalue(double r, double* ar, double* size) { for (int i = 0; ar < size; ar++,i++) *ar = (*ar) * r; }
8、在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a. 使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b. 使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种设计与使用array类的基本设计类似。
答案:
a.
#include<iostream> using namespace std; const int Seasons = 4; const char* Snames[Seasons] = { "Spring", "Summer","Fall","Winter" }; void fill(double ar[], const int n); void show(double ar[], const int n); int main() { double arPay[Seasons]; fill(arPay,Seasons); show(arPay,Seasons); return 0; } void fill(double ar[], const int n) { for (int i = 0; i < n; i++) { cout << "Enter " << Snames[i] << " expenses"; cin >> ar[i]; } } void show(double ar[], const int n) { double total = 0.0; cout << "nEXPENSESn"; for (int i = 0; i < n; i++) { cout << Snames[i] << ": $" << ar[i] << endl; total += ar[i]; } cout << "Total Expenses: $" << total << endl; }
b.
#include<iostream> using namespace std; const int Seasons = 4; const char* Snames[Seasons] = { "Spring", "Summer","Fall","Winter" }; struct pay { double arPay[4]; }; void fill(pay *p, const int n); void show(pay *p, const int n); int main() { pay* expenses = new pay; fill(expenses,Seasons); show(expenses,Seasons); return 0; } void fill(pay *p, const int n) { for (int i = 0; i < n; i++) { cout << "Enter " << Snames[i] << " expenses"; cin >> p->arPay[i]; } } void show(pay *p, const int n) { double total = 0.0; cout << "nEXPENSESn"; for (int i = 0; i < n; i++) { cout << Snames[i] << ": $" << p->arPay[i] << endl; total += p->arPay[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; }; // getinfo() has two arguments: a pointer to the first element of // an array of student structures and an int representing the // number of elements of the array. The function solicits and // stores data about students. It terminates input upon filling // the array or upon encountering a blank line for the student // name. The function returns the actual number of array elements // filled. int getinfo(student pa[], int n); //display1() takes a student structures as an argument //and displays its contents void display1(student st); //display2() takes the address of student structure as an //argument and displays the structure's contents void display2(const student* ps); //display3() takes the address of the first element of an array //of student structures and the number of array elements as //arguments and displays the contents of the structures 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 << "Donen"; return 0; }
答案:
#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 << "Donen"; return 0; } int getinfo(student pa[], int n) { for (int i = 0; i < n; i++) { cout << "Enter the information of NO." << i + 1 << " student:n"; cout << "Fullnam: "; cin.getline(pa[i].fullName, SLEN); cout << "Hobby: "; cin.getline(pa[i].hobby, SLEN); cout << "OOPLevel: "; cin >> pa[i].ooplevel; cin.get(); } return n; } void display1(student st) { cout << "Fullname: " << st.fullName << endl; cout << "Hobby: " << st.hobby << endl; cout << "OOPLevel: " << st.ooplevel << endl; } void display2(const student* ps) { cout << "FullName: " << ps->fullName << endl; cout << "Hobby: " << ps->hobby << endl; cout << "OOPLevel: " << ps->ooplevel << endl; } void display3(const student pa[], int n) { for (int i = 0; i < n; i++) { cout << "Fullname: " << pa[i].fullName << endl; cout << "Hobby: " << pa[i].hobby << endl; cout << "OOPLevel: " << pa[i].ooplevel << endl; } }
10、设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值。calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假设add()函数的定义如下:
double add(double x, double y)
{
return x + y;
}
则下述代码中的函数调用将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9):
double q = calculate(2.5, 10.4, add);
请编写一个程序,它调用上述两个函数和至少另一个与add()类似的函数。该程序使用循环来让用户成对地输入数字。对于每对数字,程序都使用calculate()来调用add()和至少一个其他的函数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。提示:下面是声明这种指针数组的方式,其中包含三个指针:
double (*pf[3])(double, double);
可以采用数组初始化语法,并将函数名作为地址来初始化这样的数组。
答案:
#include<iostream> using namespace std; double add(double, double); double subtract(double, double); double multiply(double, double); double(*p[3])(double, double) = { add,subtract,multiply }; double calculate(double, double, double(*pf)(double, double)); int main() { double x, y; cout << "Enter two numbers: n"; while (cin >> x >> y) { cout << x << " + " << y << " = " << calculate(x, y, p[0]) << endl; cout << x << " - " << y << " = " << calculate(x, y, p[1]) << endl; cout << x << " * " << y << " = " << calculate(x, y, p[2]) << endl; cout << "Enter two numbers: n"; } cout << "Done!n"; return 0; } double calculate(double n, double m, double(*pf)(double a, double b)) { return(*pf)(n, m); } double add(double x, double y) { return x + y; } double subtract(double x, double y) { return x - y; } double multiply(double x, double y) { return x * y; }
最后
以上就是欣喜皮皮虾为你收集整理的C++PrimerPlus 第七章 函数-C++的编程模块(编程练习含答案)C++PrimerPlus 第七章 函数-C++的编程模块(编程练习含答案)的全部内容,希望文章能够帮你解决C++PrimerPlus 第七章 函数-C++的编程模块(编程练习含答案)C++PrimerPlus 第七章 函数-C++的编程模块(编程练习含答案)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复