Essential C++ 第五章习题

目录

5.1

5.2

5.3

5.4


5.1

C++代码:

//Stack.h

#include
#include
#include
using namespace std;

#pragma once

#ifndef _STACK_H_
#define _STACK_H_

typedef string elemType;

class Stack
{
public:
	//基类的析构函数设置为虚函数
	virtual ~Stack(){};
	//纯虚函数
	virtual bool pop(elemType& elem)=0;
	virtual bool push(const elemType& elem) = 0;
	virtual bool peek(int index, elemType& elem) = 0;
	virtual int top()const = 0;
	virtual int size()const = 0;
	virtual bool empty()const = 0;
	virtual bool full()const = 0;
	virtual void print(ostream& os = cout)const = 0;
	friend ostream& operator<<(ostream& os , Stack& rhs);
};

class LIFO_Stack :public Stack
{
private:
	vector _stack;
	int _top; //用于标识当前栈顶的位置
public:
	LIFO_Stack(vectorvec, int top = 0);
	virtual bool pop(elemType& elem);
	virtual bool push(const elemType& elem);
	virtual bool peek(int index, elemType& elem);
	virtual int top()const;
	virtual int size()const;
	virtual bool empty()const;
	virtual bool full()const;
	virtual void print(ostream& os = cout)const;
};

//Peekback_Stack可以在不更改stack元素的前提下,访问任何一个元素 -- 对成员函数peek进行修改即可,其他成员函数与LIFO_Stack中的基本相同
class Peekback_Stack:public Stack
{
private:
	vector _stack;
	int _top; //用于标识当前栈顶的位置
public:
	Peekback_Stack(vectorvec, int top = 0);
	virtual bool pop(elemType& elem);
	virtual bool push(const elemType& elem);
	virtual bool peek(int index, elemType& elem);
	virtual int top()const;
	virtual int size()const;
	virtual bool empty()const;
	virtual bool full()const;
	virtual void print(ostream& os = cout)const;
};
#endif


//Stack.cpp

#include"Stack.h"

//Stack
ostream& operator<<(ostream& os, Stack& rhs)
{
	rhs.print();
	return os;
}

//LIFO_Stack
LIFO_Stack::LIFO_Stack(vectorvec, int top)
{
	_top = top;
	_stack = vec;
}

bool LIFO_Stack::pop(elemType& elem)
{
	if (empty())
	{
		cout << "栈为空" << endl;
		return false;
	}
	else
	{
		//_top所指的为栈的最高且未插入元素的地方,所以 --_top所指示的地方才是此时栈的最后一个元素
		elem = _stack[--_top];
		_stack.pop_back();
		return true;
	}
}

bool LIFO_Stack::push(const elemType& elem)
{
	if (full())
	{
		cout << "栈已满" << endl;
		return false;
	}
	else
	{
		_stack.push_back(elem);
		_top++;
		return true;
	}
}

bool LIFO_Stack::peek(int index, elemType& elem)
{
	return false;
}

int LIFO_Stack::top()const
{
	return _top;
}

int LIFO_Stack::size()const
{
	return _stack.size();
}

bool LIFO_Stack::empty()const
{
	if (_top ==0)
	{
		return true;
	}
	return false;
}

bool LIFO_Stack::full()const
{
	if (_stack.size() == _stack.max_size())
	{
		return true;
	}
	return false;
}

void LIFO_Stack::print(ostream& os)const
{
	os << "栈中的元素:" << endl;
	for (int i = 0; i < _stack.size(); i++)
	{
		os << _stack[i]<<" ";
	}
	os << endl;
	os << "栈的大小:" << size() << endl;
	os << "栈指针此时的位置:" << top() << endl;
}

//Peekback_Stack
Peekback_Stack::Peekback_Stack(vectorvec, int top)
{
	_top = top;
	_stack = vec;
}

bool Peekback_Stack::pop(elemType& elem)
{
	if (empty())
	{
		cout << "栈为空" << endl;
		return false;
	}
	else
	{
		//_top所指的为栈的最高且未插入元素的地方,所以 --_top所指示的地方才是此时栈的最后一个元素
		elem = _stack[--_top];
		_stack.pop_back();
		return true;
	}
}

bool Peekback_Stack::push(const elemType& elem)
{
	if (full())
	{
		cout << "栈已满" << endl;
		return false;
	}
	else
	{
		_stack.push_back(elem);
		_top++;
		return true;
	}
}

bool Peekback_Stack::peek(int index, elemType& elem)
{
	if (index > _top)
	{
		return false;
	}
	else
	{
		elem = _stack[index];
		return true;
	}
}

int Peekback_Stack::top()const
{
	return _top;
}

int Peekback_Stack::size()const
{
	return _stack.size();
}

bool Peekback_Stack::empty()const
{
	if (_top == 0)
	{
		return true;
	}
	return false;
}

bool Peekback_Stack::full()const
{
	if (_stack.size() == _stack.max_size())
	{
		return true;
	}
	return false;
}

void Peekback_Stack::print(ostream& os)const
{
    os << "栈中的元素:" << endl;
	for (int i = 0; i < _stack.size(); i++)
	{
		os << _stack[i]<<" ";
	}
	os << endl;
	os << "栈的大小:" << size() << endl;
	os << "栈指针此时的位置:" << top() << endl;
}

//main.cpp

#include"Stack.h"

int main()
{
	string arr[3] =
	{
		"GodFishhh",
		"AFish",
		"Fish"
	};
	vectorvec(arr, arr + 3);
	LIFO_Stack Lifo(vec,vec.size());
	Peekback_Stack Peek(vec,vec.size());

	//测试LIFO_Stack类的data member
	cout << "Lifo:" << endl;
	cout << Lifo << endl;
	cout << "经过push后:" << endl;
	Lifo.push("RubFish");
	cout << Lifo << endl;
	cout << "经过pop后:" << endl;
	string temp;
	Lifo.pop(temp);
	cout << Lifo << endl;
	cout << "pop出的元素为:" << temp << endl;

	cout << "---------------------" << endl;

	//测试Peekback_Stack类的data member
	cout << "Peek:" << endl;
	cout << Peek << endl;
	cout << "经过push后:" << endl;
	Peek.push("DogFish");
	cout << Peek << endl;
	cout << "经过pop后:" << endl;
	Peek.pop(temp);
	cout << Peek << endl;
	cout << "pop出的元素为:" << temp << endl;

	cout << "使用Peekback_Stack类额外的peek()函数" << endl;
	string temp1;
	Peek.peek(0, temp1);
	cout << "peek出的元素为:" << temp1 << endl;
	system("pause");
	return 0;
}

程序运行结果:

Essential C++ 第五章习题_第1张图片

5.2

C++代码:

//Stack.h

#include
#include
#include
using namespace std;

typedef string elemType;

#ifndef _STACK_H_
#define _STACK_H_

#pragma once

class Stack
{
public:
	//基类的析构函数设置为虚函数
	virtual ~Stack() {};
	//纯虚函数
	Stack(vectorvec, int top = 0);
	bool pop(elemType& elem);
	bool push(const elemType& elem);
	int top()const;
	int size()const;
	bool empty()const;
	bool full()const;
	void print(ostream& os = cout)const;
	friend ostream& operator<<(ostream& os, Stack& rhs);

	//唯一在派生类中实现功能不同的函数
	virtual bool peek(int index, elemType& elem) = 0;
protected:
	vector_stack;
	int _top;
};

class LIFO_Stack :public Stack
{
public:
	LIFO_Stack(vectorvec, int top = 0) :Stack(vec, top) {};
	//派生类一定要实现抽象基类的纯虚函数才可以创建对象
	virtual bool peek(int index, elemType& elem) { return false; };
};

class Peekback_Stack :public Stack
{
public:
	Peekback_Stack(vectorvec, int top = 0) :Stack(vec, top) {};
	virtual bool peek(int index, elemType& elem)
	{
		{
			if (index > Stack::_top)
			{
				return false;
			}
			else
			{
				elem = _stack[index];
				return true;
			}
		}
	}
};

#endif

//Stack.cpp

#include"Stack.h"


//Stack
ostream& operator<<(ostream& os, Stack& rhs)
{
	rhs.print();
	return os;
}

Stack::Stack(vectorvec, int top)
{
	_top = top;
	_stack = vec;
}

bool Stack::pop(elemType& elem)
{
	if (empty())
	{
		cout << "栈为空" << endl;
		return false;
	}
	else
	{
		//_top所指的为栈的最高且未插入元素的地方,所以 --_top所指示的地方才是此时栈的最后一个元素
		elem = _stack[--_top];
		_stack.pop_back();
		return true;
	}
}

bool Stack::push(const elemType& elem)
{
	if (full())
	{
		cout << "栈已满" << endl;
		return false;
	}
	else
	{
		_stack.push_back(elem);
		_top++;
		return true;
	}
}

bool Stack::peek(int index, elemType& elem)
{
	return false;
}

int Stack::top()const
{
	return _top;
}

int Stack::size()const
{
	return _stack.size();
}

bool Stack::empty()const
{
	if (_top == 0)
	{
		return true;
	}
	return false;
}

bool Stack::full()const
{
	if (_stack.size() == _stack.max_size())
	{
		return true;
	}
	return false;
}

void Stack::print(ostream& os)const
{
	os << "栈中的元素:" << endl;
	for (int i = 0; i < _stack.size(); i++)
	{
		os << _stack[i] << " ";
	}
	os << endl;
	os << "栈的大小:" << size() << endl;
	os << "栈指针此时的位置:" << top() << endl;
}

//main.cpp

//main.cpp

#include"Stack.h"

int main()
{
	string arr[3] =
	{
		"GodFishhh",
		"AFish",
		"Fish"
	};
	vectorvec(arr, arr + 3);
	LIFO_Stack Lifo(vec, vec.size());
	Peekback_Stack Peek(vec, vec.size());

	//测试LIFO_Stack类的data member
	cout << "Lifo:" << endl;
	cout << Lifo << endl;
	cout << "经过push后:" << endl;
	Lifo.push("RubFish");
	cout << Lifo << endl;
	cout << "经过pop后:" << endl;
	string temp;
	Lifo.pop(temp);
	cout << Lifo << endl;
	cout << "pop出的元素为:" << temp << endl;

	cout << "---------------------" << endl;

	//测试Peekback_Stack类的data member
	cout << "Peek:" << endl;
	cout << Peek << endl;
	cout << "经过push后:" << endl;
	Peek.push("DogFish");
	cout << Peek << endl;
	cout << "经过pop后:" << endl;
	Peek.pop(temp);
	cout << Peek << endl;
	cout << "pop出的元素为:" << temp << endl;

	cout << "使用Peekback_Stack类额外的peek()函数" << endl;
	string temp1;
	Peek.peek(0, temp1);
	cout << "peek出的元素为:" << temp1 << endl;
	system("pause");
	return 0;
}

程序运行结果:

Essential C++ 第五章习题_第2张图片

5.3

1.是is-a关系

2.不是is-a关系

3.是is-a关系

4.是is-a关系

5.不是is-a关系,应当为has-a关系,卡车有一个(has-a)引擎

6.是is-a关系

7.是is-a关系

8.不是is-a关系

9.不是is-a关系,借书人是图书馆的成分(component)

5.4

继承体系(每次缩排都代表一个继承关系):

library_lending_material

   book

           audio book

           rental book

           CD-ROM book

  children's puppet

  record

  video

  video game

            Sege

            Sony playstation

            Nintendo

你可能感兴趣的:(Essential,C++,C++,开发语言,c++)