C++ primer plus(第六版)第七章练习题

来源:互联网 发布:music算法matlab 编辑:程序博客网 时间:2024/06/05 19:47
/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第一题/********************************************************************************************************************************************************/#include<iostream>double cal_hamony(double x, double y);int main(void){double x = 0, y = 0;std::cout << "Enter two numbers: ";while (std::cin >> x >> y){if ((x + y) == 0)std::cout << "x can not equal to -y.\n";else if ((x != 0) && (y != 0)){std::cout << cal_hamony(x, y) << std::endl;}else if ((x == 0) || (y == 0))break;std::cout << "Enter two numbers: ";}return 0;}double cal_hamony(double x, double y){return 2.0 * x * y / (x + y);}


/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第二题/********************************************************************************************************************************************************/#include<iostream>using namespace std;int input(double arr[]);double cal_average(double arr[], int n);void show_average(double output);int main(void){double score[10] = { 0 };int count = 0;cout << "Enter the scores: \n";count = input(score);show_average(cal_average(score, count));return 0;}int input(double arr[]){int i = 0;for (; i < 10; ++i){cin >> arr[i];if (cin.fail())break;}return i;}double cal_average(double arr[], int n){double temp = 0;for (int i = 0; i < n; ++i){temp += arr[i];}return temp / n;}void show_average(double output){cout << "The average is " << output << endl;}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第三题/********************************************************************************************************************************************************/#include<iostream>using namespace std;struct box{char maker[40];float height;float width;float length;float volume;};void show_box(box);void set_box_volume(box*);int main(void){box metalbox = { "Li Ha", 20, 30, 20, 0 };show_box(metalbox);set_box_volume(&metalbox);cout << endl << endl;show_box(metalbox);return 0;}void show_box(box a){cout << "maker: " << a.maker << endl<< "heigth: " << a.height << endl<< "width: " << a.width << endl<< "length:" << a.length << endl<< "volume: " << a.volume << endl;}void set_box_volume(box *a){a->volume = a->height * a->length * a->width;}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第四题/********************************************************************************************************************************************************/#include<iostream>using namespace std;long double probality(unsigned int numbers, unsigned picks, unsigned low, unsigned hig);int main(void){unsigned total = 0, choices = 0;unsigned special_low = 0, special_hig = 0;cout << "Enter the total number of choices on the game card and the number of picks allowed.\n";cin >> total >> choices;cout << "Enter the special range.\n";cin >> special_low >> special_hig;while ((special_hig >= special_low) && (total >= choices) && (total >= special_hig)){cout << "You have one chance in ";cout << probality(total, choices, special_low, special_hig);cout << " of winning.\n";cout << "Next four numbers (q to quit):";if (!(cin >> total >> choices))break;cin >> special_low >> special_hig;}cout << "bye.\n";return 0;}long double probality(unsigned int numbers, unsigned picks, unsigned low, unsigned hig){long double result = 1.0;for (; picks > 0; --picks, --numbers)result = result * numbers / picks;result = result * (hig - low + 1);return result;}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第五题/********************************************************************************************************************************************************/#include<iostream>using namespace std;long double factory(int n);int main(void){cout << "Enter a number: ";int n;while (cin >> n){cout << n << "! = " << factory(n) << endl;cout << "Enter a number: ";}cout << "bye.\n";return 0;}long double factory(int n){if (n == 0 || n == 1)return 1;elsereturn n * factory(n - 1);}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第六题/********************************************************************************************************************************************************/#include<iostream>using namespace std;int Fill_array(double arr[], int n);void Show_array(double arr[], int n);void Reverse_array(double arr[], int n);int main(void){cout << "Enter the max length of a array: ";int n = 0, count = 0;cin >> n;double * data = new double[n];count = Fill_array(data, n);cout << "The array is: ";Show_array(data, count);Reverse_array(data, count);cout << "After the first reverse: ";cout << endl;Show_array(data, count);Reverse_array(data + 1, count - 2);cout << "After the second reverse: ";for (int i = 0; i < count; ++i)cout << data[i] << " ";cout << endl;delete[] data;cout << "bye.\n";return 0;}int Fill_array(double * arr, int n){int i = 0;cout << "Enter a array (the max length is " << n << "):";for (; i < n; ++i){if (!(cin >> *(arr + i)))break;}cout << "done.\n";return i;}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){double temp = 0;for (int i = 0, j = n - 1; i < j; ++i, --j){temp = *(arr + i);*(arr + i) = *(arr + j);*(arr + j) = temp;}}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第七题/********************************************************************************************************************************************************/#include<iostream>using namespace std;double* Fill_array(double arr[], int n);void Show_array(double arr[], double *);void Reverse_array(double arr[], double *);int main(void){cout << "Enter the max length of a array: ";int n = 0, count = 0;cin >> n;double * start_data = new double[n];double * end_data = Fill_array(start_data, n);cout << "The array is: ";Show_array(start_data, end_data);Reverse_array(start_data, end_data);cout << "After the first reverse: ";cout << endl;Show_array(start_data, end_data);Reverse_array(start_data + 1, end_data - 1);cout << "After the second reverse: ";//for (int i = 0; i < count; ++i)//cout << start_data[i] << " ";cout << endl;Show_array(start_data, end_data);delete[] start_data;cout << "bye.\n";return 0;}double * Fill_array(double * arr, int n){int i = 0;cout << "Enter a array (the max length is " << n << "):";for (; i < n; ++i){if (!(cin >> *(arr + i)))break;}cout << "done.\n";return (arr + i);}void Show_array(double * start_arr, double * end_arr){for (int i = 0; i < (end_arr - start_arr); ++i){cout << *(start_arr + i) << " ";}cout << endl;}void Reverse_array(double * start_arr, double * end_arr){double temp = 0;for (int i = 0, j = (end_arr - start_arr - 1); i < j; ++i, --j){temp = *(start_arr + i);*(start_arr + i) = *(start_arr + j);*(start_arr + j) = temp;}}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第八题a/********************************************************************************************************************************************************/#include<iostream>using namespace std;void fill(double * data,int n);void show(const double * data, const int n);const int seasons = 4;const char * Snames[4] = { "Spring", "Summer", "Fall", "Winter" };int main(void){double *  expense = new double[seasons];fill(expense,seasons);show(expense,seasons);delete[] expense;return 0;}void fill(double * data, int n){for (int i = 0; i < n; ++i){cout << "Enter " << Snames[i] << " expense:";cin >> *(data + i);}}void show(const double * data, const int n){double total = 0;for (int i = 0; i < n; ++i){cout << Snames[i] << ": " << *(data + i) << endl;total += *(data + i);}cout << "total: " << total << endl;}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第八题b/********************************************************************************************************************************************************/#include<iostream>using namespace std;struct cost{double expense;};void fill(cost * data, int n);void show(const cost * data, const int n);const int seasons = 4;const char * Snames[4] = { "Spring", "Summer", "Fall", "Winter" };int main(void){//double *  cost_season = new double[seasons];cost cost_season[seasons] = { 0 };fill(cost_season, seasons);show(cost_season, seasons);return 0;}void fill(cost * data, int n){for (int i = 0; i < n; ++i){cout << "Enter " << Snames[i] << " expense:";cin >> (data + i)->expense;}}void show(const cost * data, const int n){double total = 0;for (int i = 0; i < n; ++i){cout << Snames[i] << ": " << (data + i)->expense << endl;total += (data + i)->expense;}cout << "total: " << total << endl;}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第九题/********************************************************************************************************************************************************/#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(void){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 << "student's name    :"; cin.getline(pa[i].fullname, SLEN);if ((pa + i)->fullname == "\n")break;cout << "student's hobby   :"; cin.getline(pa[i].hobby, SLEN);cout << "student's ooplevel:"; (cin >> pa[i].ooplevel).get();++count;}return count;}void display1(student st){cout << st.fullname << ": " << st.hobby << " " << st.ooplevel << endl;}void display2(const student * ps){cout << (*ps).fullname << ": " << (*ps).hobby << " " << (*ps).ooplevel << endl;}void display3(const student pa[], int n){for (int i = 0; i < n; ++i){cout << pa[i].fullname << ": " << pa[i].hobby << " " << pa[i].ooplevel << endl;}}

/********************************************************************************************************************************************************/C++ primer plus(第六版)第七章 第十题/********************************************************************************************************************************************************/#include<iostream>using namespace std;const int LOOP = 3;double add(double, double);double sub(double, double);double mul(double, double);double calculate(double, double, double (*pf)(double, double));int main(void){double(*pf[LOOP])(double, double);pf[0] = add; pf[1] = sub; pf[2] = mul;double x = 0, y = 0;cout << "Enter tow numbers: ";while (cin >> x >> y){for (int i = 0; i < LOOP; ++i){cout << calculate(x, y, (*pf[i])) << ", ";}cout << endl << "Enter tow numbers: ";}return 0;}double add(double x, double y){return x + y;}double sub(double x, double y){return x - y;}double mul(double x, double y){return x * y;}double calculate(double x, double y, double(*pf)(double, double)){return (*pf)(x, y);}


0 0