任何人可以帮助我的图书馆目录C程序? [英] Can anyone help me with my library catalog C program?

查看:125
本文介绍了任何人可以帮助我的图书馆目录C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用BST和文件处理,但是由于displayCatalog功能似乎不起作用,所以我被困住了。

I need to use BST and file handling but I'm stuck with this one because the displayCatalog function doesn't seem to work.

我想建立结构化之前,我打印到文件。我该怎么做?

I want to establish the struct first before I print it to the file. How do I go about this?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

typedef struct book catalog;
FILE *fx,*fy;

struct book{
    char title[20];
    char author[20];
    int isbn[15];
    char genre[10];
    char publisher[20];
    int year[5];
    char synopsis[100];
    catalog *left;
    catalog *right;
};

catalog *root;

void addBooks(); 
void removeBooks(); //
void modify(); //
void searchBook(); //
void perTitle(); //
void perAuthor(); //
void perISBN(); //
void perGenre(); //
void displayCatalog(catalog *root); //
int ifexists(char *x); 
void insert(catalog *root,catalog *x);
catalog remove(catalog *root,catalog *x);
void menu();
void gotoxy(int x,int y);

void main(){
    root=NULL;

    menu();
}

void menu(){
    int choice;

    system("cls");
    gotoxy(22,5);
    printf("----------WELCOME TO THE CARD CATALOG----------");
    gotoxy(22,7);
    printf("1] Add a book");
    gotoxy(22,8);
    printf("2] Remove a book");
    gotoxy(22,9);
    printf("3] Modify a book");
    gotoxy(22,10);
    printf("4] Search a book");
    gotoxy(22,11);
    printf("5] Display the catalog");
    gotoxy(22,12);
    printf("6] Exit");

    gotoxy(22,14);
    printf("Enter your choice: ");
    scanf("%d",&choice);

    switch(choice){
    case 1: addBooks();
        break;
    case 2: removeBooks();
        break;
    case 3: modify();
        break;
    case 4: searchBook();
        break;
    case 5: displayCatalog(root);
        break;
    case 6: exit(0);
    }
}

void addBooks(){
    char y;
    catalog *ptr;
    ptr=(catalog*)malloc(sizeof(catalog));

    system("cls");
    gotoxy(22,8);
    printf("Title: ");
    scanf("%s",ptr->title);
    gotoxy(22,9);
    printf("Author: ");
    scanf("%s",ptr->author);
    gotoxy(22,10);
    printf("Genre: ");
    scanf("%s",ptr->genre);
    gotoxy(22,11);
    printf("ISBN: ");
    scanf("%s",ptr->isbn);
    gotoxy(22,12);
    printf("Publisher: ");
    scanf("%s",ptr->publisher);
    gotoxy(22,13);
    printf("Year: ");
    scanf("%s",ptr->year);
    gotoxy(22,14);
    printf("Synopsis: ");
    scanf("%s",ptr->synopsis);fflush(stdin);
    ptr->left=NULL;
    ptr->right=NULL;

    if(root==NULL){
        root=ptr;
    } else {
        insert(root,ptr);
    }

    menu();
}

void insert(catalog *root,catalog *x){
    if(x->isbn < root->isbn){
        if(root->left==NULL){
            root->left=x;
        } else {
            insert(root->left,x);
        }
    }

    if(x->isbn > root->isbn){
        if(root->right==NULL){
            root->right=x;
        } else {
            insert(root->right,x);
        }
    }
}

void removeBooks(){
    catalog *ptr,*temp;
    int x;
    char title[20],y;

    system("cls");

    if(root==NULL){
        gotoxy(22,10);
        printf("No records to show...");
        getch();
        menu();
    }

    gotoxy(22,10);
    printf("Enter title of book to delete: ");
    scanf("%s",title);

    ptr=root;
    while(ptr!=NULL){
        if(strcmp(ptr->title,title)==0){
            gotoxy(22,11);
            printf("The book is in the catalog.");
            gotoxy(22,12);
            printf("Title: %s",root->title);
            gotoxy(22,13);
            printf("Author: %s",root->author);
        } else {
            gotoxy(22,11);
            printf("No book with that title in the catalog.");
            getch();
            menu();
        }

        if(strcmp(ptr->title,title)==0){
            gotoxy(22,14);
            printf("Remove book record? (y/n) ");
            if(getch()=='y'){
                remove(root,ptr);
                menu();
            } else {
                menu();
            }
    }
    }
}

catalog remove(catalog *root,catalog *x){
    catalog *temp;
    char y;

    if(x==root){
        temp=root;
        free(temp);
        root=NULL;
    } else if(x->isbn < root->isbn){
        *root->left=remove(root->left,x);
    } else if(x->isbn > root->isbn){
        *root->right=remove(root->right,x);
    } else if(root==NULL){
        gotoxy(22,10);
        printf("Nothing to remove...");
        getch();
        menu();
    }

    printf("The book has been removed.");
    printf("Remove another book? (y/n) ");
    scanf("%c",&y);

    return *x;
}

void modify(){
    catalog *ptr;
    int num;
    char title[20];

    system("cls");

    if(root==NULL){
        printf("No records exist...");
        getch();
        menu();
    }

    printf("Enter title of book to be modified: ");
    scanf("%s",title);

    ptr=root;
    while(ptr!=NULL){
        if(strcmp(ptr->title,title)==0){
            printf("Input new information.");
            printf("Title: ");
            scanf("%s",root->title);
            printf("Author: ");
            scanf("%s",root->author);
            printf("ISBN: ");
            scanf("%d",root->isbn);
            printf("Publisher: ");
            scanf("%s",root->publisher);
            printf("Year: ");
            scanf("%d",root->year);
            printf("Synopsis: ");
            scanf("%s",root->synopsis);

            printf("The book's information has been modified.");
        } else {
                printf("No book found.");
                break;
            }
    }

    getch();
    menu();
}

void searchBook(){
    int choice;
    char title[20], author[20];
    int num;

    system("cls");

    if(root==NULL){
        printf("No records to show...");
        getch();
        menu();
    }

    gotoxy(22,10);
    printf("-----SEARCH A BOOK-----");
    gotoxy(22,12);
    printf("1] By title");
    gotoxy(22,13);
    printf("2] By author");
    gotoxy(22,14);
    printf("3] By ISBN");
    gotoxy(22,15);
    printf("4] Back to menu");
    gotoxy(22,18);
    printf("Enter your choice: ");
    scanf("%d",&choice);

    switch(choice){
    case 1: perTitle();
        break;
    case 2: perAuthor();
        break;
    case 3: perISBN();
        break;
    case 4: perGenre();
        break;
    case 5: menu();
    }
}

void perTitle(){
    catalog *ptr;
    char title[20],ans;

    system("cls");
    gotoxy(22,15);
    printf("Enter book title: ");
    scanf("%s",title);

    ptr=root;
    while(strcmp(ptr->title,title)!=0){
        ptr=ptr->left;
        if(ptr==NULL)
            menu();
    }

    system("cls");
        if(ptr!=NULL){
        gotoxy(22,10);
        printf("That book is in the catalog.");
        gotoxy(22,11);
        printf("Title: %s",ptr->title);
        gotoxy(22,12);
        printf("Author: %s",ptr->author);
        gotoxy(22,13);
        printf("ISBN: %d",ptr->isbn);
        gotoxy(22,14);
        printf("Genre: %s",ptr->genre);
        gotoxy(22,15);
        printf("Publisher: %s",ptr->publisher);
        gotoxy(22,16);
        printf("Year: %d",ptr->year);
        gotoxy(22,17);
        printf("Synopsis: %s",ptr->synopsis);
        } else {
            gotoxy(22,10);
            printf("No records to show...");
        }

    printf("Try another? (y/n) ");
    scanf("%c",&ans);

    switch(ans){
        case 'y': searchBook();
            break;
        case 'n': menu();
    }
}

void perAuthor(){
    catalog *ptr;
    char author[20],ans;

    system("cls");
    gotoxy(22,15);
    printf("Enter book title: ");
    scanf("%s",author);

    ptr=root;
    while(strcmp(ptr->author,author)!=0){
        ptr=ptr->left;
        if(ptr==NULL)
            menu();
    }

    system("cls");
        if(ptr!=NULL){
        gotoxy(22,10);
        printf("That book is in the catalog.");
        gotoxy(22,11);
        printf("Title: %s",ptr->title);
        gotoxy(22,12);
        printf("Author: %s",ptr->author);
        gotoxy(22,13);
        printf("ISBN: %d",ptr->isbn);
        gotoxy(22,14);
        printf("Genre: %s",ptr->genre);
        gotoxy(22,15);
        printf("Publisher: %s",ptr->publisher);
        gotoxy(22,16);
        printf("Year: %d",ptr->year);
        gotoxy(22,17);
        printf("Synopsis: %s",ptr->synopsis);
        } else {
            gotoxy(22,10);
            printf("No records to show...");
        }

    printf("Try another? (y/n) ");
    scanf("%c",&ans);

    switch(ans){
        case 'y': searchBook();
            break;
        case 'n': menu();
        }
}

void perISBN(){
    catalog *ptr;
    int isbn[20];
    char ans;

    system("cls");
    gotoxy(22,15);
    printf("Enter book ISBN: ");
    scanf("%s",isbn);

    ptr=root;
    while(ptr->isbn==isbn){
        ptr=ptr->left;
        if(ptr==NULL)
            menu();
    }

    system("cls");
        if(ptr!=NULL){
        gotoxy(22,10);
        printf("That book is in the catalog.");
        gotoxy(22,11);
        printf("Title: %s",ptr->title);
        gotoxy(22,12);
        printf("Author: %s",ptr->author);
        gotoxy(22,13);
        printf("ISBN: %d",ptr->isbn);
        gotoxy(22,14);
        printf("Genre: %s",ptr->genre);
        gotoxy(22,15);
        printf("Publisher: %s",ptr->publisher);
        gotoxy(22,16);
        printf("Year: %d",ptr->year);
        gotoxy(22,17);
        printf("Synopsis: %s",ptr->synopsis);
        } else {
            gotoxy(22,10);
            printf("No records to show...");
        }

    printf("Try another? (y/n) ");
    scanf("%c",&ans);

    switch(ans){
        case 'y': searchBook();
            break;
        case 'n': menu();
        }
}

void perGenre(){
    catalog *ptr;
    char genre[20],ans;

    system("cls");
    gotoxy(22,15);
    printf("Enter book title: ");
    scanf("%s",genre);

    ptr=root;
    while(strcmp(ptr->genre,genre)!=0){
        ptr=ptr->left;
        if(ptr==NULL)
            menu();
    }

    system("cls");
        if(ptr!=NULL){
        gotoxy(22,10);
        printf("That book is in the catalog.");
        gotoxy(22,11);
        printf("Title: %s",ptr->title);
        gotoxy(22,12);
        printf("Author: %s",ptr->author);
        gotoxy(22,13);
        printf("ISBN: %d",ptr->isbn);
        gotoxy(22,14);
        printf("Genre: %s",ptr->genre);
        gotoxy(22,15);
        printf("Publisher: %s",ptr->publisher);
        gotoxy(22,16);
        printf("Year: %d",ptr->year);
        gotoxy(22,17);
        printf("Synopsis: %s",ptr->synopsis);
        } else {
            gotoxy(22,10);
            printf("No records to show...");
        }

    printf("Try another? (y/n) ");
    scanf("%c",&ans);

    switch(ans){
        case 'y': searchBook();
            break;
        case 'n': menu();
        }
}

void displayCatalog(catalog *root){
    catalog *ptr;
    system("cls");

    ptr=root;
    while(ptr!=NULL){
        displayCatalog(root->left);
        printf("Title: %s",ptr->title);
        printf("Author: %s",ptr->author);
        printf("ISBN: %d",ptr->isbn);
        printf("Genre: %s",ptr->genre);
        printf("Publisher: %s",ptr->publisher);
        printf("Year: %d",ptr->year);
        printf("Synopsis: %d",ptr->synopsis);
        displayCatalog(root->right);
    }

    menu();
    }

void gotoxy(int x,int y){
    COORD coord;
    coord.X=x;
    coord.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

更新:所以我编辑了一个我的代码的BIT,以便它已经可以显示。但是,由于perTitle,perAuthor和perGenre函数不起作用,因此将用户输入的字符串和字符串与我的结构进行比较似乎有问题。 perISBN的工作完全正常。

UPDATE: So I edited A BIT of my code so that it can already display. However, there seems to be a problem with comparing the user input's string and the string from my struct because the perTitle, perAuthor, and perGenre functions won't work. The perISBN works perfectly fine.

以下是perTitle()现在的样子:

Here is how the perTitle() looks now:

void perTitle(){
catalog *ptr;
char title[20],ans;

system("cls");
gotoxy(22,15);
printf("Enter book title: ");
scanf("%s",title);fflush(stdin);

ptr=root;
while(strcmp(ptr->title,title)!=0){
    system("cls");
    if(ptr!=NULL){
    gotoxy(22,10);
    printf("That book is in the catalog.");
    gotoxy(22,11);
    printf("Title: %s",ptr->title);
    gotoxy(22,12);
    printf("Author: %s",ptr->author);
    gotoxy(22,13);
    printf("ISBN: %d",ptr->isbn);
    gotoxy(22,14);
    printf("Genre: %s",ptr->genre);
    gotoxy(22,15);
    printf("Publisher: %s",ptr->publisher);
    gotoxy(22,16);
    printf("Year: %d",ptr->year);
    gotoxy(22,17);
    printf("Synopsis: %s",ptr->synopsis);
    } else {
        gotoxy(22,10);
        printf("No records to show...");
    }
}

printf("Try another? (y/n) ");
scanf("%c",&ans);

switch(ans){
    case 'y': searchBook();
        break;
    case 'n': menu();
}

}

鉴于我已将书籍记录添加到列表中,它总是显示尝试另一个(y / n)?

Given that I have added a book record into the list, it always displays "Try another (y/n)?"

推荐答案

代码中有很多错误(这让我甚至想知道它编译)和编译器应该已经发出了很多警告,如果你愿意的话。

There are a lot of things wrong in the code (it makes me even wonder that it compiles at all) and your compiler should have uttered a lot of warnings if you would have let it.

我认为主要的罪魁祸首是在结构体中,特别是在 int 零件。 int isbn [15] 保留一个数组为15 int s,这不是你想要的。任何ISBN需要多于一个 int (假设每个 int 为4个字节,每个字节为8位),因此使用一个字符串一开始只需将 int 更改为 char 。另外: int year [5] 将为五个 int 创建一个数组,但我确定一个更多(再次:假设每个 int 为4个字节,每个字节为8位)。所以你的结构现在是

I think the main culprit is in the struct, especially in the int parts. int isbn[15] reserves an array of 15 ints and that is not what you want. Any ISBN needs more than one int (assuming 4 bytes per int and 8 bit per byte) so use a string for a start. Just change int to char. Also: int year[5] will make an array for five ints but I'm pretty sure one is more than sufficient (again: assuming 4 bytes per int and 8 bit per byte). So your struct is now

struct book{
    char title[20];
    char author[20];
    char isbn[14]; // 13 digits max. plus NUL
    char genre[10];
    char publisher[20];
    int year;
    char synopsis[100];
    catalog *left;
    catalog *right;
};

注意:应直接放置 typedef 在结构下

NB: the typedef should be placed directly under the struct

您还需要更改年份的方式:

You also need to change the way you put the year in:

scanf("%d",&ptr->year);

而在insert函数中的比较需要能够比较字符串,所以

And the comparing in the insert function needs to be able to compare strings now, so

void insert(catalog *root,catalog *x){
    if(strcmp(x->isbn, root->isbn) < 0){
        if(root->left==NULL){
            root->left=x;
        } else {
            insert(root->left,x);
        }
    }

    if( strcmp(x->isbn, root->isbn) > 0){
        if(root->right==NULL){
            root->right=x;
        } else {
            insert(root->right,x);
        }
    }
}

我无法测试它没有更大的重写(我没有Windows与编译器),所以请注意打字错误。而且您的代码中还有更多的错误,所以它可能仍然无法正常工作,尽管有不同的原因。

I'm not able to test it without a larger rewrite (I don't have Windows with a compiler) so beware of typos. And there are many more errors in your code so it may still not work, although with different reasons that time.

树遍历有点混乱了

void displayCatalog(catalog *root123){
    catalog *ptr;
    system("cls");
    ptr=root123;
    while(ptr!=NULL){
        if(root123->left != NULL){
           displayCatalog(root123->left);
        }
        printf("Title: %s\n",ptr->title);
        printf("Author: %s\n",ptr->author);
        printf("ISBN: %s\n",ptr->isbn);
        printf("Genre: %s\n",ptr->genre);
        printf("Publisher: %s\n",ptr->publisher);
        printf("Year: %d\n",ptr->year);
        printf("Synopsis: %s\n",ptr->synopsis);
        if(root123->right != NULL){
           displayCatalog(root123->right);
        }
        break;
      }

    menu();
    }

这篇关于任何人可以帮助我的图书馆目录C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆