1.回文指的是顺读和逆读都一样的字符串。例如,“tot”和“otto”都是简短的回文。编写一个程序,让用户输入字符串,并将字符串引用传递给一个bool函数。如果字符串是回文,该函数将返回true,否则返回false。此时,不要担心诸如大小写、空格和标点符号这些复杂的问题。即这个简单的版本将拒绝“Otto”和“Madam,I’m Adam”。请查看附录F中的字符串方法列表,以简化这项任务。
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/6/12
* 描述:
************************************************* */
#include <iostream>
#include <string>
//#include "class.h"
using namespace std;
bool isPali(string& s);
int main()
{
using namespace std;
cout << "Enter Palindrome words(q to quit): \n";
string str;
while (cin >> str && str != "q")
{
if (isPali(str))
cout << "Yes, it's palindrome.\n";
else
cout << "No,it's not palindrome.\n";
continue;
}
cout << "Bye.\n";
return 0;
}
bool isPali(string& s)
{
string rs = s;
reverse(rs.begin(),rs.end());
if (s == rs)
return true;
else
return false;
}
2.与编程练习1中给出的问题相同,但要考虑诸如大小写、空格和标点符号这样的复杂问题。即“Madam,I’m Adam”将作为回文来测试。例如,测试函数可能会将字符串缩略为“madamimadam”,然后测试倒过来是否一样。不要忘了有用的cctype库,您可能从中找到几个有用的STL函数,尽管不一定非要使用它们。
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/6/12
* 描述:
************************************************* */
#include <iostream>
#include <string>
#include <cctype>
//#include "class.h"
using namespace std;
bool isPali(string& s);
int main()
{
using namespace std;
cout << "Enter Palindrome words(q to quit): \n";
string str;
while (cin >> str && str != "q")
{
if (isPali(str))
cout << "Yes, it's palindrome.\n";
else
cout << "No,it's not palindrome.\n";
continue;
}
cout << "Bye.\n";
return 0;
}
bool isPali(string& s)
{
//清除标点符号
for (int i = 0;i < s.length();i++)
{
if (!isalpha(s[i]))
s.erase(i);
}
string rs = s;
reverse(rs.begin(),rs.end());
if (s == rs)
return true;
else
return false;
}
3.修改程序清单16.3,使之从文件中读取单词。一种方案是,使用vector对象而不是string数组。这样便可以使用push_back( )将数据文件中的单词复制到vector对象中,并使用size( )来确定单词列表的长度。由于程序应该每次从文件中读取一个单词,因此应使用运算符>>而不是getline( )。文件中包含的单词应该用空格、制表符或换行符分隔。
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/6/12
* 描述:
************************************************* */
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <vector>
using std::string;
int main()
{
using std::cout;
using std::cin;
using std::tolower;
using std::endl;
std::vector<string> wordlist;
string temp;
std::ifstream fin;
fin.open("words.txt");
while (fin >> temp)
wordlist.push_back(temp);
const int NUM = wordlist.size();
srand((int)time(0));
char play;
cout << "Will you play a word game?<y/n> ";
cin >> play;
play = tolower(play);
while (play == 'y') {
string target = wordlist[rand() % NUM];
size_t length = target.length();
string attemp(length, '-');
string badchars;
int guesses = 6;
cout << "Guess my secret word. Is has " << length
<< " letters, and you guess\none lettle at a time. You get " << guesses << " wrong guesses.\n";
cout << "Your word: " << attemp << endl;
while (guesses > 0 && attemp != target) {
char letter;
cout << "Guess a letter: ";
cin >> letter;
if (badchars.find(letter) != string::npos || attemp.find(letter) != string::npos) {
cout << "You already guessed that. Try again.\n";
continue;
}
size_t loc = target.find(letter);
if (loc == string::npos) {
cout << "Oh, bad guess!\n";
--guesses;
badchars += letter;
}
else
{
cout << "Good guess!\n";
attemp[loc] = letter;
loc = target.find(letter, loc + 1);
while (loc != string::npos) {
attemp[loc] = letter;
loc = target.find(letter, loc + 1);
}
}
cout << "Your word: "