第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/5/18 21:01:49 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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(第五版)习题解答 的文档
C++Primer - Plus(第五版)习题解答.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4ipnz2rizz3pebe0ilfj_6.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top