C++ PRIMER PLUS(第5版)第十章 编程练习(上)

//bank.h
#pragma once

const int SIZE = 20;

class Bank{
private:
	char name[SIZE];
	char account[SIZE];
	int money;
public:
	Bank();
	Bank(char* bname, char* baccount, int bmoney=0);
	void showBank();
	void push(int m);
	void pop(int m);
};
//bank.cpp
#include "stdafx.h"
#include "bank.h"
#include<iostream>
using namespace std;

Bank::Bank() {
	strcpy_s(name, "none");
	strcpy_s(account, "none");
	money = 0;
}

Bank::Bank(char* bname, char* baccount, int bmoney) {
	strcpy_s(name, bname);
	strcpy_s(account, baccount);
	money = bmoney;
}

void Bank::push(int m) {
	money += m;
	cout << "You have placed $" << m << "into the account.\n";;
}

void Bank::pop(int m) {
	if ((money - m) < 0) {
		cout << "You have no such money.\n";
		return;
	}
	money -= m;
	cout << "You have taken $" << m << "out of the account.\n";
}

void Bank::showBank()
{
	cout << "name: " << name << endl;
	cout << "account: " << account << endl;
	cout << "balance: " << money << endl;
}
//practice.cpp
#include "stdafx.h"
#include "bank.h"
#include<iostream>
using namespace std;


int main()
{
	Bank ac1 = Bank("Claire", "103051611189267", 0);
	ac1.showBank();
	ac1.push(10000);
	ac1.showBank();
	ac1.pop(2000);
	ac1.showBank();

	return 0;
}
//person.h
#include<string>
using namespace std;

class Person{
private:
	static const int LIMIT = 25;
	string lname;//last name
	char fname[LIMIT];//first name
public:
	Person() { lname = ""; fname[0] = '\0'; }
	Person(const string& ln, const char* fn = "Heyyou");
	void Show() const;//firstname lastname format
	void FormalShow() const;//lastname,firstname format
};
//person.cpp
#include "stdafx.h"
#include "person.h"
#include<iostream>
#include<string>
using namespace std;

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

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

void Person::FormalShow() const {
	cout << "name: " << lname << "," << fname << endl;
}
//practice.cpp
#include "stdafx.h"
#include "person.h"
#include<iostream>
using namespace std;


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

	return 0;
}
#pragma once
//golf.h
const int Len = 40;

class Golf
{private:
	char fullname[Len];
	int handicap;
public:
	Golf();
	Golf(const char *name, int hc);
	void setHandicap(int hc);
	void showgolf();
};
//golf.cpp
#include "stdafx.h"
#include "golf.h"
#include<iostream>
#include<string>
using namespace std;

Golf::Golf() {
	cout << "Please input your name:\n";
	cin.getline(fullname, Len);
	cout << "Please input your handicap:\n";
	cin >> handicap;
}

Golf::Golf(const char *name, int hc) {
	strcpy_s(fullname, name);
	handicap = hc;
}

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

void Golf::showgolf() {
	cout << "--------------------------------\n";
	cout << "fullname: " << fullname << endl;
	cout << "handicap: " << handicap << endl;
	cout << "--------------------------------\n";
}
//practice.cpp
#include "stdafx.h"
#include "golf.h"
#include<iostream>
using namespace std;


int main()
{
	char name1[] = "Jackson";
	int hc1 = 6;
	Golf g1=Golf(name1,hc1);
	Golf g2;
	g1.showgolf();
	g2.showgolf();
	g1.setHandicap(10);
	g1.showgolf();

	return 0;
}
//sale.h

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

		Sales();

		void showSales();
	};

}
//sale.cpp
#include "stdafx.h"
#include "sale.h"
#include<iostream>
#include<string>
using namespace std;

namespace SALES
{
	Sales::Sales(const double ar[], int n)
	{
		double sum = 0;
		max = ar[0];
		min = ar[0];
		if (n >= 4)
			for (int i = 0; i < 4; i++)
				sales[i] = ar[i];
		else
		{
			for (int i = 0; i < n; i++)
				sales[i] = ar[i];
			for (int i = n; i < 4; i++)
				sales[i] = 0;
		}

		for (int i = 0; i < 4; i++) {
			if (ar[i] > max)
				max = ar[i];
			if (ar[i] < min)
				min = ar[i];
			sum += ar[i];
		}

		average = sum / n;
	}

	Sales::Sales()
	{
		double sum = 0;
		cout << "Input sales for 4 quaters:\n";
		for (int i = 0; i < 4; i++) {
			cin >>sales[i];
		}
		max = sales[0];
		min = sales[0];
		for (int i = 0; i < 4; i++) {
			if (sales[i] > max)
				max = sales[i];
			if (sales[i] < min)
				min = sales[i];
			sum += sales[i];
		}
		average = sum / 4;
	}


	void Sales::showSales() {
		cout << "-----------------------------\n";
		cout << "sales: ";
		for (int i = 0; i < 4; i++)
			cout << sales[i] << ' ';
		cout << endl;
		cout << "average: " << average << endl;
		cout << "maximum: " << max << endl;
		cout << "minumum: " << min << endl;
		cout << "-----------------------------\n";
	}

}
#include "stdafx.h"
#include "sale.h"
#include<iostream>
using namespace std;


int main()
{
	using namespace SALES;
	double ar[5] = { 101,50.4,90.5,98.2,78.6 };
	Sales s2= Sales(ar, 3);
	s2.showSales();
	Sales s1;
	s1.showSales();

	return 0;
}
//customer.h
struct customer {
	char fullname[35];
	double payment;
};

typedef customer Item;

class Stack{
private:
	enum {MAX=10};
	Item items[MAX];
	int top;
	double allpay;
public:
	Stack();
	bool isempty() const;
	bool isfull()const;
	bool push(const Item& item);
	bool pop(Item& item);
};
//customer.cpp
#include "stdafx.h"
#include "customer.h"
#include<iostream>
using namespace std;

Stack::Stack() {
	top = 0;
	allpay = 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];
		allpay += item.payment;
		cout << "allpay: " << allpay << endl;
		return true;
	}
	else
		return false;
}
//practice.cpp
#include "stdafx.h"
#include "customer.h"
#include<iostream>
using namespace std;


int main()
{
	Stack st;
	customer cus;
	customer tem;
	strcpy_s(cus.fullname,"Claire");
	cus.payment = 100.0;
	st.push(cus);
	strcpy_s(cus.fullname, "Mike");
	cus.payment = 78.5;
	st.push(cus);
	st.pop(tem);

	return 0;
}

//move.h

class Move {
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);
	void showmove() const;//show current x,y
	Move add(const Move& m);
	void reset(double a = 0, double b = 0);//resets x,y to a,b
};
//move.cpp
#include "stdafx.h"
#include "move.h"
#include<iostream>
using namespace std;

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

void Move::showmove() const {
	cout << "position: (" << x << "," << y << ")\n";
}

Move Move::add(const Move& m){
	x += m.x;
	y += m.y;
	return *this;
}

void Move::reset(double a, double b) {
	x = a;
	y = b;
}
//practice.cpp
#include "stdafx.h"
#include "move.h"
#include<iostream>
using namespace std;

int main()
{
	Move m1=Move(2,8);
	Move m2;
	m1.showmove();
	m2 = m1.add(4);
	m2.showmove();
	m2.reset(100, 90);
	m2.showmove();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值