4.1
很奇怪,每次我输入的字符串最后一个都被吃掉了。
Stack.h
#pragma once
#include<vector>
#include<string>
using namespace std;
class Stack {
public:
bool push(const string&);
bool pop(string &elem);
bool peek(string &elem);
bool empty();
bool full();
//size定义于class本身中,其他member仅仅只是声明
int size() { return _stack.size(); }
private:
vector<string> _stack;//作者编码习惯 在data member之前加下划线
};
inline bool Stack::empty() { return _stack.empty(); }
inline bool Stack::full() { return _stack.size() == _stack.max_size(); }
Stack.cpp
#include<vector>
#include<string>
#include"Stack.h"
//取出stack内最后一个被push进来的数值
bool Stack::pop(string &elem)
{
if (empty())
return false;
elem = _stack.back();
_stack.pop_back();
return true;
}
//查看最后一个被pushed的数值
bool Stack::peek(string &elem)
{
if (empty())
return false;
elem = _stack.back();
return true;
}
//将新数值堆放到stack内
bool Stack::push(const string &elem)
{
if (full())
return false;
_stack.push_back(elem);
return true;
}
main.cpp
#include<iostream>
#include<streambuf>
#include<string>
#include<vector>
#include"Stack.h"
using namespace std;
int main()
{
Stack castle_black;
string word;
char c;
while (cin >> word && !castle_black.full()&& (c = cin.get()) != '\n')
{
castle_black.push(word);
}
if (castle_black.empty())
cout << "你咋不输啊" << endl;
cout << "一共输入了" << castle_black.size() << "个单词" << endl;
castle_black.peek(word);
cout << "反序结果\n";
while (castle_black.size())
if (castle_black.pop(word))
cout << word << ' ';
system("pause");
return 0;
}
4.2
#include<iostream>
#include<streambuf>
#include<string>
#include<vector>
#include"Stack.h"
using namespace std;
int main()
{
Stack castle_black;
string word;
char c;
while (cin >> word && !castle_black.full())
{
if (cin.get() == '\n')
break;
else
castle_black.push(word);
}
if (castle_black.empty())
cout << "你咋不输啊" << endl;
cout << "一共输入了" << castle_black.size() << "个单词" << endl;
cin.clear();
cout << "你想找啥\n";
cin >> word;
bool found = castle_black.find(word);
int count = found ? castle_black.count(word) : 0;
if (found)
cout <<word<<"找到了. \n"<< "出现了" << count << "次\n";
system("pause");
return 0;
}
Stack.cpp
#include<vector>
#include<string>
#include"Stack.h"
#include<algorithm>
//取出stack内最后一个被push进来的数值
bool Stack::pop(string &elem)
{
if (empty())
return false;
elem = _stack.back();
_stack.pop_back();
return true;
}
//查看最后一个被pushed的数值
bool Stack::peek(string &elem)
{
if (empty())
return false;
elem = _stack.back();
return true;
}
//将新数值堆放到stack内
bool Stack::push(const string &elem)
{
if (full())
return false;
_stack.push_back(elem);
return true;
}
//用同名的泛型算法实现find和count
bool Stack::find(const string &elem) const
{
vector<string>::const_iterator end_it = _stack.end();
//使用全局作用域运算符::来调用泛型算法,否则会递归调用到自己
return ::find(_stack.begin(), end_it, elem) != end_it;
}
int Stack::count(const string &elem) const
{
return ::count(_stack.begin(), _stack.end(), elem);
}
Stack.h
#pragma once
#include<vector>
#include<string>
using namespace std;
class Stack {
public:
bool push(const string&);
bool pop(string &elem);
bool peek(string &elem);
bool empty();
bool full();
bool find(const string &elem) const;
int count(const string &elem) const;
//size定义于class本身中,其他member仅仅只是声明
int size() { return _stack.size(); }
private:
vector<string> _stack;//作者编码习惯 在data member之前加下划线
};
inline bool Stack::empty() { return _stack.empty(); }
inline bool Stack::full() { return _stack.size() == _stack.max_size(); }
4.3
#include<iostream>
#include<string>
#include<vector>
using namespace std;
using std::string;
class globalWrapper {
public:
static int tests_passed() { return _tests_passed; }
static int tests_run() { return _tests_run; }
static int version_number() { return _version_number; }
static string program_name() { return _program_name; }
static string version_stamp() { return _version_stamp; }
static void tests_passed(int nval) { _tests_passed = nval; }
static void testss_run(int nval) { _tests_run = nval; }
static void version_number(int nval) { _version_number = nval; }
static void program_name(const string& nstamp) { _version_stamp = nstamp; }
static void version_stamp(const string& npn) { _program_name = npn; }
private:
static string _program_name;
static string _version_stamp;
static int _version_number;
static int _tests_run;
static int _tests_passed;
};