小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. C/C++
  5. 正文

C++ Primer Plus(第五版)第10章编程题答案

2018年11月18日 1948点热度 0人点赞 1条评论

1.为复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性。

bankacc.h

#ifndef BANKACC_H_1
#define BANKACC_H_1
class Bank {
	char name[30];
	char account[7];
	double debt;
public:
	Bank();
	Bank(const char *co, const char *ac, double db = 0.0);
	~Bank();
	void show() const;
	void deposit(const double money);
	void withdrawal(const double money);
};
#endif // !BANKACC_H_1

bankacc.cpp

#include<iostream>
#include<string>
#include<cstring>
#include"bankacc.h"
using namespace std;
Bank::Bank() {
	strcpy(name, "no name");
	name[30] = '\0';
	strcpy(account, "100000");
	account[6] = '\0';
	debt = 0.0;
}
Bank::Bank(const char *co, const char *ac, double db)
{
	strncpy(name, co, 29);
	strncpy(account, ac,6);
	name[30] = '\0';
	account[6] = '\0';
	debt = db;

}
Bank::~Bank() {
	cout << "Your account has been deleted.Thank your for using our service.\n";
}
void Bank::show() const
{
	cout << "Account Name: " << name << endl;
	cout << "Account ID: " << account << endl;
	cout << "Debt: $" << debt << endl;
}
void Bank::deposit(const double money)
{
	debt += money;
	cout << "Your Debt: $" << debt << endl;
	cout << "Thank you for using deposit service.Bye~\n";

}
void Bank::withdrawal(const double money) {
	if (money <= debt)
	{
		debt -= money;
		cout << "Your Debt: $" << debt << endl;
	}
		
	else
		cout << "Error, you do not have enough money.\n";
	cout << "Thank you for using withdrawal service.Bye~\n";

}

banker.cpp

#include<iostream>
#include<string>
#include<cstring>
#include"bankacc.h"
using namespace std;

int main() {
	cout << "This is Bank example 1:" << endl;
	Bank banks1;
	banks1.show();
	cout << "After depositing..."<<endl;
	double depos1 = 3692.21;
	banks1.deposit(depos1);
	cout << "After withdrawal..." << endl;
	banks1.withdrawal(1252.63);
	cout << "#########################################" << endl;
	double money2 = 8945.32;
	Bank banks2 = Bank("Yushuai Zhang", "H6365", money2);
	banks2.show();
	cout << "After depositing..." << endl;
	double depos2 = 235.21;
	banks2.deposit(depos2);
	cout << "After withdrawal..." << endl;
	banks2.withdrawal(625.63);

	system("pause");
	return 0;
}

2.下面是一个非常简单的类定义:
(……代码省略……)
通过提供未定义的方法来完成这个类的实现。然后,编写一个使用这个类的程序,它使用了几种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。

person.cpp

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int LIMIT = 25;
class Person {
private:
	string lname;
	char fname[LIMIT];
public:
	Person();
	Person(const string &ln, const char*fn = "Heyyou");
	void Show() const;
	void FormalShow() const;
};
Person::Person()
{
	lname = "";
	fname[0] = '\0';

}
Person::Person(const string & ln, const char *fn) {
	lname = ln;
	strncpy(fname, fn, LIMIT-1);
	fname[LIMIT - 1] = '\0';

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

}

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

}
int main() {
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	cout << endl;
	one.FormalShow();
	cout << endl;
	two.FormalShow();
	cout << endl;
	three.Show();
	system("pause");
	return 0;
}

3.完成第9章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替换setgolf(golf &, const char *, int), 以提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,即*this)。

golf.h

#ifndef GOLF_H
#define GOLF_H
class Golf {
private:
	static const int Len = 40;
	char fullname[Len];
	int handicap;
public:
	Golf(const char * name = "HHHHHH", int hc = 10);
	~Golf();
	bool setGolf();
	void sethandicap(int hc);
	void showGolf()const;
};
#endif // GOLF_H

golf.cpp

#include <iostream>
#include <cstring>
#include "golf.h"
using namespace std;

Golf::Golf(const char * name, int hc)
{
	strncpy(fullname, name, 40);
	fullname[39] = '\0';
	handicap = hc;
}

Golf::~Golf()
{
	cout << "Bye!" << endl;
}

bool Golf::setGolf()
{
	char fullname[Len];
	int handicap;

	cout << "Please input the name of the golf player: ";
	cin.get(fullname, Len); 
	if (cin)
	{
		cout << "Please input the handicap of the golf player: ";
		cin >> handicap;
		cin.get();
		*this = Golf(fullname, handicap);
		return true;
	}
	else
		return false;

}

void Golf::sethandicap(int hc)
{
	handicap = hc;
}

void Golf::showGolf()const
{
	std::cout << "hullname: " << fullname << ", \thandicap: " << handicap << std::endl;
}

main.cpp

#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
	Golf one;
	Golf two;
	Golf three;

	one.showGolf();         // 默认构造函数
	two.setGolf();          // 交互版本
	two.showGolf();
	two.sethandicap(5000);  //成员函数
	two.showGolf();         //成员函数
	three.sethandicap(20);
	three.showGolf();
	system("pause");
	return 0;
}

5.考虑下面的结构声明:

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

 

编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不作修改;只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。

stack.h

#pragma once
#ifndef STACK_H_
#define STACK_H_
typedef struct customer
{
	char fullname[35];
	double payment;
}CUSTOMER;

typedef customer Item;

class Stack
{
private:
	enum { MAX = 10 };    
	Item items[MAX];    
	int top;
	int total;
public:
	Stack();
	~Stack();
	bool isempty()const;
	bool isfull()const;
	bool push(const Item & item);
	bool pop(Item & item); 
};
#endif // STACK_H_

static.cpp

#include <iostream>
#include "stack.h"
using namespace std;
Stack::Stack() 
{
	top = 0;
	total = 0;
}

Stack::~Stack()
{
	cout << "Bye!\n";
}

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

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

bool Stack::push(const Item & item)
{
	if (top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
		return false;
}

bool Stack::pop(Item & item)
{
	if (top > 0)
	{
		item = items[--top];
		total +=item.payment;
		cout << "PO#" << item.fullname << endl;
		cout << "Total = " << total << endl;
		return true;
	}
	else
		return false;
}

main.cpp

#include <iostream>
#include <cctype>
#include "stack.h"

int main()
{
	using namespace std;
	Stack st;   // create an empty stack
	char ch;
	CUSTOMER po;
	cout << "Please enter A to add a purchase order.\n"
		<< "P to process a PO,or Q to quit.\n";
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
			continue;
		if (!isalpha(ch))
		{
			cout << '\a';
			continue;
		}
		switch (ch)
		{
		case 'A':
		case 'a':
			cout << "Enter a PO fullname to add:\t";
			cin.getline(po.fullname, 35);
			cout << "Enter a PO payment to add:\t";
			cin >> po.payment;
			if (st.isfull())
				cout << "stack already full\n";
			else
				st.push(po);
			break;
		case 'P':
		case 'p':if (st.isempty())
			cout << "Stack already empty\n";
				 else
					 st.pop(po);
			break;
		}
		cout << "Please enter A to add a purchase order,\n"
			<< "P to process a PO, or Q to quit.\n";
	}
	cout << "Bye\n";
	return 0;
}

6. 根据类声明,提供成员函数的定义和测试这个类的程序。

#include<iostream>
using namespace std;
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);
};
Move::Move(double a, double b)
{
	x = a;
	y = b;
}
void Move::showmove()const {
	cout << "x= " << x << endl;
	cout << "y= " << y << endl;
}
Move Move::add(const Move & m) const {
	return Move(x + m.x, y + m.y);

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

int main() {
	Move move1;
	cout << "Move1:\n";
	move1.showmove();
	Move move2 = Move(2.3, 1.9);
	cout << "Move2:\n";
	move2.showmove();
	move1 = move1.add(move2);
	cout << "Move1 after adding move2:\n";
	move1.showmove();
	move1.reset();
	move2.reset();
	cout << "Reset Move1:\n"; 
	move1.showmove();
	cout << "Reset Move2:\n";
	move2.showmove();
	system("pause");
	return 0;


}

备注:

操作系统:Windows 10 x64(1803)

编译器:Microsoft Visual Studio 2015

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: C++ C++学习笔记
最后更新:2018年11月18日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
Deep Learning in Neural Networks: An Overview(自己翻译版) 高中,就这样过去了一个月 这是一种成长吗? 已修:英语写作指导Ⅰ【UC Berkeley】[2015-06-25] 『2013:创世纪』官方网站正式上线! 每日一感0922:严重抗议日本的这种暴行!
标签聚合
算法 Python python学习 学习 高中 leetcode 鸟哥的linux私房菜 生活 linux Java
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号