C++PrimerPlus中文第六版第7章编程练习答案
1、
#include<iostream> using namespace std; double computeHarmonicMean(double x, double y); int main() { double x, y; cout << "Enter two numbers(x!=0 and y!=0): "; bool valid; valid = (bool)(cin >> x >> y); while (!valid) { cin.clear(); while (cin.get() != '\n'); cout << "Enter invalid! enter again!\n"; cout << "Please enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); } while (x != 0 and y != 0) { cout <<"Harmonic mean: " << computeHarmonicMean(x, y) << endl; cout << "Enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); while (!valid) { cin.clear(); while (cin.get() != '\n'); cout << "Enter invalid! enter again!\n"; cout << "Please enter two numbers(x!=0 and y!=0): "; valid = (bool)(cin >> x >> y); } } cout << "x==0 or y==0 holds to quit\n"; } double computeHarmonicMean(double x, double y) { return 2.0 * x * y / (x + y); }
2、
#include<iostream> using namespace std; const int Size = 10; int getScore(double* score, int n); void showScore(double* score, int n); void compute_showAve(double* score, int n); int main() { double golfScore[Size]; int getNumber = getScore(golfScore, Size); showScore(golfScore, getNumber); compute_showAve(golfScore, getNumber); } void compute_showAve(double* score, int n) { double sum = 0; for (int i = 0; i < n; i++) sum += score[i]; cout << "Ave is " << sum / n << endl; } void showScore(double* score, int n) { for (int i = 0; i < n; i++) cout << "score[" << i << "]=" << score[i] << endl; } int getScore(double* score, int n) { double x; int i = 0; cout << "Enter the " << i + 1 << "th score(negative or not number to quit): "; while (cin >> x and x >= 0 and i < n) { cout << "Enter the " << i + 1 << "th score(negative or not number to quit): "; *(score+i) = x; i++; } cout << "Entering up to " << n << " numbers or error occurred, the input process is to end. " << i << " numbers have been gotten.\n"; return i; }
3、
#include<iostream> using namespace std; struct box { char maker[40]; float height; float width; float length; float volume; }; void showBox(box crate); void setVolume(box* crate); int main() { box crate = { "lenovo", 0.6, 0.2, 0.35, 0.00}; showBox(crate); setVolume(&crate); showBox(crate); } void setVolume(box* crate) { crate->volume = (crate->height) * (crate->width) * (crate->length); } void showBox(box crate) { cout << "maker: " << crate.maker << endl; cout << "height: " << crate.height << endl; cout << "width: " << crate.width << endl; cout << "length: " << crate.length << endl; cout << "volume: " << crate.volume << endl; }
4、
#include<iostream> using namespace std; int main() { double prob = 1; for (int i = 0; i < 5; i++) { prob *= (47 - i) / (1 + i); } prob *= 27; cout.precision(10); cout.setf(ios::fixed); cout << "The probability of first prize is: " << 1/prob * 100 << "%\n"; }
5、
#include<iostream> using namespace std; long long getFactorial(int n); int main() { int n; long long factorial; cout << "Enter n(negative or NoN to quit): "; while (cin >> n and n >= 0) { factorial = getFactorial(n); cout << "the factorial of " << n << " is " << factorial << endl; cout << "Enter n(negative or NaN to quit): "; } cout << "n<0 or invalid input for n to quit.\n"; } long long getFactorial(int n) { if (n == 0 or n == 1) { return 1; } else { return n * getFactorial(n - 1); } }
6、
#include<iostream> using namespace std; const int Size = 10; int Fill_array(double* ar, int n); void Show_array(const double ar[], int n); void Reverse_array(double* begin, double* end); int main() { double dar[Size]; int fillNum; fillNum = Fill_array(dar, Size); Show_array(dar, fillNum); Reverse_array(dar, dar+fillNum-1); Show_array(dar, fillNum); Reverse_array(dar + 1, dar + fillNum-2); Show_array(dar, fillNum); } void Reverse_array(double* begin, double* end) { if (begin >= end) { cout << "quit(Because the length of the array to be flipped is less than or equal to 0)\n"; return; } else { double temp; while (begin < end) { temp = *begin; *begin = *end; *end = temp; begin++; end--; } } } void Show_array(const double ar[], int n) { for (int i = 0; i < n; i++) cout << "ar[" << i << "]=" << ar[i] << endl; } int Fill_array(double* ar, int n) { int i = 0; cout << "Enter the " << i + 1 << "th number of the array(NaN to quit): "; while (cin >> *(ar + i++) and i < n) { cout << "Enter the " << i+1 << "th number of the array(NaN to quit): "; } cout << "Filling quit by up to " << i << " numbers filled or NaN entered.\n" << i << " numbers have been filled.\n"; return i; }
7、
#include<iostream> using namespace std; const int Max = 5; double* fill_array(double* begin, double* end); void show_array(const double* begin, const double* end); void revalue(double r, double* begin, double* end); int main() { double properties[Max]; double* end = fill_array(properties, properties+Max); show_array(properties, end); if (end > properties) { cout << "Enter revaluation factor: "; double factor; while (!(cin >> factor)) { cin.clear(); while (cin.get() != '\n'); cout << "Bad input; Please enter a number: "; } revalue(factor, properties, end); show_array(properties, end); } cout << "Done.\n"; } void revalue(double r, double* begin, double* end) { while (begin < end) { *begin *= r; begin++; } } void show_array(const double* begin, const double* end) { for (int i = 0; begin + i != end; i++) { cout << "properties[" << i << "]=" << *(begin + i) << endl; } } double* fill_array(double* begin, double* end) { int i = 0; while ((begin+i) < end) { cout << "Enter value #" << i + 1<<": "; cin >> *(begin + i); if (!cin or *(begin+i) < 0) { cin.clear(); while (cin.get() != '\n'); cout << "Bad input; input process terminated.\n"; break; } i++; } return begin + i; }
8、
#include<iostream> using namespace std; const int Seasons = 4; const char* Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" }; struct expense { double expenses[Seasons]; }; void fill(double* enpenses); void fill(expense* exp); void show(double expenses[]); void show(expense* exp); int main() { double expenses[Seasons]; fill(expenses); show(expenses); expense exp; fill(&exp); show(&exp); } void show(expense* exp) { double total = 0.0; for (int i = 0; i < Seasons; i++) { cout << Snames[i] << ": $" << exp->expenses[i] << endl; total += exp->expenses[i]; } cout << "Total Expenses: $" << total << endl; } void fill(expense* exp) { for (int i = 0; i < Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: $"; cin >> exp->expenses[i]; } } void show(double expenses[]) { double total = 0.0; for (int i = 0; i < Seasons; i++) { cout << Snames[i] << ": $" << expenses[i] << endl; total += expenses[i]; } cout << "Total Expenses: $" << total << endl; } void fill(double* expenses) { for (int i = 0; i < Seasons; i++) { cout << "Enter " << Snames[i] << " expenses: "; cin >> expenses[i]; } }
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(student* ps); void display3(const student pa[], int n); int main() { cout << "Enter class size: "; int class_size; while (!(cin >> class_size)) { cin.clear(); while (cin.get() != '\n'); cout << "class size invalid! please enter again: "; } cin.clear(); while (cin.get() != '\n'); 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"; } void display3(const student pa[], int n) { for (int i = 0; i < n; i++) { cout << "student #" << i + 1 <<": " << endl; cout << "fullname: " << pa[i].fullname << endl; cout << "hobby: " << pa[i].hobby << endl; cout << "ooplevel: " << pa[i].ooplevel << endl; } } void display2(student* ps) { cout << "display2:\n"; cout << "fullname: " << ps->fullname << endl; cout << "hobby: " << ps -> hobby << endl; cout << "ooplevel: " << ps->ooplevel << endl; } void display1(student st) { cout << "display1:\n"; cout << "fullname: " << st.fullname << endl; cout << "hobby: " << st.hobby << endl; cout << "ooplevel: " << st.ooplevel << endl; } int getinfo(student pa[], int n) { int i; for (i = 0; i < n; i++) { cout << "Enter #" << i + 1 << " student's info:\n"; cout << "fullname: "; cin.getline(pa[i].fullname, SLEN); if (strlen(pa[i].fullname) == 0) { cout << "fullname is empty, quit!\n"; break; } cout << "hobby: "; cin.getline(pa[i].hobby, SLEN); cout << "ooplevel: "; cin >> pa[i].ooplevel; cin.clear(); while (cin.get() != '\n'); } cout << i << " students' info have been gotten.\n"; return i; }
10、
#include<iostream> using namespace std; double add(double x, double y); double sub(double x, double y); double mul(double x, double y); double calculate(double x, double y, double (*pf)(double, double)); int main() { double x, y; cout << "Enter x and y: "; double (*pf[3])(double, double) = { add, sub, mul }; while (cin >> x >> y) { for (int i = 0; i < 3; i++) cout << "the result of calculate is: " << calculate(x, y, pf[i]) << endl; cout << "Enter x and y: "; } /* while (cin >> x >> y) { cout << "The sum of x and y is: " << calculate(x, y, add) << endl; cout << "The difference between x and y is: " << calculate(x, y, sub) << endl; cout << "The multiply of x and y is: " << calculate(x, y, mul) << endl; cout << "Enter x and y: "; } */ } double calculate(double x, double y, double (*pf)(double, double)) { return (*pf)(x,y); } double mul(double x, double y) { return x* y; } double sub(double x, double y) { return x - y; } double add(double x, double y) { return x + y; }

浙公网安备 33010602011771号