我最近在学习C,并且我试图从书的文件详细信息中读取。一本书有作者,标题和标签。一本书可以有多个标签。如果是这种情况,则应根据作者姓名对详细信息进行排序。例如:
这是我的输入文件。
Plato; Feast; philosophy
Shakespeare; Hamlet; tragedy, baroque
Cormen; Algorithms; computer science, handbook
Mickiewicz; Pan Tadeusz; classics, romance, reading
Aquinas; Summa of Theology; philosophy, Medieval
我想根据标签对书籍进行分类,如果标签下面有超过1本书(就像哲学一样,细节应该合并)。
philosophy -> (Aquinas; Summa of Theology)-> (Plato; Feast)
|
V
tragedy -> (Shakespeare; Hamlet)
|
V
baroque -> (Shakespeare; Hamlet)
....
到目前为止,这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct authors {//authors struct to store the author's name and title of the book
char *author;
struct authors *next_author;
};
struct authors *head_author = NULL;
struct labels {// label struct for storing the labels
char *label;
struct labels *next_label;
struct authors *authors_data;
};
struct labels *head_label = NULL;
//=============function Prototypes===============
void insert_authors(char *value);
void append_authors(struct authors** head_ref, char *new_data);
void print_authors();
int main()
{
FILE *fp;
char container[1024];
fp = fopen("input.txt", "r");
while(fgets(container, sizeof(container), fp)){
/*removing trailing \n from the lines*/
size_t len = strlen(container);
if (len > 0 && container[len-1] == '\n') {
container[--len] = '\0';
}
/*string toking the data from the input file*/
char *values = strtok(container, ";,");//first call to strtok
char *values2 = strtok(NULL, ";,");// second call to strtok
char con[100];
/*(first and second call to the strtok gets me the authors & title like this:)
Plato Feast, Shakespeare Hamlet,Cormen Algorithms, Mickiewicz Pan Tadeusz,
Aquinas Summa of Theology*/
strcpy(con, values);
strcat(con, values2);
char *dpcon = strdup(con);
//printf("%s\n", con);
/*The 3rd call to strtok starts the labels for me like this:
philosophy, tragedy, computer science,classic and philosophy*/
char *values3 = strtok(NULL, ";,");//THREE
char *dpvalues3 = strdup(values3);
//printf("%s\n", values3);
/*sending the authors and labels to my authors list*/
append_authors(&head_author, dpvalues3);
append_authors(&head_author, dpcon);
char *values4 = strtok(NULL, ";,");//FOUR
char *dpvalues4 = strdup(values4);
//printf("%s\n", values4);
append_authors(&head_author, dpvalues4);
char *values5 = strtok(NULL, ";,");//FIVE
char *dpvalues5 = strdup(values5);
//printf("%s\n", values5);
append_authors(&head_author, dpvalues5);
/*strtok ends here and the 5th call finished reading the labels*/
}
fclose(fp);
print_authors();
return 0;
}
void insert_authors(char *value) {
struct authors *temp = (struct authors*)malloc(sizeof(struct authors));
temp->author = value;
temp->next_author = NULL;
if(head_author != NULL) { temp->next_author = head_author;}
head_author = temp;
}
void print_authors() {
struct authors* temp = head_author;
while(temp != NULL) {
printf(" %s\n",temp->author);
temp = temp->next_author;
}
printf("\n");
}
如何使用此功能检查重复标签并将每个标签链接到其各自作者的详细信息?
实施例
philosophy -> (Aquinas; Summa of Theology)-> (Plato; Feast)
|
V
tragedy -> (Shakespeare; Hamlet)
|
V
baroque -> (Shakespeare; Hamlet)
因此:
void append_authors(struct authors** head_ref, char *new_data)
{
struct authors* new_node = (struct authors*) malloc(sizeof(struct authors));
struct authors *last = *head_ref;
new_node->author = new_data;
new_node->next_author = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next_author != NULL)
last = last->next_author;
last->next_author = new_node;
return;
}