[C/C++]据说是某年的华为机试题

       练习了一道据说是某年的华为机试题,没看答案自己敲的,功能实现了,可能不太完善,贴出来大家瞧瞧,题目如下:

       输入一段英文文本,用程序统计出现频率最高和最低的两个单词;

       仅大小写不同的单词算同一个单词;

       如果两个单词出现次数相同,则在文本中首次出现的单词优先返回。

       返回的单词统一用小写字母返回

       英文文本中仅出现这四类字符:空格( )、英文逗号(,)、英文句号(.)、英文大小写字母(a-z、A-Z)

       单词之间的分隔符仅考虑这三种:空格( )、英文逗号(,)、英文句号(.);

例如:
       输入字符串“Hello world, i said hello world to the world”,返回“world”,“i”

       输入字符串“Somebody like somebody,i do not like it”,返回“somebody”,“i”


要求实现函数: 

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord);

       【输入】 pInputStr:  输入字符串,指向一段英文文本

       【输出】 pOutputHotWord: 输出字符串,返回出现次数最多的单词,该指针所指存储空间已经分配好,且足够大

                      pOutputColdWord:输出字符串,返回出现次数最少的单词,该指针所指存储空间已经分配好,且足够大

示例 
输入:“Hello world, i said hello world to the world”


       代码如下,编译环境是VS2005,下下来就能跑;代码具体实现步骤我就不细说了,在我的注释里有,有什么问题一起讨论啦~

#include <iostream>

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord);

int main()
{
	char line[] ="Hello world, i said hello world to the world";
	//char line[] ="Somebody like somebody,i do not like it";
    char hot[20];
	char cold[20];

    WordStat(line, hot, cold);    

    std::cout << "The hot word is: " << hot << '\n'          //打印字符串,显示出现次数最多的单词
	          << "and the cold word is: " << cold << '\n';   //打印字符串,显示出现次数最少的单词

    system("pause");
    return 0;
}

void WordStat(const char * pInputStr, char * pOutputHotWord, char * pOutputColdWord)
{
	char str[11][10];                                     //创建一个二维字符数组,可以存放11个长度为10的字符串
	int i = 0,j = 0,k = 0;                                //j表示第j个字符串,k表示字符串的第k个字符

/*
  此代码块的作用为:将句子分割成j个单词,并转换成小写,存入字符串数组
*/
	for(i = 0;i < strlen(pInputStr);i++)                  //
	{
	   if(('a' <= pInputStr[i] && pInputStr[i] <= 'z') || ('A' <= pInputStr[i] && pInputStr[i] <= 'Z'))
	   { 
		   if(('a' <= pInputStr[i] && pInputStr[i] <= 'z')) 
		   {
		       str[j][k++] = pInputStr[i];                    //当句子中的字母为小写时,直接存入字符串数组中
		   }else
			   str[j][k++] = pInputStr[i] - 'A' + 'a';        //当句子中有字母是大写时,将其转换为小写后再存入字符串数组

	   } else if(('a' <= pInputStr[i+1] && pInputStr[i+1] <= 'z') || ('A' <= pInputStr[i+1] && pInputStr[i+1] <= 'Z'))
	     {
		      str[j][k] = '\0';             //当检测到字符不是字母时,再字符串后加空字符
		      j++;                          //并换到下一行字符串
		      k = 0;
	      }	
	   
	}
	if(k)                           //当k不为0,即句子最后一个字符是字母,给字符串后加上空字符
	{
	str[j][k] = '\0';               //k可以,k+1不行
	}


/*
  创建cnt[]数组,将每个字符串出现的次数对应字符串数组的顺序放入cnt[]中
*/
	int cnt[20] = {0};
	int temp = 0,max = 0,min = 0;
    for(i = 0;i< j + 1;i++)
	{
		for(int n = 0;n < j+1; n++)
		{
		if(strcmp(str[i],str[n])==0)
			cnt[i]++;
				
		}	
	}
	temp = cnt[0];

/*
  找出出现次数最多的单词,str[max]为出现次数最多的单词
*/
	for(i = 1;i < j + 1;i++)
	{
		if(temp < cnt[i])      //当有多个单词出现次数相同时,max保存第一次出现的字符串的下标
		{
			temp = cnt[i];
			max = i;
		
		}	
	}

	for(i = 0; i <j + 1;i++)
	{
		std::cout<< "No." << i+1 << " word: " << str[i]                        //分别打印各个单词,并显示对应的出现的次数
		         << "      showed " << cnt[i] << " times" << std::endl;
	}


//	std::cout << "the max word is : " << max << ": " <<str[max] << std::endl;


/*
  找出出现次数最少的单词,str[min]为出现次数最少的单词
*/
	temp = cnt[0];
	for(i = 1;i < j + 1;i++)      //当有多个单词出现次数相同时,min保存第一次出现的字符串的下标
	{
		if(temp > cnt[i])
		{
			temp = cnt[i];
			min = i;
		
		}	
	}
//	std::cout << "the min word is : " << min << ": " <<str[min] << '\n';


/*
  将hot word和cold word分别放入 pOutputHotWord和 pOutputColdWord 指向的字符串中
*/
for(i = 0; i<strlen(str[max])+1;i++)
   pOutputHotWord[i] = str[max][i];

for(i = 0; i<strlen(str[min])+1;i++)
   pOutputColdWord[i] = str[min][i];
}
       祝大家下午饭有个好胃口。 再见

你可能感兴趣的:(C++,c,华为,机试)