云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C++Primer - Plus(第五版)习题解答

C++Primer - Plus(第五版)习题解答

  • 62 次阅读
  • 3 次下载
  • 2025/5/7 16:19:41

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

else

std::cout << \}

void BankAccount::withdraw(double cash) {

if (cash < 0)

std::cout << \ else if (cash <= balance) balance -=cash; else

std::cout << \}

// sample use

int main() {

BankAccount bird;

BankAccount frog(\

bird.show(); frog.show();

bird = BankAccount(\ bird.show();

frog.deposit(20); frog.show();

frog.withdraw(4000); frog.show();

frog.withdraw(50); frog.show(); }

PE10-4

// pe10-4.h

#ifndef SALES__ #define SALES__

namespace SALES {

const int QUARTERS = 4; class Sales {

private:

double sales[QUARTERS]; double average; double max; double min; public:

// default constructor Sales();

// copies the lesser of 4 or n items from the array ar // to the sales member and computes and stores the

SP 21 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

// average, maximum, and minimum values of the entered items; // remaining elements of sales, if any, set to 0 Sales(const double ar[], int n);

// gathers sales for 4 quarters interactively, stores them // in the sales member of object and computes and stores the // average, maximum, and minumum values void setSales();

// display all information in object void showSales(); }; }

#endif

// pe10-4a.cpp

#include #include \

int main() {

using SALES::Sales;

double vals[3] = {2000, 3000, 5000}; Sales forFiji(vals, 3); forFiji.showSales();

Sales red;

red.showSales(); red.setSales(); red.showSales();

return 0; }

// pe10-4b.cpp

#include #include \

namespace SALES {

using std::cin; using std::cout; using std::endl;

Sales::Sales(const double ar[], int n) {

if (n < 0) n = 0;

int limit = n < QUARTERS ? n : QUARTERS; double total = 0; min = 0; max = 0;

average = 0;

SP 22 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

if (limit > 0)

min = max = ar[0]; int i;

for (i = 0; i < limit; i++) {

sales[i] = ar[i]; total += ar[i]; if (ar[i] > max) max = ar[i];

else if (ar[i] < min) min = ar[i]; }

for (i = limit; i < QUARTERS; i++) sales[i] = 0; if (limit > 0)

average = total / limit; }

Sales::Sales() {

min = 0; max = 0;

average = 0;

for (int i = 0; i < QUARTERS; i++) sales[i] =0; }

void Sales::setSales() {

double sa[QUARTERS]; int i;

for (i = 0; i < QUARTERS; i++) {

cout << \ cin >> sa[i]; }

// create temporary object, copy to invoking object *this = Sales(sa, QUARTERS); }

void Sales::showSales() {

cout << \

for (int i = 0; i < QUARTERS; i++)

cout << \ << sales[i] << endl;

cout << \ cout << \ cout << \ } }

SP 23 of 73 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

PE 10-5

// pe10stack.h -- class definition for the stack ADT // for use with pe10-5.cpp #ifndef _STACK_H_ #define _STACK_H_

struct customer {

char fullname[35]; double payment; };

typedef customer Item;

class Stack {

private:

enum {MAX = 10}; // constant specific to class Item items[MAX]; // holds stack items int top; // index for top stack item public:

Stack();

bool isempty() const; bool isfull() const;

// push() returns false if stack already is full, true otherwise bool push(const Item & item); // add item to stack

// pop() returns false if stack already is empty, true otherwise bool pop(Item & item); // pop top into item };

#endif

// pe10stack.cpp -- Stack member functions // for use with pe10-5.cpp

// exactly the same as stack.cpp in the text

#include \

Stack::Stack() // create an empty stack {

top = 0; }

bool Stack::isempty() const {

return top == 0; }

bool Stack::isfull() const {

return top == MAX; }

bool Stack::push(const Item & item) {

if (top < MAX) {

SP 24 of 73 September 2, 2004

搜索更多关于: C++Primer - Plus(第五版)习题解答 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition else std::cout << \} void BankAccount::withdraw(double cash) { if (cash < 0) std::cout << \ else if (cash <= balance) balance -=cash; else std::cout << \} // sample use int main() { BankAccount bird; B

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com