一、復習題
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"); }

