zoukankan      html  css  js  c++  java
  • C++ primer 练习9.49

       如果一个字母延伸到中线之上,如d或f,则称其有上出头部分。如果一个字母延伸到中线之下,如p或g,

    则称其有下出头部分。编写程序,读入一个单词,输出最长的即不包含上出头部分,也不包含下出头部分单

    词。

    // 9_49.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<string>
    #include<iostream>
    using namespace std;
    
    string& func(string &str)
    {
        string ascenderAndDescender = "bdfghjklpqty";//把上出头部分和下出头部分的字母包含进来
        static string maxLengthStr = "";             //用来储存最长的要求字符串
        int pos = 0,posAAD=0;                        //pos用来表示所要求字符的位置,posADD用来表示上出头或者下出头字符的位置
        while ((pos = str.find_first_not_of(ascenderAndDescender, posAAD)) != str.npos)//求第一个所要求字符的位置
        {
            if ((posAAD = str.find_first_of(ascenderAndDescender, pos)) != str.npos&&maxLengthStr.size() < (posAAD - pos))
            {                                                            //求第一个上出头或者下出头字符的位置
                maxLengthStr = string(str,pos,posAAD-pos);               //如果新的所要求的字符串长度教大,赋予它新值
            }
        }
        if (pos = str.find_last_not_of(ascenderAndDescender,0) && maxLengthStr.size() < (str.length() - pos))
            maxLengthStr = string(str,pos);                              //这是为了检验有可能在最后的情况
        return maxLengthStr;
    }
    
    int main()
    {
        string str = "asaaaaaaashgasjgpdhgasjqwghnaanbmnna";
        cout << func(str) << endl;
        return 0;
    }

    // 9_49.cpp : 定义控制台应用程序的入口点。//
    #include "stdafx.h"#include<string>#include<iostream>using namespace std;
    string& func(string &str){string ascenderAndDescender = "bdfghjklpqty";//把上出头部分和下出头部分的字母包含进来static string maxLengthStr = "";             //用来储存最长的要求字符串int pos = 0,posAAD=0;                        //pos用来表示所要求字符的位置,posADD用来表示上出头或者下出头字符的位置while ((pos = str.find_first_not_of(ascenderAndDescender, posAAD)) != str.npos)//求第一个所要求字符的位置{if ((posAAD = str.find_first_of(ascenderAndDescender, pos)) != str.npos&&maxLengthStr.size() < (posAAD - pos)){                                                            //求第一个上出头或者下出头字符的位置maxLengthStr = string(str,pos,posAAD-pos);               //如果新的所要求的字符串长度教大,赋予它新值}}if (pos = str.find_last_not_of(ascenderAndDescender,0) && maxLengthStr.size() < (str.length() - pos))maxLengthStr = string(str,pos);                              //这是为了检验有可能在最后的情况return maxLengthStr;}
    int main(){string str = "asaaaaaaashgasjgpdhgasjqwghnaanbmnna";cout << func(str) << endl;    return 0;}

  • 相关阅读:
    win7上装红米4手机驱动提示空间不足
    HBuilder中改造console.info
    Thinkphp 出现 “_CACHE_WRITE_ERROR” 错误的可能解决办法
    Linux上跑两个版本的php,5.4.45和5.3.24
    JavaScript中对日期格式化的新想法.
    怪不得知乎急着招前端开发.
    菜鸟利用python处理大文本数据的血泪路
    Python:数字
    Python:列表,元组
    Python:映像、集合
  • 原文地址:https://www.cnblogs.com/csudanli/p/5338272.html
Copyright © 2011-2022 走看看