标签归档:string

hangman猜字游戏

C++ primer plus 16.3例题

实际上,书中的例题是有bug的。下面贴出正确的代码

// hangman.cpp -- some string methods
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
using std::string;

const int NUM = 26;
const string wordlist[NUM] = {
	"apiary", "beetle", "cereal", "danger", "ensign", "florid", "garage",
	"health", "insult", "jackal", "keeper", "loaner", "manage", "nonce",
	"onset", "plaid", "quilt", "remote", "stolid", "train", "useful",
	"valid", "whence", "xenon", "yearn", "zippy"
};

int main(void)
{
	using std::cout;
	using std::cin;
	using std::tolower;
	using std::endl;

	std::srand(std::time(0));
	char play;
	cout << "Will you play a word game? <y/n> ";
	cin >> play;
	play = tolower(play);
	while (play == 'y') {
		string target = wordlist[std::rand() % NUM];
		int length = target.length();
		string attempt(length, '-');
		string badchars;
		int guesses = 6;
		cout << "Guess may secret word. It has " << length
 			<< " letters, and you guess\n"
			<< "one letter at a time. You get " << guesses
			<< " wrong guesses.\n";
		cout << "Your word: " << attempt << endl;

		while (guesses > 0 && attempt != target) {
			char letter;
			cout << "Guess a letter: ";
			cin >> letter;
			if (badchars.find(letter) != string::npos
				|| attempt.find(letter) != string::npos) {
				cout << "You already guessed that. \
					Try again.\n";
				continue;
			}
			unsigned long loc = target.find(letter);// 这里一定要用unsigned long来声明变量
			if (loc == string::npos) {
				cout << "Oh, bad guess!\n";
				--guesses;
				badchars += letter;	// add to string
			} else {
				cout << "Good guess!\n";
				attempt[loc] = letter;
				// check if letter appears again
				loc = target.find(letter, loc + 1);
				while (loc != string::npos) {
					attempt[loc] = letter;
					loc = target.find(letter, loc + 1);
				}
			}
			cout << "Your word: " << attempt << endl;
			if (attempt != target) {
				if (badchars.length() > 0)
					cout << "Bad choices: " << badchars 
						<< endl;
				cout << guesses << " bad guesses left\n";
			}
		}
		if (guesses > 0)
			cout << "That's right!\n";
		else
			cout << "Sorry, the word is " << target << ".\n";

		cout << "Will you play another? <y/n> ";
		cin >> play;
		play = tolower(play);
	}

	cout << "Bye\n";

	return 0;
}

自己编写的string类

基本上是照着视频写的, 但是使用了自己的算法

功能如下:
/****************************************************************************
*
* 1. 可对字符串初始化
* mystring s1 = “hello world”;
* 2. 可计算字符串的长度
* cout < < s1.get_len() << endl; * 3. 可对字符串进行赋值运算 * mystring s2; * s2 = s1; * 4. 可以重载运算符"<<"输出字符串, 并且可以级联输出 * cout << s1 << s2 <>”输入字符串, 并且可以级联输入
* cin >> s1 >> s2;
* 6. 可将char形字符串直接赋值给mystring形字符串
* char ch[] = “not at all”;
* s1 = ch;
* 7. 可以像构造对象定义字符串
* mystring s3; //定义一个空字符串
* mystring s4(“mother”); //定义一个非空字符串
* 8. 可以通过下标运算符操作字符串
* cout < < s3[999] << endl; //越界安全保护 * 9. 可以比较两个mystring类字符串, 如:s1 == s2, s1 < s2, s1 > s2
* cout < < s3 < s4 ? s3 : s4 << endl; * 10.可以将两个字符串相加 * s3 = s1 + s2; * cout << s3 << endl; * 11.可执行+=操作 * s1+=s2; * cout << s1 << endl; * 12.可以根据字符串的大小自动保存字符串数组的大小 * *******************************************************************************/

继续阅读