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 ...
随机推荐
- Sbt的使用初步和用sbt插件生成eclipse工程
以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...
- codevs 1294 全排列 next_permuntation
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- HTML笔记(三) 列表
1. 无序ul标签: <html> <body> <h4>一个无序列表:</h4> <ul> <li>咖啡</li> ...
- 函数对象适配器之ptr_fun的使用示例
//============================================================================ // Name : CopyInts4.c ...
- 防止SQL注入问题
-----解决方案--------------------------------------------------------过滤URL中的一些特殊字符,动态SQL语句使用PrepareState ...
- ScriptX.cab打印控件的使用,控件文件里有
1.在head里添加 <object id="factory" style="display:none;" viewastext classid=&quo ...
- mysql 求时间段平均值
考虑下面的需求,在一段时间内,间隔一段时间,取一个平均值,把所有的平均值取出来,怎么办?思路:在存储过程中,拼接sql语句.根据起始时间和结束时间,while循环每次加一段时间.DROP PROCED ...
- 关于js的兼容问题(小办法)!
今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=docu ...
- 从linux系统mysql导出数据库
原文:http://blog.csdn.net/lifuxiangcaohui/article/details/50763674 1.MySQL数据库导出 /usr/local/mysql/bin/m ...
- java语法糖3 深入剖析Java中的装箱和拆箱
装箱 在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i = new Integer(10); 而在从Java SE5开始就提供了自动装箱的特性, ...