C++ Primer Plus: 第10章(2)

这篇具有很好参考价值的文章主要介绍了C++ Primer Plus: 第10章(2)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

第10章编程题:
(1)
Account.h:

#ifndef ACCOUNT_H_
#define ACCOUNT_H_

#include <string>

class Account
{
private:
	std::string name ;
	std::string code ;
	double money ;
public:
	Account() ;
	Account(std::string Name, std::string Code, double Money) ;
	~Account() ;
	void ShowAccout() ;
	void InputMoney(double Money) ;
	void OutputMoney(double Money) ;
} ;

#endif

Account.cpp:

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

Account::Account()
{}

Account::Account(string Name, string Code, double Money)
{
	name = Name ;
	code = Code ;
	money = Money ;
}

Account::~Account()
{}

void Account::ShowAccout()
{
	using std::ios_base ;
	ios_base::fmtflags orig = 
		cout.setf(ios_base::fixed, ios_base::floatfield) ;
	std::streamsize prec = cout.precision(0) ;
	cout << "This customer, who is called " << name 
	     << ", and has $" << money 
 	     << " in " << code << " Account.\n";
	cout.setf(orig, ios_base::floatfield) ;
	cout.precision(prec) ;
}

void Account::InputMoney(double Money)
{
	money += Money ;
}

void Account::OutputMoney(double Money)
{
	money -= Money ;
}

main.cpp:

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

const int Num = 8 ;

int main()
{
	Account A[Num] = {
		Account("Jackie Chen", "098425345", 56943),
		Account("John Rebort", "*&%$#4586", 12366), 
		Account("Bluse Amy", "3425834", 9032455),
		Account("Killy Joe", "@#%^&*890", 234568690),
                Account("Beyonce Jim", "*&^%$#678", 999912366),
                Account("Alan London", "5555555", 222222222222),
		Account("Jackie Chen", "098425345", 56943),
                Account("Floyd Dork", "4324049", 247)
	} ;

	std::cout << "Let's see everyone: \n" ;
	for (int i=0; i<Num; i++)
	{
		A[i].ShowAccout() ;
	}
	
	std::cout << "\nNow everyone increases $100: \n" ;
	for (int i=0; i<Num; i++)
        {
                A[i].InputMoney(100) ;
        }
	for (int i=0; i<Num; i++)
        {
                A[i].ShowAccout() ;
        }
	
	std::cout << "\nNow everyone decreases $100: \n" ;
 	for (int i=0; i<Num; i++)
        {
                A[i].OutputMoney(100) ;
        }
        for (int i=0; i<Num; i++)
        {
                A[i].ShowAccout() ;
        }
}

(2)
Person.h:

#ifndef PERSON_H_
#define PERSON_H_

#include <string>
using namespace std ;

class Person
{
private:
	static const int LIMIT = 25 ;
	string lname ;
	char fname[LIMIT] ;
public:
	Person() {lname = "" ; fname[0] = '\0' ;} ;
	Person(const string & ln, const char * fn = "Heyyou") ;
	void Show() const ;
	void FormalShow() const ; 
} ;

#endif

Person.cpp:

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

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

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

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

main.cpp:

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

int main()
{
	Person one ;
	Person two("Smythecraft") ;
	Person three("Dimwiddy", "Sam") ;
	
	std::cout << "The name of one:\n" ;
	one.Show() ;
	std::cout << std::endl ;
	one.FormalShow() ;
	
	std::cout << "\nThe name of two:\n" ;
	two.Show() ;
    std::cout << std::endl ;
    two.FormalShow() ;

	std::cout << "\nThe name of three:\n" ;
	three.Show() ;
    std::cout << std::endl ;
    three.FormalShow() ;
	std::cout << std::endl ;
}

(3)
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 = "", int hc = 0) ;
	~golf() ;
	int setgolf() ;
	void Handicap() ;
	void showgolf() const ;
} ;

#endif

golf.cpp:

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

golf::golf(const char * name, int hc)
{
	strncpy(fullname, name, Len) ;
	handicap = hc ;
}

golf::~golf()
{
	std::cout << "delete!\n" ;
}

int golf::setgolf()
{
	using namespace std ;
	char name[Len] = {0} ;
	int hc ;
	cout << "Fullname: " ;
	if (cin.get(name, Len).get() == -1)
	{
		cin.clear() ;        // 务必对输入队列进行重置
		cout << "Enter Fail!\n" ;
		return 0 ;
	}
	cout << "Handicap: " ;
	while(!(cin >> hc))
	{
		cin.clear() ;
		while(cin.get() != '\n') ;
		cout << "Please enter a number: " ;
	}
	cin.get() ;
	golf G(name, hc) ;
	*this = G ;
	return 1 ;
}

void golf::Handicap()
{
	int hc ;
	std::cin >> hc ;
	(*this).handicap = hc ;
}

void golf::showgolf() const
{
	std::cout << "The " << handicap << " player is "
		      << fullname << std::endl ;
}

main.cpp:

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

const int Num = 5 ;

int main()
{
	using namespace std ;
	golf G[Num] ;
	int num = 0 ;
	cout << "Please input the information of all of players:\n" ;
	for (int i=0; i<Num; i++)
	{
		cout << "#" << i+1 << ": \n" ;
		if(!G[i].setgolf()) break ;
		else num ++ ;
	}
	
	cout << "\nNow change everyone's handicap:\n" ;
	for (int i=0; i<num; i++)
    {
		cout << "#" << i+1 << ": " ;
        G[i].Handicap() ; 
	}
	
	cout << "\nAnd then display all players:\n" ;
	for (int i=0; i<num; i++)
    {
        G[i].showgolf() ;
    }
}

(4)
Sales.h:

#ifndef SALES_H_
#define SALES_H_

namespace SALES
{
	class Sales
	{
	private:
		static const int QUARTERS = 4 ;
		double sales[QUARTERS] ;
		double average ;
		double max ;
		double min ;
	public:
		Sales() ;
		Sales(const double ar[], int n) ;
		~Sales() ;
		void setSales() ;
		void ShowSales() const ;
	} ;
} 

#endif

Sales.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;

Sales::Sales()
{
	min = max = average = 0 ;
	for (int i=0; i<QUARTERS; i++)
		sales[i] = 0 ;
}

Sales::Sales(const double ar[], int n)
{
	int i ;
	for (i=0; i<n && i<QUARTERS; i++)
		sales[i] = ar[i] ;
	while (i<QUARTERS)
	{
		sales[i] = 0 ;
		i ++ ;
	} 
	
	min = max = sales[0] ;
	double total = 0 ;
	for (i=0; i<QUARTERS; i++)
	{
		total += sales[i] ;
		if (min > sales[i]) min = sales[i] ;
		if (max < sales[i]) max = sales[i] ;
	}
	average = total / QUARTERS ;
}

Sales::~Sales()
{
	std::cout << "delete!\n" ;
}

void Sales::setSales()
{
	using namespace std ;
	int n ;
	cout << "Enter the number of double you want to input: " ;
	cin >> n ;
	double *ar = new double[n] ;
	cout << "Enter these numbers you want to input:\n" ;
	for (int i=0; i<n; i++)
	{
		cin >> ar[i] ;
	}
	Sales S(ar, n) ;
	*this = S ;
	delete [] ar ;
}

void Sales::ShowSales() const
{
	std::cout << "The members of Sales:\n" ;
	int i ;
	for (i=0; i<QUARTERS; i++)
    {
		std::cout << sales[i] << " " ;
    }
	std::cout << std::endl ;
	std::cout << "max = " << max << std::endl ;
	std::cout << "min = " << min << std::endl ;
	std::cout << "average = " << average << std::endl ;
}

main.cpp:

#include "Sales.h"
#include <iostream>
using namespace SALES ;

int main()
{
	using namespace std ;
	double ar[7] = {7, 6, 5, 4, 3, 2, 1} ;
	Sales S1(ar, 7) ;
	cout << "The content of S1:\n" ;
	S1.ShowSales() ;
	
	Sales S2 ;
	S2.setSales() ;
	cout << "The content of S2:\n" ;
    S2.ShowSales() ;
}

(5)
Stack.h:

#ifndef STACK_H_
#define STACK_H_

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

typedef customer Item ;

class Stack
{
private:
	static const int MAX = 10 ;
	Item items[MAX] ;
	int top ;
	double total_payment ;
public:
	Stack() ;
	~Stack() ;
	bool isempty() const ;
	bool isfull() const ;
	bool push(const Item & e) ;
	bool pop(Item & e) ;
} ;

#endif

Stack.cpp:

#include "Stack.h"
#include <cstring>
#include <iostream>

Stack::Stack()
{
	top = -1 ;
	total_payment = 0 ;
}

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

bool Stack::isempty() const
{
	if (top == -1) return true ;
	else return false ;
}

bool Stack::isfull() const
{
	if (top == (MAX-1)) return true ;
	else return false ;
}

bool Stack::push(const Item & e)
{
	if (top == (MAX-1)) return false ;
	top++ ;
	strcpy(items[top].fullname, e.fullname) ;
	items[top].payment = e.payment ;
	return true ;
}

bool Stack::pop(Item & e)
{
	if (top == -1) return true ;
	strcpy(e.fullname, items[top].fullname) ;
	e.payment = items[top].payment ;
	top -- ;
	total_payment += e.payment ;
	std::cout << "total_payment = " << total_payment << std::endl ;
	return true ;
}

main.cpp:

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

int main()
{
	using namespace std ;
	Stack St ;
	char ch ;
	Item item ;
	cout << "Please enter A to add a customer,\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 customer:\n" ;
				  cout << "Fullname: " ;
				  cin.get(item.fullname, 35).get() ;
				  cout << "Payment: " ;
				  cin >> item.payment ;
				  if (!St.push(item))
					cout << "The Stack is full!\n" ;
				  break ;
			case 'P':
			case 'p': cout << "Delete a customer:\n" ;
				  if (!St.pop(item))
					cout << "The Stack is empty!\n" ;
				  cout << "Fullname: " ;
				  cout << item.fullname << endl ;
				  cout << "Payment: " ;
				  cout << item.payment << endl ;
				  break ;
		}
		cout << "Please enter A to add a customer,\n"
	             << "P to process a PO, or Q to quit.\n" ;
	}
	cout << "Bye!\n" ;
	return 0 ;
}

(6)
Move.h:

#ifndef MOVE_H_
#define MOVE_H_

class Move
{
private:
	double x ;
	double y ;
public:
	Move(double a = 0, double b = 0) ;
	~Move() ;
	void showmove() const ;
	Move add(const Move & m) const ;
	void reset(double a = 0, double b = 0) ;
} ;

#endif

Move.cpp:

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

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

Move::~Move()
{
	std::cout << "Delete!\n" ;
}

void Move::showmove() const
{
	std::cout << "In this object, x = " << x << ", "
	          << "y = " << y << std::endl ;
}

Move Move::add(const Move & m) const
{
	Move M(m.x, m.y);
	return M ;
}

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

main.cpp:

#include "Move.h"

int main()
{
	Move Begining ;
	Move Next(9, 8) ;
	Begining.showmove() ;
	Next.showmove() ;
	Move Destination = Begining.add(Next) ;
	Destination.showmove() ;
}

(7)
Plorg.h:

#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
private:
	static const int MAX = 19 ;
	char fullname[MAX] ;
	int CI ;
public:
	Plorg(const char * name = "Plorga") ;
	~Plorg() ;
	void ResetCI(int ci) ;
	void ShowPlorg() const ;
} ;

#endif

Plorg.cpp:

#include "Plorg.h"
#include <iostream>
#include <cstring>

Plorg::Plorg(const char * name){
	strncpy(fullname, name, MAX) ;
	CI = 50 ;
}

Plorg::~Plorg(){
	std::cout << "Delete!\n" ;
}

void Plorg::ResetCI(int ci){
	CI = ci ;
}

void Plorg::ShowPlorg() const
{
	std::cout << "The fullname: " << fullname 
		  << ", CI = " << CI << std::endl ;
}

main.cpp:

#include "Plorg.h"

int main()
{
	Plorg P1("Jim Killy") ;
	Plorg P2("Jackie London") ;
	Plorg P3("Trump Jim") ;
	P1.ShowPlorg() ;
	P2.ShowPlorg() ;
	P3.ShowPlorg() ;
	P1.ResetCI(90) ;
	P2.ResetCI(80) ;
	P3.ResetCI(70) ;
	P1.ShowPlorg() ;
    P2.ShowPlorg() ;
    P3.ShowPlorg() ;
}

(8)
List.h:

#ifndef LIST_H_
#define LIST_H_

typedef int Item ;
struct LinkList {
	Item ElemType ;
	LinkList * next ;
} ;

class List
{
private:
	static const int MAX = 10 ;
	LinkList * head ;
	LinkList * rail ;
	LinkList * p ;
	int num ;
public:
	List() ;
	~List() ;
	bool Add(const Item & i) ;
	bool isEmpty() ;
	bool isFull() ;
	void visit(void (*pf)(Item & i)) ;
} ;
	
#endif

List.cpp:

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

List::List() {
	head = new LinkList ;
	head->next = NULL ;
	rail = p = head ;
	num = 0 ;
}

List::~List() {
	while (head != rail) {
		p = head ;
		head = head -> next ;
		delete p ;
	}
	delete head ;
	head = rail = p = NULL ;
	num = 0 ;
	std::cout << "This List has been destroyed!\n" ;
}

bool List::Add(const Item & i) {
	if (num == MAX) return false ;
	p = rail ;
	p->next = new LinkList ;
	p->next->ElemType = i ;
	p->next->next = NULL ;
	rail = p->next ;
	num++ ;
	return true ;
}

bool List::isEmpty() {
	if (num == 0) return true ;
	else return false ;
}

bool List::isFull() {
        if (num == MAX) return true ;
        else return false ;
}

void List::visit(void (*pf)(Item & i)) {
	for (p = head->next; p; p=p->next)
		(*pf)(p->ElemType) ;
}

main.cpp:文章来源地址https://www.toymoban.com/news/detail-652795.html

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

void Watch(Item & i) ;
void AddOne(Item & i) ;

int main()
{
	using namespace std ;
	List L ;
	cout << "Let's input some numbers:\n" ;
	Item i ;
	while (cin>>i) {
		if (L.isFull()) {
			cout << "This List is filled!\n" ;
			break ;
		}
		else L.Add(i) ;
	}

	cout << "Let's see all of members from this List:\n" ;
	L.visit(Watch) ;
	cout << endl ;
	
	cout << "Let's add one to every member from this List:\n" ;
    L.visit(AddOne) ;
	L.visit(Watch) ;
    cout << endl ;
}

void Watch(Item & i) {
	std::cout << i << " " ;
}

void AddOne(Item & i) {
	i += 1 ;
}

到了这里,关于C++ Primer Plus: 第10章(2)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】C++ primer plus第二章练习题

    c++程序的模块叫什么? 函数。 下面的预处理器编译指令是做什么用的? 包含头文件,将iostream文件的内容添加·到代码中 下面的语句是做什么用的? using namespace std; using是预编译器指令,让其使用std命名空间 什么语句可以用来打印短语“Hello, world”,然后开始新的一行? s

    2024年02月06日
    浏览(40)
  • C Primer Plus(第六版)13.10 复习题 第5题

    #include stdio.h #include stdlib.h #include string.h #define SIZE 256 int main (int argc, char *argv[] ) {     int i=0;     char ch;     char str[SIZE];     FILE *fp;     if (argc != 3)         exit(EXIT_FAILURE);     else     {         if ((fp = fopen (argv[2],\\\"r\\\")) == NULL)             exit(EXIT_FAILURE) ;      

    2024年01月22日
    浏览(32)
  • C++ Primer Plus第五章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44 2.使用array对

    2024年02月09日
    浏览(39)
  • C++ primer plus第七章编程练习答案

    1.编写一个程序,不断要求用户输入两个数,直到其中的一个为 0。对于每两个数,程序将使用一个南数来计算它们的调和平均数,并将结果返回给 main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下: 调和平均数=2.0*xy/(x + y) 2.编写一个程序,要求用

    2024年02月10日
    浏览(32)
  • C++ Primer Plus第二章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个C++程序,它显示您的姓名和地址。 2.编写一个C程序它要求用户输入一个以 long 为单位的距离,然后将它转换为码(- ng等于220码) 3.编写1个C++程序它使用3个用户定义的函数(括mai()),并生成下面的输出Three blind

    2024年02月09日
    浏览(45)
  • 【CPP_Primer_Plus】C++ IDE推荐

    C++编译器推荐 windows 推荐 Resharper++插件 vcpkg 功能介绍 编辑器 Visual Studio 的编辑器具有出色的代码补全功能、语法突出显示、快速信息提示、附带代码修复建议的错误和警告。 IntelliSense 比 IntelliCode(内置于编辑器中的 AI 工具) 调试器 顶部的绿色运行按钮可启动调试程序。

    2024年02月09日
    浏览(30)
  • C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

    🌴 C Primer Plus第10章编程练习~ 加油加油!🍭 ☘️欢迎大家讨论 批评指正~ 🍎1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。计算每年的总降水量、年平均降水量和5年中每月的平均降水量 🍐编写一个程序,初始化一个double类型的数组,然后把

    2024年02月04日
    浏览(40)
  • C++ Primer Plus笔记: 2023.06.05 AND 2023.06.06

    1.在C++的赋值语句: 赋值将从右向左进行,首先,88被赋值给steinway,然后,steinway的值(现在是88)被赋给baldwin,然后baldwin的值88被赋给yamaha。 2.类与对象: ** 类是用户定义的一种数据类型。要定义类,需要描述它能够表示什么信息和可对数据执行哪些操作,类之于对象就像

    2024年02月08日
    浏览(31)
  • 《C++ Primer Plus》学习笔记——第5章 循环和文本输入

    C++中支持三种循环,for循环、while循环和do while循环。 for循环遍历字符串输出 输出 这里的++i通常也有人写常i++,有前缀和后缀的区别。从语义上来说,二者都是对i进行自增。但是从效率上来说,可能存在差异。通常i++会将i复制一个副本,然后加一,然后再写回,而++i则是直

    2024年02月09日
    浏览(32)
  • C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽

    本章内容包括: 通过第 7 章,您了解到很多有关 C++函数的知识,但需要学习的知识还很多。C++还提供许多新的函 数特性,使之有别于 C 语言。新特性包括内联函数、按引用传递变量、默认的参数值、函数重载(多态) 以及模板函数。 本章介绍的 C++在 C 语言基础上新增的特

    2024年02月15日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包