C 學習-統(tǒng)計附件wordCount.txt中每個單詞出現(xiàn)的次數(shù)

  1. 編寫c程序,統(tǒng)計附件wordCount.txt中每個單詞出現(xiàn)的次數(shù)
    要求:分別使用帶緩存以及不帶緩存的文件讀寫方式

單詞文本如下:
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.

實現(xiàn)代碼如下:

/*
 * @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函數(shù) ,去除單詞后帶上的符號(",",".",";")
//因為fscanf函數(shù)是讀取一個字符串,因此單詞后面有符號時,也會被讀取
void dealwith(char a[][30],int len);

//統(tǒng)計單詞出現(xiàn)的次數(shù)
//沒有對符號進行過濾
void statistical(char a[][30],int len);

//大寫英文字母轉(zhuǎn)換成小寫
void toLower(char a[]);

//快速排序
void quicksort(char array[][30],int start,int end);

//結(jié)構(gòu)體Word 用于存儲單詞和出現(xiàn)次數(shù)
struct Word
{
  char *word;
  int times;
};

int main(){
    char a[200][30];
    FILE *fp;
    int index=0; 
    int fd;   

    //以下打了注釋的是使用帶緩存的fscanf函數(shù)從文本讀取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);  //關(guān)閉文件
    // fp=NULL;

    //以下是使用不帶緩存的read函數(shù)從文本讀取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); //關(guān)閉文件
    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;
}

//大寫英文字母轉(zhuǎn)換成小寫
void toLower(char a[]){
   for(int i=0;a[i]!='\0';i++)
        if(a[i]>='A'&&a[i]<='Z') 
            a[i]+=32;
}

//針對fscanf函數(shù) ,去除單詞后帶上的符號(",",".",";")
//因為fscanf函數(shù)是讀取一個字符串,因此單詞后面有符號時,也會被讀取
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]);
    }
  }
}

//統(tǒng)計單詞出現(xiàn)的次數(shù)
//沒有對符號進行過濾
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,   次數(shù):%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);
    }
}

運行結(jié)果如下:


1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容