笔试题之字符串是否符合给定词法

题目描述:

  写一个函数,给定一个字符串,返回该字符串是否符合下列英文词法:

  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;
}

结果测试:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 实验内容 1、定义一个右线性正规文法,示例如(仅供参考) G[S]:S→aU|bV| U→bV|aQ V→aU|bQ Q→aQ|bQ|e 实验前要考虑清楚用哪种数据结构存储上述文法。 2、构造其有穷确定自动机,如 3、利用有穷确定自动机M=(K,Σ,f, S,Z)行为模拟程序算法,来对于任意给定的串,若属于该语言时,该过程经有限次计算后就会停止并回答“是”,若不属于,要么能停止并回答“不是”。 K:=S; c:=getchar; while ceof do {K:=f(K,c); c:=getchar; }; if K is in Z then return (‘yes’) else return (‘no’) 2. 实验设计分析 2.1 实验设计思路 根据实验指导书和书本上的相关知识,实现算法。 2.2 实验算法 (1)输入正规文法。RG到FA (2)将NFA化为DFA (3)输入一个字符串判断是否符合文法。 ①最开始记A为开始状态a为第一个字符。 ②然后A经过字符a到达下一个状态记为B,A状态指向B状态,a指向字符串的下一个字符。 ③循环②步直到B状态为终态时停止则该字符串符合该文法或a指向最后一个字符时都没到终态停止则该字符窜不符合该文法。 2.3 实验流程 ①预习实验,实验前阅读实验指导书和阅读书本。 ②通过书本了解判断文法的原理。首先在纸上模拟文法的判断过程。 ③上机实现模拟过程。 ④调试程序,知道能得到预期的结果。 2.4 实验的基本技术设计方案 (用到哪些技术,包括编译原理中,程序设计中,离散数学中等的哪些技术) ①java的基础语法。 ②数据结构里的结构体及简单算法。 ③编译原理的理论知识。 ④运用了java里的一些集合类。 2.5 数据结构 class edge { char PriorityState; char ch; char NextState; edge(char p,char c, char n){ PriorityState = p; ch = c; NextState = n; } @Override public String toString() { return "edge [PriorityState=" + PriorityState + ", ch=" + ch + ", NextState=" + NextState + "]"; } } 2.6 实验输入输出 2.7 实验设计语言 Java语言。 3. 实验主要源代码及分析说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值