• 图书管理系统2


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    #define books "f:\books.txt"
    #define booksbak "f:\booksbak.txt"
    struct bookinfo
    {
    char isbn[20];
    char title[30];
    char author[20];
    int count;
    };

    struct book
    {
    struct bookinfo onebook;
    struct book *next;
    };

    struct book *searchBook ( struct book *listptr,char isbn[])
    {
    while(listptr!=(struct book *)0)
    if (strcmp(listptr->onebook.isbn,isbn)==0)
    return listptr;
    else
    listptr=listptr->next;
    return (struct book *)0;
    }


    void MainSearchbook(struct book *firstptr)
    {
    struct book *ptr;
    char isbnno[20];
    printf("请输入ISBN:");
    scanf("%s",&isbnno);
    ptr=searchBook(firstptr,isbnno);
    if (ptr!=(struct book *)0)
    {
    printf("找到了!!! ");
    printf("ISBN:%s ",ptr->onebook.isbn);
    printf("Title:%s ",ptr->onebook.title);
    printf("Author:%s ",ptr->onebook.author);
    }
    else
    printf("sorry,not found!!! ");
    }



    int addBook(struct book *listptr,struct bookinfo note)
    {
    while(listptr->next!=0)
    listptr=listptr->next;
    listptr->next=(struct book *)malloc(sizeof(struct book));
    listptr->next->onebook=note;
    listptr->next->next=0;
    return 0;

    }

    void MainAdd(struct book *listptr,FILE *fp)
    {
    int ok;
    struct bookinfo note;
    printf("请输入ISBN:");
    scanf("%s",note.isbn);
    printf("请输入Title:");
    scanf("%s",note.title);
    printf("请输入Author:");
    scanf("%s",note.author);
    ok=addBook(listptr,note);
    if (ok==0)
    {
    //将加入的图书写到文件中保存
    fprintf(fp," %s %s %s %d",note.isbn,note.title,note.author,0);
    printf("添加图书成功!!! ");
    }
    else
    printf("添加图书失败!!! ");
    }


    int removeBook(struct book *listptr,char isbn[])
    {
    while(listptr->next!=(struct book *)0)
    {
    if (strcmp(listptr->next->onebook.isbn,isbn)==0)
    {
    listptr->next=listptr->next->next;
    return 0;
    }
    else
    listptr=listptr->next;
    }

    return -1;
    }

    void MainRemove(struct book *listptr,FILE *fp)
    /************************************************************************/
    /* 删除书籍函数,通过ISBN删除链表节点,同时删除文件中对应信息 */
    /* 删除文件中一行,用的是笨方法,把需要的信息写到新文件,删除旧文件,重命名..*/
    /************************************************************************/
    {
    char isbnno[20];
    int ok;
    struct bookinfo onebook;
    printf("请输入ISBN:");
    scanf("%s",&isbnno);
    ok=removeBook(listptr,isbnno);
    if (!ok)
    {
    FILE *fpbak;
    if ((fpbak=fopen(booksbak,"a+"))==NULL)
    printf("文件打开失败!!! ");
    fseek(fp,0,SEEK_SET); //移到文件开始
    while((fscanf(fp,"%s %s %s %d ",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
    {
    if (strcmp(onebook.isbn,isbnno)!=0)
    {
    fprintf(fpbak,"%s %s %s %d ",onebook.isbn,onebook.title,onebook.author,onebook.count);
    }
    }
    fclose(fp);
    fclose(fpbak);
    if (remove(books)) //删除失败返回非0
    {
    printf("删除文件失败!!! ");
    return ;
    }
    else
    if (rename(booksbak,books)) //重命名失败返回非0值
    {
    printf("重命名失败!!! ");
    return ;
    }

    printf("删除成功!!! ");
    }
    else
    printf("查无此书!!!");
    }

    int choice(void)
    {
    int c;
    printf("1.查看图书 ");
    printf("2.添加图书 ");
    printf("3.删除图书 ");
    printf("4.退出程序 ");
    printf("请选择序号:");
    return c=getchar();
    //return c=getche();
    printf(" ");

    }

    int addEntry(FILE *fp,struct book *firstptr)
    /************************************************************************/
    /* 主要用来加载文件中存放的图书信息 */
    /************************************************************************/

    {
    struct bookinfo onebook;
    while((fscanf(fp,"%s %s %s %d ",&onebook.isbn,&onebook.title,&onebook.author,&onebook.count))!=EOF)
    {
    while(firstptr->next!=0)
    firstptr=firstptr->next;
    firstptr->next=(struct book *)malloc(sizeof(struct book));
    firstptr->next->onebook=onebook;
    firstptr->next->next=0;
    }
    return 0;

    }


    int main(int argc,char *argv[])
    {
    int ch;
    struct book first;
    strcpy(first.onebook.isbn,"123456");
    strcpy(first.onebook.title,"Programming C");
    strcpy(first.onebook.author,"yhb");
    first.next=0;
    struct book *firstptr=&first; //链表头指针
    FILE *fp;
    if ((fp=fopen(books,"a+"))==NULL)
    printf("文件打开失败!!!");
    addEntry(fp,firstptr);
    while(1)
    {
    system("CLS"); //清屏
    /************************************************************************/
    /* 想想这里为什么要清空缓冲区? */
    /*由于上一次(choice函数)的getchar(),还有一个' '留在缓冲区.... */
    /*可以把这句话注释掉看看,没有这句话会遇到麻烦 */
    /*如果不用fflush,可以将上面的getchar()换成getche() */
    /*比较getchar(),getch(),getche()...... */
    /************************************************************************/
    fflush(stdin);
    ch=choice()-48;
    switch (ch)
    {
    case 1:
    MainSearchbook(firstptr);
    break;
    case 2:
    MainAdd(firstptr,fp);
    break;
    case 3:
    MainRemove(firstptr,fp);
    break;
    case 4:
    printf("谢谢使用... ");
    exit(0);
    default:
    printf("请输入正确序号!");
    }
    system("PAUSE");
    }
    return 0;
    }

  • 相关阅读:
    程序11
    程序9
    程序10
    程序8
    提示框
    程序6
    莫名其妙的“网线未连接”
    UI自动化测试笔记(2)
    UI自动化测试笔记(1)
    三年开发项目经验总结
  • 原文地址:https://www.cnblogs.com/wlp1115/p/6576589.html
Copyright © 2020-2023  润新知