当前位置:   article > 正文

C++PrimerPlus(第六版)第十章答案_c++ primer plus第六版第十章答案

c++ primer plus第六版第十章答案

第1题:

main.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using namespace std;
  4. int main()
  5. {
  6. BankAccount a1= BankAccount("a1", "fad@123", 123);
  7. BankAccount a2 = BankAccount("a2", "fad@456", 456);
  8. a1.show();
  9. a2.show();
  10. a1.deposit(20);
  11. a1.show();
  12. a1.withdraw(66);
  13. a1.show();
  14. return 0;
  15. }

 stock.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using std::cout;
  4. using std::endl;
  5. BankAccount::BankAccount(const char* client, const char* num, double bal )
  6. {
  7. strncpy(name,client,39);
  8. name[39] = '\0';
  9. strncpy(acctnum,num,24);
  10. acctnum[24] = '\0';
  11. balance = bal;
  12. }
  13. void BankAccount::show(void) const
  14. {
  15. cout << "name:"<<name<<endl;
  16. cout << "anntnum:" << acctnum<<endl;
  17. cout << "balance:" << balance<<endl;
  18. cout << endl;
  19. }
  20. void BankAccount::deposit(double cash)
  21. {
  22. balance += cash;
  23. }
  24. void BankAccount::withdraw(double cash)
  25. {
  26. balance -= cash;
  27. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <cstring>
  3. class BankAccount
  4. {
  5. private:
  6. char name[40];
  7. char acctnum[25];
  8. double balance;
  9. public:
  10. BankAccount(const char* client,const char*num,double bal=0.0);
  11. void show(void)const;
  12. void deposit(double cash);
  13. void withdraw(double cash);
  14. };

第2题:

main.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using namespace std;
  4. int main()
  5. {
  6. Person one;
  7. Person two("Smythecraft");
  8. Person three("Dimwiddy","Sam");
  9. two.Show();
  10. cout << endl;
  11. two.FormalShow();
  12. return 0;
  13. }

stock.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using std::cout;
  4. using std::endl;
  5. using std::string;
  6. Person::Person(const string& ln, const char* fn)
  7. {
  8. lname = ln;
  9. strncpy(fname,fn,24);
  10. fname[24] = '\0';
  11. }
  12. void Person::Show() const
  13. {
  14. cout << "first name:" << fname<<"\n";
  15. cout << "last name:" << lname << "\n";
  16. cout << endl;
  17. }
  18. void Person::FormalShow() const
  19. {
  20. cout << "last name:" << lname << "\n";
  21. cout << "first name:" << fname << "\n";
  22. cout << endl;
  23. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <cstring>
  3. using std::string;
  4. class Person
  5. {
  6. private:
  7. static const int LIMIT = 25;
  8. string lname;
  9. char fname[LIMIT];
  10. public:
  11. Person()
  12. {
  13. lname = " ";
  14. fname[0] = '\0';
  15. }
  16. Person(const string& ln,const char* fn="Heyyou");
  17. void Show() const;
  18. void FormalShow() const;
  19. };

第3题:

main.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. int main()
  4. {
  5. using namespace std;
  6. golf in[5];
  7. for (int i = 0; i < 5; i++)
  8. {
  9. char name[Len];
  10. int hc;
  11. int result = 1;
  12. if (i % 2 == 0)
  13. {
  14. cout << "此为模式2:\n";
  15. result = in[i].setgolf();
  16. }
  17. else
  18. {
  19. cout << "此为模式1:\n请输入姓名:";
  20. cin.get(name, Len);
  21. cin.clear();
  22. cin.ignore((numeric_limits<std::streamsize>::max)(), '\n');
  23. cout << "请输入hc:";
  24. cin >> hc;
  25. cin.get();
  26. in[i].setgolf(name, hc);
  27. if (strcmp(name, " ") == 0)
  28. {
  29. result = 0;
  30. }
  31. }
  32. if (result == 0)
  33. {
  34. break;
  35. }
  36. if (i == 3)
  37. {
  38. in[i].handicap(7);
  39. }
  40. in[i].showgolf();
  41. }
  42. return 0;
  43. }
  44. //adadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasdadadadasd

stock.cpp

  1. #pragma warning (disable :4996)
  2. #include <iostream>
  3. #include "stock.h"
  4. #include <string>
  5. using namespace std;
  6. golf::golf(const char* name, int hc)
  7. {
  8. strcpy(m_fullname, name);
  9. m_fullname[39] = '\0';
  10. m_handicap = hc;
  11. }
  12. golf::golf()
  13. {
  14. strcpy(m_fullname, " ");
  15. m_handicap = -1;
  16. }
  17. void golf::setgolf(const char* name, int hc)
  18. {
  19. strcpy(m_fullname, name);
  20. m_fullname[39] = '\0';
  21. m_handicap = hc;
  22. }
  23. int golf::setgolf()
  24. {
  25. char name[Len];
  26. int hc;
  27. cout << "请输入姓名:";
  28. cin.get(name, Len);
  29. cin.clear();
  30. cin.ignore((numeric_limits<std::streamsize>::max)(), '\n');
  31. cout << "请输入hc:";
  32. cin >> hc;
  33. cin.get();
  34. if (strcmp(name, " ") == 0)
  35. {
  36. return 0;
  37. }
  38. else
  39. {
  40. *this = golf(name, hc);
  41. return 1;
  42. }
  43. }
  44. void golf::handicap(int hc)
  45. {
  46. m_handicap = hc;
  47. }
  48. void golf::showgolf() const
  49. {
  50. cout << "name:" << m_fullname << "\t";
  51. cout << "handicap:" << m_handicap << endl;
  52. }

stock.h

  1. #pragma warning (disable :4996)
  2. #include <string>
  3. const int Len = 40;
  4. class golf
  5. {
  6. private:
  7. char m_fullname[Len];
  8. int m_handicap;
  9. public:
  10. golf(const char* name, int hc);
  11. golf();
  12. int setgolf();
  13. void setgolf(const char* name, int hc);
  14. void handicap(int hc);
  15. void showgolf() const;
  16. };

第4题:

main.cpp

  1. #include"stock.h"
  2. using namespace std;
  3. using namespace SALES;
  4. int main()
  5. {
  6. Sales s1, s2;
  7. double ar1[2] = { 462.3,580.66 };
  8. s1.setSales(ar1, 2);
  9. s2.setSales();
  10. cout << "s1:" << endl;
  11. s1.showSales();
  12. cout << "s2:" << endl;
  13. s2.showSales();
  14. return 0;
  15. }

stock.cpp

  1. #include "stock.h"
  2. namespace SALES
  3. {
  4. Sales::Sales()
  5. {
  6. for (int i=0;i< QUARTERS;i++)
  7. {
  8. sales[i] = 0.0;
  9. }
  10. m_min = 0.0;
  11. m_max = 0.0;
  12. m_average = 0.0;
  13. }
  14. Sales::Sales(const double ar[], int n)
  15. {
  16. double min, max, aver;
  17. min = ar[0];
  18. max = ar[0];
  19. aver = 0.0;
  20. int i;
  21. for (i = 0; i < n; i++)
  22. {
  23. sales[i] = ar[i];
  24. if (min > ar[i])
  25. {
  26. min = ar[i];
  27. }
  28. if (max < ar[i])
  29. {
  30. max = ar[i];
  31. }
  32. aver += ar[i];
  33. }
  34. while (i < QUARTERS)
  35. {
  36. sales[i] = 0.0;
  37. i++;
  38. }
  39. aver /= n;
  40. m_min = min;
  41. m_max = max;
  42. m_average = aver;
  43. }
  44. void Sales::setSales()
  45. {
  46. std::cout << "请输入s的值:";
  47. double min;
  48. double max;
  49. double aver;
  50. double ar[4];
  51. for (int i = 0; i < 4; i++)
  52. {
  53. std::cin >> ar[i];
  54. }
  55. *this = Sales(ar, 4);
  56. }
  57. void Sales::setSales(const double ar[], int n)
  58. {
  59. *this = Sales(ar,n);
  60. }
  61. void Sales::showSales()const
  62. {
  63. using namespace std;
  64. cout << "sales:" << endl;
  65. for (int i = 0; i < 4; i++)
  66. cout << sales[i] << "\t";
  67. cout << endl;
  68. cout << left << setw(20) << "average:" << m_average << endl;
  69. cout << left << setw(20) << "max:" << setw(20) << m_max << endl;
  70. cout << left << setw(20) << "min:" << setw(20) << m_min << endl;
  71. }
  72. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <iostream>
  3. #include <string>
  4. #include <new>
  5. #include<iomanip>
  6. namespace SALES
  7. {
  8. class Sales
  9. {
  10. private:
  11. static const int QUARTERS = 4;
  12. double sales[QUARTERS];
  13. double m_average;
  14. double m_max;
  15. double m_min;
  16. public:
  17. Sales(const double ar[], int n);
  18. Sales();
  19. void setSales(const double ar[], int n);
  20. void setSales();
  21. void showSales()const;
  22. };
  23. }

第5题:

main.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using namespace std;
  4. int main()
  5. {
  6. int total=0;
  7. Stack S;
  8. customer temp = {"name1",10};
  9. S.push(temp);
  10. temp = { "name2",456 };
  11. S.push(temp);
  12. S.pop(temp);
  13. total += temp.payment;
  14. cout << temp.fullname << "\t" << total << endl;
  15. S.pop(temp);
  16. total += temp.payment;
  17. cout << temp.fullname<<"\t"<<total<<endl;
  18. return 0;
  19. }

stock.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using std::cout;
  4. using std::endl;
  5. Stack::Stack()
  6. {
  7. top = 0;
  8. }
  9. bool Stack::isempty() const
  10. {
  11. return top == 0;
  12. }
  13. bool Stack::isfull() const
  14. {
  15. return top == MAX;
  16. }
  17. bool Stack::push(const Item& item)
  18. {
  19. if (top < MAX)
  20. {
  21. items[top++] = item;
  22. return true;
  23. }
  24. else
  25. return false;
  26. }
  27. bool Stack::pop(Item& item)
  28. {
  29. if (top > 0)
  30. {
  31. item = items[--top];
  32. return true;
  33. }
  34. else
  35. return false;
  36. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <cstring>
  3. #ifndef STACK_H_
  4. #define STACK_H_
  5. struct customer
  6. {
  7. char fullname[35];
  8. double payment;
  9. };
  10. typedef struct customer Item;
  11. class Stack
  12. {
  13. private:
  14. enum {MAX=10};
  15. Item items[MAX];
  16. int top;
  17. public:
  18. Stack();
  19. bool isempty() const;
  20. bool isfull() const;
  21. bool push(const Item & item);
  22. bool pop(Item & item);
  23. };
  24. #endif

第6题:

main.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using namespace std;
  4. int main()
  5. {
  6. Move a = Move{ 23.5,96.32 };
  7. const Move b = Move{45.36,89.65};
  8. a.showmove();
  9. b.showmove();
  10. a=a.add(b);
  11. a.showmove();
  12. a.reset(0.0, 0.0);
  13. a.showmove();
  14. return 0;
  15. }

stock.cpp

  1. #include <iostream>
  2. #include "stock.h"
  3. using std::cout;
  4. using std::endl;
  5. Move::Move(double a , double b )
  6. {
  7. x = a;
  8. y = b;
  9. }
  10. void Move::showmove() const
  11. {
  12. cout << "x:" << x<<endl;
  13. cout << "y:" << y<<endl;
  14. cout << endl;
  15. }
  16. Move Move::add(const Move& m)const
  17. {
  18. Move temp;
  19. temp.x = m.x + this->x;
  20. temp.y = m.y + this->y;
  21. return temp;
  22. }
  23. void Move::reset(double a , double b )
  24. {
  25. x = a;
  26. y = b;
  27. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <cstring>
  3. class Move
  4. {
  5. private:
  6. double x;
  7. double y;
  8. public:
  9. Move(double a = 0,double b=0);
  10. void showmove() const;
  11. Move add(const Move & m)const;
  12. void reset(double a = 0,double b=0);
  13. };

第7题:

main.cpp

  1. #include"stock.h"
  2. using namespace std;
  3. int main()
  4. {
  5. Plorg p1, p2;
  6. p1.show();
  7. p2.show();
  8. p2.setplorg(5);
  9. p1.setci(40);
  10. p1.show();
  11. p2.show();
  12. return 0;
  13. }

stock.cpp

  1. #include "stock.h"
  2. using namespace std;
  3. Plorg::Plorg()
  4. {
  5. cin >> name;
  6. ci = 50;
  7. }
  8. void Plorg::setplorg(int n, const char*iname)
  9. {
  10. strcpy(name,iname);
  11. ci = n;
  12. }
  13. void Plorg::setci(int n)
  14. {
  15. ci = n;
  16. }
  17. void Plorg::show()
  18. {
  19. cout << "name:"<<name<<endl;
  20. cout << "ci:" << ci << endl;
  21. cout << endl;
  22. }

stock.h

  1. #pragma warning (disable:4996)
  2. #include <iostream>
  3. #include <string>
  4. #include <new>
  5. #include<iomanip>
  6. class Plorg
  7. {
  8. private:
  9. char name[20];
  10. int ci;
  11. public:
  12. Plorg();
  13. void setplorg(int n,const char* iname = "Plorga");
  14. void setci(int n);
  15. void show();
  16. };

第8题:

main.cpp

  1. #include"list.h"
  2. using namespace std;
  3. void set(Item& n)
  4. {
  5. n = 100;
  6. }
  7. void show(Item & n)
  8. {
  9. cout << n << endl;
  10. }
  11. int main()
  12. {
  13. List L1, L2;
  14. L1.IsEmpty();
  15. L2.IsFull();
  16. Item ar1[5] = {1,2,3,4,5};
  17. Item a = 5;
  18. L1.log(ar1,5);
  19. L2.modify(50,0);
  20. L1.visit(show);
  21. cout << endl;
  22. L2.visit(show);
  23. cout << endl;
  24. L1.visit(set);
  25. L2.visit(set);
  26. L1.visit(show);
  27. cout << endl;
  28. L2.visit(show);
  29. cout << endl;
  30. return 0;
  31. }

list.cpp

  1. #include "list.h"
  2. using namespace std;
  3. List::List()
  4. {
  5. for (int i=0;i<50;i++)
  6. {
  7. stuff[i] = -5;
  8. }
  9. }
  10. void List::log(Item ar[],int n)
  11. {
  12. for (int i=0;i<n;i++)
  13. {
  14. stuff[i] = ar[i];
  15. }
  16. }
  17. void List::modify(Item item,int n)
  18. {
  19. stuff[n] = item;
  20. }
  21. void List::IsFull()
  22. {
  23. if (stuff[49] != -5)
  24. cout << "Full" << endl<<endl;
  25. else
  26. cout << "not Full" << endl << endl;
  27. }
  28. void List::IsEmpty()
  29. {
  30. if (stuff[0] == -5)
  31. cout << "Empty" << endl << endl;
  32. else
  33. cout << "not Empty" << endl << endl;
  34. }
  35. void List::visit(void(*pf)(Item& n))
  36. {
  37. int i;
  38. for ( i=0;i<50;i++)
  39. {
  40. if (stuff[i]==-5)
  41. {
  42. break;
  43. }
  44. }
  45. for (int j=0;j<i;j++)
  46. {
  47. (*pf)(stuff[j]);
  48. }
  49. }

list.h

  1. #pragma warning (disable:4996)
  2. #include <iostream>
  3. #include <string>
  4. #include <new>
  5. #include<iomanip>
  6. typedef int Item ;
  7. class List
  8. {
  9. private:
  10. Item stuff[50];
  11. public:
  12. List();
  13. void log(Item ar[], int n);
  14. void modify(Item item, int n);
  15. void IsFull();
  16. void IsEmpty();
  17. void visit(void(*pf)(Item & n));
  18. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/342799?site
推荐阅读
相关标签
  

闽ICP备14008679号