2020 我的C++学习之路 C++PrimerPlus第七章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第七章课后习题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以C++ Primer Plus為參考書籍,自身歸納知識(shí)點(diǎn),加深記憶。僅供參考,DEV C++已通過編譯運(yùn)行
。
練習(xí)1
練習(xí)2
#include<iostream> const int maxsize = 10; using namespace std;void show(int ar[], int size); int input(int ar[],int size); double avg(int ar[], int size);int main() {int golf[maxsize];int acsize;acsize = input(golf,maxsize);show(golf, acsize);cout << "golf average score is : " << avg(golf, acsize) << endl;return 0; }int input(int ar[],int size)//返回實(shí)際數(shù)組數(shù)以供后續(xù)函數(shù)調(diào)用該實(shí)際數(shù) {int cnt = 0;cout << "Enter golf score#" << cnt+1 << " : ";while ((cin >> ar[cnt]) && cnt < size){++cnt;cout << "Enter golf score#" << cnt+1 << " : ";}return cnt; }void show(int ar[], int size) {int cnt;cout << "Golf score is: ";for (cnt = 0; cnt < size; cnt++)cout << ar[cnt] << " ";cout << endl; }double avg(int ar[], int size) {int cnt, sum, average;for (cnt = 0,sum = 0; cnt < size; cnt++)sum += ar[cnt];average = sum / size;return average; }練習(xí)3
#include<iostream> using namespace std;struct box {char maker[40];float height;float width;float length;float volume; };void show(box abox); void showvolume(box* pbox);int main() {box xbox;cout << "Enter name: ";cin.getline(xbox.maker, 40);cout << "Enter height: ";cin >> xbox.height;cout << "Enter width: ";cin >> xbox.width;cout << "Enter length: ";cin >> xbox.length;showvolume(&xbox);show(xbox);return 0; }void show(box abox) {cout << "Name: " << abox.maker << endl;cout << "Height: " << abox.height << endl;cout << "Width: " << abox.width << endl;cout << "Length: " << abox.length << endl;cout << "Volume: " << abox.volume << endl; }void showvolume(box* pbox) {pbox->volume = pbox->height * pbox->length * pbox->width; }練習(xí)4
#include<iostream>long double probability(unsigned numbers, unsigned picks, unsigned special);int main() {using namespace std;unsigned total, choice, schoice;cout << "Enter the total and special number of choices and the pick allowed:" << endl;while ((cin >> total >> choice >> schoice) && choice <= total){cout << "You have one chance in ";cout << probability(total, choice, schoice);cout << " of winning." << endl;cout << "Next one: ";}cout << "Bye!" << endl;return 0; }long double probability(unsigned numbers, unsigned picks, unsigned special) {long double result = 1.0;long double n;unsigned p;for (n = numbers, p = picks; p > 0; n--, p--)result *= (n / p);result *= special;return result; }練習(xí)5
#include<iostream> using namespace std;long Factorial(int n);int main() {int num;cout << "Enter a number(alpha to quit):";while ((cin >> num) && num>=0){cout << num << "!= " << Factorial(num) << endl;cout << "NEXT: ";}return 0; }long Factorial(int n) {long fac;if (n > 0)fac = n * Factorial(n - 1);if (n == 0)//將遞歸最底層的fac(0)=1,否則fac會(huì)返回0;fac = 1;return fac;//vs2019顯示此處C6001警告,目前暫不清楚原因,但不影響。 }練習(xí)6
#include<iostream> using namespace std; const int maxsize = 10; int Fill_array(double*, int); void Show_array(double*, int); void Reverse_array(double*, int);int main() {double Array[maxsize];int size;size = Fill_array(Array, maxsize);Show_array(Array, size);Reverse_array(Array, size);Show_array(Array, size);return 0; }int Fill_array(double* array, int n) {int count = 0;cout << "Enter array elements#" << count + 1 << " : ";while ((cin >> array[count]) && count < n){++count;cout << "Enter array elements#" << count + 1 << " : ";}return count; }void Show_array(double* array, int n) {int count = 0;for (; count < n; ++count)cout << "array[" << count + 1 << "] = " << array[count] << endl; \cout << endl; }void Reverse_array(double* array, int n) {double temp;int begin, end;for (begin = 1, end = n - 2; begin < (n / 2); ++begin, --end){temp = array[begin];array[begin] = array[end];array[end] = temp;} }練習(xí)7
#include<iostream> using namespace std; const int max = 5; double* fill_array(double*, int); void show_array( double*, double*); void revalue(double, double*, double*);int main() {double properties[max];double* End = fill_array(properties, max);show_array(properties, End);if ((End - properties) >= 0){cout << "Enter revaluation factor: ";double factor;while (!(cin >> factor)){cin.clear();while (cin.get() != '\n')continue;cout << "Bad input;Please enter a number: ";}revalue(factor, properties, End);show_array(properties, End);}cout << "Done!" << endl;cin.get();cin.get();return 0; }double* fill_array(double* array, int n) {double temp ;int i;for (i = 0; i < n ; ++i){cout << "Enter value #" << i + 1 << " : ";cin >> temp;if (!cin){cin.clear();while (cin.get() != '\n')continue;cout << "Bad input; input process terminated" << endl;break;}else if (temp < 0)break;array[i] = temp;}return &array[i - 1]; } void show_array( double* begin, double* end) {double* start;for (start = begin; start <= end; ++start){cout << "Property #" << (start - begin + 1) << ":$ ";cout << *start << endl;} }void revalue(double r, double* begin, double* end) {double* start;for (start = begin; start <= end; ++start){*start *= r;} }練習(xí)8-1
#include<iostream> using namespace std; const int season = 4; const char *pseason[season] = { "Spring","Summer","Fall","Winter" }; void fill(double*, int); void show(double*, int);int main() {double Array[season];fill(Array, season);show(Array, season);return 0; }void fill(double* array, int n) {for (int i = 0; i < season; ++i){cout << "Enter " << pseason[i] << " espenses: ";cin >> array[i];} }void show(double* array, int n) {double total = 0;cout << "\nEXPENSES\n";for (int i = 0; i < season; ++i){cout << pseason[i] << ":$ " << array[i] << endl;total += array[i];}cout << "Total Expenses :$ " << total << endl; }練習(xí)8-2
#include<iostream> using namespace std; struct expense {double money; }; const int season = 4; const char* pseason[season] = { "Spring","Summer","Fall","Winter" };void fill(double*); void show(double*);int main() {expense Money[season];fill(&Money[0].money);show(&Money[0].money);return 0; } void fill(double* array) {for (int i = 0; i < season; ++i){cout << "Enter " << pseason[i] << " espenses: ";cin >> array[i];} } void show(double* array) {double total = 0;cout << "\nEXPENSES\n";for (int i = 0; i < season; ++i){cout << pseason[i] << ":$ " << array[i] << endl;total += array[i];}cout << "Total Expenses :$ " << total << endl; }練習(xí)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);int main() {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;cout << "Enter name: ";while ((cin.getline(pa[count].fullname, SLEN)) && count < n){cout << "Enter hobby: ";cin.getline(pa[count].hobby, SLEN);cout << "Enter ooplevel: ";cin >> pa[count].ooplevel;cin.get();count++;if (count < n)cout << "Enter name: ";elsebreak;}cout << "Input complete!" << endl;return count; } void display1(student st) {cout << "Name: " << st.fullname << endl;cout << "Hobby: " << st.hobby << endl;cout << "Ooplevel: " << st.ooplevel << endl; } void display2(const student* ps) {cout << "Name: " << ps->fullname << endl;cout << "Hobby: " << ps->hobby << endl;cout << "Ooplevel: " << ps->ooplevel << endl; } void display3(const student pa[], int n) {int count = 0;for (; count < n; ++count){cout << "Name: " << pa[count].fullname << endl;cout << "Hobby: " << pa[count].hobby << endl;cout << "Ooplevel: " << pa[count].ooplevel << endl;} }練習(xí)10
#include<iostream> using namespace std; double calculate(double num1, double num2, double(*pfun)(double num1, double num2)); double add(double num1, double num2); double subtract(double num1, double num2);int main() {double num1, num2;double (*pfun[2])(double num1, double num2);pfun[0] = add;pfun[1] = subtract;cout << "Enter 2 numbers : " << endl;while (cin >> num1 >> num2){for (int cnt = 0; cnt < 2; ++cnt)cout << "Result is: " << (*pfun[cnt])(num1, num2) << endl;}cout << "Complete!" << endl;return 0; }double calculate(double num1, double num2, double(*pfun)(double num1, double num2)) {return (*pfun)(num1, num2); }double add(double num1, double num2) {return num1 * num1 + num2 * num2; }double subtract(double num1, double num2) {return num1 * num1 - num2 * num2; }總結(jié)
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第七章课后习题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++学习之路 C++Pr
- 下一篇: 2020 我的C++学习之路 C++Pr