码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-05-06 12:42:42      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

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;
}














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

标签:

原文地址:http://blog.csdn.net/liujiayu1015/article/details/51324610

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!