Essential c++ 第四章课后练习

版权声明:瞎几把写 https://blog.csdn.net/weixin_43184615/article/details/82897458

练习4.1-4.2

Stack.h 头文件

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

//class主体
class Stack {
public:
	bool   push(const string&);
	bool   pop(string &elem);
	bool   peek(string &elem);

	bool   find(const string &elem) ;
	int    count(const string &elem);
	//以上五个成员函数全是申明
	//以下三个成员函数直接定义于class本身中
	bool   empty() { return _stack.empty(); }
	bool   full()  { return _stack.size() == _stack.max_size(); }
	int    size()  { return _stack.size(); }

private:
	vector<string> _stack;
};

//class主体之外定义成员函数
bool
Stack::pop(string &elem)     //pop 移除栈顶元素
{
	if (empty())
		return false;

	elem = _stack.back();   //back()返回当年vector最后一个元素的引用
	_stack.pop_back();      //删除最后一个元素

	return true;
}

bool
Stack::peek(string &elem)   //peek  获得栈顶元素(即最后一个进栈元素)
{
	if (empty())
		return false;

	elem = _stack.back();
	return true;
}

bool
Stack::push(const string &elem)  //在栈顶增加元素
{
	if (full())
		return false;

	_stack.push_back(elem);
	return true;
}

//Stack中实现fing 函数
bool
Stack::find(const string &elem) 
{
	return std::find(_stack.begin(), _stack.end(), elem) != _stack.end();
}

//Stack中实现count 函数
int
Stack::count(const string &elem)
{
	return std::count(_stack.begin(), _stack.end(), elem);
}

int main()函数,调用Stack.h头文件

#include "Stack.h"

int main()
{
	Stack st;
	string str;

	cout << "Please enter a series of strings.\n";
	while (cin >> str && !st.full())
		st.push(str);

	if (st.empty()) {
		cout << '\n' << "Oops: no strings were read -- bailing out\n ";
	}

	//st.peek(str);
	if (st.size() == 1 && str.empty()) {
		cout << '\n' << "Oops: no strings were read -- bailing out\n ";
	}

	cout << '\n' << "Read in " << st.size() << " strings!" << endl;
	
	//开始调用find和count函数
	//cin.clear后,可以进行第二波输入。
	cin.clear(); //***
	string word;
	cout << "Which word do you want search: ";
	cin >> word;
	if (st.find(word))
	{
		cout << "The word is in the stack,and it occurs " << st.count(word) << " times" << endl;
	}
	else
		cout << "Oops: this word not in the stack\n";

	//将stack的元素取出打印,pop在取出的同时删除该元素
	cout<< "The strings, in reverse order: ";
	while (st.size())
	if (st.pop(str))
		cout << str << ' ';

	cout << '\n' << "There are now " << st.size()
		<< " elements in the stack!\n";

	system("pause");
	return 0;
}

练习4.3

考虑以下所定义的全局数据:
string program_name;
string version_stamp;
int version_number;
int tests_run;
int tests_passed;
编写一个用以包装这些数据的类。

#include<string>
using namespace std;
class global_package{
public:
       //5个调用函数声明定义
        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;}
       //5个赋值函数声明定义
        static void tests_passed( int nval)  { _tests_passed = nval;}
        static void tests_run(int nval)  { _tests_run=nval;}
        static void version_number(int nval) {_version_number=nval;}
        static void version_stamp(const string &nstamp) {_version_stamp=nstamp;}
        static void program_name(const string &npn) { _program_name=npn;}
private:
        static int _tests_passed;
        static int _tests_run;
        static int _version_number;
        static string _version_stamp;
        static string  _program_name;
  };
  //static 成员函数需要在类外进行初始化分配内存!!!!
  int global_package::_tests_passed;
  int global_package::_tests_run;
  int global_package::_version_number;
  string global_package::_version_stamp;
  string global_package::_program_name;

猜你喜欢

转载自blog.csdn.net/weixin_43184615/article/details/82897458