词频统计程序

释放双眼,带上耳机,听听看~!

具体代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
1#include <iostream>
2#include <string>
3#include <ctype.h>
4#define N 50
5typedef struct BiTNode
6{
7    char data[N];
8    int count;
9    struct BiTNode *lChild;
10    struct BiTNode *rChild;
11}BiTNode,*BiTree;
12
13int GetWord(int start,int end,char* pBuf,char* word);       //词汇
14void CreateSearchTree(char* item,BiTree& T);                //创建二叉树进行查找
15void InOrderAndPrint(BiTree T,FILE* pf);                    //进行中序遍历查找并写入文件
16int sum=0;
17char temp1[N];
18char temp2[N];
19
20int main()
21{   while(1){
22    //获取输入文件名
23    char fileName[30];
24    memset(fileName,0,sizeof(fileName));
25    printf("请输入查找的文件名(例:example.txt): ");
26    scanf("%s",fileName);
27    //打开需要统计词汇的文件
28    FILE* pf;
29    pf=fopen(fileName,"rb");
30    if(pf==NULL)
31    {
32        printf("找不到文件!无法打开文件\n");
33        return false;
34    }
35    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
36    printf("成功打开文件--- %s !\n\n",fileName);
37    printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
38  printf("原文内容为:\n");
39    //读取文件
40    fseek(pf,0,SEEK_END);
41    int len=ftell(pf);
42    rewind(pf);
43    char *pBuf=new char[len+1];
44    pBuf[len]=0;
45    fread(pBuf,1,len,pf);
46    fclose(pf);
47
48    //读取单词
49    printf("%s\n",pBuf);
50    printf("*****************************************************************************\n\n");
51  printf("将统计写入文件(如:1.txt )\n");
52 
53
54
55    int i=0;
56    char word[N];
57    BiTree T=NULL;
58    while(i<len)
59    {
60        i=GetWord(i,len,pBuf,word);
61        if(strlen(word)==0)
62        {
63            break;
64        }
65        CreateSearchTree(word,T);      
66    }
67
68    //写入文件并显示在屏幕上
69    memset(fileName,0,sizeof(fileName));
70    printf("请输入写出文件名 : ");
71    scanf("%s",fileName);
72    pf=fopen(fileName,"w");
73    if(pf==NULL)
74    {
75        printf("不能写入文件!");
76        return false;
77    }
78
79    printf("******************************************************************************\n");
80
81    fprintf(pf,"\t\t词频统计结果: \n");
82    printf("\t\t词频统计结果:\n");
83    fprintf(pf,"*********************************************************\n");
84    InOrderAndPrint(T,pf);
85    fprintf(pf,"*********************************************************\n");
86    fprintf(pf,"这篇文章单词总数: %d\n",sum);
87    printf("这篇文章单词总数:%d\n",sum);
88    fclose(pf);
89
90    printf("******************************************************************************\n");
91    printf("统计结果写入文件 --- %s !\n\n",fileName);
92}
93
94
95    return 0;
96
97}
98
99
100//词汇
101int GetWord(int start,int end,char* pBuf,char* word)
102{
103    int i;
104    int j=0;
105    memset(word,0,sizeof(char));
106    for(i=start;i<end;i++)
107    {
108        if(isalpha(pBuf[i]))
109        {
110            word[j]=pBuf[i];
111            j++;
112        }
113        else
114        {
115            if(j==0)
116            {
117                continue;
118            }
119            word[j]='\0';
120            j=0;
121            sum++;
122            break;
123        }
124    }
125    return i;
126}
127
128//创建二叉树进行查找
129void CreateSearchTree(char* item,BiTree& T)
130{
131    strcpy(temp1,item);
132    temp1[0]=tolower(item[0]);
133
134    if(T==NULL)
135    {
136        T=(BiTree)malloc(sizeof(BiTNode));
137        strcpy(T->data,item);
138        T->count=1;
139        T->lChild=NULL;  
140        T->rChild=NULL;
141    }
142    else
143    {
144        strcpy(temp2,T->data);
145        temp2[0]=tolower(T->data[0]);
146        if(strcmp(temp1,temp2)==-1)
147        {
148            CreateSearchTree(item,T->lChild);
149        }
150        else if(strcmp(temp1,temp2)==1)
151        {  
152            CreateSearchTree(item,T->rChild);
153        }
154        else
155        {
156            T->count++;
157        }
158    }
159}
160
161//进行中序遍历查找并写入文件
162void InOrderAndPrint(BiTree T,FILE* pf)
163{
164    if(T!=NULL)
165    {
166        InOrderAndPrint(T->lChild,pf);
167        fprintf(pf,"出现的词汇:%-30s 频率:%-9d\t\n",T->data,T->count);
168        printf("出现的词汇:%-30s 频率:%-9d\t\n",T->data,T->count);
169        InOrderAndPrint(T->rChild,pf);
170    }
171}
172
173
174
175

 

给TA打赏
共{{data.count}}人
人已打赏
安全运维

MySQL到MongoDB的数据同步方法!

2021-12-11 11:36:11

安全运维

Ubuntu上NFS的安装配置

2021-12-19 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索