当前位置:编程学习 > C/C++ >>

至急!一个C语言程序 各部分的作用 有追加悬赏

各位高手,告诉我这个计算英文文章共有多少不同单词的程序的各个部分的作用,要从实现“计算英文文章共有多少不同单词”和程序本身两个方面解释,谢谢~~


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int compare(const void *a,const void *b);

int main(void){
FILE *fp;
int i,t= 0, count=0;
char line[80],*stp,**word,**wordtemp,*p;

fp=fopen("alice.txt","r");
if(fp==NULL){
printf("can not open the file\
");
exit(1);
}

word=(char **)malloc(sizeof(char *));
if(word==NULL){
printf("memory error\
");
exit(1);
}
word[0]=(char *)malloc(sizeof(char)*40);
if(word[0]==NULL){
printf("memory error\
");
exit(1);
}


while(fgets(line,80,fp)!=NULL){


for(i=0;i<80;i++){
if(line[i]=='\\0'){

break;
}
if(isupper(line[i])!=0){

line[i]=tolower(line[i]);
}
}


for(i=0;i<80;i++){
if(line[i]=='\\0'){

break;
}



if(isalnum(line[i])==0&&line[i]!='\\''){

line[i]=' ';
}



if(line[0]=='\\''){

line[0]=' ';
}



if(line[i]=='\\''&&(isalnum(line[i+1])==0||isalnum(line[i-1])==0)){

line[i]=' ';
}
}


stp=strtok(line," ");
if(stp!=NULL){
strncpy(word[t],stp,40);
}
while(stp!=NULL){
t++;
stp=strtok(NULL," ");
wordtemp=(char **)realloc(word,sizeof(char *)*(t + 1));
if(wordtemp==NULL){

printf("memory error\
");

free(word);

exit(1);
}else{

word=wordtemp;

word[t]=(char *)malloc(sizeof(char)*40);

if(word[t]==NULL){

printf("memory error\
");

exit(1);

}
}
if(stp!=NULL){

strncpy(word[t],stp,40);
}
}
}

qsort(word,t,sizeof(char *),compare);
for(i=0;i<t;i++){
p=bsearch(&word[i],&word[i+1],1,sizeof(char *),compare);

if(p==NULL){
count++;

}

}

printf("%d\
",count);


for(i=0;i<t;i++){
free(word[i]);
}
free(word);

return 0;
}

int compare(const void *a,const void *b){
return strcmp(*(const char **)a,*(const char **)b);


}
答案:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int compare(const void *a,const void *b); /*比较两个字符串*/

int main(void){
FILE *fp;
int i,t= 0, count=0;
char line[80],*stp,**word,**wordtemp,*p;

fp=fopen("alice.txt","r");/*打开文件alice.txt*/
if(fp==NULL){
printf("can not open the file\
");
exit(1);
}

word=(char **)malloc(sizeof(char *)); /*建立一个用来存储字符串指针的指针*/
if(word==NULL){
printf("memory error\
");
exit(1);
}
word[0]=(char *)malloc(sizeof(char)*40); /*word[0]是一个40字节字符串*/
if(word[0]==NULL){
printf("memory error\
");
exit(1);
}


while(fgets(line,80,fp)!=NULL){/*从文件里顺序读取一行*/


for(i=0;i<80;i++){
if(line[i]=='\\0'){

break;
}
if(isupper(line[i])!=0){

line[i]=tolower(line[i]); /*将每个字符都转化成小写*/
}
}


for(i=0;i<80;i++){
if(line[i]=='\\0'){

break;
}



if(isalnum(line[i])==0&&line[i]!='\\''){

line[i]=' '; /*将所有数字过滤掉*/
}



if(line[0]=='\\''){

line[0]=' '; /*将斜线符号替换为空格*/
}



if(line[i]=='\\''&&(isalnum(line[i+1])==0||isalnum(line[i-1])==0)){

line[i]=' '; /*将转义字符替换为空格*/
}
}


stp=strtok(line," "); /*从当前行取得一个单词*/
if(stp!=NULL){
strncpy(word[t],stp,40); /*将单词拷贝到word[t],长度限额40*/
}
while(stp!=NULL){
t++;
stp=strtok(NULL," ");/*同样的方法将整个行转化成为一系列字符串*/
wordtemp=(char **)realloc(word,sizeof(char *)*(t + 1));
if(wordtemp==NULL){

printf("memory error\
");

free(word);

exit(1);
}else{

word=wordtemp;

word[t]=(char *)malloc(sizeof(char)*40);

if(word[t]==NULL){

printf("memory error\
");

exit(1);

}
}
if(stp!=NULL){

strncpy(word[t],stp,40);
}
}
}

/*将这个由一整行转换来的一组字符串按照compare函数定义的规则进行快速排序*/
qsort(word,t,sizeof(char *),compare);
for(i=0;i<t;i++){
/*排序完成后,这一行形成的字符串数组变得有序,这样就可以统计单词数量了-如果排序后前后两个单词是一样的就忽略*/
p=bsearch(&word[i],&word[i+1],1,sizeof(char *),compare);

if(p==NULL){
count++;

}

}

/*显示单词数量*/
printf("%d\
",count);


/*是否内存*/
for(i=0;i<t;i++){
free(word[i]);
}
free(word);

return 0;
}

/*比较函数采用了现成的字符串函数*/
int compare(const void *a,const void *b){
return strcmp(*(const char **)a,*(const char **)b);


}


/*整个程序你可以用EXCEL表格模拟一遍。找到一个英文小说,随便取出一行,
贴到EXCEL表格中,把所有单词列成一列,然后选择排序,再删掉重复的单词,就可以额统计出来总共当前行包括多少个单词了。*/

上一个:C语言程序 哪位高手帮忙找下错(简单的职工管理系统)
下一个:急~~~~~~~~~~c语言问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,