C++primer plus 第七章程式設計練習
阿新 • • 發佈:2018-12-20
本人用code::block 編寫,如需參考,善用Ctrl+shift+C 和 Ctrl + shift + X 快捷鍵 如有任何錯誤或疑問,歡迎留言
#include <iostream> using namespace std; double p7_1(); int p7_2enter(double score[]); void p7_2show(double score[], int num); double p7_2ave(double score[], int num); struct box { char maker[40]; float height; float width; float length; float volume; }; void p7_3show(box a); void p7_3setv(box *p); double p7_4(unsigned numbers, unsigned picks, unsigned senumbers); long p7_5(int n); int p7_6Fill_array(double execute[], int num); void p7_6Show_array(double execute[], int num); void p7_6Reverse_array(double execute[], int num); double* p7_7fill_array(double ar[], int limit); void p7_7show_array(const double ar[], double* last); void p7_7revalue(double r, double ar[], double* last); const int Seasons = 4; const char* Snames[Seasons] = {"Spring", "Summer", "Fall", "Winter"}; struct Expenses { double expense; }; void p7_8fill(double ar[], const int Seasons); void p7_8fill2(Expenses ar[], const int Seasons); void p7_8show(double ar[], const int seasons); void p7_8show2(Expenses ar[], const int seasons); const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int opplevel; }; int p7_9getinfo(student pa[], int n); void p7_9display1(const student st); void p7_9display2(const student *st); void p7_9display3(const student[], int n); double p7_10caculate(double, double, double (*p)(double, double)); double p7_10add1(double,double); double p7_10add2(double,double); double p7_10add3(double,double); int main() { // 7.1 double ave = p7_1(); // while(1) // { // if (ave == -1) // break; // cout << "the harmonic average of the two number is: " << ave; // cout << endl; // ave = p7_1(); // } //7.2 double scores[10]={0}; // int num; // double ave; // // num = p7_2enter(scores); // p7_2show(scores, num); // ave = p7_2ave(scores, num); // cout << "The average of your scores is: " << ave << endl; //7.3 box haha = {"adidas", 10.0f, 20.0f, 30.0f}; // p7_3setv(&haha); // p7_3show(haha); // unsigned numbers, picks, senumbers; // double P; // cout << "Please enter the first numbers, picks and second numbers(q to quit):\n"; // while(cin >> numbers >> picks >> senumbers) // { // P = p7_4(numbers, picks, senumbers); // cout << "The rate you win is: " << P << endl; // cout << "next two three numbers (q to quit):\n"; // } //7.5 int n; // long answer; // cout << "Enter a integer, and I will give you the factorial: " << endl; // while(cin >> n) // { // answer = p7_5(n); // cout << "The answer is: " << answer << endl; // cout << "next: " << endl; // } // const int num = 10; // double test[num]; // int counter; // counter = p7_6Fill_array(test, num); // p7_6Show_array(test, counter); // p7_6Reverse_array(test, counter); // p7_6Show_array(test, counter); // const int Max = 5; // double properties[Max]; // double* endp = p7_7fill_array(properties, Max); // p7_7show_array(properties, endp); // if (endp > properties ) // { // cout << "Enter revaluation factor: "; // double factor; // while(!(cin >> factor)) // { // cin.clear(); // while (cin.get() != '\n') // { // continue; // } // } // p7_7revalue(factor, properties, endp); // p7_7show_array(properties, endp); // } // cout << "Done!\n"; // cin.get(); // cin.get(); //7.8 double expenses[Seasons]; // Expenses expenses2[Seasons]; // p7_8fill(expenses, Seasons); // cout << "next kind of array:\n"; // p7_8fill2(expenses2, Seasons); // // p7_8show(expenses, Seasons); // cout << "next kind of array:\n"; // p7_8show2(expenses2, Seasons); //7.9 cout << "Enter class size: "; // int class_size; // cin >> class_size; // while (cin.get() != '\n') // continue; // student * ptr_stu = new student[class_size]; // int entered = p7_9getinfo(ptr_stu, class_size); // cout << endl; // for (int i = 0; i < entered; i++) // { // p7_9display1(ptr_stu[i]); // p7_9display2(&ptr_stu[i]); // cout << endl; // } // p7_9display3(ptr_stu, entered); // delete [] ptr_stu; // cout << "Done!\n"; double a, b; cout << "Enter two numbers, and I will give you some kind of add include them:\n"; cout << "q to quit:\n"; double (*padd[3])(double, double) = {p7_10add1, p7_10add2, p7_10add3}; int slecter = 0; double answer; while (cin >> a >> b) { answer = p7_10caculate(a, b, padd[slecter % 3]); ++slecter; cout << "This time, add of the two number is: " << answer << endl; } return 0; } double p7_1() { double x,y; double ave; cout << "please enter 2 numbers:\n"; cin >> x >> y; ave = x*y; if (0 == ave) { cout << "IO termination, return -1:"; return -1.0; } ave = 2.0*ave / (x+y); return ave; } int p7_2enter(double score[]) { cout << "Please enter you scores, no more than 10, q to end early: " << endl; int i = 0; int count = 0; while (cin >> score[i]) { ++count; if (count >= 10) break; i++; } return count; } void p7_2show(double score[], int num) { cout << "this is your scores:\n"; for (int i =0; i<num; i++) { cout << score[i] << " "; } cout << endl; return; } double p7_2ave(double score[], int num) { double sum = 0; for (int i = 0; i<num; i++) { sum += score[i]; } return sum/num; } void p7_3show(box a) { cout << "maker: " << a.maker << endl; cout << "height: " << a.height << endl; cout << "length: " << a.length << endl; cout << "width: " << a.width << endl; cout << "volume: " << a.volume << endl; } void p7_3setv(box *p) { p->volume = p->height*p->length*p->width; } double p7_4(unsigned numbers, unsigned picks, unsigned senumbers) { double p = 1.0; unsigned i; for (i = 1; i<=picks; i++, numbers--) p = p * double(numbers) / double(i); p = (1.0 / p) * (1.0 / senumbers); return p; } long p7_5(int n) { static long m = 1; if(n == 0) return 1; else m = n*p7_5(n-1); return m; } int p7_6Fill_array(double execute[], int num) { int count = 0; cout << "enter the double number(q to quit): " << count+1 << endl; while (cin >> execute[count]) { count ++; if (count >= num) { cout << "too much!!" << endl; break; } cout << "enter the double number(q to quit):" << count+1 << endl; } return count; } void p7_6Show_array(double execute[], int num) { int i = 0; cout << "This is your array: " << endl; for (; i<num; i++) { cout << execute[i] << ' '; } cout << endl; } void p7_6Reverse_array(double execute[], int num) { double temp; int i = 0; num -=1; for (; i< num; i++, num--) { temp = execute[i]; execute[i] = execute[num]; execute[num] = temp; } } double* p7_7fill_array(double ar[], int limit) { double temp; int i; for(i = 0; i < limit; i++) { cout << "Enter value #" << (i + 1) << ": "; cin >> temp; if (!cin) { while(cin.get() != '\n') continue; cout << "bad input; input process terminated.\n"; break; } else if(temp < 0) break; ar[i] = temp; } cout << endl; return (ar + i-1); } void p7_7show_array(const double ar[], double* endp) { int i; for (i =0; (ar+i) <= endp; i++) { cout << "Property #" << (i+1) << ": $"; cout << *(ar+i) << endl; } } void p7_7revalue(double r, double ar[], double* endp) { for (int i =0; (ar+i)<= endp; i++) ar[i] = r; } void p7_8fill(double ar[], const int Seasons) { for ( int i = 0; i<Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: "; cin >> ar[i]; } } void p7_8show(double ar[], const int seasons) { double total = 0; cout << "\n EXPENSES\n"; for (int i = 0; i<Seasons; i++) { cout << Snames[i] << ": " << ar[i] << endl; total += ar[i]; } cout << "Total: " << total << endl; } void p7_8fill2(Expenses ar[], const int Seasons) { for ( int i = 0; i<Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: "; cin >> ar[i].expense; } } void p7_8show2(Expenses ar[], const int seasons) { double total = 0.0; cout << "\n EXPENSES\n"; for (int i = 0; i<Seasons; i++) { cout << Snames[i] << ": " << ar[i].expense<< endl; total += ar[i].expense; } cout << "Total: " << total << endl; } int p7_9getinfo(student pa[], int n) { int i; cout << "Enter a set of students' information, no more than " << n << "\nand a blank line name to quit: \n"; for(i = 0; i< n; i++) { cout << "Student " << i+1 << " name: "; cin.get(pa[i].fullname, 30).get(); if(!cin) { cin.clear(); cout << "blank line, input terminated!!\n"; break; } cout << "His hobby: "; cin.getline(pa[i].hobby, 30); cout << "His opplevel: "; cin >> pa[i].opplevel; cin.get(); } return i; } void p7_9display1(student st) { cout << "Name: " << st.fullname << endl; cout << "Hobby: " << st.hobby << endl; cout << "OPPlevel: " << st.opplevel << endl; } void p7_9display2(const student *st) { cout << "Name: " << st->fullname << endl; cout << "Hobby: " << st->hobby << endl; cout << "OPPlevel: " << st->opplevel << endl; } void p7_9display3(const student st[], int n) { cout << "STUDENT INFORMATION" << endl; for (int i = 0; i<n; i++) { cout << "Name: " << st[i].fullname << endl; cout << "Hobby: " << st[i].hobby << endl; cout << "OPPlevel: " << st[i].opplevel << endl << endl; } } double p7_10caculate(double a, double b, double (*p)(double, double)) { return p(a, b); } double p7_10add1(double a,double b) { return a *10 + b*10; } double p7_10add2(double a,double b) { return a * b; } double p7_10add3(double a,double b) { return a * 2 * b; }