给定一个英文原文,统计文件里面一共有多少个不同的英文单词
来源:互联网 发布:纳什体测数据 编辑:程序博客网 时间:2024/05/01 13:57
// wordsCounter.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "wordsCounter.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**//////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//功能:
//给定一个英文原文,统计文件里面一共有多少个不同的英文单词,建议使用C++,注意程序效率.
//说明:
//不支持中文,不支持长度大于100的单词
#define ISUPPER(x) (x>='A' && x<='Z')
#define ISLOWER(x) (x>='a' && x<='z')
#define ISNUM(x) (x>='0' && x<='9')
#define isalnum(x) (ISUPPER(x) || ISLOWER(x) || ISNUM(x))
#define CHECK(x) {if(!(x)){printf("ERROR IN " #x);return;}}
struct NODE
...{
NODE *next;
char text[100];
int num;
};
void InsertWord(NODE *&root,char *text)
...{
CHECK(text);
if(!root)//如果是空链表,就直接插入作为根
...{
root=new NODE;
strcpy(root->text,text);
root->num=1;
root->next=0;
}
else
...{
NODE *p=root;
NODE *prev=0;
while (p)//否则,找链表中等于当前单词的项
...{
if(stricmp(p->text,text)==0)//如果找到,就增加引用次数num
...{
p->num++;
break;
}
else//否则继续找
...{
prev=p;
p=p->next;
}
}
if(!p)//如果找不到,就插入到链表最后面
...{
p=new NODE;
strcpy(p->text,text);
p->num=1;
p->next=0;
prev->next=p;
}
}
}
void Parse(NODE *&root,char *str)
...{
char *p1=str,*p2=str;//p1指向单词开头,p2指向单词结尾
char word[100];
while(*p1)
...{
//找单词的开头
while (*p1 && !isalnum(*p1))
p1++;
if(!*p1)
break;
else
...{
p2=p1;
}
//printf("enter while *p2 ");
//找单词的结尾
while (*p2 && /**//**p2!=' ' && *p2!=' ' &&*/ isalnum(*p2) )
...{
p2++;
}
//printf("p2-p1 %d,*p2 %c ",p2-p1,*p2);
//将单词加入到链表中
if(p2>p1)
...{
CHECK(p2-p1<100);
strncpy(word,p1,p2-p1);
word[p2-p1]=0;
//printf("Find %s ",word);
InsertWord(root,word);
p1=p2;
}
else
break;
}
}
void PrintNodes(NODE *root)
...{
CHECK(root);
int wordsNum=0;
int diffWordsNum=0;
NODE *pmaxNum=0;
while (root)
...{
wordsNum+=root->num;
diffWordsNum++;
if(!pmaxNum)
pmaxNum=root;
else if(pmaxNum->num < root->num)
pmaxNum=root;
printf("%s %d ",root->text,root->num);
root=root->next;
}
printf("共%d个单词,共%d个不同单词,出现最多的单词是 %s [%d] ",wordsNum,diffWordsNum,pmaxNum->text,pmaxNum->num);
}
void ReleaseLinks(NODE *&root)
...{
if(!root)
return;
NODE *p=root;
while (root)
...{
p=root;
root=root->next;
delete p;
}
}
void _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
...{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
...{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
...{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
NODE *root=0;
//char str[1000]="hello test what hello HeLLO 123 3432 what is a dog my name is hejinshou";
FILE *fp=fopen("wordscounter.cpp","r");
int len=0;
char *str=0;
CHECK(fp);
fseek(fp,0,2);
len=ftell(fp);
CHECK(len>0);
str=new char[len+1];
fseek(fp,0,0);
fread(str,len,1,fp);
str[len]=0;
Parse(root,str);
PrintNodes(root);
ReleaseLinks(root);
delete []str;
}
return ;
}
//
#include "stdafx.h"
#include "wordsCounter.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/**//////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//功能:
//给定一个英文原文,统计文件里面一共有多少个不同的英文单词,建议使用C++,注意程序效率.
//说明:
//不支持中文,不支持长度大于100的单词
#define ISUPPER(x) (x>='A' && x<='Z')
#define ISLOWER(x) (x>='a' && x<='z')
#define ISNUM(x) (x>='0' && x<='9')
#define isalnum(x) (ISUPPER(x) || ISLOWER(x) || ISNUM(x))
#define CHECK(x) {if(!(x)){printf("ERROR IN " #x);return;}}
struct NODE
...{
NODE *next;
char text[100];
int num;
};
void InsertWord(NODE *&root,char *text)
...{
CHECK(text);
if(!root)//如果是空链表,就直接插入作为根
...{
root=new NODE;
strcpy(root->text,text);
root->num=1;
root->next=0;
}
else
...{
NODE *p=root;
NODE *prev=0;
while (p)//否则,找链表中等于当前单词的项
...{
if(stricmp(p->text,text)==0)//如果找到,就增加引用次数num
...{
p->num++;
break;
}
else//否则继续找
...{
prev=p;
p=p->next;
}
}
if(!p)//如果找不到,就插入到链表最后面
...{
p=new NODE;
strcpy(p->text,text);
p->num=1;
p->next=0;
prev->next=p;
}
}
}
void Parse(NODE *&root,char *str)
...{
char *p1=str,*p2=str;//p1指向单词开头,p2指向单词结尾
char word[100];
while(*p1)
...{
//找单词的开头
while (*p1 && !isalnum(*p1))
p1++;
if(!*p1)
break;
else
...{
p2=p1;
}
//printf("enter while *p2 ");
//找单词的结尾
while (*p2 && /**//**p2!=' ' && *p2!=' ' &&*/ isalnum(*p2) )
...{
p2++;
}
//printf("p2-p1 %d,*p2 %c ",p2-p1,*p2);
//将单词加入到链表中
if(p2>p1)
...{
CHECK(p2-p1<100);
strncpy(word,p1,p2-p1);
word[p2-p1]=0;
//printf("Find %s ",word);
InsertWord(root,word);
p1=p2;
}
else
break;
}
}
void PrintNodes(NODE *root)
...{
CHECK(root);
int wordsNum=0;
int diffWordsNum=0;
NODE *pmaxNum=0;
while (root)
...{
wordsNum+=root->num;
diffWordsNum++;
if(!pmaxNum)
pmaxNum=root;
else if(pmaxNum->num < root->num)
pmaxNum=root;
printf("%s %d ",root->text,root->num);
root=root->next;
}
printf("共%d个单词,共%d个不同单词,出现最多的单词是 %s [%d] ",wordsNum,diffWordsNum,pmaxNum->text,pmaxNum->num);
}
void ReleaseLinks(NODE *&root)
...{
if(!root)
return;
NODE *p=root;
while (root)
...{
p=root;
root=root->next;
delete p;
}
}
void _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
...{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
...{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
...{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
NODE *root=0;
//char str[1000]="hello test what hello HeLLO 123 3432 what is a dog my name is hejinshou";
FILE *fp=fopen("wordscounter.cpp","r");
int len=0;
char *str=0;
CHECK(fp);
fseek(fp,0,2);
len=ftell(fp);
CHECK(len>0);
str=new char[len+1];
fseek(fp,0,0);
fread(str,len,1,fp);
str[len]=0;
Parse(root,str);
PrintNodes(root);
ReleaseLinks(root);
delete []str;
}
return ;
}
- 给定一个英文原文,统计文件里面一共有多少个不同的英文单词
- 给定一个文件夹,计算出文件中一共有多少个文件
- 查看一个或多个文件一共有多少行
- 统计一个字符串中一共多少个单词
- 中国字一共有多少个?
- 一共有多少个三角形
- C语言快速计算出一个无符号数一共有多少个“”1”位数的方法
- 爬楼梯问题,一共有n阶台阶,每次你可以走1或者2个台阶,到达顶端一共有多少种不同的走法?
- 统计一个目录下有多少个文件组合命令
- 统计一个文件英文单词的个数
- 世界上一共有多少个理发师
- 全世界一共有多少个IP地址?
- 对n个不同正数进行排列,一共有多少种排列方式
- 写一个程序,统计出一个文本文件中一共出现了多少个字母‘a’?
- 随机生成20个100以内的正整数,统计其中有多少个不同的数
- 统计一个文件有多少行
- PKU1401:给定一个整数N,那么N的阶乘N!末尾有多少个0。
- 给定一个整数N,那么N的阶乘N!末尾有多少个0
- Winsock, ConnectEx, shutdown, SO_UPDATE_CONNECT_CONTEXT and WSANOTCONN
- “映像劫持”技术
- asp无组件上传的原理
- js操作cookies的类
- 随机8位数字和字母组合的恶性U盘病毒的分析
- 给定一个英文原文,统计文件里面一共有多少个不同的英文单词
- GLUT 教程
- 猫猫确实不喜欢计算机。。。
- 超级SQL之报表心得-Oracle
- 用ASPSmartUpload组件实现ASP文件上传
- SAP-近期快照
- runauto..病毒的解决方案
- JS弹出日期控件升级版(带时间选择)
- 英语天天秀(2007/06/18 ) -- 日常起居口语