- 编写c程序,统计附件wordCount.txt中每个单词出现的次数
要求:分别使用带缓存以及不带缓存的文件读写方式
单词文本如下:
Eric Rice walked along the Venice Beach boardwalk on a balmy February afternoon, his eyes peeled.
He was searching for Jacob, a homeless person around 20 years old he`d met at a Safe Place for Youth,
a drop-in center where Rice has done research and volunteered. An hour later he found Jacob on the beach,
passing a joint back and forth with friends. Rice told Jacob he`d been selected as a peer educator for a pilot HIV education program.
Jacob had never struck Rice as an exemplary leader; he usually was high when he came by the center, and he stayed far from adults.
But Rice hadn`t picked Jacob - an algorithm did.
The machine learning had undercut human assumptions. When Rice pulled up to the center at 8:30 the next morning for training,
Jacob was waiting with his skateboard and a cup of coffee. Throughout the project, he proved to be a crucial connector to homeless youth living in Venice, Rice says.
实现代码如下:
/*
* @Author: Zedi Liu
* @Date: 2019-04-25 14:09:42
* @Last Modified by: Zedi Liu
* @Last Modified time: 2019-04-25 16:52:15
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/time.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//针对fscanf函数 ,去除单词后带上的符号(",",".",";")
//因为fscanf函数是读取一个字符串,因此单词后面有符号时,也会被读取
void dealwith(char a[][30],int len);
//统计单词出现的次数
//没有对符号进行过滤
void statistical(char a[][30],int len);
//大写英文字母转换成小写
void toLower(char a[]);
//快速排序
void quicksort(char array[][30],int start,int end);
//结构体Word 用于存储单词和出现次数
struct Word
{
char *word;
int times;
};
int main(){
char a[200][30];
FILE *fp;
int index=0;
int fd;
//以下打了注释的是使用带缓存的fscanf函数从文本读取word
// if ((fp=fopen("/media/lzd/1A12F4A712F488D1/学习/Linux/期中考试/5/wordCount.txt","r"))==NULL)
// {
// printf("input file open failed!\n");
// return 1;
// }
// while (fscanf(fp,"%s",a[index])!=EOF)
// {
// index++;
// }
// fclose(fp); //关闭文件
// fp=NULL;
//以下是使用不带缓存的read函数从文本读取word
if ((fd=open("/media/lzd/1A12F4A712F488D1/学习/Linux/期中考试/5/wordCount.txt",O_RDONLY))<0)
{
printf("input file open failed!\n");
return 1;
}
char ch;
int j=0;
while ((read(fd,&ch,1))>0)
{
if (ch=='\n'||ch=='\0'||ch==' '||ch==','||ch==';'||ch=='.') //每个char[][]存储一个字符,并过滤符号
{
a[index][j]='\0'; //判断是一个字符
index++;
j=0;
ch=' ';
}
a[index][j]=ch;
j++;
}
close(fd); //关闭文件
for (int j = 0; j < index; j++)
{
printf("%d :: %s\n",j,a[j]);
}
dealwith(a,index);
quickSort(a,0,index-1);
statistical(a,index);
return 0;
}
//大写英文字母转换成小写
void toLower(char a[]){
for(int i=0;a[i]!='\0';i++)
if(a[i]>='A'&&a[i]<='Z')
a[i]+=32;
}
//针对fscanf函数 ,去除单词后带上的符号(",",".",";")
//因为fscanf函数是读取一个字符串,因此单词后面有符号时,也会被读取
void dealwith(char a[][30],int len){
for (int i = 0; i < len; i++)
{
toLower(a[i]);
if (strstr(a[i],".")||strstr(a[i],",")||strstr(a[i],";"))
{
int j=0;
while (a[i][j]!='\0')
{
j++;
}
a[i][--j]='\0';
printf("%s\n",a[i]);
}
}
}
//统计单词出现的次数
//没有对符号进行过滤
void statistical(char a[][30],int len){
struct Word word[150];
struct Word *p=&word[0];
int j=1;
char b[30];
strcpy(b,a[0]);
p->word=b;
p->times=1;
for (int i = 0; i < len; i++)
{
if (strcmp(b,a[i])==0)
{
p->times++;
}
else
{
strcpy(b,a[i]);
(++p)->word=a[i];
p->times=1;
j++;
}
}
for (; j > 1; j--)
{
printf("单词如下:%s, 次数:%d\n",p->word,p->times); //倒序输出
p--;
}
}
//快速排序代码代码如下
void quicksort(char array[][30],int start,int end){
int i=start;
int j=end;
char c[30];
if (start<end)
{
strcpy(c,array[i]);
while(i<j){
while(j>i&&strcmp(c,array[j])<0){
j--;
}
if (i<j) {
strcpy(array[i],array[j]);
i++;
}
while(j>i&&strcmp(c,array[i])>0){
i++;
}
if(i<j){
strcpy(array[j],array[i]);
j--;
}
}
strcpy(array[i],c);
sort(array,start,i-1);
sort(array,i+1,end);
}
}
运行结果如下:
