C++ Primer Plus第十六章练习

不得不说16章的STL初学者噩梦 看到算法那直接省略跳过了
16.10.1

#include <iostream>
#include <string>
using namespace std;
bool Petest(const string &);
int main(void)
{
   
    cout << "otto是否是回文: " << (Petest("otto") ? "是" : "不是") << endl;
    return 0;
}
bool Petest(const string & s)
{
   
    for(int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--)
    {
   
        if(s[i] == s[j])
            continue;
        return false;
    }
    return true;
}

16.10.2

#include <iostream>
#include <cctype>
using namespace std;
bool Petest(const char *);
int main(void)
{
   
    cout << "Madam, I'm Adam是否是回文: " << (Petest("Madam, I'm Adam") ? "是" : "不是") << endl;
    return 0;
}
bool Petest(const char * s)
{
   
    char temp[30];
    int i, j, l;
    for(i = 0, j = 0; s[i]; i++)
    {
   
        if(isalpha(s[i]))
        {
   
            temp[j] = tolower(s[i]);
            j++;
        }
    }
    for(i = 0, l = j - 1; i < j / 2; i++, l--)
    {
   
        if(temp[i] == temp[l])
            continue;
        return false;
    }
    return true;
}

16.10.3
先康康16.3分析

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
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)
{
   
    srand(time(nullptr));
    char play;//此变量判断是否要继续游戏
    cout << "Will you play a word game? <y/n> ";
    cin >> play;
    play = tolower(play);//将输入转换为小写
    while(play == 'y')//是否开始or继续游戏
    {
   
        string target = wordlist[rand() % NUM];//随机抽取一个单词
        int length = target.length();
        string attempt(length, '-');//创建一个与需猜测单词相同长度的string对象 并将该对象用'-'初始化
        string badchars;//猜错的字母放进此对象中
        int guesses = 6;//一共有6次错误机会
        cout << "Guess my secret word. It has " << length
             << " letters, and you guess\n"
             << "one letter at a time. Yout get " << guesses
             << " wrong guesses.\n";//显示一些基本信息
        cout << "Yout 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)
            {
   //如果在猜错单词对象中找到此字母or在才对单词对象中找到此字母
                cout << "You already guessed that. Try again.\n";
                continue;//打印猜过了的提示信息回到循环开始
            }
            int loc = target.find(letter);//看看此字母是否在我们所猜的单词中
            if(loc == string::npos)//如果不在
            {
   
                cout << "Oh, bad guess!\n";
                guesses--;
                badchars += letter;
            }
            else//如果在
            {
   
                cout << "Good guess!\n";
                attempt[loc] = letter;//让我们猜测的存储对象的指定索引成为对的字母
                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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值