c语言课程设计(图书馆借阅系统)

话不多说直接上代码。

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<string.h>
#define N 1000
#define LEN sizeof(book)

typedef struct book
{
    int num;                            //编号
    char name[31];                        //书名
    char author_name[27];                //作者名
    char chubanshe[20];                    //出版社
    int jieyucishu;                        //借阅次数
}book;

struct book temp;
struct book temp2;
int  tushuzongshu;

int luru();
void chaxun();
int shanchu();
void xiugai();
void chaxuncishu();
void tongji();

void main()
{    
    int n;
    Sleep(300);
    loop1:
    tongji();    
    printf("\n\n\t\t********************************图书管理系统**************************************");
    printf("\n\n\n\t\t\t*********欢迎使用图书馆管理系统,请选择你需要的操作**********");
    printf("\n\n\n\n当前图书馆藏书一共有%d本。",tushuzongshu);
    printf("\n\n\n\t\t\t\t\t\t1.图书录入系统");
    printf("\n\n\n\t\t\t\t\t\t2.图书查询系统");
    printf("\n\n\n\t\t\t\t\t\t3.图书删除系统");
    printf("\n\n\n\t\t\t\t\t\t4.图书修改系统");
    printf("\n\n\n\t\t\t\t\t\t5.图书借阅次数最值系统");
    printf("\n\n\n\t\t\t\t\t\t6.退出图书管理系统");
    printf("\n\n\n\t\t\t\t\t\t请选择您所需要的操作:    ");
    scanf("%d",&n);
    if(n>=1&&n<=5){
        system("ClS");
    }
    else if(n==6){
        system("ClS");
        printf("再次按下回车键退出,谢谢使用!");
    }
    else{
        system("CLS");
        goto loop1;
    }
    switch(n)
    {
        case 1:luru();goto loop1;
        case 2:chaxun();goto loop1;
        case 3:shanchu();goto loop1;
        case 4:xiugai();goto loop1;
        case 5:chaxuncishu();goto loop1;
    }
}

int luru()
{    
    FILE *fp=NULL;
    book stu;
    char flag = 'y';
    fp=fopen("book1.dat","ab+");
    
    while((flag == 'y'||flag == 'Y')){
        printf("\n\n\n\n\t\t\t\t****************这里是图书录入系统*******************");
        printf("\n请输入图书id: ");
        scanf("%d",&stu.num);
        printf("\n请输入图书书名: ");
        scanf("%s",stu.name);
        printf("\n请输入图书作者: ");
        scanf("%s",stu.author_name);
        printf("\n请输入图书出版社: ");
        scanf("%s",stu.chubanshe);
        stu.jieyucishu=0;
        
        fwrite(&stu,LEN,1,fp);
        fflush(stdin);
        
        printf("\n\n\n是否继续输入?继续请输入y或Y:");
        scanf("%c",&flag);
    }
    system("CLS");
    fclose(fp);
    return;
    
}

void chaxun()
{
    FILE *fp=NULL;
    book stu;
    int n,i,j,t=-1;
    int nums;
    char name[N],authors_name[40],chubanshe1[40];
    loop3:
    fp = fopen("book1.dat","rb+");        
    printf("\n\n\n\n\t\t\t\t****************这里是图书查询系统*******************");
    printf("\n\t\t\t\t\t请选择你所查询的方式:");
    printf("\n\n\t\t\t\t\t1.编号查询;");
    printf("\n\n\t\t\t\t\t2.书名查询;");
    printf("\n\n\t\t\t\t\t3.作者查询;");
    printf("\n\n\t\t\t\t\t4.出版社查询;");
    printf("\n\n\t\t\t\t\t5.返回主菜单;");
    printf("请输入您的选择:");
    scanf("%d",&n);
    system("CLS");
    if(n==1){
        printf("\n\t\t\t\t\t\t请您输入图书编号:");
        scanf("%d",&nums);
        fseek(fp,0,SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(nums==stu.num)
            {
                t=1;
                printf("\n\t\t\t\t\t此书为%s,借阅数为%d,是否选择借阅?1.是   2.否",stu.name,stu.jieyucishu);
                printf("\n请输入你的选择:");
                scanf("%d",&j);
                if(j==1){
                    stu.jieyucishu++;
                    fseek(fp,-4,SEEK_CUR);
                    fwrite(&stu.jieyucishu,sizeof(int),1,fp);
                    printf("\n借阅成功!请从管理员处领取书籍。3秒钟后返回主菜单");
                    t=-1;
                    fclose(fp);
                    Sleep(3000);
                    system("CLS");
                    goto loop3;
                }
                else{
                    printf("\n3秒钟后返回主菜单");
                    t=-1;
                    fclose(fp);
                    Sleep(3000);
                    system("CLS");
                    goto loop3;
                }
            }
        }
        fclose(fp);
        if(t==-1){
            printf("未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop3;
        }
    }

    else if(n==2){
        printf("\n\t\t\t\t\t请输入书名:");
        scanf("%s",&name);
        fseek(fp,0,SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(name,stu.name) == 0)
            {
                t=1;
                printf("\n\t\t\t\t\t此书为%s,借阅数为%d,是否选择借阅?1.是   2.否",stu.name,stu.jieyucishu);
                printf("\n请输入你的选择:");
                scanf("%d",&j);
                if(j==1){
                    stu.jieyucishu++;
                    fseek(fp,-4,SEEK_CUR);
                    fwrite(&stu.jieyucishu,sizeof(int),1,fp);
                    printf("\n借阅成功!请从管理员处领取书籍。3秒钟后返回主菜单");
                    t=-1;
                    fclose(fp);
                    Sleep(3000);
                    system("CLS");
                    goto loop3;
                }
                else{
                    printf("\n3秒钟后返回主菜单");
                    t=-1;
                    fclose(fp);
                    Sleep(3000);
                    system("CLS");
                    goto loop3;
                }                
            }
        }
        fclose(fp);
        if(t==-1){
            printf("未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop3;
        }
    
    }

    else if(n==3){
        printf("\n\t\t\t\t\t请输入书作者:");
        scanf("%s",&authors_name);
        fseek(fp,0,SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(authors_name,stu.author_name) == 0){
                printf("\n\t\t\t\t\t作者的图书有%s\n",stu.name);
            }
            t=1;
        }
        fclose(fp);
        if(t==-1){
            printf("\n\t\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop3;
        }
        else if(t!=-1){
            printf("\n\t\t\t\t\t是否返回主菜单?1.是  2.否");
            t=-1;
            scanf("%d",&j);
            if(j==1){
                printf("\n\t\t\t\t\t3秒钟后返回主菜单");
                Sleep(3000);
                system("CLS");
            }
            else{
                printf("\n\t\t\t\t\t3秒钟后返回主菜单");
                Sleep(3000);
                system("CLS");
            }
        }
    }
    else if(n==4){
        printf("\n\t\t\t\t\t请输入书出版社:");
        scanf("%s",&chubanshe1);
        fseek(fp,0,SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(chubanshe1,stu.chubanshe) == 0){
                printf("\n\t\t\t\t\t出版社的图书有%s\n",stu.name);
            }
            t=1;
        }        
        fclose(fp);
        if(t==-1){
            printf("\t\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            goto loop3;
        }
        else if(t!=-1){
            printf("\n\t\t\t\t\t是否返回主菜单?1.是  2.否");
            scanf("%d",&j);
            if(j==1){
                printf("\n\t\t\t\t\t3秒钟后返回主菜单");
                Sleep(3000);
                system("CLS");
            }
            else{
                printf("\n\t\t\t\t\t3秒钟后返回主菜单");
                Sleep(3000);
                system("CLS");
            }
        }
    }
    else if(n==5){
        printf("\n\t\t\t\t\t3秒钟后返回主菜单");
        Sleep(3000);
        system("CLS");
    }
}

int shanchu()
{
    int n,i,j,t=-1;
    int nums;
    book stu;
    FILE *fp;
    
    char name[20],authors_name[40],chubanshe1[40];
    loop2:
    fp = fopen ("book1.dat","rb+");
    printf("\n\n\n\n\t\t\t\t****************这里是图书删除系统*******************");
    printf("\n\t\t\t\t请输入您想要删除的书的数据:");
    printf("\n\t\t\t\t1.书籍编号。");
    printf("\n\t\t\t\t2.书籍书名。");
    printf("\n\t\t\t\t3.书籍作者。");
    printf("\n\t\t\t\t4.书籍出版社。");
    printf("\n\t\t\t\t5.返回主菜单。");
    printf("\n\t\t\t\t请输入您的选择:");
    scanf("%d",&n);
    system("CLS");
    if(n==1){
        printf("\n\t\t\t\t请输入书籍编号:");
        scanf("%d",&nums);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(nums==stu.num){
                t=1;
                printf("\n\t\t\t\t\t此书为%s\n",stu.name);
                printf("\n\t\t\t\t是否确定删除图书%s。1.是   2.否",stu.name);
                scanf("%d",&j);
                if(j==1){
                    printf("\n\t\t\t\t图书删除成功");
                    printf("\n\t\t\t\t3秒后返回之前界面。");
                    fseek(fp,0,SEEK_SET);
                    FILE *fp1;
                    fp1 = fopen("linshi.dat","ab+");
                
                    while(fread(&stu,LEN,1,fp)){
                        if(nums!=stu.num){
                            fwrite(&stu,LEN,1,fp1);
                        }
                    }
                    fclose(fp);
                    fclose(fp1);
                    while(remove("book1.dat")!=0);
                    rename("linshi.dat","book1.dat");                    
                    fflush(stdin);
                    Sleep(3000);
                    system("CLS");
                    return;                                    
                }
                else{
                    printf("\n\t\t\t\t3秒后返回之前界面。");
                }
                Sleep(3000);
                system("CLS");
                
                goto loop2;                 
            }
        }
        if(t==-1){
            printf("\n\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
    }
    else if(n==2){
        printf("\n\t\t\t\t请输入书籍名称:");
        scanf("%s",&name);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(name,stu.name) == 0){
                t=1;
                printf("\n\t\t\t\t\t此书为%s\n",stu.name);
                printf("\n\t\t\t\t是否确定删除图书%s。1.是   2.否",stu.name);
                scanf("%d",&j);
                if(j==1){
                    printf("\n\t\t\t\t图书删除成功");
                    printf("\n\t\t\t\t3秒后返回主界面。");
                    fseek(fp,0,SEEK_SET);
                    FILE *fp1;
                    fp1 = fopen("linshi.dat","ab+");
                
                    while(fread(&stu,LEN,1,fp)){
                        if(strcmp(name,stu.name) != 0){
                            fwrite(&stu,LEN,1,fp1);
                        }
                    }
                    fclose(fp);
                    fclose(fp1);
                    while(remove("book1.dat")!=0);
                    rename("linshi.dat","book1.dat");
                
                    fflush(stdin);
                    Sleep(3000);
                    system("CLS");
                    return;
                }
                else{
                    printf("\n\t\t\t\t3秒后返回上一界面。");
                    Sleep(3000);
                    system("CLS");
                    goto loop2;
                }        
            }

        }
        if(t==-1){
            printf("\n\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
    }
    else if(n==3){
        printf("\n\t\t\t\t请输入书籍作者:");
        scanf("%s",&authors_name);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(authors_name,stu.author_name) == 0){
                printf("\n\t\t\t\t\t此书为%s\n",stu.name);
            }
            t=1;
        }
        fclose(fp);        
        if(t==-1){
            printf("\n\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
        else if(t!=-1){
            printf("\n\t\t\t\t请确认好需要删除图书的书名,返回上次界面从新选择删除方式。\n\t\t\t\t3秒后返回之前界面。");    
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
    }
    else if(n==4){
        printf("\n\t\t\t\t请输入书籍出版社:");
        scanf("%s",&chubanshe1);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(chubanshe1,stu.chubanshe) == 0){
                printf("\n\t\t\t\t\t此书为%s\n",stu.name);
            }
            t=1;
        }
        fclose(fp);    
        if(t==-1){
            printf("\n\t\t\t\t未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
        else if(t!=-1){
            printf("\n\t\t\t\t请确认好需要删除图书的书名,返回上次界面从新选择删除方式。\n\t\t\t\t3秒后返回之前界面。");    
            Sleep(3000);
            system("CLS");
            goto loop2;
        }
    }
    else if(n==5){
        printf("\n\t\t\t\t3秒钟后返回主菜单");
        Sleep(3000);
        system("CLS");    
    }
}
 
void xiugai(){
    book stu;
    FILE *fp;
    int n,m,t=-1;
    int nums;
    char name[N],authors_name[40],chubanshe1[40];
    loop4:
    fp = fopen("book1.dat","rb+");
    printf("\n\n\n\n\t\t\t\t****************这里是图书修改系统*******************");
    printf("\n\t\t\t\t\t请选择你所需要修改图书的信息:");
    printf("\n\n\t\t\t\t\t1.编号;");
    printf("\n\n\t\t\t\t\t2.书名;");
    printf("\n\n\t\t\t\t\t3.作者;");
    printf("\n\n\t\t\t\t\t4.出版社;");
    printf("\n\n\t\t\t\t\t5.返回主菜单;");
    printf("\n\n\t\t\t\t\t请输入您的选择:");    
    scanf("%d",&n);
    system("CLS");
    if(n==1){
        printf("\n\n\t\t\t\t\t请输入修改图书的编号:");
        scanf("%d",&nums);
        fseek(fp, 0, SEEK_SET);
        while (fread(&stu, LEN, 1, fp))
        {

            if (nums==stu.num)
            {
                printf("\n\n\t\t\t\t\t请重新输入图书编号:   ");
                scanf("%d", &stu.num);
    
                printf("\n\n\t\t\t\t\t请重新输入书名:    ");
                scanf("%s", &stu.name);

                printf("\n\n\t\t\t\t\t请重新输入书籍作者: ");
                scanf("%s", &stu.author_name);

                printf("\n\n\t\t\t\t\t请重新输入图书出版社: ");
                scanf("%s", &stu.chubanshe);

                fflush(stdin);
                fseek(fp, 0-LEN, SEEK_CUR);
                fwrite(&stu, LEN, 1, fp);
                fclose(fp);
            }
        }
        printf("\n\t\t\t\t图书修改完成,3秒钟后返回主菜单");
        Sleep(3000);
        system("CLS");
    }
    if(n==2){
        printf("\n\n\t\t\t\t\t请输入修改图书的书名:");
        scanf("%s",&name);
        fseek(fp, 0, SEEK_SET);
        
        while (fread(&stu, LEN, 1, fp))
        {

            if (strcmp(name,stu.name) == 0)
            {
                printf("请重新输入图书id:   ");
                scanf("%d", &stu.num);

                printf("请重新输入书名:    ");
                scanf("%s", &stu.name);

                printf("请重新输入书籍作者  : ");
                scanf("%s", &stu.author_name);
    
                printf("请重新输入图书出版社  : ");
                scanf("%s", &stu.chubanshe);

                fflush(stdin);
                fseek(fp,0-LEN, SEEK_CUR);
                fwrite(&stu, LEN, 1, fp);
                fclose(fp);
            }
        }
        printf("\n\t\t\t\t图书修改完成,3秒钟后返回主菜单");
        Sleep(3000);
        system("CLS");        
    }
    if(n==3){
        printf("\n\t\t\t\t请输入作者名称:");
        scanf("%s",&authors_name);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(authors_name,stu.author_name) == 0){
                printf("\n\t\t\t\t\t此出版社的图书为%s\n",stu.name);
            }
            t=1;
        }
        fclose(fp);
        if(t==-1){
            printf("未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop4;
        }
        else if(t!=-1){
            printf("\n3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop4;
            }                            
        }
    
    if(n==4){
        printf("\n\t\t\t\t请输入出版社名称:");
        scanf("%s",&chubanshe1);
        fseek(fp, 0, SEEK_SET);
        while(fread(&stu,LEN,1,fp)){
            if(strcmp(chubanshe1,stu.chubanshe) == 0){
                printf("\n\t\t\t\t\t此出版社的图书为%s\n",stu.name);
            }
            t=1;
        }
        fclose(fp);
        if(t==-1){
            printf("未找到您所需要的书籍,3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop4;
        }
        else if(t!=-1){
            printf("\n3秒后返回上一界面。");
            Sleep(3000);
            system("CLS");
            goto loop4;
            }
        }
    if(n==5){
        printf("\n3秒后返回主菜单。");
        Sleep(3000);
        system("CLS");
    }
}

void chaxuncishu()
{
    FILE *fp;
    book stu;
    int min=10000,max=0,m;
    loop7:
    fp = fopen("book1.dat","rb+");
    fseek(fp, 0, SEEK_SET);
    while (fread(&stu,LEN,1,fp)){
        if(stu.jieyucishu>=max){
            max=stu.jieyucishu;
            strcpy(temp.name,stu.name);    
        }
        if(stu.jieyucishu<=min&&stu.jieyucishu>=0){
            min=stu.jieyucishu;
            strcpy(temp2.name,stu.name);
        }
    }
    fclose(fp);
    printf("\n\n\t\t******************这里是图书借阅次数最值界面*********************");
    printf("\n\n\t\t******************图书借阅次数最多的图书为:%s,借阅次数为:%d********************",temp.name,max);
    printf("\n\n\t\t******************图书借阅次数最少的图书为:%s,借阅次数为:%d********************",temp2.name,min);
    printf("\n\n\t\t******************是否返回主菜单?1.是 2.否********************");
    scanf("%d",&m);
    if(m==1){
        printf("\n\n\t\t******************3秒后返回主菜单。***********************");
        Sleep(3000);
        system("CLS");
    }
    else if(m==2){
        system("CLS");
        goto loop7;
    }            
}

void tongji(){
    FILE *fp;
    book stu;
    tushuzongshu=0;
    fp = fopen("book1.dat","rb+");
    fseek(fp, 0, SEEK_SET);
    while(fread(&stu,LEN,1,fp)){
        tushuzongshu++;
    }
    fclose(fp);    
}

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
根据提供的引用内容,我无法直接提供C语言简单图书借阅管理系统的代码。但是,我可以为您提供一个简单的示例,以帮助您理解如何设计和实现这样一个系统。 下面是一个简单的C语言图书借阅管理系统的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_BOOKS 100 typedef struct { char title[100]; char author[100]; int year; int available; } Book; Book library[MAX_BOOKS]; int numBooks = 0; void addBook() { if (numBooks >= MAX_BOOKS) { printf("图书馆已满,无法添加新书。\n"); return; } Book newBook; printf("请输入书名:"); scanf("%s", newBook.title); printf("请输入作者:"); scanf("%s", newBook.author); printf("请输入出版年份:"); scanf("%d", &newBook.year); newBook.available = 1; library[numBooks] = newBook; numBooks++; printf("成功添加新书。\n"); } void borrowBook() { char title[100]; printf("请输入要借阅的书名:"); scanf("%s", title); int found = 0; for (int i = 0; i < numBooks; i++) { if (strcmp(library[i].title, title) == 0) { if (library[i].available) { library[i].available = 0; printf("成功借阅《%s》。\n", library[i].title); } else { printf("《%s》已被借出。\n", library[i].title); } found = 1; break; } } if (!found) { printf("未找到《%s》。\n", title); } } void returnBook() { char title[100]; printf("请输入要归还的书名:"); scanf("%s", title); int found = 0; for (int i = 0; i < numBooks; i++) { if (strcmp(library[i].title, title) == 0) { if (!library[i].available) { library[i].available = 1; printf("成功归还《%s》。\n", library[i].title); } else { printf("《%s》未被借出。\n", library[i].title); } found = 1; break; } } if (!found) { printf("未找到《%s》。\n", title); } } void displayBooks() { printf("图书馆中的书籍:\n"); for (int i = 0; i < numBooks; i++) { printf("书名:%s\n", library[i].title); printf("作者:%s\n", library[i].author); printf("出版年份:%d\n", library[i].year); printf("状态:%s\n", library[i].available ? "可借阅" : "已借出"); printf("\n"); } } int main() { int choice; do { printf("图书借阅管理系统\n"); printf("1. 添加书籍\n"); printf("2. 借阅书籍\n"); printf("3. 归还书籍\n"); printf("4. 显示所有书籍\n"); printf("0. 退出\n"); printf("请选择操作:"); scanf("%d", &choice); switch (choice) { case 1: addBook(); break; case 2: borrowBook(); break; case 3: returnBook(); break; case 4: displayBooks(); break; case 0: printf("感谢使用图书借阅管理系统,再见!\n"); break; default: printf("无效的选择,请重新输入。\n"); break; } printf("\n"); } while (choice != 0); return 0; } ``` 这个示例代码实现了一个简单的图书借阅管理系统,包括添加书籍、借阅书籍、归还书籍和显示所有书籍的功能。您可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你已春风摇曳,我仍一身旧雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值