一、复习题
6.为什么不对基本数据类型的函数参数使用const?
8.编写一个函数,将字符串中所有c1替换成c2,并返回替换次数。
#include<iostream> using namespace std; int replace(char *str, char c1, char c2) { int n = 0; while (*str) { if (*str == c1) { *str = c2; n++; } str++; } return n; } void main() { char str[] = "i love china,i love cpp"; cout << str << endl; int n=replace(str, 'l', 'L'); cout << str << endl<<n<<endl; system("pause"); }
typedef int zheng;//给int取个小名,可以用zheng代替int zheng a = 10;
struct app { char name[20]; int credit[3]; }; void f1(app *a); const char * f2(const app *a1, const app *a2); typedef void(*p_f1) (app *); typedef const char * (*p_f2) (const app *, const app *); void main() { p_f1 p1 = f1;//void(*p1)(app *) = f1; p_f2 p2 = f2;//const char *(*p1)(const app*, const app*) = f2; p_f1 ap[5];//void(*ap[5])(app *); p_f2 ap2[10];p_f2 *pa = ap2;//p_f2 (*pa)[10]; //或者 const char *(*pa[10])(const app*, const app*); }
二、编程练习
1.编写一个程序,不断要求用户输入两个数,直到其中一个为0。对于每两个数,程序将使用一个函数来计算它们的调和平均数,并将结果返回给main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下:
调和平均数 = 2.0 * x * y / (x + y)
#include <iostream> using namespace std; double fun(double a, double b) { if (a == -b) { cout << "调和平均数的分母不能为0" << endl; return 0; } return 2.0*a*b / (a + b); } void main() { cout << "请输入两个数(输入0结束):"; double a, b, ave; while (cin >> a >> b&&a&&b) { ave = fun(a, b); if (ave == 0) cout << "请重新输入两个数(输入0结束):"; else { cout << "调和平均数为:" << ave << endl; cout << "请输入两个数(输入0结束):"; } } system("pause"); }
2.编写一个程序,要求用户输入最多10个高尔夫成绩,并将其存储在一个数组中。程序允许用户提早结束输入,并在 一行上显示所有成绩,然后报告平均成绩。请使用3个数组处理函数来分别进行输入、显示和计算平均成绩。请使用3个数组 处理函数来分别进行输入、显示和计算平均成绩。
#include <iostream> using namespace std; int input(double *arr) { cout << "请输入高尔夫成绩:" << endl; int n; for (n = 0; n < 10; n++) { cout << "请输入第" << n+1 << "次的成绩(q结束输入):"; if (!(cin >> arr[n])) break; } return n; } void show(double *arr,int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } double ave(double *arr,int n) { if (n == 0) return 0; double sum=0.0; for (int i = 0; i < n; i++) sum += arr[i]; return sum / n; } void main() { double arr[10]; int n=input(arr); show(arr,n); cout <<endl<< "平均成绩为" << ave(arr,n) << endl; system("pause"); }
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 set(box *b) { b->volume = b->height*b->length*b->width; } void show(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; } void main() { box b = {"made in china",15,8,12}; set(&b); show(b); system("pause"); }
4.许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。在这些玩法中,玩家从一组被称为域号码 (field number)的号码中选择几个。例如,可以从域号码1~47中选择5个号码:还可以从第二个区间(如1~27)选择一个号码 (称为特选号码)。要赢得头奖,必须正确猜中所有的号码。中头奖的几率是选中所有域号码的几率与选中特选号码几率的乘积。 例如,在这个例子中,中头奖的几率是从47个号码中正确选取5个号码的几率与从27个号码中选择1个号码的几率的乘积。请修改程序清单7.4,以计算中得这种彩票头奖的几率。
#include <iostream> using namespace std; void main() { long double ans = 1.0; for (double i = 0.0; i < 5.0; i++) ans *=(5 - i)/(47 - i); ans *=1.0/ 27.0; cout << ans << endl; system("pause"); }
5.定义一个递归函数,接受一个整型参数,并返回该参数的阶乘。前面讲过,3的阶乘写作3!,等于3 * 2!,以此类推:而0!被定义为1.通用的计算公式是,如果n大于零 , 则n! = n * (n - 1)!。在程序中对该函数进行测试,程序使用循环让用户输入不同的值,程序将报告这些值的阶乘。
#include <iostream> using namespace std; long long recursion(int n) { if (n == 0 || n == 1) return 1; return n*recursion(n - 1); } void main() { int n; long long factorial; cout << "请输入一个整数:"; while (cin>>n) { factorial = recursion(n); cout << n << "的阶乘为" << factorial << endl; cout << "请输入一个整数:"; } system("pause"); }
6.编写一个程序,它使用下列函数:
Fill_array()将一个double数组的名称和长度作为参数。它提示用户输入double值,并将这些值存储到数组中。当数组被填满或 用户输入了非数字时,输入将停止,并返回实际输入了多少个数字。 Show_array()将一个double数组的名称和长度作为参数,并显示该数组的内容。 Reverse_array()将一个double数组的名称和长度作为参数,并将存储在数组中的值的顺序反转。 程序将使用这些函数来填充数组,然后显示数组;反转数组,然后显示数组;反转数组中除第一个和最后一个元素之外的所有元素, 然后显示数组。
#include <iostream> using namespace std; void Fill_array(double arr[], int n) { cout << "请输入一个double值:"; int i = 0; while (n&&cin >> arr[i]) //&&左右表达式换顺序会有截然不同的结果 { i++, n--; if(n!=0) cout << "请zai输入一个double值:"; } cout << "实际输入了"<<i<<"个数字"<<endl; } void Show_array(double arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } void Reverse_array(double arr[], int n) { for (int i = 1; i < n-1; i++) { if (i >= n / 2) break; int t = arr[i]; arr[i] = arr[n - 1 - i]; arr[n - 1 - i] = t; } } void main() { double arr[5]; Fill_array(arr, 5); Show_array(arr, 5); Reverse_array(arr, 5); Show_array(arr, 5); system("pause"); }
7.修改程序清单7.7中的3个数组处理函数,使之使用两个指针参数来表示区间。file_array()函数不返回实际读取了多少个数字,而是返回一个指针,该指针指向最后被填充的位置:其他的函数可以将该指针作为第二个参数,以标识数据结尾。
#include <iostream> using namespace std; double *fill_array(double *a) { int i = 0; while (cin >> a[i++]) { if (i == 5) break; } return &a[i]; } void show_array(double *a, double *b) { while (a != b) { cout << *a << "\t"; ++a; } cout << endl; } void revalue(double r, double *a, double *b) { while (a != b) { (*a) *= r; ++a; } } void main() { double a[5]; double *e = fill_array(a); show_array(a, e); revalue(0.5, a, e); show_array(a, e); system("pause"); }
8.在不使用array类的情况下完成程序清单7.15所做的工作。编写两个这样的版本:
a.使用const char *数组存储表示季度名称的字符串,并使用double数组存储开支。
b.使用const char *数组存储表示季度名称的字符串,并使用一个结构,该结构只有一个成员——一个用于存储开支的double数组。这种设计与使用array类的基本设计类似。
#include<iostream> using namespace std; const char *Sname[4] = { "Spring","Summer","Fall","Winter" }; void fill(double *p) { for (int i = 0; i < 4; i++) { cout << "Enter " << Sname[i] << " expenses:"; cin >> p[i]; } } void show(double *p) { double sum = 0.0; cout << "EXPENSES" << endl; for (int i = 0; i < 4; i++) { cout << Sname[i] << ": $" << p[i] << endl; sum += p[i]; } cout << "Sum expenses: $" << sum << endl; } void main() { double expense[4]; fill(expense); show(expense); system("pause"); }
#include<iostream> using namespace std; const char *Sname[4] = { "Spring","Summer","Fall","Winter" }; struct EXPENSES { double expense[4]; }; void fill(EXPENSES *p) { for (int i = 0; i < 4; i++) { cout << "Enter " << Sname[i] << " expenses:"; cin >> (*p).expense[i]; } } void show(EXPENSES *p) { double sum = 0.0; cout << "EXPENSES" << endl; for (int i = 0; i < 4; i++) { cout << Sname[i] << ": $" << (*p).expense[i] << endl; sum += (*p).expense[i]; } cout << "Sum expenses: $" << sum << endl; } void main() { EXPENSES e; fill(&e); show(&e); system("pause"); }
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); void main() { cout << "Enter class size: "; int class_size; cin >> class_size; while (cin.get() != '\n') continue; student * ptr_stu = new student[class_size];//用了new记得要delete 创建一个student数组 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"; system("pause"); } int getinfo(student pa[], int n) { for (int i = 0; i<n; i++) { cout << "Please enter the fullname:"; cin >> pa[i].fullname; cout << "Please enter the hobby:"; cin >> pa[i].hobby; cout << "Please enter the ooplevel:"; cin >> pa[i].ooplevel; } cout << "Enter end!"<<endl; return n; } void display1(student st) { cout << "display1:FullName:" << st.fullname << "\nhobby:" << st.hobby << "\nooplevel:" << st.ooplevel << endl; } void display2(const student *ps) { cout << "dispaly2:FullName:" << ps->fullname << "\nhobby:" << ps->hobby << "\nooplevel:" << ps->ooplevel << endl; } void display3(const student pa[], int n) { cout << "dispaly3:" << endl; for (int i = 0; i<n; i++) cout << i << ":FullName:" << pa[i].fullname << "\nhobby:" << pa[i].hobby << "\nooplevel:" << pa[i].ooplevel << endl; }
10.设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值、calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假如add()函数的定义如下:
{
return x + y;
}
则下述代码中的函数调用:
double q = calculate(2.5,10.4,add);
将导致calculate()把2.5和10.4传递给add()函数,并返回add()的返回值(12.9).请编写一个程序,它调用上述两个函数和至少另一个与add()类似的数。如果读者爱冒险,可以尝试创建一个指针数组,其中的指针指向add()样式的函数,并编写一个循环,使用这些指针连续让calculate()调用这些函数。
#include<iostream> using namespace std; double calculate(double x, double y, double(*pf)(double, double))//pf表示函数指针 { return (*pf)(x, y);//pf表示函数指针,是一个地址,*pf表示调用函数 } 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; } void main() { double(*pf[3])(double, double) = { add, sub, mean };//函数指针数组,分别指向三个不同函数 const char(*pch[3]) = { "sum", "difference", "mean" }; double a, b; cout << "Enter pairs of numbers (q to quit):"; int i; while (cin >> a >> b) { for (i = 0; i<3; i++) cout << calculate(a, b, pf[i]) << " = " << pch[i] << "\n"; cout << "Enter pairs of numbers (q to quit):"; } system("pause"); }