C++ Primer Plus(第6版)---编程作业完成(第十章)

此篇博客介绍了C++编程中的各种实例,包括银行账户管理、个人对象操作、高尔夫游戏、销售记录、数据结构(栈和列表)以及一些基本的类和方法。通过p10_1到p10_8的函数实现,展示了面向对象编程的实践应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

charpter10_homework.cpp

#include<iostream>
#include"bank_account.h"
#include"Person.h"
#include"golf.h"
#include"sales.h"
#include "stack.h"//引入栈
#include "move.h"
#include "plorg.h"
#include "List.h"
using namespace std;
using namespace SALES;
//函数原型
void p10_1(void);
void p10_2(void);
void p10_3(void);
void p10_4(void);
void p10_5(void);
void p10_6(void);
void p10_7(void);
void p10_8(void);
//主函数
int main()
{
	//p10_1();
	//p10_2();
	//p10_3();
	//p10_4();
	//p10_5();
	//p10_6();
	//p10_7();
	p10_8();
	return 0;
}
//------------作业一-------
void p10_1(void)
{
	//显示调用构造函数
	BankAccount b1=BankAccount("AAA","123",100);
	BankAccount b2("BBB","456",200);
	BankAccount b3;
	b1.show();
	b2.show();
	b3.show();
	b1.withdraw(20);
	b1.show();
	b2.withdraw(500);
	b2.show();

}

//------------作业二---------
void p10_2(void)
{
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy","Sam");
	one.Show();
	two.FormalShow();
	three.Show();
}

//-----------作业三--------------
void p10_3(void)
{
	Golf g1;
	g1.showgolf();
	Golf g2("FIRST",10);
	g2.showgolf();
	g2.setgolf();
	g2.showgolf();
	g2.hdicap(2500);
	g2.showgolf();
}

//------------作业四------------
void p10_4(void)
{
	Sales s1;
	s1.showsales();
	double arr[4]={1,2,3,4};
	Sales s2(arr,4);
	s2.showsales();

}

//-------------作业五--------

void p10_5(void)
{
	Stack st;//创建一个栈
	double total=0.0;
	const customer c1={"AAA",20.0};
	const customer c2={"BBB",40.0};
	if(st.push(c1))
		cout<<"c1 push!\n";
	else
		cout<<"Full\n";
	if(st.push(c2))
		cout<<"c2 push!\n";
	else
		cout<<"Full\n";
	
	customer c3,c4;
	if(st.pop(c3))
	{
		cout<<"c3 pop!\n";
		total++;
	}	
	else
		cout<<"Empty!\n";
	if(st.pop(c4))
	{
		cout<<"c4 pop!\n";
		total++;
	}
	else
		cout<<"Empty!\n";

	cout<<c3.fullname<<" "<<c3.payment<<endl;
	cout<<c4.fullname<<" "<<c4.payment<<endl;
	cout<<total<<endl;
}

//----------作业六-----------------
void p10_6(void)
{
	Move m1;
	m1.showmove();
	m1.reset(2,2);
	m1.showmove();
	Move m2(3,3);
	m2=m1.add(m2);
	m2.showmove();
}

//--------作业七-----------
void p10_7(void)
{
	Plorg p1;
	p1.show();
	p1.setCI(10);
	p1.show();
	char arr[]="apple";
	Plorg p2(arr);
	p2.show();
}

//-------作业八--------
void p10_8(void)
{
	List list1;//创建一个空列表
	unsigned long temp;
	for(int i=0;i<10;i++)
	{
		temp=i+1;
		list1.add(temp);
	}
	list1.visit(revise);
}

bank_account.h

#ifndef BANK_ACCOUNT_H_
#define BANK_ACCOUNT_H_

#include<string>
using namespace std;

class BankAccount
{
private://默认属性
	static const int MAX=50;
	char m_name[MAX];
	string m_account;
	double m_money;
public://公共属性
	//自定义构造函数
	BankAccount(char* name,const string str,const double m);
	//默认构造函数
	BankAccount();
	//析构函数,无生命类型
	~BankAccount();
	//显示函数
	void show()const;
	//取出参数指定款项
	void withdraw(double m);
};

#endif 

 bank_account.cpp

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

//自定义构造函数
BankAccount::BankAccount(char* name,const string str,const double m)
{
	strcpy(m_name,name);
	m_account=str;
	m_money=m;
}
//默认构造函数
BankAccount::BankAccount()
{
	strcpy(m_name,"NULL");
	m_account="0";
	m_money=0.0;
}
//析构函数,无声明返回类型
BankAccount::~BankAccount()
{
	cout<<"Bye! "<<m_name<<endl;
}
//显示函数
void BankAccount::show()const
{
	cout<<"name: "<<m_name<<endl;
	cout<<"count: "<<m_account<<endl;
	cout<<"money: "<<m_money<<endl;
}
//取出参数指定款项
void BankAccount::withdraw(double m)
{
	if(m<0)
		cout<<"False!\n";
	else if(m>m_money)
		cout<<"You don't have these money!\n";
	else
	{
		cout<<"You have withdraw "<<m<<endl;
		m_money-=m;
	}
}

Person.h

#ifndef PERSON_H_
#define PERSON_H_

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

#endif

Person.cpp

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

#include<cstring>

//自定义构造函数
//!!!!
//C++规定参数的默认值只可以出现在函数声明中,不可以出现在函数的定义中,否则会出现参数重复定义默认参数的错误
Person::Person(const std::string& ln,const char* fn)
{
	lname=ln;
	strcpy(fname,fn);
}
void Person::Show()const
{
	for(int i=0;i<LIMIT && fname[i]!='\0';i++)
		cout<<fname[i];
	cout<<" , "<<lname<<endl;
}
void Person::FormalShow()const
{
	cout<<lname<<" , ";
	for(int i=0;i<LIMIT && fname[i]!='\0';i++)
		cout<<fname[i];
	cout<<endl;
}

golf.h

#ifndef GOLF_H_
#define GOLF_H_

#include <cstring>

class Golf
{
private:
	static const int Len=40;//类中声明的符号常量
	char fullname[Len];
	int handicap;
public:

	//默认构造函数,没有参数,允许创建一个对象,无参数,无初始化
	Golf(){fullname[0]='\0';handicap=0;};
	//自定义构造函数,空或者默认参数,只能在原型中,内联函数,原型+定义
	Golf(const char* name,int hc){strcpy(fullname,name);handicap=hc;};

	//setgolf交互版本
	void setgolf();
	
	void hdicap(int hc);
	void showgolf()const;
	
	//析构函数
	~Golf(){;};
};


#endif

golf.cpp

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

//setgolf交互版本
void Golf::setgolf()
{
	cout<<"Enter your fullname: ";
	char fname[Len];
	cin.getline(fname,Len);//读取一行字符
	cout<<"Enter handicap:";
	int hdcap;
	cin>>hdcap;
	while(cin.get()!='\n')//读取cin>>留在输入缓冲行里
		continue;
	//调用构造函数创建临时对象,并将其赋给调用对象
	*this=Golf(fname,hdcap);
}
	
void Golf::hdicap(int hc)
{
	handicap=hc;
}
void Golf::showgolf()const
{
	cout<<"fullname: "<<fullname<<endl;
	cout<<"handicap: "<<handicap<<endl;
}

 sales.h

#ifndef SALES_H_
#define SALES_H_

//SALE名称空间
namespace SALES
{
	const int QUARTERS=4;
	class Sales
	{
	private:
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	public:
		//自定义构造函数
		Sales(const double arr[],int n);

		//默认构造函数
		Sales();

		void showsales()const;

		//析构函数
		~Sales(){;};
	};
}

#endif

sales.cpp

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

SALES::Sales::Sales()
{
	cout<<"Enter the sales: \n";
	for(int i=0;i<QUARTERS;i++)
	{
		cout<<"# "<<i+1<<": ";
		cin>>sales[i];
	}
	max=sales[0];
	min=sales[0];
	double sum=0;
	for(int i=0;i<QUARTERS;i++)
	{
		if(max<sales[i])
			max=sales[i];
		if(min>sales[i])
			min=sales[i];
		sum+=sales[i];
	}
	average=sum/QUARTERS;
}

Sales::Sales(const double arr[],int n)
{
	if(n<=QUARTERS)
	{
		max=arr[0];
		min=arr[0];
		double sum=0;
		for(int i=0;i<n;i++)
		{
			sales[i]=arr[i];
			if(max<arr[i])
				max=arr[i];
			if(min>arr[i])
				min=arr[i];
			sum+=arr[i];
		}
		average=sum/n;
	}
	else 
		cout<<"n is too big!\n";
}

void Sales::showsales()const
{

	for(int i=0;i<QUARTERS;i++)
		cout<<sales[i]<<" ";
	cout<<endl;
	cout<<"average: "<<average<<endl;
	cout<<"max: "<<max<<endl;
	cout<<"min: "<<min<<endl;
}

stack.h

#ifndef STACK_H_
#define STACK_H_

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

typedef customer Item;

class Stack
{
private:
	//enum {MAX=10};//符号常量,在类中,使用枚举,不算是数据成员
	static const int Max=10;
	Item items[Max];//使用数组来形成栈
	int top;
public:
	Stack();//构造函数
	~Stack();//析构函数
	bool is_empty()const;
	bool is_full()const;
	bool push(const Item& item);
	bool pop(Item & item);
};

#endif

stack.cpp

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

//默认构造函数
Stack::Stack()
{
	top=0;
}
//析构函数
Stack::~Stack()
{
	std::cout<<"Bye!"<<std::endl;
}
bool Stack::is_empty()const
{
	return top==0;
}
bool Stack::is_full()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;
}

move.h

#ifndef MOVE_H_
#define MOVE_H_

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);//默认参数重置
};

#endif

move.cpp

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

using std::cout;
using std::endl;
//默认构造函数,要么没有参数,要么默认参数
Move::Move(double a,double b)//默认参数只能出现在原型中
{
	x=a;
	y=b;
}
void Move::showmove()const
{
	cout<<"x= "<<x<<endl;
	cout<<"y= "<<x<<endl;
}
Move Move::add(const Move& m)const
{
	Move m1;
	m1.x=this->x+m.x;
	m1.y=this->y+m.y;
	return m1;
}
void Move::reset(double a,double b)//默认参数重置
{
	x=a;
	y=b;
}

plorg.h

#ifndef PLORG_H_
#define PLORG_H_

static const int Size=20;
class Plorg
{
private:
	char m_name[Size];
	int CI;
public:
	//默认构造函数,默认参数原型中声明
	Plorg(char name[Size]="Plorga");
	//修改CI
	void setCI(int n);
	//展示
	void show()const;
	//析构函数
	~Plorg(){;};
};

#endif

plorg.cpp

#ifndef PLORG_H_
#define PLORG_H_

static const int Size=20;
class Plorg
{
private:
	char m_name[Size];
	int CI;
public:
	//默认构造函数,默认参数原型中声明
	Plorg(char name[Size]="Plorga");
	//修改CI
	void setCI(int n);
	//展示
	void show()const;
	//析构函数
	~Plorg(){;};
};

#endif

List.h

#ifndef LIST_H_
#define LIST_H_

typedef unsigned long Item1;
class List
{
private:
	static const int Max1=10;
	Item1 items[Max1];
	int top;
public:
	List();
	bool isempty()const;
	bool isfull()const;
	bool add(Item1& item);
	//函数指针作为参数
	void visit(void (*pf)(Item1& item));
};
//非成员函数---数据+100
void revise(Item1& n);

#endif

List.cpp

#include<iostream>
#include"List.h"
using namespace std;
List::List()
{
	top=0;//创建一个空对象
}
bool List::isempty()const
{
	return top==0;
}
bool List::isfull()const
{
	return top==Max1;
}
bool List::add(Item1& item)
{
	if(!isfull())
	{
		*(items+top)=item;
		top++;
		return true;
	}
	else
		return false;
}
//非成员函数---数据+100
void revise(Item1& n)
{
	n=n+100;
	cout<<n<<endl;
}

//函数指针作为参数,visit只有一个参数,接收一个函数指针
void List::visit(void (*pf)(Item1& item))
{
	for(int i=0;i<Max1;i++)
	{
		cout<<"# "<<i+1<<" : ";
			(*pf)(items[i]);
	//或者 pf(items[i])
	}
}

如有错误,欢迎指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值