⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordcount.c

📁 ics lab0 题目 用c实现单词计数
💻 C
字号:
#include "std.h"#include "stdlib.h"#include "stdio.h"#include "limits.h"#include "ctype.h"#define BUFSIZE 100#define INSTATE 1#define OUTSTATE 0#define MAXWORD 100struct key{            char *word;            int count;        };long getFileLength(FILE *fp){        long length = 0;        fseek(fp,0,SEEK_END);        length = ftell(fp);        fseek(fp,0,SEEK_SET);       return length;}/*int getWordCount(FILE *fp){ printf("%c",fgetc(fp));  int c,wordcount=0;      while((c=fgetc(fp))!=EOF){        if(c>=65&&c<=122){         // if(( c >= 'A' && c <= 'Z')||(c >= 'a'&&c<='z')){         // if(c>=65&&c<=122&&c!=91&&c!=92&&c!=93&&c!=94&&c!=95&&c!=96){           while((c=fgetc(fp))>=65&&c<=122);         printf("%c",fgetc(fp));            // while((  c >='A' && c<='Z' )||( c>='a' && c<='z' ));            // while(c>=65&&c<=122&&c!=91&&c!=92&&c!=93&&c!=94&&c!=95&&c!=96);              wordcount++;        }       } return wordcount;}*//*int getWordCount(FILE *fp,int length){ int wordcount=0; int i=0; int j = 0; char s[length]; char ch;  fgets(s,length,fp);  printf("%s\n",s);  while(s[i]!='\0'){   for(j=0;j<length;j++){     if(s[j]>=65&&s[j]<=122){        while(s[++j]>=65&&s[++j]<=122);         wordcount++;               }          }//for  i++;} return wordcount;}*//*char*  getwordarray(FILE *fp,int wordcount){     int c;     int i,j;     char *wordarray[wordcount];    for(i=0;i<wordcount;i++){     wordarray[i]=(char *)malloc(sizeof(char)*20);    }     for(i=0;i<wordcount;i++)     {	       for(j=0;j<20;j++)	       {                  // printf("%c\n",fgetc(fp));		  if((c=fgetc(fp))>=65 && c <= 122)                   {		    wordarray[i][j]=c+'0';                                        printf("%c\n",wordarray[i][j]);                                      }		   else{                       break;                       printf("%s","else");		   }	       }      }return *wordarray;	      }*///void sortarray(struct key* keytab)void sortarray(struct key keytab[]){ int m,n; char* t;    for(m=0;m<999;m++){       for(n=m+1;n<1000;n++){         if(strcmp(keytab[m].word,keytab[n].word)<0)            {              strcpy(t,keytab[m].word);              strcpy(keytab[m].word,keytab[n].word);              strcpy(keytab[n].word,t);            }        }     }                                                                                                           }/*struct key *binsearch(char *word,struct key *tab,int n){ int cond; struct key *low = &tab[0]; struct key *high = &tab[n]; struct key *mid; while(low<high){   mid=low+(high-low)/2;   if((cond = strcmp(word,mid->word))<0)     high=mid;   else if(cond<0)     low = mid+1;   else     return mid; } return NULL;}*/int binsearch(char *word,struct key tab[],int n){  int cond;  int low,high,mid;  low=0;  high=n-1;  while(low<=high){    mid=(low+high)/2;    if((cond=strcmp(word,tab[mid].word))<0)       high=mid-1;    else if(cond>0)       low=mid+1;    else        return mid;  }  return -1;}struct key* wordArray(FILE *fp){  int c;  int i=0;  int m,n,k,p;  int u=0;   struct key *keytab;  keytab=(struct key* )malloc(sizeof(struct key)*5000);  for(u=0;u<5000;u++)  {   keytab[u].word = (char*)malloc(sizeof(char)*30);  }  struct key *repkeytab;  repkeytab=(struct key*)malloc(sizeof(struct key)*5000);  for(u=0;u<5000;u++)  {   repkeytab[u].word = (char*)malloc(sizeof(char)*30);  }   char str[1000];    while((c=fgetc(fp))!=EOF){           if(isalpha(c)){        int y=0;                 while(isalpha(c)){                repkeytab[i].word[y]=c;                          y++;             c=fgetc(fp);                          }        strcpy(keytab[i].word,repkeytab[i].word);                for(k=0;k<i;k++)        {            if(strcmp(keytab[k].word,repkeytab[i].word)==0)           {               keytab[k].count++;           }                   }                  printf("%s\t%d\n",keytab[i].word,keytab[i].count);           }         i++;       }// sortarray(keytab); return keytab;}	int main(int argc, char *argv[]){		int ndisp = 0;				struct timeval starttime, endtime, durtime;		int i;				/* step0: get cmd parameter and check it */		if(argv[2]!=NULL)				ndisp = atoi(argv[2]);			   		else				ndisp = 10;		FILE *fp;	       char *fileName=(char*)malloc(sizeof(char)*30);                   strcpy(fileName,argv[1]);               fp=fopen(fileName,"r"); 	       if(fp==NULL)	       {		 printf("can't open %s\n",fileName);		 exit(1);	       }		long length=getFileLength(fp);			     if(length>INT_MAX){                 printf("the file is too large\n");                 exit(1);                }             else if(length==1){                 printf("the file has no content\n");                 exit(1);                }                     		/* step1: prepare work (e.g.open file, load file to memory ...) */	printf("wordcount: Running...\n");	gettimeofday(&starttime, 0);	/* step2: do word count */                         int len, offset = 0;        int wordcount=28;          //int wordcount = getWordCount(fp,length);        printf("the number of words of the file is %d\n",wordcount);       // struct key keytab[wordcount];          struct key *keytab;          keytab=wordArray(fp);       // for(i=0;i<28;i++)       // {         // keytab=wordArray(fp);         // printf("%s",keytab[i].word);       // }               //getwordarray(fp,wordcount);      	gettimeofday(&endtime, 0);	GET_TIME(starttime, endtime, durtime);	printf("wordcount: completed %ld.%ld(s)\n", durtime.tv_sec, durtime.tv_usec);	printf("wordsort: running ...\n");	gettimeofday(&starttime, 0);		/* step3: sort the results */	gettimeofday(&endtime, 0);	GET_TIME(starttime, endtime, durtime);	printf("wordsort: completed %ld.%ld(s)\n", durtime.tv_sec, durtime.tv_usec);	/* step4: print results */	printf("\nwordcount: results (TOP %d from %d):\n", ndisp, len);	/* step5: destroy work (e.g. free memory, close file ...) */        fclose(fp);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -