天天看點

Essential c++ 第四章課後練習

練習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;