C++统计一段文字中各单词出现的频率
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class SqString
{
private:
char * base;
int length;
public:
SqString()
{
}
SqString(char * s)
{
length=0;
base=s;
int i=0;
while(s[i]!='\0')
{
++i;
++length;
}
}
char * getBase()
{
return base;
}
int getLength()
{
return length;
}
void StrConcat(SqString ss)
{
char * newbase=new char[ss.getLength()+length+1];
for(int i=0;i<length;i++)
{
newbase[i]=base[i];
}
for(int j=0;j<=ss.getLength();j++)
{
newbase[j+length]=ss.base[j];
}
base=newbase;
length=ss.getLength()+length;
}
int getNumOfSonSqString(SqString son)
{
int Num=0;
for(int i=0;i<=length-son.getLength();i++)
{
int j=0;
for(j=0;j<son.getLength();j++)
{
if(son.base[j]!=base[i+j])
{
break;
}
}
if(j==son.getLength())
{
Num++;
}
}
return Num;
}
bool isEqual(SqString s)
{
int i=0;
while(s.base[i]!='\0'&&s.base[i]==base[i]&&base[i]!='\0')
{
++i;
}
if(i==length&&i==s.length)
{
return true;
}
else
{
return false;
}
}
};
class SqStringNode
{
public:
SqStringNode * pNext;
SqString data;
};
class SqStringList
{
public:
SqStringNode * pHead;
SqStringNode * pTail;
int length;
public:
SqStringList()
{
pHead=new SqStringNode;
pTail=pHead;
pTail->pNext=NULL;
length=0;
}
void insertIntoList(SqString s)
{
SqStringNode * sNode=new SqStringNode;
sNode->data=s;
sNode->pNext=NULL;
pTail->pNext=sNode;
pTail=sNode;
length++;
}
void show()
{
SqStringNode * temp=pHead->pNext;
while(temp)
{
cout<<temp->data.getBase()<<endl;
temp=temp->pNext;
}
}
int getListLength()
{
return length;
}
bool isHave(SqString s)
{
SqStringNode * temp=pHead->pNext;
int i=0;
while(temp)
{
if(temp->data.isEqual(s))
{
break;
}
temp=temp->pNext;
i++;
}
if(i>=length)
{
return false;
}
else
{
return true;
}
}
};
class Text
{
private:
SqString content;
char * inside;
int length;
public:
Text(SqString & s)
{
content=s;
length=s.getLength();
inside=content.getBase();
}
char * getInside()
{
return inside;
}
SqString getContent()
{
return content;
}
int getTextLength()
{
return length;
}
SqStringList getAllWordsFromText()
{
char * temp=new char[20];
SqStringList list;
int j=0;
for(int i=0;i<length;i++)
{
if(inside[i]==' '||inside[i]==','||inside[i]=='.')
{
temp[j]='\0';
if(temp[0]!='\0')
{
SqString sqstring(temp);
if(!list.isHave(temp))
{
list.insertIntoList(sqstring);
}
}
temp=new char[20];
j=0;
while(inside[i]==' '||inside[i]==','||inside[i]=='.')
{
++i;
}
}
temp[j]=inside[i];
j++;
}
//最后一个单词的判断
temp[j]='\0';
SqString sqstring(temp);
if(!list.isHave(temp))
{
list.insertIntoList(sqstring);
}
return list;
}
};
int main(int argc, char *argv[]) {
SqString sqstring("flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie,flypie is good,flypie is better,flypie is best,I love flypie");
Text text(sqstring);
cout<<"这段文字为:"<<endl;
cout<<text.getInside()<<endl;
cout<<"----------------------------------"<<endl;
cout<<"统计结果为:"<<endl;
SqStringList list=text.getAllWordsFromText();
//list.show();
SqStringNode * temp=list.pHead->pNext;
while(temp)
{
cout<<temp->data.getBase()<<" : "<<sqstring.getNumOfSonSqString(temp->data)<<endl;
temp=temp->pNext;
}
return 0;
}
C++统计一段文字中各单词出现的频率的更多相关文章
- 统计一段文字中出现频率最高的10个单词(c语言)
注:这次使用C语言做的这个程序.个别不懂的地方和算法部分是请教的其他同学,交流并吸收,所以收获颇多! 在程序中每一个地方我都做了注释,方便同学之间交流.也让老师容易看.程序也有很多不足的地方,但限于本 ...
- java-统计一段句子中各单词出现的次数
问题:统计一段句子中各单词出现的次数. 思路: 1.使用split方法将文章进行分割,我们这里以空格.逗号和句点为分隔符,然后存到一个字符串数组中. 2.创建一个hashMap集合,key是字符串类型 ...
- 使用PHP的strstr()函数来统计一段字符串中元音字母的个数(区分大小写)
<?php/**练习:统计一段字符串中所有元音字母的个数(区分大小写)*/$str='This is a test file.'; //原始字符串echo $str.'<br>'; ...
- 一段文字中的几个keyword显示高亮
将一段文字中的几个keyword显示高亮 演示样例:将"我的愿望是当个绿巨人,所以我想让我的皮(derma)肤是绿色"中的"皮肤"显示绿色. <span ...
- js或者jq判断一段文字中是否有自己想要的那几个字,如果有就把那几个字变成红色
如何判断一段文字中是否有自己想要的那几个字,如果有就把那几个字变成红色(在html中)比如有body中有这么一串文字“驾驶的后视镜的华盛顿”,想要判断里面是否有“驾驶”这两个字,如果有就把这两个字变成 ...
- java 实现统计某段文字在内容中出现的次数
http://outofmemory.cn/code-snippet/815/java-zishutongji 一个api,位于apache.commons.lang.StringUtils类下的一个 ...
- JS 实现计算一段文字中的字节数,字母数,数字数,行数,汉字数。
看到了匹配,第一个想到了用正则表达式,哈哈,果然很方便.不过正则表达式高深莫测!我还没有研究明白啊..目前学了点皮毛.代码如下: <!DOCTYPE html PUBLIC "-//W ...
- 使用hadoop统计多个文本中每个单词数目
程序源码 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Con ...
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
仅简单统计英文. from collections import Counter f = open('1') c = Counter() for line in f: g = (x for x in ...
随机推荐
- 二叉树hdu1710
学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...
- C#强制清除缓存
#region 强制清除缓存 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize" ...
- 解决iOS内存泄露
文章很好,摘自:http://www.codeceo.com/article/xcode-instruments-ios-memory.html 虽然iOS 5.0版本之后加入了ARC机制,由于相互引 ...
- HDU 5430 Reflect(欧拉函数)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 ...
- (一)使用springAPI以及自定义类 实现AOP-aop编程
Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...
- Java编程思想学习笔记_3(继承,内部类)
一.继承与清理 如果某个类需要去清理自身的资源,那么必须用心为其创建回收垃圾的方法,而如果此类有导出的子类,那么必须在导出类中覆盖回收的方法,当覆盖被继承类的回收垃圾的方法的时候,需要注意销毁的顺序应 ...
- Android 摇一摇之双甩功能
Android 摇一摇之双甩功能 最近做一个摇一摇的功能 网上相关代码很多 但是这次的需求有点奇葩 要求是摇两次才生效 看起来好像很简单 但真正要做遇到的问题还是很多 时间限制 机型灵敏性 摇动的方式 ...
- Date 与 switch的运用
<html><head><title>日期</title><script>function toChinese(day){ switch(d ...
- Android动画之translate(位移动画)
上一篇文章讲了 Android的左右滑动切换,实现过程是非常简单,一些新手可能会向深入了了解Activity切换的原理,下面主要对左右滑动进行深入的探讨,并以项目中的一个切换效果来进一步了解. Act ...
- maven编译项目时提示:cached in the local repository
今天使用命令mvn compile编译maven项目时提示错误信息,部分错误信息如下: ...... was cached in the local repository, resolution wi ...