温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用C语言实现图书管理系统

发布时间:2022-07-22 11:43:34 来源:亿速云 阅读:133 作者:iii 栏目:开发技术

这篇文章主要介绍“怎么用C语言实现图书管理系统”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用C语言实现图书管理系统”文章能帮助大家解决问题。

设计要求

设计图书管理系统

要求如下:

1.对书名,作者,编号,出版单位,出版日期,类别,存放位置进行添加、删除、修改
2.查询相关信息
3.保存所有信息

实现代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
typedef struct book                                             /*定义结构体*/
{
    char bookname[100] ;        /*书名*/
    char writer[100];           /*作者*/
    char classnumber[100];      /*编号*/
    char publisher[100];        /*出版单位*/

    char time[100];             /*出版日期*/
    char  type[100];                /*类别*/ 
    char   place[100];              /*存放位置*/
      struct  book *next;
}Book;

int mima()
{
    int i=0;
    char user[10]="aa";
    char Code[10]="123456";
    char getuser[10];
    char Getskey[10];

    while(1)
    {
        printf("\t\t\t请输入账号:");
        scanf("%s",getuser);
        printf("\t\t\t请输入密码:");
        while(1)
        {
            Getskey[i]=getch();
            if(Getskey[i]=='\r')
                break;
            else if(Getskey[i]=='\b')
            {
                if(i==0)
                    continue;
                printf("\b");
                printf(" ");
                printf("\b");
                i--;
            }
            else
            {
                printf("*");
                i++;
            }
        }
        Getskey[6]='\0';
        if(strcmp(user, getuser)==0&&strcmp(Code, Getskey)==0)
            return 1;
        else
        {
            printf("\n\n\t\t\t账号不存在或密码输入错误,请重新输入\n");
            i = 0;
        }
    }
}

void showmenu1()                                                /*显示管理员菜单*/ 
{
    system("cls");
    printf("\n\n\n\n\n");

    printf("\t                                                          \n");
    printf("\t                 欢迎来到图书信息管理系统                 \n");
    printf("\t                                                          \n");
    printf("\t                                                          \n");
    printf("\t                          主菜单                          \n");
    printf("\t                                                          \n");
    printf("\t                     1.录入图书信息                       \n");
    printf("\t                                                          \n");
    printf("\t                     2.删除图书信息                       \n");          
    printf("\t                                                          \n");
    printf("\t                     3.修改图书信息                       \n");
    printf("\t                                                          \n");
    printf("\t                     4.查询图书信息                       \n");
    printf("\t                                                          \n");
    printf("\t                     5.显示所有图书信息                   \n");        
    printf("\t                                                          \n");
    printf("\t                     6.返回登录界面                       \n");
    printf("\t                                                          \n");
    printf("\t                     7.保存数据并退出系统                 \n"); 
    printf("\t                                                          \n");
}

void showmenu2()                                                /*显示游客菜单*/ 
{
    system("cls");
    printf("\n\n\n\n\n");
       printf("\t                                                          \n");
       printf("\t                 欢迎来到图书信息管理系统                 \n");
       printf("\t                                                          \n");
       printf("\t                                                          \n");
    printf("\t                          主菜单                          \n");
    printf("\t                                                          \n");
    printf("\t                     1.查询图书信息                       \n");
    printf("\t                                                          \n");
    printf("\t                     2.显示所有图书信息                   \n");        
       printf("\t                                                          \n");
       printf("\t                     3.返回登录界面                       \n");
       printf("\t                                                          \n");
    printf("\t                     4.退出系统                           \n"); 
       printf("\t                                                          \n");
}

int savebookdata(Book *head)                                    /*将数据保存到文件中*/
{
    Book *p;
    FILE*fp;
    fp=fopen("D:\\java\\zuoye.txt","w");
    p=head->next;
    do
    {
        fprintf(fp,"%s\t",p->bookname);         /* 把这个结点的数据全部写入至文件内由于next指针不需要写入至文件,*/
        fprintf(fp,"%s\t",p->writer);          /*因此,数据大小就是sizeof(book)-sizeof(book*)总数据大小减去指针的大小 */
        fprintf(fp,"%s\t",p->classnumber);
        fprintf(fp,"%s\t",p->publisher);
        fprintf(fp,"%s\t",p->time);
        fprintf(fp,"%s\t",p->type);
        fprintf(fp,"%s\t\n",p->place);
        fprintf(fp,"\n");
        p=p->next;                 /*转换到下一个结点*/
    }while(p!=NULL);
    fclose(fp);
    return 0;
}

void addnewbook(Book **head)                                    /*录入图书信息*/
{
    int i;
    Book *p,*s;
    system("cls");
    if(*head == NULL) {                 /* 如果表头为空,先为它分配内存 */
        *head = (Book *)calloc(1, sizeof(Book));
        p = *head;
    } else {                            /* 否则,就说明有内容,直接切换到尾结点 */
        p = *head;
        while(p->next) 
        p = p->next; 
    }
    printf("\n\n\n");
    printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
    printf("\t\t\t请问添加几本书:"); 
    scanf("%d",&i);
    printf("\t--------------------------------------------------------\n");
    while(i)
    {
        s = (Book *)calloc(1, sizeof(Book));                        /*为这个结点分配内存 */
        printf("\t\t\t请输入书名:");
        scanf("%s",s->bookname);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入作者:");
        scanf("%s",s->writer);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入编号:");
        scanf("%s",s->classnumber);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入出版单位:");
        scanf("%s",s->publisher);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入出版日期:");
        scanf("%s",s->time);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入类别:");
        scanf("%s",s->type);
        printf("\t--------------------------------------------------------\n");
        printf("\t\t\t请输入存放位置:");
        scanf("%s",s->place);
        printf("\t--------------------------------------------------------\n");

        printf("\n");
        printf("\t--------------------------------------------------------\n");
        i--;
            
        p->next=s;    /*保存输进去的这个结点*/
        p=p->next;    /*切换到下一个结点*/ 
        p->next=NULL; /*最后一个结点为NULL*/
    }
            
    printf("\t\t\t图书添加成功!!!\n");
    system("pause");
}

int readbookdata(Book **head)                                   /*将数据从文件中读取*/ 
{
    int n;
    Book *s, *p;
    FILE *fp;
    
    fp=fopen("d:\\java\\zuoye.txt","r");
    /* 如果文件打开失败,就返回-1 */
    if(NULL == fp) { 
        *head = NULL; 
        return -1;
    } 
     
    *head = (Book *)calloc(1, sizeof(Book));
    p = *head; 
     
      while (!feof(fp))
    {
        s = (Book *)calloc(1, sizeof(Book));                    /* 为下个结点分配内存空间 */
        fscanf(fp,"%s",s->bookname);
        fscanf(fp,"%s",s->writer);
        fscanf(fp,"%s",s->classnumber);
        fscanf(fp,"%s",s->publisher);
        fscanf(fp,"%s",s->time);
        fscanf(fp,"%s",p->type);
        fscanf(fp,"%s\n",p->place);
        p->next=s;   /*保存该结点*/
        p=p->next;   /*切换到下一个结点*/
        p->next=NULL; /*保证最后一个结点为NULL*/
    }
     
    fclose(fp);/* 关闭文件 */
    return 0; 
}

void deletebook(Book *head)                                     /*删除图书信息*/
{
    system("cls");
    int flag=0;
    char name[100],ch[10];
    Book *p,*t;
    
    /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!\n");
        system("pause");
        return;
    }
    
    p=head;
    printf("\n\n\n");
    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
    printf("请输入要删除的书名;");
    scanf("%s",&name);
    printf("----------------------------------------------------------------------\n");
    printf("书名\t\t作者名\t\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
    printf("----------------------------------------------------------------------\n");                                                                                                                                    
    while(p->next)
    {
        t=p;
        p=p->next;                                               /*转换到下一个结点*/
        if(strcmp(name,p->bookname)==0)
        {
            flag=1;
            printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
            break;
        }
    }
     
    if(flag==1)
    {
        printf("是否删除该本图书信息(y/n)");
        scanf("%s",ch);
        printf("----------------------------------------------------------------------\n");
        if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
        {
            t->next=p->next;
            printf("该书已删除!\n");
        }
        else
            return;
    }
    else if(flag==0)
        printf("没找到那本书!\n");
    
    system("pause");
}

void modifybook(Book *head)                                     /*修改图书信息*/
{
    system("cls");
    int flag=0;
    char name[100],ch[10];
    Book *p,*t;
    
    /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!\n");
        system("pause");
        return ;
    }
    
    p=head;
    
    printf("\n\n\n");
    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
    printf("请输入要修改的书名:");
    scanf("%s",name);
    printf("--------------------------------------------------------------------\n");

    while(p->next)
    {
        p=p->next;                 /*切换到下一个结点*/
        if(strcmp(name,p->bookname)==0)
        {
            flag=1;
            t=p;
            printf("书名\t\t作者名\t\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
            printf("--------------------------------------------------------------------\n");
            printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
        }
    }
    
    if(flag==1)
    {
        printf("--------------------------------------------------------------------\n");
        printf("\t\t是否修改该本图书信息(y/n):");
        scanf("%s",ch);
        printf("\t--------------------------------------------------------------------\n");
        if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)
        {
            int c;
            printf("\t\t【1】书名\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【2】作者\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【3】编号\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【4】出版单位\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【5】出版日期\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【6】类别\n");
            printf("--------------------------------------------------------------------\n");
            printf("\t\t【7】存放位置\n");
            printf("--------------------------------------------------------------------\n");

            printf("\t请输入你要修改的内容;");
            scanf("%d",&c);
            printf("--------------------------------------------------------------------\n");
            p=t;
            switch(c)
            {
                case 1:
                    printf("\t请输入书名:");
                    scanf("%s",p->bookname);
                    break;
                case 2:
                    printf("\t请输入作者:");
                    scanf("%s",p->writer);
                    break;
                case 3:
                    printf("\t请输入编号:");
                    scanf("%s",p->classnumber);
                    break;
                case 4:
                    printf("\t请输入出版单位:");
                    scanf("%s",p->publisher);
                    break;
                case 5:
                    printf("\t请输入出版日期:");
                    scanf("%s",p->time);
                    break;
                case 6:
                    printf("\t请输入类别:");
                    scanf("%s",p->type);
                    break;
                     case 7:
                    printf("\t请输入存放位置:");
                    scanf("%s",p->place);
                    break;
            }
            printf("--------------------------------------------------------------------\n");
            printf("\t恭喜大佬修改成功!!!\n");
        }
    }
    
    else if(flag==0)
        printf("对不起,该书库没有该书\n");
        system("pause");
}

void searchbook(Book *head)                                     /*查询图书信息*/
{
    system("cls");
    int choose,flag=0;
    Book *p;
    char b[100];
    
    /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!\n");
        system("pause");
        return ;
    }
    
    do
    {
        system("cls");
        printf("\n\n\n");
        printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
        printf("\t\t1.按书名查找\n");
        printf("----------------------------------------------------------------------\n");
        printf("\t\t2.按作者名查找\n");
        printf("----------------------------------------------------------------------\n");
        printf("\t\t3.按编号查找\n");
        printf("----------------------------------------------------------------------\n");
        printf("\t\t4.按出版单位查找\n");
        printf("----------------------------------------------------------------------\n");
        printf("\t\t返回主菜单(其他数字)\n");
        printf("----------------------------------------------------------------------\n");
        printf("\t\t请输入您的选择:");
        scanf("%d",&choose);
        printf("----------------------------------------------------------------------\n");
        p=head;
        
        if(choose==1)
        {
            printf("输入所查书名:");
            scanf("%s",b);
            printf("----------------------------------------------------------------------\n\n");
            while(p->next)
            {
                p=p->next;                           /*转换到下一个结点*/
                if(strcmp(b,p->bookname)==0)         /*判断是否找到并输出*/
                {
                     flag=1;
                     printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
                     printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
                         printf("----------------------------------------------------------------------\n");
                     printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
                }
            }
            if(flag==0)
                     printf("不存在该信息\n");
        }
        
        else if(choose==2)
        {
            printf("输入所查作者名:");
            scanf("%s",b);
            printf("----------------------------------------------------------------------\n\n");
            while(p->next)
            {
                p=p->next;                         /*转换到下一个结点*/
                if(strcmp(b,p->writer)==0)         /*判断是否找到并输出*/
                {
                    flag=1;
                    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
                    printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
                    printf("----------------------------------------------------------------------\n");
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
                }
            }
            if(flag==0)
                    printf("不存在该信息\n");
        }
        else if(choose==3)
        {
            printf("输入所查编号:");
            scanf("%s",b);
            printf("----------------------------------------------------------------------\n\n");
            while(p->next)
            {
                p=p->next;                         /*转换到下一个结点*/
                if(strcmp(b,p->classnumber)==0)         /*判断是否找到并输出*/
                {
                    flag=1;
                    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
                    printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
                    printf("----------------------------------------------------------------------\n");
                    printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
                }
            }
            if(flag==0)
                    printf("不存在该信息\n");
        }
        else if(choose==4)
        {
            printf("输入所查出版单位:");
            scanf("%s",b);
            printf("----------------------------------------------------------------------\n\n");
            while(p->next)
            {
                p=p->next;                         /*转换到下一个结点*/
                if(strcmp(b,p->publisher)==0)         /*判断是否找到并输出*/
                {
                    flag=1;
                    printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
                    printf("书名\t作者名\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
                    printf("----------------------------------------------------------------------\n");
                    printf("%s\t%s\t%s\t%s\t%s%s\t%s\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
                }
            }
            if(flag==0)
                    printf("不存在该信息\n");
        }        
        else
            return;
        system("pause");
    }while(1);
}

int printfbook(Book *head)                                     /*显示所有图书信息*/          
{
    Book *p;
    system("cls");
    
    /* 如果表头为空 */
    if(head == NULL || head->next == NULL) {
        printf("没有记录图书信息!\n");
        system("pause");
        return 0;
    }
    
    p=head->next;
    printf("\n\n\n");
    printf("**********************************************************************************\n");
    printf("                                       图书列表                                   \n");
    printf("----------------------------------------------------------------------------------\n");
    printf("书名\t作者\t编号\t出版单位\t出版时间\t类别\t存放位置\n");   
    printf("----------------------------------------------------------------------------------\n");
    
    do
    {
        printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",p->bookname,p->writer,p->classnumber,p->publisher,p->time,p->type,p->place);
        p=p->next;                           /*转换到下一个结点*/
    }while(p!=NULL);
    
    system("pause");
}

int main(void)
{
    system("cls");
    Book *head=NULL;
    readbookdata(&head);
    int choice,z,c;
    printf("\n\n\n");
    printf("\t※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");
    printf("\t\t\t欢迎光临图书信息管理系统\n");
    printf("\t--------------------------------------------------------\n");
    printf("\t\t\t【1】游客\n");
    printf("\t\t\t【2】管理员\n");
    printf("\t\t\t【3】退出系统\n");
    printf("\t--------------------------------------------------------\n");
    while(printf("\t\t\t请选择身份:")&&scanf("%d",&c)!=EOF)
    {
        if(c==1)
        {
            showmenu2();
            while (printf("\t请输入选择项(1-3):")&&scanf("%d",&choice)!=EOF)
            {
                switch (choice)
                {
                    case 1:                                                /*查询图书信息*/
                        searchbook(head);
                        break;
                    case 2:                                                /*显示所有图书信息*/
                        printfbook(head);
                        break; 
                    case 3:                                                /*返回登录界面*/
                        main();
                        break;
                    case 4:
                        exit(0); 
                    default:
                        printf("\t输入错误!!!\n");
                        system("pause");
                        break;
                }
                showmenu2();
            }
        }
        else if(c==2)
        {
            z=mima();
            showmenu1();
            while (printf("\t请输入选择项(1-6):")&&z==1&&scanf("%d",&choice)!=EOF)
            {
                switch (choice)
                {
                    case 1:                                                /*录入图书信息*/
                        addnewbook(&head);
                        break;  
                    case 2:                                                /*删除图书信息*/
                        deletebook(head);
                        break;
                    case 3:                                                /*修改图书信息*/
                        modifybook(head);
                            break;
                    case 4:                                                /*查询图书信息*/
                        searchbook(head);
                        break;
                    case 5:                                                /*显示所有图书信息*/
                        printfbook(head);
                        break;
                    case 6:                                                /*返回登录界面*/
                        main();
                        break; 
                    case 7:                                                /*保存数据并退出系统*/
                        savebookdata(head);
                        exit(0);
                    default:
                        printf("\t输入错误!!!\n");
                        system("pause");
                        break;
                }
                showmenu1();
            }
        }
        else if(c==3)
            exit(0);
        else
        {
            printf("\t\t\t输入错误!!!\n\n");
        }
    }
    return 0;
}

运行结果

这里提供的是管理员身份

怎么用C语言实现图书管理系统

怎么用C语言实现图书管理系统

这里提供游客身份(供选择的功能相对管理员少)

怎么用C语言实现图书管理系统

怎么用C语言实现图书管理系统

关于“怎么用C语言实现图书管理系统”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI