Essential C++ 第四章习题

目录

4.1

4.2

4.3

4.4

4.5


4.1

C++代码:

//Stack.h

#pragma once

// #ifndef #define #endif 可以防止重复定义
#ifndef _STACK_H_
#define _STACK_H_

#include<vector>
#include<string>
using namespace std;

class Stack
{
public:
	Stack(vector<string>vec);
	bool push(const string&);
	bool pop(string& elem);
	bool peek(string& elem);
	bool empty();
	bool full();

	int size()
	{
		return _stack.size();
	}
private:
	vector<string>_stack;
};

#endif

//Stack.cpp

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

Stack::Stack(vector<string>vec)
{
	_stack = vec;
}

bool Stack::push(const string& s1)
{
	if (full())
	{
		return false;
		cout << "容器已满,无法继续push" << endl;
	}
	else
	{
		_stack.push_back(s1);
		return true;
	}
}

bool Stack::pop(string& elem)
{
	if (empty())
	{
		cout << "容器为空,无法pop" << endl;
		return false;
	}
	else
	{
		elem = _stack.back();//将要移出的元素赋给参数( vector成员函数 .back() 返回容器的最后一个元素)
		_stack.pop_back();
		return true;
	}
}

bool Stack::peek(string& elem)
{
	if (empty())
	{
		cout << "容器为空" << endl;
		return false;
	}
	else
	{
		elem = _stack.back();
		return true;
	}
}

bool Stack::empty()
{
	if (_stack.empty())
	{
		return true;
	}
	return false;
}

bool Stack::full()
{
	// vector成员函数 .max_size() 返回容器最大能储存的元素个数 ( .max_element返回容器范围内的最大元素)
	if (_stack.size() == _stack.max_size())
	{
		return true;
	}
	return false;
}

//main.cpp

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


int main()
{
	vector<string> vec(10, "GodFishhh");
	Stack St1(vec);

	int size = St1.size();
	cout << "此时容器的大小为:" << size << endl;

	string temp_st3 = "AFish";
	St1.push(temp_st3);
	cout << "向容器中push的元素为:" << temp_st3 << endl;

	size = St1.size();
	cout << "此时容器的大小为:" << size << endl;

	string temp_st1;
	St1.pop(temp_st1);
	cout << "从容器中pop出的元素为:" << temp_st1 << endl;

	string temp_st2;
	St1.peek(temp_st2);
	cout << "此时容器中最后一个元素为:" << temp_st2 << endl;


	system("pause");
	return 0;
}

程序运行结果:

4.2

C++代码:

//Stack.h

#pragma once

// #ifndef #define #endif 可以防止重复定义
#ifndef _STACK_H_
#define _STACK_H_

#include<vector>
#include<string>
using namespace std;

class Stack
{
public:
	Stack(vector<string>vec);
	bool push(const string&);
	bool pop(string& elem);
	bool peek(string& elem);
	bool empty();
	bool full();

	bool find(const string elem);
	int count(const string elem);

	int size()
	{
		return _stack.size();
	}
private:
	vector<string>_stack;
};

#endif

//Stack.cpp

#include"Stack.h"
#include<iostream>
//#include<algorithm> 可以使用 find(.begin(),.end(),value)来寻找,如果范围内有则返回指向这个地址的iterator,没有则返回 end() )

Stack::Stack(vector<string>vec)
{
	_stack = vec;
}

bool Stack::push(const string& s1)
{
	if (full())
	{
		return false;
		cout << "容器已满,无法继续push" << endl;
	}
	else
	{
		_stack.push_back(s1);
		return true;
	}
}

bool Stack::pop(string& elem)
{
	if (empty())
	{
		cout << "容器为空,无法pop" << endl;
		return false;
	}
	else
	{
		elem = _stack.back();//将要移出的元素赋给参数( vector成员函数 .back() 返回容器的最后一个元素)
		_stack.pop_back();
		return true;
	}
}

bool Stack::peek(string& elem)
{
	if (empty())
	{
		cout << "容器为空" << endl;
		return false;
	}
	else
	{
		elem = _stack.back();
		return true;
	}
}

bool Stack::empty()
{
	if (_stack.empty())
	{
		return true;
	}
	return false;
}

bool Stack::full()
{
	// vector成员函数 .max_size() 返回容器最大能储存的元素个数 ( .max_element返回容器范围内的最大元素)
	if (_stack.size() == _stack.max_size())
	{
		return true;
	}
	return false;
}

bool Stack::find(const string elem)
{
	vector<string>::const_iterator it = _stack.end(); //类成员函数可以访问private中的私有数据,其他位置若向访问private中的私有数据必须通过类成员函数
	for (; it != _stack.end(); it++)
	{
		if (elem == *it)
		{
			return true;
		}
	}
	return false;
}

int Stack::count(const string elem)
{
	vector<string>::const_iterator it = _stack.begin();
	int counts = 0;
	for (; it != _stack.end(); it++)
	{
		if (elem == *it)
		{
			counts++;
		}
	}

	return counts;
}

//main.cpp

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


int main()
{
	vector<string> vec(10, "GodFishhh");
	Stack St1(vec);

	int size = St1.size();
	cout << "此时容器的大小为:" << size << endl;

	string temp_st3 = "AFish";
	St1.push(temp_st3);
	cout << "向容器中push的元素为:" << temp_st3 << endl;

	size = St1.size();
	cout << "此时容器的大小为:" << size << endl;

	string temp_st1;
	St1.pop(temp_st1);
	cout << "从容器中pop出的元素为:" << temp_st1 << endl;

	string temp_st2;
	St1.peek(temp_st2);
	cout << "此时容器中最后一个元素为:" << temp_st2 << endl;

	//find()
	string temp_st4;
	cout << "输入想要在类成员St1中寻找的元素:";
	cin >> temp_st4;
	if (St1.find(temp_st4))
	{
		cout << "找到了!" << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}

	//count()
	string temp_st5;
	cout << "输入某个字符串:";
	cin >> temp_st5;
	int counts = St1.count(temp_st5);
	cout << temp_st5 << " 在类成员St1中出现了" << counts << "次" << endl;


	system("pause");
	return 0;
}

程序运行结果:

4.3

C++代码:

//4.3

#include<iostream>
using std::string;

class globalWrapper
{
private:
	static string _program_name;
	static string _version_stamp;
	static int _version_number;
	static int _tests_run;
	static int _tests_passed;
public:
	globalWrapper(string p_n, string v_s, int v_n, int t_r, int t_p)
	{
		_program_name = p_n;
		_version_stamp = v_s;
		_version_number = v_n;
		_tests_run = t_r;
		_tests_passed = t_p;
	}
	static int tests_passed() { return _tests_passed; }
	static int tests_run() { return _tests_run; };
	static int version_number() { return _version_number; };
	static string version_stamp() { return _version_stamp; };
	static string program_name() { return _program_name; };
};

将全局变量声明为static member(保证全局对象都仅有一份),并且将它们的访问函数声明为static。

4.4

C++代码:

//UserProfile.h

//通过一个静态变量static来保证同名的多个用户有自己独有的登录对话(在构造函数中定义,每次调用构造函数都使这个static变量发生相应的变化以表示不同的用户)

#include<string>
#include<iostream>
using namespace std;
#pragma once

#ifndef _USERPROFILE_H_
#define _USERPROFILE_H_

class UserProfile
{
private:
	string _login; //用于区分同名的用户
	string _name;
	string _level;  //Rookie, Intermediate, Advance, Pro
	int _login_times;
	int _guess_times;
	int _correct_times;
public:
	//构造函数(默认参数的设置应当从右往左)
	UserProfile(string name = "guest", string level = "Rookie");
	//重新设置各种数据
	void set_name(string name) { _name = name; };
	void set_level(string level) { _level = level; };
	void set_login_times(int login_times) { _login_times = login_times; };
	void set_guess_times(int guess_times) { _guess_times = guess_times; };
	void set_correct_times(int correct_times) { _correct_times = correct_times; };
	//读取各种数据
	string _return_login() { return _login; };
	string _return_name() { return _name; };
	string _return_level() { return _level; }
	int _return_login_times() { return _login_times; };
	int _return_guess_times() { return _guess_times; };
	int _return_correct_times() { return _correct_times; };
	//猜测的命中率
	double average_guess();
	//重载运算符
	bool operator==(const UserProfile User);
	
	bool operator!=(const UserProfile User);
	//输入
	friend istream& operator>>(istream& is, UserProfile& User);
	//输出
	friend ostream& operator<<(ostream& os, UserProfile& User);
};


#endif


//UserProfile.cpp

#pragma warning(disable:4996)


#include"UserProfile.h"



UserProfile::UserProfile(string name, string level) :_login(name)
{
	//为每个用户设置独一无二的标识符
	static int id = 0;
	char buffer[16];
	_itoa(id++, buffer, 10);
	_login += buffer;

	_name = name;
	_level = level;
	_login_times = 0;
	_guess_times = 0;
	_correct_times = 0;
}


double UserProfile::average_guess()
{
	//_correct_times和_guess_times的类型均为int,而此函数的返回值是double类型,所以需要对变量进行类型转换再运算
	return ((double)_correct_times / (double)_guess_times * 100);
}


bool UserProfile::operator==(const UserProfile User)
{
	if (_login == User._login && _name == User._name)
	{
		return true;
	}
	return false;
}

bool UserProfile::operator!=(const UserProfile User)
{
	return !(*this == User);
}

//友元函数利用作用域解析运算符声明
istream& operator>>(istream& is, UserProfile& User)
{
	cout << "分别输入用户的login_times,guess_times,correct_times: ";
	is >>User._login_times >> User._guess_times >> User._correct_times;
	return is;
}

ostream& operator<<(ostream& os, UserProfile& User)
{
	os <<User._login << " " << User._level << " " << User._login_times << " " << User._guess_times << " " << User._correct_times<<" "<<User.average_guess()<<"%";
	os<<endl;
	return os;
}


     //main.cpp

#include"UserProfile.h"
#include<vector>



int main()
{
	//level_rank
	vector<string>vec(4);
	vec[0] = "Rookie";
	vec[1] = "Intermediata";
	vec[2] = "Advance";
	vec[3] = "Pro";

	UserProfile User1;
	User1.set_login_times(1);
	User1.set_guess_times(10);
	User1.set_correct_times(4);
	cout << User1;

	UserProfile User2;
	User2.set_login_times(2);
	User2.set_guess_times(30);
	User2.set_correct_times(18);
	cout << User2;

	UserProfile User3("AFish", vec[1]);
	User3.set_login_times(1);
	User3.set_guess_times(100);
	User3.set_correct_times(40);
	cout << User3;

	User3.set_level(vec[2]);
	User3.set_login_times(2);
	User3.set_guess_times(200);
	User3.set_correct_times(160);
	cout << User3;

	UserProfile User4("GodFishhh", vec[3]);
	cin >> User4;
	cout << User4;



	system("pause");
	return 0;
}

程序运行结果:

1.通过一个静态变量static来保证同名的多个用户有自己独有的登录对话(在构造函数中定义,每次调用构造函数都使这个static变量发生相应的变化以表示不同的用户)(静态变量只定义一次,后续调用不会重复定义)

具体实现:

UserProfile::UserProfile(string name, string level) :_login(name)
{
	//为每个用户设置独一无二的标识符
	static int id = 0;
	char buffer[16];
	_itoa(id++, buffer, 10);
	_login += buffer;
}

构造完成后,输出过程只需要输出_login,其中包括 name 和特殊标识符 buffer。

(成员名的最终形式应当为:name+buffer)

2.调用函数 _itoa(),将整数转换为对应的ASCII字符串形式

static int id = 0;
char buffer[16];
_itoa(id++, buffer, 10);

原型为:char* _itoa(int value,char*stirng,int radix) 

参数为:value -- 想要转换的数据 string -- 目标字符串的地址 radix -- 转换后的进制数

4.5

C++代码:

//Matrix.h

#pragma once

#include<iostream>
using namespace std;

#ifndef _MARTRIX_H_
#define _MARTRIX_H_

class Matrix
{
private:
	int Matr[4][4]; //4x4的矩阵
public:
    //构造函数:
	
	//constructor
	Matrix(int arr[][4]);

	//default constructor
	Matrix(int a11,int a12,int a13,int a14,int a21,int a22,int a23,int a24,int a31,int a32,int a33,int a34,int a41,int a42,int a43,int a44);

	//矩阵加法:
	friend Matrix operator+(Matrix& M1,Matrix&M2);
	//矩阵乘法:
	friend Matrix operator*(Matrix& M1,Matrix&M2);
	//打印矩阵内容函数print()
	void print();
	//复合运算符+=
	void operator+=(Matrix M1);
	//function call运算符(返回参数对应位置矩阵的元素值)
	int& operator()(int row, int column)
	{
		return Matr[row][column];
	}

	int operator()(int row, int column) const
	{
		return Matr[row][column];
	}

	//输出
	friend ostream& operator<<(ostream&os,const Matrix M1);
};



#endif


//Matrix.cpp

#include"Matrix.h"


//构造函数:

//constructor
Matrix::Matrix(int arr[][4])
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			Matr[i][j] = arr[i][j];
		}
	}
}

//default constructor
Matrix::Matrix(int a11, int a12, int a13, int a14, int a21, int a22, int a23, int a24, int a31, int a32, int a33, int a34, int a41, int a42, int a43, int a44)
{
	Matr[0][0] = a11;
	Matr[0][1] = a12;
	Matr[0][2] = a13;
	Matr[0][3] = a14;
	Matr[1][0] = a21;
	Matr[1][1] = a22;
	Matr[1][2] = a23;
	Matr[1][3] = a24;
	Matr[2][0] = a31;
	Matr[2][1] = a32;
	Matr[2][2] = a33;
	Matr[2][3] = a34;
	Matr[3][0] = a41;
	Matr[3][1] = a42;
	Matr[3][2] = a43;
	Matr[3][3] = a44;
}

//友元函数无需声明所属类
//友元函数可以直接访问类中的私有数据

//矩阵加法:
Matrix operator+(Matrix& M1, Matrix& M2)
{
	int temp_arr[4][4] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			temp_arr[i][j] = M1.Matr[i][j] + M2.Matr[i][j];
		}
	}
	return Matrix(temp_arr);
}

//矩阵乘法:
Matrix operator*(Matrix& M1, Matrix& M2)
{
	int temp_arr[4][4] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
	for (int i = 0; i < 4; i++)  //行遍历
	{
		for (int j = 0; j < 4; j++) //列遍历
		{
			for (int x = 0; x < 4; x++)
			{
				temp_arr[i][j] += M1.Matr[i][x] * M2.Matr[x][j]; //一行一行的元素计算
			}
		}
	}
	return Matrix(temp_arr);
}

//打印矩阵内容函数print()
void Matrix::print()
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << Matr[i][j] << " ";
		}
		cout << endl;
	}
}

//复合运算符+=
void Matrix::operator+=(Matrix M1)
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			Matr[i][j] += M1.Matr[i][j];
		}
	}
}

//输出
ostream& operator<<(ostream& os, const Matrix M1)
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			cout << M1.Matr[i][j] << " ";
		}
		cout << endl;
	}
	return os;
}

//main.cpp

#include"Matrix.h"


int main()
{
	Matrix m(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	cout << m << endl;

	int ar[4][4] =
	{
		1,0,0,0,
		0,1,0,0,
		0,0,1,0,
		0,0,0,1
	};

	Matrix identity(ar);
	cout << identity << endl;

	Matrix m2(identity);
	m = identity;
	cout << m2 << endl;
	cout << m << endl;

	int ar2[4][4] =
	{
		1,0,2,8,
		6,1,1,8,
		4,7,2,1,
		6,8,5,6
	};

	Matrix m3(ar2);
	m3.print();

	Matrix m4 = m3 * identity;
	cout << m4 << endl;

	Matrix m5 = m3 + m4;
	cout << m5 << endl;

	m3 += m4;
	cout << m3 << endl;

	system("pause");
	return 0;
}

程序运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值