题目描述:
写一个函数,给定一个字符串,返回该字符串是否符合下列英文词法:
Sentence:
Noun Verb //例如 C++ rules
Sentence Conjunction Sentence //例如 Birds fly but fish swim
Conjunction:
"and"
"or"
"but"
Noun:
"birds"
"fish"
"c++"
Verb:
"rules"
"fly"
"swim"
解题思路:
首先,待判定的字符串长度不定,要判定词法需要对每一个单词进行判断,所以有必要改变这种长度不定的情况。
然后,题中给出了连接词,名词,动词三个判断依据。可以看出:
1、若没有连接词,则字符串=名词+动词才符合条件
2、若有连接词,则字符串=名词+动词+连接词+名词+动词+连接词+........
实际上,情况2包含情况1,我们采用对数字3求余的方法将不确定的字符串长度转换为0、1、2三种情况下条件的判定。这里余为0的单词为名词,余为1的单词为动词,余为2的单词为连接词,字符串中所有单词满足这个条件则符合文法,否则不符合。
需求:
1、提取输入的字符串中的单词——存取单词的容器
2、判断某单词是否为名词——名词库
3、判断某单词是否为动词——动词库
4、判断某单词是否为连接词——连接词
源代码(C++):
#include <iostream>
#include <string>
#include <vector>
//判断某单词是否为Noun
bool isNoun(std::vector <std::string> strNoun,std::string strToBeJudge)
{
//如果strToBeJudge和strNoun中任何一个单词相同,则返回true
int i;
int count;
count=strNoun.size();
//std::cout<<"count: "<<count<<std::endl;
//std::cout<<"strToBeJudge: "<<strToBeJudge<<std::endl;
for (i=0;i<count;i++)
{
// std::cout<<"strNoun"<<"["<<i<<"]"<<": "<<strNoun[i]<<std::endl;
if ((strToBeJudge.compare(strNoun[i]))==0)
{
std::cout<<"the word '"<<strToBeJudge<<"' is a Noun."<<std::endl;
return true;
}
else
{
continue;
}
}
if (i==count)
{
std::cout<<"the word '"<<strToBeJudge<<"' is Not a Noun!"<<std::endl;
return false;
}
}
//判断某单词是否为动词
bool isVerb(std::vector <std::string> strVerb,std::string strToBeJudge)
{
//如果strToBeJudge和strVerb中任何一个单词相同,则返回true
int i;
int count;
count=strVerb.size();
/*std::cout<<"count: "<<count<<std::endl;
std::cout<<"strToBeJudge: "<<strToBeJudge<<std::endl;*/
for (i=0;i<count;i++)
{
//std::cout<<"strNoun"<<"["<<i<<"]"<<": "<<strVerb[i]<<std::endl;
if ((strToBeJudge.compare(strVerb[i]))==0)
{
std::cout<<"the word '"<<strToBeJudge<<"' is a Verb."<<std::endl;
return true;
}
else
{
continue;
}
}
if (i==count)
{
std::cout<<"the word '"<<strToBeJudge<<"' is Not a Verb!"<<std::endl;
return false;
}
}
//判断某单词是否为连接词
bool isConjunction(std::vector <std::string> strConjunction,std::string strToBeJudge)
{
//如果strToBeJudge和strConjunction中任何一个单词相同,则返回true
int i;
int count;
count=strConjunction.size();
/*std::cout<<"count: "<<count<<std::endl;
std::cout<<"strToBeJudge: "<<strToBeJudge<<std::endl;*/
for (i=0;i<count;i++)
{
//std::cout<<"strNoun"<<"["<<i<<"]"<<": "<<strConjunction[i]<<std::endl;
if ((strToBeJudge.compare(strConjunction[i]))==0)
{
std::cout<<"the word '"<<strToBeJudge<<"' is a Conjunction."<<std::endl;
return true;
}
else
{
continue;
}
}
if (i==count)
{
std::cout<<"the word '"<<strToBeJudge<<"' is Not a Conjunction!"<<std::endl;
return false;
}
}
//提取字符串中的单词,并进行存储
std::vector<std::string> WordExtraction(std::string strToBeExtract)
{
std::vector<std::string> vecStr;
int flag=-1;
std::string strNew;
for (int i=0;i<strToBeExtract.length();i++)
{
if (i==strToBeExtract.length()-1)
{
strNew.clear();
for (int j=flag+1;j<=i;j++)
{
strNew+=strToBeExtract.at(j);
}
flag=-1;
vecStr.push_back(strNew);
}
else if (strToBeExtract.at(i)==32 )
{
strNew.clear();
for (int j=flag+1;j<i;j++)
{
strNew+=strToBeExtract.at(j);
}
flag=i;
vecStr.push_back(strNew);
}
}
return vecStr;
}
int main()
{
std::vector<std::string> strNoun,strVerb,strConjunction;
strNoun.push_back("birds");
strNoun.push_back("fish");
strNoun.push_back("c++");
strVerb.push_back("rules");
strVerb.push_back("fly");
strVerb.push_back("swim");
strConjunction.push_back("and");
strConjunction.push_back("or");
strConjunction.push_back("but");
std::string str;
do
{
std::cout<<"please enter the string:"<<std::endl;
getline(std::cin,str);
std::vector<std::string> vecStr;
vecStr=WordExtraction(str);
int i;
for (i=0;i<vecStr.size();i++)
{
int j=i%3;
if (j==0)
{
if (!isNoun(strNoun,vecStr.at(i)))
{
std::cout<<"the string do not confirm to the conditions!"<<std::endl;
break;
}
}
else if (j==1)
{
if (!isVerb(strVerb,vecStr.at(i)))
{
std::cout<<"the string do not confirm to the conditions!"<<std::endl;
break;
}
}
else
{
if (!isConjunction(strConjunction,vecStr.at(i)))
{
std::cout<<"the string do not confirm to the conditions!"<<std::endl;
break;
}
}
}
if (i==vecStr.size())
{
std::cout<<"the string is eligible~~"<<std::endl;
}
std::cout<<std::endl;
} while (1);
return 0;
}
结果测试: