E có đoạn code dưới nhờ m.n chỉ ra lỗi sai của e với
Đề bài là tạo 1 dslk chứa cụm từ, dòng số bn, trang số bn(25 dòng<=>1trang) trong 1 file tu5chucai.txt
Lúc e chạy thì lúc đc lúc ko nên e nghĩ sai ở đâu đó. E dùng dev C++
Mong m.n chỉ giúp e
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct library
{
char str[30];
int *so_dong;
int *so_trang;
int chi_so;
struct library*next;
}lib;
void add_data(lib*node, char* str, int so_dong, int so_trang, int chi_so)
{
node->chi_so=chi_so;
node->so_dong=(int*)realloc(node->so_dong,node->chi_so+1);
node->so_trang=(int*)realloc(node->so_trang,node->chi_so+1);
node->so_dong[node->chi_so]=so_dong;
node->so_trang[node->chi_so]=so_trang;
strcpy(node->str,str);
}
lib* create_node(char* str, int so_dong, int so_trang)
{
lib *newnode=(lib*)malloc(sizeof(lib));
if(newnode==NULL){printf("loi cap phat bo nho");return;}
add_data(newnode,str,so_dong,so_trang,0);
newnode->next=NULL;
return newnode;
}
void in_ds(lib* first)
{
if(first==NULL) return;
lib*p=first;
printf("\n");
while(p!=NULL)
{
printf("\n%s",p->str);
printf("\n%d",p->so_dong[0]);
printf("\n%d",p->so_trang[0]);
p=p->next;
}
}
lib *insert_to_last(lib*first, char*str, int so_dong, int so_trang)
{
lib*newnode=(lib*)malloc(sizeof(lib));
if(newnode==NULL){printf("loi cap phat bo nho");return;}
lib*p=first;
newnode->next=NULL;
add_data(newnode,str,so_dong,so_trang,0);
while(p->next!=NULL)
p=p->next;
p->next=newnode;
return first;
}
FILE* kiem_tra(FILE*fp, lib*first, int so_dong, int so_trang)
{
lib*p=first;
char temp[30];
fscanf(fp,"%s",temp);
while(p!=NULL)
{
if(strcmp(temp,p->str)==0)
{
p->chi_so=p->chi_so+1;
p->so_dong=(int*)realloc(p->so_dong,p->chi_so+1);
p->so_trang=(int*)realloc(p->so_trang,p->chi_so+1);
p->so_dong[p->chi_so]=so_dong;
p->so_trang[p->chi_so]=so_trang;
}
p=p->next;
}
first=insert_to_last(first,temp,so_dong,so_trang);
return fp;
}
void doc_file(lib* first)
{
int so_dong=1;
int so_trang=1;
int dat_lai=1;
FILE*fp;
fp=fopen("D:\\tu5chucai.txt","rt");
if(fp==NULL) printf("loi mo tep");
char kt;
while(kt=fgetc(fp)!=EOF)
{
lib*p=first;
fp=kiem_tra(fp,p,so_dong,so_trang);
if(kt=='\n')
{
so_dong++;
dat_lai++;
}
if(dat_lai==26)
{
so_trang++;
dat_lai=1;
}
}
fclose(fp);
}
int main()
{
/*
char*str1="vvvvvv";
char*str2="hahahu";
lib*first=NULL;
lib*first=(lib*)malloc(sizeof(lib));
lib*first=create_node(str1,9,9);
lib*p=first;
first=insert_to_last(p,str2,10,10);
in_ds(first);*/
char str1[]="Danh sach";
lib* first=NULL;
first=create_node(str1, 1, 1);
doc_file(first);
in_ds(first);
return 0;
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?