C++ Primer Plus 第六版课后习题答案(第十章)

10_1.h 

#ifndef _10_1_H_
#define _10_1_H_
#include <string>

class BankAccount {
 public:
  BankAccount(const std::string& client,
              const std::string& num,
              double bal = 0.0);
  void show() const;
  void deposit (double cash);
  void withdraw (double cash);
 private:
  std::string _name;
  std::string _account;
  double _balance;
};

#endif

10_1.cpp

#include "10_1.h"

#include <cctype>
#include <iostream>

BankAccount::BankAccount(const std::string& client,
            const std::string& num,
            double bal) {
  _name = client;
  _account = num;
  _balance =  bal;        
}

void BankAccount::show() const {
  using std::cout;
  using std::ios_base;
  // set format to #.##
  ios_base::fmtflags orig =
      cout.setf(ios_base::fixed, ios_base::floatfield);
  std::streamsize prec = cout.precision(2);

  cout << "Client: " << _name
       << ", Account: " << _account
       << ", Balance: " << _balance << std::endl;
  // restore original format
  cout.setf(orig, ios_base::floatfield);
  cout.precision(prec);
}

void BankAccount::deposit(double cash) {
  if(cash <= 0.0) {
    std::cout << "Invalid num of " << cash << "!" << std::endl;
    return;
  }
  _balance += cash;
  std::cout << "Deposit success!" << std::endl;
}

void BankAccount::withdraw(double cash) {
  if(cash > _balance) {
    std::cout << cash << " is larger than bank balance!"<< std::endl;
    return;
  }
  _balance -= cash;
  std::cout << "Withdraw success!" << std::endl;
}

int main() {
  std::string name, account;
  double balance, cash;
  std::cout << "Please enter client name:" << std::endl;
  std::cin >> name;
  std::cin.get();
  std::cout << "Please enter client account num:" << std::endl;
  std::cin >> account;
  std::cin.get();
  std::cout << "Please enter client balance:" << std::endl;
  std::cin >> balance;
  if (name.empty() || account.empty() || balance < 0.0) {
    std::cout << "Invalid argument!" << std::endl;
    return 0;
  }
  BankAccount client(name, account, balance);
  client.show();
  std::cout << "Please enter D to deposit money,"
            << "W to withdraw cash, or Q to quit.\n";
  char ch;
  while (std::cin >> ch && toupper(ch) != 'Q')
  {
    while (std::cin.get() != '\n')
      continue;
    if (!isalpha(ch)) {
      std::cout << '\a';
      continue;
    }
    switch (ch) {
      case 'd':
      case 'D':
          std::cout << "Enter the cash you want to deposit:";
          std::cin >> cash;
          client.deposit(cash);
          break;
      case 'w':
      case 'W':
          std::cout << "Enter the cash you want to withdraw:";
          std::cin >> cash;
          client.withdraw(cash);
    }
    client.show();
    std::cout << "Please enter D to deposit money,\n"
              << "W to withdraw cash, or Q to quit.\n";
  }
}

10_2.h

#ifndef _10_2_H_
#define _10_2_H_
#include <string>

clas
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值