C++ Primer Plus第十章练习

10.10.1
main.cpp

#include "bank.h"
int main(void)
{
    bank a, b("伞兵", "111111", 999999);
    a.show();
    b.show();
    b.fetch(5631);
    b.show();
    b.save(5631);
    b.show();
    return 0;
}

bank.h

#ifndef BANK_H
#define BANK_H
#include <iostream>
#include <cstring>
using namespace std;
class bank
{
public:
    bank(const char * s1 = "默认", const char * s2 = "123456", int n = 0) : money(n) { strcpy(name, s1); strcpy(num, s2); }
    void show(void);
    bool save(int);
    bool fetch(int);
private:
    char name[20];
    char num[15];
    int money;
};
#endif

bank.cpp

#include "bank.h"
void bank::show(void)
{
    cout << "姓名: " << this->name << endl
         << "账号: " << this->num << endl
         << "余额: " << this->money << endl;
}
bool bank::save(int num)
{
    if(num < 0)
        return false;
    else
    {
        this->money += num;
        return true;
    }
}
bool bank::fetch(int num)
{
    if(num < 0)
        return false;
    else
    {
        if(this->money < num)
            return false;
        this->money -= num;
        return true;
    }
}

10.10.2
mian.cpp

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

Person.h

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

Person.cpp

#include "Person.h"
Person::Person(const string & ln, const char * fn)
{
    this->lname = ln;
    strcpy(this->fname, fn);
}
void Person::Show(void) const
{
    cout << this->fname << ' ' << this->lname;
}
void Person::FormalShow(void) const
{
    cout << this->lname << ", " << this->fname;
}

10.10.3
main.cpp

#include "golf.h"
int main(void)
{
    golf a, b("xswl", 100);
    a.show();
    b.show();
    b.handicap(1);
    b.show();
    return 0;
}

golf.h

#ifndef GOLF_H
#define GOLF_H
#include <iostream>
#include <cstring>
using namespace std;
class golf
{
public:
    golf(const char * s1 = "默认", int n = 0) { strcpy(this->fullname, s1); this->_handicap = n; }
    void handicap(int);
    void show(void);
private:
    enum { LEN = 40 };
    char fullname[LEN];
    int _handicap;
};
#endif

golf.cpp

#include "golf.h"
void golf::handicap(int n)
{
    this->_handicap = n;
}
void golf::show(void)
{
    cout << "姓名:" << this->fullname << endl
         << "等级:" << this->_handicap << endl;
}

10.10.4
mian.cpp

#include "sales.h"
int main(void)
{
	double ar[4] = {1.1, 2.2, 3.3, 4.4};
	SALES::sales a = ar;
	a.show();
	SALES::sales b;
	b.show();
	return 0;
}

sales.h

#ifndef SALES_H
#define SALES_H
#include <iostream>
using namespace std;
namespace SALES
{
	class sales
	{
	public:
		sales(const double *);
		sales(void);
		void show(void) const;
	private:
		enum { QUARTERS = 4 };
		double data[QUARTERS];
		double average;
		double max;
		double min;
	};
}
#endif

sales.cpp

#include "sales.h"
namespace SALES
{
	sales::sales(const double * ar)
	{
		double sum = 0, max = ar[0], min = ar[0];
		for(int i = 0; i < this->QUARTERS; sum += ar[i], i++)
		{
			this->data[i] = ar[i];
			if(ar[i] > max)
				max = ar[i];
			if(ar[i] < min)
				min = ar[i];
		}
		this->average = sum / QUARTERS;
		this->max = max;
		this->min = min;
	}
	sales::sales(void)
	{
		double sum = 0, max, min;
		for(int i = 0; i < this->QUARTERS; sum += data[i], i++)
		{
			cout << "请输入第" << i + 1 << "个数: ";
			cin >> data[i];
			if(!i)
				max = min = data[i];
			if(data[i] > max)
				max = data[i];
			if(data[i] < min)
				min = data[i];
		}
		this->average = sum / QUARTERS;
		this->max = max;
		this->min = min;
	}
	void sales::show(void) const
	{
		cout << "对象数组值为: { ";
		for(int i = 0; i < 4; i++)
		    cout << this->data[i] << ' ';
		cout << '}' << endl;
		cout << "对象平均值: " << this->average << endl;
		cout << "对象最大值: " << this->max << endl;
		cout << "对象最小值: " << this->min << endl;
	}
}

10.10.5
main.cpp

#include "Stock.h"
int main(void)
{
    Stack a;
    Item xx = {"xswl", 55.2};
    a.push(xx);
    a.pop(xx);
    return 0;
}

Stock.h

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
struct customer
{
    char fullname[35];
    double payment;
};
typedef customer Item;
class Stack
{
public:
    Stack(void);
    bool isempty(void) const;
    bool isfull(void) const;
    bool push(const Item &);
    bool pop(Item &);
private:
    enum { MAX = 10 };
    Item items[MAX];
    int top;
    double sum;
};
#endif

Stock.cpp

#include "Stock.h"
Stack::Stack(void)
{
    this->top = 0;
    this->sum = 0;
}
bool Stack::isempty(void) const
{
    return this->top == 0;
}
bool Stack::isfull(void) const
{
    return this->top == this->MAX;
}
bool Stack::push(const Item & item)
{
    if(this->top < this->MAX)
    {
        this->items[top++] = item;
        return true;
    }
    return false;
}
bool Stack::pop(Item & item)
{
    if(this->top > 0)
    {
        this->sum += this->items[top - 1].payment;
        item = this->items[top--];
        cout << "此时的总数为: " << this->sum << endl;
        return true;
    }
    return false;
}

10.10.6
main.cpp

#include "Move.h"
int main(void)
{
    Move a(5.1, 63.2), b(1.2), c;
    a.showmove();
    c = a.add(b);
    c.showmove();
    return 0;
}

Move.h

#ifndef MOVE_H
#define MOVE_H
#include <iostream>
using namespace std;
class Move
{
public:
    Move(double a = 0, double b = 0);
    void showmove(void) const;
    Move add(const Move &) const;
    void reset(double a = 0, double b = 0);
    double getx(void) const;
    double gety(void) const;
private:
    double x;
    double y;
};
#endif

Move.cpp

#include "Move.h"
Move::Move(double a, double b)
{
    this->x = a;
    this->y = b;
}
void Move::showmove(void) const
{
    cout << "x: " << this->x << endl
         << "y: " << this->y << endl;
}
Move Move::add(const Move & m) const
{
    double x, y;
    x = this->x + m.getx();
    y = this->y + m.gety();
    Move temp(x, y);
    return temp;
}
void Move::reset(double a, double b)
{
    this->x = a;
    this->y = b;
}
double Move::getx(void) const
{
    return this->x;
}
double Move::gety(void) const
{
    return this->y;
}

10.10.7
mian.cpp

#include "Plorg.h"
int main(void)
{
    plorg a("测试", 99), b;
    a.show();
    b.show();
    a.renum(5);
    a.show();
    return 0;
}

plorg.h

#ifndef PLORG_H
#define PLORG_H
#include <iostream>
#include <cstring>
using namespace std;
class plorg
{
public:
    plorg(const char * s1 = "Plorga", int n = 50);
    void renum(int);
    void show(void) const;
private:
    char name[20];
    int CI;
};
#endif

plorg.cpp

#include "Plorg.h"
plorg::plorg(const char * s1, int n)
{
    strcpy(this->name, s1);
    this->CI = n;
}
void plorg::renum(int n)
{
    this->CI = n;
}
void plorg::show(void) const
{
    cout << "name: " << this->name << endl
         << "CI: " << this->CI << endl;
}

10.10.8
没写测试程序
list.h

#ifndef LIST_H
#define LIST_H
struct data
{
    char name[30];
    int year;
};
class list
{
public:
    list(void);
    bool isempty(void);
    bool isfull(void);
    bool add(const data &);
    void visit(void (*)(data &));
private:
    enum { MAX = 10 };
    data items[MAX];
    int count;
};
#endif

list.cpp

#include "list.h"
list::list(void)
{
    this->count = 0;
}
bool list::isempty(void)
{
    return this->count == 0;
}
bool list::isfull(void)
{
    return this->count == 10;
}
bool list::add(const data & d)
{
    if(!isfull())
    {
        this->items[this->count++] = d;
        return true;
    }
    return false;
}
void list::visit(void (*pf)(data &))
{
    for(int i = 0; i < this->count; i++)
        (*pf)(this->items[i]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值