神秘博客

C++ Primer Plus [第六版]第10章编程练习题答案

1.

#ifndef BANK_H_
#define BANK_H_
#include <string>

class Bank
{
private:
  std::string name;
  std::string cardNumber;
  double money;
public:
  Bank();
  Bank(const std::string &str, const std::string &n, const double &m);
  void inBank(const double &m);
  void outBank(const double &m);
  void showBank();
};


#endif

#include <iostream>
#include "bank0.h"

Bank::Bank()
{
  name = "no name";
  cardNumber = "0";
  money = 0.0;
}

Bank::Bank(const std::string &str, const std::string &n, const double &m)
{
  name = str;
  cardNumber = n;
  money = m;
}

void Bank::inBank(const double &m)
{
  money += m;
  std::cout << "存款成功...\n";
}

void Bank::outBank(const double &m)
{
  if (money < m)
    std::cout << "取现金额超限\n";
  else
  {
    money -= m;
    std::cout << "取款成功...\n";
  }
}

void Bank::showBank()
{
  using std::cout;
  using std::endl;
  cout << "存款人: " << name << " \t卡号: " << cardNumber << endl;
  cout.precision(15);
  cout << "金额: " << money << endl;
}
#include <iostream>
#include "bank0.h"

void menu();

int main()
{
  using namespace std;
  Bank P = Bank("神秘", "6217002710000684874", 88888);
  char num;
  double m;

  menu();
  while (cin >> num && num != '4')
  {
    while (cin.get() != '\n')
      continue;
    switch (num)
    {
    case '1':
      cout << "请输入存款金额: ";
      while (!(cin >> m))
      {
        cin.clear();
        while (cin.get() != '\n')
          continue;
        cout << "请输入数字: ";
      }
      P.inBank(m);
      break;
    case '2':
      cout << "请输入取款金额: ";
      while (!(cin >> m))
      {
        cin.clear();
        while (cin.get() != '\n')
          continue;
        cout << "请输入数字: ";
      }
      P.outBank(m);
      break;
    case '3':
      P.showBank();
      break;
    default:
      cout << "请输入正确的选项: ";
      continue;
    }
    cout.put('\n');
    menu();
  }
  cout << "再见\n";

  system("pause");
  return 0;
}

void menu()
{
  using std::cout;
  cout << "**********银行存取款操作**********\n";
  cout << "1. 存款 \t2. 取款\n";
  cout << "3. 余额 \t4. 退出\n";
  cout << "**********************************\n";
}

2

#ifndef PERSON_H_
#define PERSON_H_
#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");
  void Show() const;
  void FormalShow() const;
};

#endif
#include <iostream>
#include <cstring>
#include "person.h"

Person::Person(const std::string &ln, const char *fn)
{
  lname = ln;
  strcpy_s(fname, LIMIT, fn);
}

void Person::Show() const
{
  std::cout << fname << " " << lname << std::endl;

}

void Person::FormalShow() const
{
  std::cout << lname << ", " << fname << std::endl;
}
#include <iostream>
#include "person.h"

int main()
{
  Person one;
  Person two("Smythecraft");
  Person three("Dimwiddy", "Sam");

  one.Show();
  one.FormalShow();
  std::cout << "********************\n";
  two.Show();
  two.FormalShow();
  std::cout << "********************\n";
  three.Show();
  three.FormalShow();
  std::cout << "********************\n";

  std::cout.put('\n');
  system("pause");
  return 0;
}

3

#ifndef GOLF_H_
#define GOLF_H_

class Golf
{
private:
  static const int Len = 40;
  char fullname[Len];
  int handicap;
public:
  Golf() { fullname[0] = '\0'; handicap = 0; }
  Golf(const char *name, const int &hc);
  int setgolf();
  void Handicap(const int &hc);
  void showgolf();
};




#endif
#include <iostream>
#include <cstring>
#include "golf.h"

Golf::Golf(const char *name, const int &hc)
{
  strcpy_s(fullname, Len, name);
  handicap = hc;
}

int Golf::setgolf()
{
  char temp[Len];
  std::cout << "请输入姓名: ";
  std::cin.getline(temp, Len);
  if (temp[0] == '\0')
    return 0;
  strcpy_s(fullname, Len, temp);
  std::cout << "请输入等级: ";
  std::cin >> handicap;
  std::cin.get();
  return 1;
}

void Golf::Handicap(const int &hc)
{
  handicap = hc;
}

void Golf::showgolf()
{
  using namespace std;
  ios_base::fmtflags temp;
  temp = cout.setf(ios_base::fixed);
  cout.setf(ios_base::left);
  cout << "姓名: ";
  cout.width(20);
  cout << fullname;
  cout << "    等级: ";
  cout.width(2);
  cout << handicap << endl;

  cout.setf(temp);
}
#include <iostream>
#include "golf.h"

int main()
{
  using namespace std;
  Golf list[10];
  Golf Temp;
  int count = 0;

  for (int i = 0; i < 10; i++)
  {
    cout << i + 1 << "#: \n";
    if (!list[i].setgolf())
      break;
    count++;
  }

  for (int i = 0; i < count; i++)
  {
    if (i == 0)
      cout << "*****列表*****\n";
    list[i].showgolf();
  }
  Golf temp = Golf("Ann Birdfree", 24);
  cout << endl;
  temp.showgolf();
  temp.Handicap(26);
  temp.showgolf();

  system("pause");
  return 0;
}

4

#ifndef SALESS_H_
#define SALESS_H_
namespace SALES
{
  class Sales
  {
  private:
    static const int QUARTERS = 4;
    double sales[QUARTERS];
    double average;
    double max;
    double min;
  public:
    Sales() { sales[0] = sales[1] = sales[2] = sales[3] = 0; average = 0; max = 0; min = 0; }
    Sales(const double ar[], const int n);
    void setSales();
    void showSales();
  };
}


#endif
#include <iostream>
#include "saless.h"

namespace SALES
{
  Sales::Sales(const double ar[], const int n)
  {
    max = 0;
    min = ar[0];
    average = 0;
    for (int i = 0; i < n; i++)
    {
      sales[i] = ar[i];
      if (max < ar[i])
        max = ar[i];
      if (min > ar[i])
        min = ar[i];
      average += ar[i];
    }
    average = average / n;
  }
  void Sales::setSales()
  {
    using namespace std;
    double total = 0;
    for (int i = 0; i < QUARTERS; i++)
    {
      cout << "#" << i + 1 << ": ";
      cin >> sales[i];
      total += sales[i];
    }
    average = total / QUARTERS;
    max = 0;
    min = sales[0];
    for (int i = 0; i < QUARTERS; i++)
    {
      if (max < sales[i])
        max = sales[i];
      if (min < sales[i])
        min = sales[i];
    }
  }
  void Sales::showSales()
  {
    using namespace std;
    for (int i = 0; i < QUARTERS; i++)
    {
      cout << "#" << i + 1 << ": ";
      cout << sales[i] << " ";
    }
    cout << endl;
    cout << "平均: " << average << endl;
    cout << "最大值: " << max << endl;
    cout << "最小值: " << min << endl;
  }
}
#include <iostream>
#include "saless.h"

int main()
{
  using namespace SALES;
  Sales p1;
  const int QUARTERS = 4;
  double num[QUARTERS] = { 87.5,68.4,74.2,59.8 };

  std::cout << "********************\n";
  p1.setSales();
  p1.showSales();
  std::cout << "********************\n";
  Sales p2 = Sales(num, QUARTERS);
  p2.showSales();
  std::cout << "完成\n";

  system("pause");
  return 0;
}

5

#ifndef STACK_H_
#define STACK_H_

typedef struct customer
{
  char fullname[35];
  double payment;
}Item;

class Stack
{
private:
  static const int SIZE = 10;
  Item items[SIZE];
  int top;
  double total;
public:
  Stack() { items[0].fullname[0] = '\0'; items[0].payment = 0; top = 0; total = 0; }
  bool isempty();
  bool isfull();
  void inStack(const char *str, const double &m);
  void outStack();
};


#endif
#include <iostream>
#include <cstring>
#include "stack.h"

bool Stack::isempty()
{
  return top == 0;
}

bool Stack::isfull()
{
  return top == SIZE;
}

void Stack::inStack(const char *str, const double &m)
{
  strcpy_s(items[top].fullname, 35, str);
  items[top++].payment = m;
  std::cout << "添加完成... \n";
}

void Stack::outStack()
{
  total += items[--top].payment;
  std::cout << "弹出完成... \n";
  std::cout << "弹出总额: " << total << std::endl;
}
#include <iostream>
#include <cctype>
#include "stack.h"

void menu();

int main()
{
  using namespace std;
  Stack P;
  char ch;
  char temp[35];
  double num;

  menu();
  while (cin >> ch && toupper(ch) != 'Q')
  {
    while (cin.get() != '\n')
      continue;
    switch (toupper(ch))
    {
    case 'A':
      if (P.isfull())
        cout << "栈已满...\n";
      else
      {
        cout << "请输入名字: ";
        cin.get(temp, 35);
        cout << "请输入金额: ";
        while (!(cin >> num))
        {
          cin.clear();
          while (cin.get() != '\n')
            continue;
          cout << "请输入数字: ";
        }
        P.inStack(temp, num);
      }
      break;
    case 'B':
      if (P.isempty())
        cout << "栈已空...\n";
      else
        P.outStack();
      break;
    default:
      cout << "请输入正确的选项: ";
      continue;
    }
    menu();
  }
  cout << "完成\n";

  system("pause");
  return 0;
}

void menu()
{
  using namespace std;
  cout << "**********栈数据操作**********\n";
  cout << "A. 从栈中添加 \tB. 从栈中弹出\n";
  cout << "Q. 退出\n";
  cout << "******************************\n";
}

6.

#ifndef MOVE_H_
#define MOVE_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);
};

#endif
#include <iostream>
#include "move.h"

Move::Move(double a, double b)
{
  x = a;
  y = b;
}

void Move::showmove() const
{
  std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  std::cout.precision(1);
  std::cout << "x = " << x << " \ty = " << y << std::endl;
}

Move Move::add(const Move &m) const
{
  Move temp;
  temp.x = x + m.x;
  temp.y = y + m.y;

  return temp;
}

void Move::reset(double a, double b)
{
  x = a;
  y = b;
}
#include <iostream>
#include "move.h"

int main()
{
  using namespace std;
  Move p1{ 5.6,8.1 };
  Move p2 = Move(4.4, 1.9);
  Move p3 = p1.add(p2);

  p1.showmove();
  p2.showmove();
  p3.showmove();
  p3.reset(13.14, 52.0);
  p3.showmove();

  system("pause");
  return 0;
}

7

#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
private:
  enum { SIZE = 19 };
  char name[SIZE];
  int ci;
public:
  Plorg(const char *str = "Plorga", const int &n = 50);
  void setplorg(const int &n);
  void showplorg();
};

#endif
#include <iostream>
#include <cstring>
#include "plorg.h"

Plorg::Plorg(const char *str, const int &n)
{
  strcpy_s(name, SIZE, str);
  ci = n;
}

void Plorg::setplorg(const int &n)
{
  ci = n;
}

void Plorg::showplorg()
{
  std::cout << "名称: " << name << " \tCI: " << ci << std::endl;
}
#include <iostream>
#include "plorg.h"

int main()
{
  using namespace std;
  Plorg p;

  p.showplorg();
  p.setplorg(60);
  p.showplorg();

  system("pause");
  return 0;
}

8

#ifndef LIST_H_
#define LIST_H_
#include <string>

typedef struct list
{
  std::string name;
  double money;
}Item;

class List
{
private:
  static const int MAX = 10;
  Item items[MAX];
  int top;
public:
  List() { top = 0; }
  bool isempty();
  bool isfull();
  void visit(void(*pf)(Item &));

};

void add(Item &n);
void show(Item &n);

#endif
#include <iostream>
#include "list.h"

bool List::isempty()
{
  return top == 0;
}
bool List::isfull()
{
  return top == MAX;
}

void List::visit(void(*pf)(Item &))
{
  for (int i = 0; i < MAX; i++)
  {
    std::cout << i + 1 << "#: \n";
    pf(items[i]);
    if (top < MAX)
      top++;
  }
}

void show(Item &n)
{
  std::cout.precision(15);
  std::cout << "姓名: " << n.name << " \t存款: " << n.money << std::endl;
}

void add(Item &n)
{
  std::cout << "请输入名字: ";
  getline(std::cin, n.name);
  std::cout << "请输入存款: ";
  std::cin >> n.money;
  std::cin.get();
}
#include <iostream>
#include "list.h"

void menu();

int main()
{
  using namespace std;
  List p;
  char ch;

  menu();
  while (cin >> ch && ch != '3')
  {
    while (cin.get() != '\n')
      continue;
    switch (ch)
    {
    case '1':
      if (p.isempty())
        cout << "列表为空...\n";
      else
        p.visit(show);
      break;
    case '2':
      if (p.isfull())
        cout << "列表已满...\n";
      else
        p.visit(add);
      break;
    default:
      cout << "请输入正确的选项: ";
      continue;
    }
    menu();
  }
  cout << "完成\n";

  system("pause");
  return 0;
}

void menu()
{
  using namespace std;
  cout << "********************\n";
  cout << "1. 查看列表 \t2. 添加列表\n";
  cout << "3. 退出\n";
  cout << "********************\n";
}

 

版权说明:
点赞
  1. 神秘说道:

    签到成功!签到时间:下午11:40:37,每日打卡,生活更精彩哦~

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏