2020 我的C++学习之路 C++PrimerPlus第十章课后习题
生活随笔
收集整理的這篇文章主要介紹了
2020 我的C++学习之路 C++PrimerPlus第十章课后习题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
以C++ Primer Plus為參考書籍,自身歸納知識點,加深記憶。僅供參考,DEV C++已通過編譯運行
。
練習1
練習2
//p2.h #include<iostream> #include<string>class Person { private:static const int LIMIT = 25;std::string lname;char fname[LIMIT]; public:Person() { lname = "", fname[0] = '\0'; }Person(const std::string& ln, const char *fn = "Heyyou");~Person();void Show()const;void FormalShow()const; }; //p2_fun #include<iostream> #include<string> #include<cstring> #include"p2.h"Person::Person(const std::string& ln, const char* fn) {lname = ln;strcpy_s(fname, fn); }Person::~Person() { }void Person::Show()const {std::cout << fname << " " << lname << std::endl; }void Person::FormalShow()const {std::cout << lname << " " << fname << std::endl; } //p2_main #include<iostream> #include"p2.h"int main() {using namespace std;Person one;Person two = Person("Smythecraft");Person three = Person("Dimwiddy", "Sam");one.Show();one.FormalShow();cout << endl;two.Show();two.FormalShow();cout << endl;three.Show();three.FormalShow();cout << endl; }練習3
//p3.h #include<iostream> #include<string>class Golf { private:std::string fullname;int handicap; public:void setgolf(const std::string fn, int hc = 0);void setgolf();void Handicap(int hc);void showgolf()const; }; //p3_fun #include<iostream> #include<string> #include"p3.h"void Golf::setgolf(const std::string ln, int hc) {fullname = ln;handicap = hc; }void Golf::setgolf() {using namespace std;cout << "Enter fullname: ";getline(cin, (*this).fullname);cout << "Enter handicap: ";cin >> (*this).handicap;cin.get(); }void Golf::Handicap(int hc) {handicap = hc; }void Golf::showgolf()const {std::cout << "FULLNAME: " << fullname << std::endl;std::cout << "HANDICAP: " << handicap << std::endl; } //p3_main #include<iostream> #include"p3.h"int main() {using namespace std;Golf ann;ann.setgolf("Ann Birdfree", 24);ann.showgolf();ann.Handicap(30);ann.showgolf();Golf andy;andy.setgolf();andy.showgolf();andy.Handicap(50);andy.showgolf();return 0; }練習4
//p4.h namespace SALES {class Sales{private:static const int QUARTES = 4;double sales[QUARTES];double average;double max;double min;public:Sales(const double ar[] = {}, int n = 0);void SetSales();void ShowSales()const;}; } //p4_fun #include<iostream> #include"p4.h"namespace SALES {Sales::Sales(const double ar[], int n){double sum = 0;for (int i = 0; i < n; ++i){sales[i] = ar[i];sum += sales[i];}average = sum / n;double tempmax, tempmin;tempmax = tempmin = sales[0];for (int i = 0; i < n; ++i){tempmax = tempmax < sales[i] ? sales[i] : tempmax;tempmin = tempmin > sales[i] ? sales[i] : tempmin;}max = tempmax;min = tempmin;}void Sales::SetSales(){using namespace std;double sum = 0;cout << "ENTER 4 elements of ar: ";for (int i = 0; i < QUARTES; ++i)cin >> sales[i];for (int i = 0; i < QUARTES; ++i){sum += sales[i];}average = sum / QUARTES;double tempmax, tempmin;tempmax = tempmin = sales[0];for (int i = 0; i < QUARTES; ++i){tempmax = tempmax < sales[i] ? sales[i] : tempmax;tempmin = tempmin > sales[i] ? sales[i] : tempmin;}max = tempmax;min = tempmin;}void Sales::ShowSales()const{using namespace std;cout << "There are 4 elements: ";for (int i = 0; i < QUARTES; ++i)cout << sales[i] << endl;cout << "AVERAGE: " << average << endl;;cout << "MAXIMUM: " << max << endl;;cout << "MINIMUM: " << min << endl;;} } //p4_main #include<iostream> #include"p4.h"int main() {using namespace SALES;double ar[4] = { 2.3,6.5,4.3,1.8 };Sales Sa = Sales(ar, 4);Sa.ShowSales();Sa.SetSales();Sa.ShowSales();return 0; }練習5
//p5.h #include<iostream>struct customer {char fullname[35];double payment; }; typedef customer cut; static double total = 0; class Stack { private:static const int LIMIT = 5;cut arcut[LIMIT];int top;double total; public:Stack();bool IsEmpty()const;bool IsFull()const;bool add(cut&);bool remove();void show()const; }; //p5_fun #include<iostream> #include"p5.h"Stack::Stack() {top = 0;total = 0; }bool Stack::IsEmpty()const {return top == 0; }bool Stack::IsFull()const {return top == LIMIT; }bool Stack::add(cut& ar) {if (top < LIMIT){arcut[top++] = ar;return true;}else{std::cout << "ARRAY is full!";return false;} }bool Stack::remove() {if (top > 0){total += arcut[--top].payment;return true;}elsereturn false; }void Stack::show()const {std::cout << total << " is the total;\n"; } //p5_main #include<iostream> #include<cctype> #include"p5.h"int main() {using namespace std;cut ar;Stack st;char ch;cout << "Enter A to add, \nEnter R to remove,\n or Q to quit.";while (cin >> ch && toupper(ch) != 'Q'){while (cin.get() != '\n')continue;if (!isalpha(ch)){cout << "\nEnter A,R or Q!\n";continue;}switch (toupper(ch)){case 'A':if (st.IsFull())cout << "STACK is full!\n";else{cout << "Enter fullname: ";cin.getline(ar.fullname, 35);cout << "Enter payment: ";(cin >> ar.payment).get();st.add(ar);}break;case'R':if (st.IsEmpty())cout << "STACK is empty!\n";else{st.remove();st.show();}break;}cout << "Enter A to add, \nEnter R to remove,\n or Q to quit.";}cout << "Bye!\n";return 0; }練習6
//p6.h class Move { private:double x;double y; public:Move(double a = 0, double b = 0);void showmove()const;Move add(const Move& m)const;void reset(double a = 0, double b = 0); }; //p6_fun #include<iostream> #include"p6.h"Move::Move(double a, double b) {x = a;y = b; }void Move::showmove()const {std::cout << "X= " << x << std::endl;std::cout << "Y= " << y << std::endl; }Move Move::add(const Move& m)const {Move temp;temp.x = (*this).x + m.x;temp.y = (*this).y + m.y;return temp; }void Move::reset(double a, double b) {x = a; y = b; } //p6_main #include<iostream> #include"p6.h"int main() {using namespace std;Move mo1 = Move(20, 15);Move mo2 = Move(10, 40);Move mo3;mo1.showmove();mo3 = mo1.add(mo2);mo3.showmove();mo3.reset();mo3.showmove();return 0; }練習7
//p7.h #include<string> class Plorg { private:static const int MAX = 19;std::string Name;int Ci; public:Plorg(std::string name = "Plorga", int ci = 50);void Change(int ci);void Show()const; }; //p7_fun #include<iostream> #include<string> #include"p7.h"Plorg::Plorg(std::string name, int ci) {Name = name;Ci = ci; }void Plorg::Change(int ci) {Ci = ci; }void Plorg::Show()const {std::cout << "NAME: " << Name << std::endl;std::cout << "CI: " << Ci << std::endl; } //p7_main #include<iostream>#include"p7.h"int main() {using namespace std;Plorg test1;test1.Show();test1.Change(20);test1.Show();Plorg test2 = Plorg("WU LALA", 35);test2.Show();test2.Change(80);test2.Show();return 0; }練習8
//p8.h class List { private:static const int LEN = 10;int array[LEN];int top; public:List();void add(int n);bool IsFull()const;bool IsEmpty()const;void visit(void(*pfun)(int&)); }; p8_fun #include<iostream> #include"p8.h"List::List() {top = 0; }void List::add(int n) {if (top < LEN)array[top++] = n;elsestd::cout << "LIST is full.\n"; }bool List::IsFull()const {return top == LEN; }bool List::IsEmpty()const {return top == 0; }void List::visit(void(*pfun)(int& n)) {for (int i = 0; i < top; ++i)pfun(array[i]); } p8_main #include<iostream> #include"p8.h"void fun(int& n);int main() {using namespace std;List test1;int temp;cout << "ENTER a number:(alpha to quit)\n";while (cin >> temp){if (test1.IsFull())cout << "ARRAY is full.\n";else{test1.add(temp);cout << "ENTER a number:(alpha to quit)\n";}}test1.visit(fun);return 0; } void fun(int& n) {std::cout << n + 4 << std::endl; }總結
以上是生活随笔為你收集整理的2020 我的C++学习之路 C++PrimerPlus第十章课后习题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2020 我的C++的学习之路 第九章
- 下一篇: 2020 我的C++的学习之路 第十章