惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

博客园 - 咨询之路

Clustering 传统聚类分析(一) 基本顺序类表 英特尔的高效能管理法 英特尔公司 顺序查找和折半查找 常见算法复杂度 算法性能评价 数的群体分类 类的基本点(二) 类的基本点(一) C++语言中学思想(二) C++语言中学思想(一) 为什么数在内存中要以补码表示[ZZ] C语言中学思想(二) 事半功倍的麦肯锡工作方法[ZZ] 职场男人时尚服饰的选择 C语言中学思想(一) 哥特式建筑
经典的统计保留字算法
咨询之路 · 2006-05-10 · via 博客园 - 咨询之路

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct KeyWord
{
char keyword[20];
int count;
};

KeyWord KeyWordTable[]=
{
{"else",0},{"for",0},{"if",0},{"include",0},{"while",0}
};

int SeqSearch(KeyWord *tab,int n,chr *word)
{
int i;
for(i=0;i<n;i++,tab++)
if(strcmp(word,tab->keyword)==0)
return i;
return -1;
}

int GetWord(ifstream fin,char w[])
{
char c;
int i=0;

while(fin.get(c) && !isalpha(c))
;

if(fin.eof())
return 0;

w[i++]=c;

while(fin.get(c) && (isalpha(c))
w[i++]=c;

w[i]='\0';

return 1;
}

void main(void)
{
const int MAXWORD=50;
const int NKEYWORDS=sizeof(KeyWordTable)/sizeof(KeyWord);
int n;
char word[MAXWORD],c;
ifstream fin;

fin.open("abc.cpp",ios::in|ios::nocreate);
if(!fin)
{
cerr<<"Could not open file 'abc.cpp'"<<endl;
exit(1);
}

while(GetWord(fin,word))
if((n=SeqSearch(KeyWordTable,NKEYWORDS,word)) != -1)
KeyWordTable[n].count++;

for(n=0;n<NKEYWORDS;n++)
if(KeyWordTable[n].count>0)
{
count<<KeyWordTable[n].count;
count<<" "<<KeyWordTable[n].keyword<<endl;
}
fin.close();
}

总结:
结构的定义包含内容和内容索引
结构的初始化及其计算所得的大小
结构内容的查找

获得已字母开头的词
需要跳过非字母的输入(!isalpgha(c))
需要判断文件是否到尾
需要保存满足条件的内容
字符串最后需要加‘\0’

打开文件
获得词汇
计数保存
输出结果