【学习C++】C++ Primer Plus (第六版)第十章编程练习1-8

1.

#include <iostream>
#include <string>
class Account{
private:
	std::string name;
	std::string number;
	double deposit;
public:
	Account(const std::string & na="no name",const std::string & nu="0",int de=0);
	void show();
	void add(double dep);
	void remove(double dep);
};
Account::Account(const std::string & na, const std::string & nu, int de){
	name = na;
	number = nu;
	deposit =de;
}
void Account::show(){
	std::cout << "name: " << name << "   number: " << number << "    deposit: " << deposit << std::endl;
}
void Account::add(double dep){
	if (dep <= 0)
		std::cout << "deposit added can not be negative.\n";
	else
		deposit += dep;
}
void Account::remove(double dep){
	if (dep < 0)
		std::cout << "deposit removed can not be negative.\n";
	else if (dep > deposit)
		std::cout << "you can not remove more than you have!\n";
	else
		deposit -= dep;
}
int main()
{
	Account a;
	a.show();
	a = Account("xiaoming", "000111", 0);
	a.show();
	a.add(500);
	a.show();
	a.remove(400);
	a.show();
	Account b("xiaohua", "111000", 5000);
	b.show();
	b.add(300);
	b.show();
	b.remove(200);
	b.show();
	std::cin.get();
	return 0;
}

2.

#include <iostream>
#include <string>
class Person{
private:
	static const int LIMIT = 25;
	std::string lname;
	char fname[LIMIT];
public:
	Person(){ lname = ""; fname[0] = '\0'; }
	Person(const std::string & ln, const char * fn = "Heyyou");
	void show() const;
	void FormalShow() const;
};
Person::Person(const std::string & ln, const char * fn){
	lname = ln;
	strcpy(fname, fn);
}
void Person::show() const{
	std::cout << fname << " " << lname;
}
void Person::FormalShow() const{
	std::cout << lname << ", " << fname << std::endl;
}
int main()
{
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	three.show();
	std::cout << std::endl;
	three.FormalShow();
	std::cin.get();
	return 0;
}

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="no name", int hc=0);
	void setgolf();
	void handi(int hc);
	void showgolf();
};
#endif
//golf.cpp
#include <iostream>
#include "golf.h"
golf::golf(const char * name,int hc){
	strcpy(fullname, name);
	handicap = hc;
}
void golf::setgolf(){
	char name[LEN];
	int handi;
	std::cout << "please input name hand handicap:\n";
	std::cin.getline(name, LEN);
	std::cin >> handi;
	std::cin.get();
	*this = golf(name, handi);
}
void golf::handi(int hc){
	handicap = hc;
}
void golf::showgolf(){
	std::cout << fullname << ": " << handicap<<std::endl;
}
//main.cpp
#include <iostream>
#include "golf.h"
int main()
{
	golf a[4];
	for (int i = 0; i < 4; i++){
		a[i].showgolf();
	}
	for (int i = 0; i < 4; i++){
		a[i].setgolf();
	}
	for (int i = 0; i < 4; i++){
		a[i].showgolf();
	}
	std::cin.get();
	return 0;
}

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(const double ar [], int n);
		void setSales();
		void showSales();
	};
}
#endif
<pre name="code" class="cpp">//sales.cpp
#include <iostream>
#include "sales.h"
namespace SALES{
	Sales::Sales(const double ar [], int n){
		int l;
		l = n > 4 ? 4 : n;
		for (int i =0; i < l; i++){
			sales[i] = ar[i];
		}
		if (l < 4)
			for (int i = l; i < 4; i++)
				sales[i] = 0;
		double sum = sales[0];
		double ma = sales[0], mi = sales[0];
		for (int i = 1; i < 4; i++){
			sum += sales[i];
			ma = ma>sales[i] ? ma : sales[i];
			mi = mi < sales[i] ? mi : sales[i];
		}
		average = sum / 4;
		max = ma;
		min = mi;
	}
	void Sales::setSales(){
		double ar[4];
		std::cout << "please enter 4 numbers: \n";
		for (int i = 0; i < 4; i++)
			std::cin >> ar[i];
		*this = Sales(ar, 4);
	}
	void Sales::showSales(){
		for (int i = 0; i < 4; i++)
			std::cout << sales[i] << " ";
		std::cout << std::endl;
		std::cout << "average: " << average << std::endl;
		std::cout << "max number: " <<max << std::endl;
		std::cout << "min number: " << min << std::endl;
	}
}

//main.cpp
#include <iostream>
#include "sales.h"
int main()
{
	double ar[4] = {0.0, 1.0, 2.0, 3.0 };
	SALES::Sales s(ar, 4);
	s.showSales();
	s.setSales();
	s.showSales();
	std::cin.get();
	std::cin.get();
	return 0;
}

5.

//stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer{
	char fullname[35];
	double payment;
};
typedef customer Item;
class Stack
{
private:
	enum{ MAX = 10 };
	Item items[MAX];
	int top;
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	bool push(const Item & item);
	bool pop(Item & item);
};
#endif
//stack.cpp
#include <iostream>
#include "stack.h"
Stack::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){
		items[top++] = item;
		return true;
	}
	else
		return false;
}
bool Stack::pop(Item & item){
	if (top > 0){
		item = items[--top];
		return true;
	}
	else
		return false;
}
//main.cpp
#include <iostream>
#include "stack.h"
int main()
{
	Stack s;
	double sum = 0;
	customer c[3] = { { "liujiayu", 35 }, { "wagnrunze", 23 }, { "wanghaojian", 10 } };
	for (int i = 0; i < 3; i++)
		s.push(c[i]);
	for (int i = 0; i < 3; i++){
		s.pop(c[i]);
		sum += c[i].payment;
		std::cout << "sum: " << sum << std::endl;
	}
	std::cin.get();
	return 0;
}

6.

#include <iostream>
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{
	std::cout << "x: " << x << "   y: " << y<<std::endl;
}
Move Move::add(const Move & m) const{
	Move M;
	M.x = x + m.x;
	M.y = y + m.y;
	return M;
}
void Move::reset(double a, double b){
	x = a;
	y = b;
}
int main()
{
	Move a(2,3);
	a.showmove();
	Move b(4, 5);
	b.showmove();
	Move c;
	c.showmove();
	c= a.add(b);
	c.showmove();
	a.reset(3, 7);
	a.showmove();
	a.reset();
	a.showmove();
	std::cin.get();
	return 0;
}

7.

#include <iostream>
class Plorg
{
private:
	char fullname[20];
	int CI;
public:
	Plorg(const char * name = "Plorga");
	void setCI(int c);
	void showPlorg();
};
Plorg::Plorg(const char *name){
	strcpy(fullname, name);
	CI = 50;
}
void Plorg::setCI(int c){
	CI = c;
}
void Plorg::showPlorg(){
	std::cout << "name: " << fullname << "   CI: " << CI << std::endl;
}
int main()
{
	Plorg p;
	p.showPlorg();
	p = Plorg("liujiayu");
	p.showPlorg();
	p.setCI(70);
	p.showPlorg();
	std::cin.get();
	return 0;
}

8.

//list.h
#ifndef LIST_H_
#define LIST_H_
typedef unsigned int Item;
class List{
private:
	enum{ MAX = 10 };
	Item items[MAX];
	int num;
public:
	List();
	void add(const Item &);
	bool isempty() const;
	bool isfull() const;
	void visit(void(*pf)(Item &));
};
#endif
//list.cpp
#include <iostream>
#include "list.h"
List::List(){
	num = 0;
}
void List::add(const Item & a){
	if (num < MAX){
		items[num++] = a;
	}
	else
		std::cout << "The list is full\n";
}
bool List::isempty()const{
	return num == 0;
}
bool List::isfull() const{
	return num == MAX;
}
void List::visit(void(*pf)(Item &)){
	for (int i = 0; i < num; i++)
		pf(items[i]);
}
//main.cpp
#include <iostream>
#include "list.h"
void show(Item &);
int main()
{
	List l;
	int i = 0;
	while (!l.isfull()){
		l.add(i*10);
		i++;
	}
		l.visit(show);
	std::cin.get();
	return 0;
}
void show(Item &a){
	std::cout << a <<std::endl;
}
时间: 2024-08-29 17:12:28

【学习C++】C++ Primer Plus (第六版)第十章编程练习1-8的相关文章

C++ Primer Plus 第六版 第16章 string类和标准模板库

1.string实际上是模板具体化basic_string<char> 的一个typedef,有默认参数,所以省略了初始化参数 2.size_type是一个依赖于实现的整形 string将string::npos定义为字符串的最大长度 3.string类的构造函数P656 4.对于c-风格字符串,3种输入方法:cin>>   cin.getline(),cin.get 对于string   ,2种输入方法:cin>>,getline(cin,string对象) 5.st

c++ primer plus 第六版程序清单16.8 vect2.cpp 手打源码编译错误!!!

坚持手打源码,编译运行,但未每个程序均进行单步调试,昨天在VS2017上手打c++ primer plus 第六版程序清单16.8 vect2.cpp后编译出错: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

OC学习之书籍-&gt;Objective-C 程序设计第六版

最近时间比较充裕,想学习IOS开发,经过多方考察还是决定从oc学起,从基础的oc语法慢慢的走向cocoa框架的学习. oc的入门语言我我选择最新版的objective-c程序设计,主要是参考豆瓣的书评.借助51cto的博客,来记录下学习历程.

C++ Primer Plus 第六版笔记

关于对象声明的思考 转自:http://www.cnblogs.com/weiqubo/archive/2009/11/02/1930042.html C++中定义对象的语法,带括号与不带括号有什么区别? #include <iostream> class MyClass { public: MyClass() { std::cout << "Hello MyClass!" << std::endl; } public: void MyMethod(

《C++ Primer Plus 第六版》读书笔记

CH1-3:处理数据 1 列表初始化 char c={31325}:不允许缩窄 char c={66}; char c={x}:不能为变量 2 强制类型转换 (typename) value typename (value) static_cast<typename> (value) 更加严格 CH4:复合类型 1 string类 strcpy(charArray, stringStr);//字符串拷贝到字符数组,注意大小 strcat(charArray, stringStr);//字符串添

C++ Primer Plus第六版编程练习---第3章 处理数据(未完待续)

1. #include <iostream> const int CONVER_FACTOR = 12; int main(int argc, char* argv[]){ int height = 0; std::cout << "Pleas enter your height with inch_ "; std::cin >> height; if(0 > height) { std::cout << "Pleas e

C++ Primer Plus第六版编程练习---第4章 复合类型

1. #include <iostream> int main(int argc, char* argv[]){ char firstName[50] = {0}; char lastName[50] = {0}; char grade; int age; std::cout << "What is your first name? "; std::cin.getline(firstName, 49); std::cout << "What

C++ Primer Plus第六版编程练习---第5章 循环和关系表达式

1. #include <iostream> int main() { int startNum = 0; int endNum = 0; std::cout << "please enter tow num:" << std::endl; std::cin >> startNum; std::cin >> endNum; long long sum = 0; for(int i = startNum; i <= end

C++ Primer Plus第六版编程练习---第6章 分支语句和逻辑运算符

1. 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 5 int main() 6 { 7 std::string inputStr; 8 std::cout<<"Enter your character list. enter @ to end." << std::endl; 9 char ch; 10 std::string srcStr; 1

C++Primer Plus第6版 4.13编程练习答案

1.答案: #include <iostream> #include <string> int main() { using namespace std; char* fname = new char[10]; char* lname = new char[6]; char grade; int age; cout<< "What is your first name? "; cin.getline(fname,10); cout<< &