File is given. It is necessary to count words from it and display (not repeating words) and their number.
I have a problem: the list is filled incorrectly, and the program crashes)
I got the following code:
#include <stdio.h> #include <stdlib.h> #include <conio.h> struct elem{ //Элемент списка char word[255]; int count; elem *next; }; elem * GDFF(char filename[], elem *dict); //Возвращает указатель на словарь. Get Dictionary From File. Получает словарь из файла. void printD(elem *dict); //Вывод на экран elem* addW(char word[255],elem *dict); elem *searchW(char t[],elem *dict); elem* freeD(elem *dict); void strcpy(char a[255],char b[]); void main(){ char fn[]=""; //Путь к файлу elem *dict=NULL; //Указатель на список dict=NULL; printf("Enter filename:\n"); scanf("%s",&fn); dict=GDFF(fn,dict); printD(dict); dict=freeD(dict); getch(); } void printD(elem *dict){ while(dict!=NULL){ printf("%s : %d\n",dict->word,dict->count); dict=dict->next; } } elem * GDFF(char filename[], elem *dict){ FILE *f=fopen(filename, "r"); elem *pointer; char t[255]; while(!feof(f)){ fscanf(f,"%s",&t); if(pointer=searchW(t,dict)){ pointer->count++; printf("plussed %s",pointer->word); }else{ dict=addW(t,dict); printf("added %s",dict->word); } //printf("%s\n",t); } return dict; } elem *searchW(char t[],elem *dict){ elem *p=dict; while(p!=NULL&&p->word!=t){ p=p->next; } return p; } elem* addW(char word[255],elem *dict){ elem *p=dict; elem *t=(elem*)malloc(sizeof(elem)); t->count=1; strcpy(t->word,word); t->next=NULL; if(p!=NULL){ while(p->next!=NULL){ p=p->next; } p->next=t; }else{ return t; } return dict; } elem* freeD(elem *dict){ while(dict!=NULL){ elem *t=dict; dict=dict->next; free(t); } return NULL; } void strcpy(char a[255],char b[]){ for(int i=0; i<255; i++){ a[i]=b[i]; } } Code corrected get the following
#include <stdio.h> #include <stdlib.h> #include <conio.h> struct elem{ //Элемент списка char word[255]; int count; elem *next; }; elem * GDFF(char filename[], elem *dict); //Возвращает указатель на словарь. Get Dictionary From File. Получает словарь из файла. void printD(elem *dict); //Вывод на экран elem* addW(char word[255],elem *dict); elem *searchW(char t[],elem *dict); elem* freeD(elem *dict); void strcpy(char a[255],char b[]); bool equal(char a[255],char b[]); void main(){ char fn[255]=""; //Путь к файлу elem *dict=NULL; //Указатель на список dict=NULL; printf("Enter filename:\n"); scanf("%s",&fn); dict=GDFF(fn,dict); printD(dict); dict=freeD(dict); getch(); } void printD(elem *dict){ while(dict!=NULL){ printf("%s : %d\n",dict->word,dict->count); dict=dict->next; } } elem * GDFF(char filename[], elem *dict){ FILE *f=fopen(filename, "r"); elem *pointer; char t[255]; while(!feof(f)){ fscanf(f,"%s",&t); if(pointer=searchW(t,dict)){ pointer->count++; }else{ dict=addW(t,dict); printf("added %s",dict->word); } } return dict; } elem *searchW(char t[],elem *dict){ elem *p=dict; while(p!=NULL&&!equal(p->word,t)){ p=p->next; } return p; } elem* addW(char word[255],elem *dict){ elem *p=dict; elem *t=(elem*)malloc(sizeof(elem)); t->count=1; strcpy(t->word,word); t->next=NULL; if(p!=NULL){ while(p->next!=NULL){ p=p->next; } p->next=t; }else{ return t; } return dict; } elem* freeD(elem *dict){ while(dict!=NULL){ elem *t=dict; dict=dict->next; free(t); } return NULL; } void strcpy(char a[255],char b[]){ for(int i=0; i<255; i++){ a[i]=b[i]; } } bool equal(char a[255],char b[]) { for(int i=0; i<255&&a[i]&&b[i]; i++){ if(a[i]!=b[i]){ return false; } } return true; }