119. El test.txt archivo de texto existente, en el que el contenido es hola, ¿cómo está usted you.welcome a China! Escribir un programa que lea test.txt, cuente el número de veces que cada palabra aparece, y cada palabra y su emergente el número de salida a la pantalla y el archivo.
Sólo una sola frase, dos líneas deben ser ligeramente modificada.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Word //定义单词链表
{
char w[20]; //单词
int count; //该单词出现的次数
struct Word *next;
};
void getData(char str[]) //从文件中逐个读入字符,保存到str字符数组中
{
FILE *fp;
char ch;
int i;
if((fp=fopen("test.txt","r"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
i=0;
while((ch=fgetc(fp))!=EOF)
str[i++]=ch;
str[i]='\0';
fclose(fp);
}
struct Word *Caculate(char str[]) //统计单词及出现的频数
{
int i=0,j=0;
char word1[20]; //找字符串中的每一个单词
struct Word *head=(struct Word *)malloc(sizeof(struct Word)); //建立单词空链表
head->next=NULL;
for(; str[i]!='\0'; i++)
{
if(!((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))) //找到了一个单词使j=0
{
word1[j]='\0'; //此时word1中已存放一个单词
/*构造一个单词结构体*/
struct Word *q;
q=(struct Word *)malloc(sizeof(struct Word));
strcpy(q->w,word1);
q->next=NULL;
/*每找到一个单词从表头开始查找,若有此单词,则个数增1;否则将此单词添加进链表*/
struct Word *p=head;
while(p->next!=NULL) //查找
{
if(strcmp(p->next->w,word1)==0)
{
p->next->count++;
break;
}
p=p->next;
}
if(p->next==NULL) //链表中无此单词,则插入链表尾
{
p->next=q;
q->count=1;
}
j=0; //为找下一个单词做好准备
}
else
word1[j++]=str[i];
}
return head;
}
void print(struct Word *head)
{
struct Word *p=head;
while(p->next!=NULL)
{
printf("%s:%d\n",p->next->w,p->next->count);
p=p->next;
}
}
int main()
{
char str[1000];
getData(str);
printf("test.txt数据:%s\n",str);
struct Word *head;
head=Caculate(str);
printf("统计单词及出现的个数:\n");
print(head);
return 0;
}
El resultado:
120. El test.txt archivo de texto existente, en el que el contenido es Hola, bienvenido a la Universidad Normal de Nanjing! Suerte! Escribir un programa que lea test.txt, cuente el número de veces que aparece cada uno de los personajes, y cada personaje y su el número de ocurrencias de la salida a la pantalla y el archivo.
preguntas similares ibid
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Ch //定义字符链表
{
char ch; //字符
int count; //该字符出现的次数
struct Ch *next;
};
void getData(char str[]) //从文件中逐个读入字符,保存到str字符数组中
{
FILE *fp;
char ch;
int i;
if((fp=fopen("test.txt","r"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
i=0;
while((ch=fgetc(fp))!=EOF)
str[i++]=ch;
str[i]='\0';
fclose(fp);
}
struct Ch *Caculate(char str[]) //统计字符及出现的频数
{
int i;
struct Ch *head=(struct Ch *)malloc(sizeof(struct Ch)); //建立字符空链表
head->next=NULL;
for(i=0; str[i]!='\0'; i++)
{
if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) //找到了一个字符
{
/*构造一个字符结构体*/
struct Ch *q;
q=(struct Ch *)malloc(sizeof(struct Ch));
q->ch=str[i];
q->next=NULL;
/*每找到一个字符从表头开始查找,若有此字符,则个数增1;否则将此字符添加进链表*/
struct Ch *p=head;
while(p->next!=NULL) //查找
{
if(p->next->ch==str[i])
{
p->next->count++;
break;
}
p=p->next;
}
if(p->next==NULL) //链表中无此字符,则插入链表尾
{
p->next=q;
q->count=1;
}
}
}
return head;
}
void print(struct Ch *head)
{
struct Ch *p=head;
while(p->next!=NULL)
{
printf("%c:%d\n",p->next->ch,p->next->count);
p=p->next;
}
}
int main()
{
char str[1000];
getData(str);
printf("test.txt数据:%s\n",str);
struct Ch *head;
head=Caculate(str);
printf("统计字符及出现的个数:\n");
print(head);
return 0;
}
El resultado: