C 程序以错误的方式对数据进行排序
我必须为学校写一个项目,这是 正如你在程序描述中看到的,我必须按标签对书籍进行分类。现在由于某种原因,不属于特定标签的书籍都是在该标签下印刷的 我找不到我在哪里犯了错误,我将感谢任何帮助 这是我的密码:C 程序以错误的方式对数据进行排序,c,C,我必须为学校写一个项目,这是 正如你在程序描述中看到的,我必须按标签对书籍进行分类。现在由于某种原因,不属于特定标签的书籍都是在该标签下印刷的 我找不到我在哪里犯了错误,我将感谢任何帮助 这是我的密码: #define _CRT_SECURE_NO_WARNINGS #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <stdio.h> #include <
#define _CRT_SECURE_NO_WARNINGS
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <stdio.h>
#include <string.h>
typedef struct BOOK Book;
struct BOOK
{
char *author;
char *title;
Book *next;
};
typedef struct LABEL Label;
struct LABEL
{
char *text;
Label *next;
Book *books;
};
void clear(Label** head);
void addLabel(Label **head, Label *elem);
void addBook(Book **head, Book **elem);
void readFromFile(char *fileName, char *outputFileName, Label *Ihead);
void saveBooks(FILE *file, Book *head);
void saveLabels(FILE *file, Label *Ihead);
void saveToFile(char *fileName, Label *Ihead);
void ComandConsole(int argc, char* argv[], int* fileinput, int* fileoutput);
Label* checkIfExists(Label **head, char *labelText);
Label* checkIfExists(Label **head, char *labelText)
{
Label *tmp = *head;
while (tmp != NULL)
{
if (strcmp(labelText, tmp->text) == 0)
return tmp;
tmp = tmp->next;
}
return NULL;
}
void addLabel(Label **head, Label *elem)
{
Label *temp = NULL;
if ((temp = checkIfExists(head, elem->text)) == NULL)
{
if (*head == NULL)
{
*head = elem;
return;
}
temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = elem;
}
else
addBook(&(temp->books), &(elem->books));
}
void addBook(Book **head, Book **elem)
{
Book *pom = *head;
if (strcmp(pom->author, (*elem)->author) > 0)
{
(*elem)->next = (*head)->next;
*head = *elem;
*elem = pom;
(*head)->next = *elem;
return;
}
while (pom->next != NULL && (strcmp((*elem)->author, pom->author) > 0))
pom = pom->next;
(*elem)->next = pom->next;
pom->next = *elem;
}
void readFromFile(char *fileName, char *outputFileName, Label *head)
{
FILE* input;
if ((input = fopen(fileName, "r")) == NULL)
{
printf("Reading failed!\n");
exit(1);
}
char buf[255];
while (fgets(buf, sizeof buf, input) != NULL)
{
Book *ksiazka = (Book*)malloc(sizeof(Book));
ksiazka->next = NULL;
char *store = strtok(buf, ";");
char * autor = (char*)malloc(sizeof(char)*strlen(store) + 1);
strcpy(autor, store);
ksiazka->author = autor;
store = strtok(NULL, ";");
char * tytul = (char*)malloc(sizeof(char)*strlen(store) + 1);
strcpy(tytul, store);
ksiazka->title = tytul;
store = strtok(NULL, "\n");
char * label = (char*)malloc(sizeof(char)*strlen(store) + 1);
strcpy(label, store);
char *tmp = strtok(label, ",\n");
while (tmp != NULL)
{
Label *newLabel = (Label*)malloc(sizeof(Label));
newLabel->books = NULL;
newLabel->next = NULL;
char *labelText = (char*)malloc(sizeof(char)*strlen(tmp) + 1);
strcpy(labelText, tmp);
newLabel->text = labelText;
newLabel->books = ksiazka;
addLabel(&head, newLabel);
tmp = strtok(NULL, ",\n");
}
}
saveToFile(outputFileName, head);
fclose(input);
}
void clear(Label** head)
{
while (*head != NULL)
{
Label* cur = *head;
*head = (*head)->next;
Book* a = cur->books;
while (a != NULL)
{
Book* b = a;
a = a->next;
free(b->author);
free(b->title);
free(b);
}
free(cur->text);
free(cur);
}
}
void saveBooks(FILE *file, Book *head)
{
Book *tmp = head;
while (tmp != NULL)
{
fprintf(file, "%s, %s\n", tmp->author, tmp->title);
tmp = tmp->next;
}
fprintf(file, "\n");
}
void saveLabels(FILE *file, Label *head)
{
Label *tmp = head;
while (tmp != NULL)
{
fprintf(file, "%s:\n", tmp->text);
if (tmp->books != NULL)
{
saveBooks(file, tmp->books);
}
tmp = tmp->next;
}
}
void saveToFile(char *fileName, Label *head)
{
FILE *output;
if ((output = fopen(fileName, "w")) == NULL)
{
printf("Writing failed!\n");
exit(1);
//clear(&head);
}
else
{
saveLabels(output, head);
}
//clear(&head);
fclose(output);
}
void ComandConsole(int argc, char* argv[], int* fileinput, int* fileoutput)
{
int i;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
switch (argv[i][1])
{
case 'i':
{
i++;
if (i == argc) break;
*fileinput = i;
}
break;
case 'o':
{
i++;
if (i == argc) break;
*fileoutput = i;
}
break;
default:
{
printf("Wrong parameter");
} break;
}
}
}
int main(int argc, char* argv[])
{
int fileinputname = 0;
int fileoutputname = 0;
ComandConsole(argc, argv, &fileinputname, &fileoutputname);
if (fileinputname == 0 || fileoutputname == 0)
{
printf("Wrong parrametes");
getchar();
return 1;
}
Label *head = NULL;
readFromFile(argv[fileinputname], argv[fileoutputname], head);
_CrtDumpMemoryLeaks();
return 0;
}
\define\u CRT\u SECURE\u NO\u警告
#定义\u CRTDBG\u映射\u ALLOC
#包括
#包括
#包括
#包括
typedef结构书;
结构书
{
char*作者;
字符*标题;
书*下一页;
};
typedef结构标签;
结构标签
{
字符*文本;
标签*下一页;
书籍*书籍;
};
空隙清除(标签**头部);
无效添加标签(标签**标题,标签*元素);
无效addBook(Book**head,Book**elem);
void readFromFile(char*fileName,char*outputFileName,Label*Ihead);
作废存储簿(文件*文件,书籍*头);
作废保存标签(文件*文件,标签*Ihead);
作废保存文件(字符*文件名,标签*Ihead);
void-ComandConsole(int-argc,char*argv[],int*fileinput,int*fileoutput);
标签*检查存在(标签**标题,字符*标签文本);
标签*checkIfExists(标签**标题,字符*labelText)
{
标签*tmp=*头部;
while(tmp!=NULL)
{
如果(strcmp(标签文本,tmp->文本)=0)
返回tmp;
tmp=tmp->next;
}
返回NULL;
}
无效添加标签(标签**标题,标签*元素)
{
标签*temp=NULL;
如果((temp=checkIfExists(head,elem->text))==NULL)
{
如果(*head==NULL)
{
*头=元素;
返回;
}
温度=*水头;
while(临时->下一步!=NULL)
温度=温度->下一步;
温度->下一步=元素;
}
其他的
addBook(&(temp->books),&(elem->books));
}
无效地址簿(簿**头,簿**元素)
{
书本*pom=*头;
如果(strcmp(pom->author,(*elem)->author)>0)
{
(*elem)->next=(*head)->next;
*head=*elem;
*elem=pom;
(*head)->next=*elem;
返回;
}
while(pom->next!=NULL&(strcmp((*elem)->author,pom->author)>0))
pom=pom->next;
(*elem)->next=pom->next;
pom->next=*元素;
}
void readFromFile(char*fileName,char*outputFileName,Label*head)
{
文件*输入;
if((输入=fopen(文件名,“r”))==NULL)
{
printf(“读取失败!\n”);
出口(1);
}
char-buf[255];
while(fgets(buf,sizeof buf,input)!=NULL)
{
Book*ksiazka=(Book*)malloc(sizeof(Book));
ksiazka->next=NULL;
char*store=strtok(buf,“;”);
char*autor=(char*)malloc(sizeof(char)*strlen(store)+1);
strcpy(自动、存储);
ksiazka->author=autor;
store=strtok(空,“;”);
char*tytul=(char*)malloc(sizeof(char)*strlen(store)+1);
strcpy(tytul,商店);
ksiazka->title=tytul;
store=strtok(空,“\n”);
char*label=(char*)malloc(sizeof(char)*strlen(store)+1);
strcpy(标签、商店);
char*tmp=strtok(标签“,\n”);
while(tmp!=NULL)
{
Label*newLabel=(Label*)malloc(sizeof(Label));
newLabel->books=NULL;
newLabel->next=NULL;
char*labelText=(char*)malloc(sizeof(char)*strlen(tmp)+1);
strcpy(labelText,tmp);
新建标签->文本=标签文本;
newLabel->books=ksiazka;
添加标签(标题和新标签);
tmp=strtok(空,“,\n”);
}
}
saveToFile(输出文件名,头);
fclose(输入);
}
空白清除(标签**头部)
{
while(*head!=NULL)
{
标签*cur=*头部;
*头部=(*头部)->下一个;
Book*a=cur->books;
while(a!=NULL)
{
书本*b=a;
a=a->next;
免费(b->作者);
免费(b->title);
免费(b);
}
免费(cur->text);
免费(cur);
}
}
作废存储簿(文件*文件,书籍*头)
{
书本*tmp=头部;
while(tmp!=NULL)
{
fprintf(文件,“%s,%s\n”,tmp->author,tmp->title);
tmp=tmp->next;
}
fprintf(文件“\n”);
}
作废保存标签(文件*文件,标签*头)
{
标签*tmp=头部;
while(tmp!=NULL)
{
fprintf(文件“%s:\n”,tmp->text);
如果(tmp->books!=NULL)
{
存折(文件,tmp->books);
}
tmp=tmp->next;
}
}
无效保存文件(字符*文件名,标签*头)
{
文件*输出;
if((输出=fopen(文件名,“w”))==NULL)
{
printf(“写入失败!\n”);
出口(1);
//清晰(头部和头部);
}
其他的
{
保存标签(输出、标题);
}
//清晰(头部和头部);
fclose(输出);
}
void ComandConsole(int-argc,char*argv[],int*fileinput,int*fileoutput)
{
int i;
对于(i=1;i
使用调试器,因此不是调试程序的站点。sizeof(char)始终为1,因此无需使用它,如果不使用它,则malloc()中的+1将无法工作。