txmlh.png

txmlh2.png

txmlh3.png

book.png

Author.png

此次练习使用了结构体、数组、指针、文件操作实现,主要数据存放生成的(Book.db)(Author.db)二进制文件中,不过数据是文本仅此而已。写的代码比较难看,可凑合看。

代码相关分享

GitHub : https://github.com/2670637999/Book-management-System
码云:https://gitee.com/glumi_admin/Book-management-System

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

//临时值
int xx = 0;
//临时的输入值 (数字)  //临时的输入值 (字符串)  //总共入库书籍   //书名数据文件指针(写)   //作者名数据文件指针(写)      //书名数据文件指针(读)    //作者名数据文件指针(读)
int inputNumber;      char input[50];        int sum;        FILE *f;                FILE *f2;                   FILE *f3;               FILE *f4;

//容器的数据类型
typedef struct{
    char name[26];
}TXT;
typedef struct{
    char name[26];
}TXT2;
//书名容器
TXT Book[20]={
    {"空"},{"C语言从入门到精通"},{"Java从入门到精通"},
    {"Python从入门到精通"},{"空"},{"空"},
    {"空"},{"空"},{"空"},    
    {"空"},{"空"},{"空"},
    {"空"},{"空"},{"空"},
    {"空"},{"空"},{"空"},
    {"空"},{"空"},
};
//作者容器
TXT2 Author[20]={
    {"空"},{"小蓝"},{"小红"},
    {"小黄"},{"空"},{"空"},
    {"空"},{"空"},{"空"},    
    {"空"},{"空"},{"空"},
    {"空"},{"空"},{"空"},
    {"空"},{"空"},{"空"},
    {"空"},{"空"},
};
/**初始化书籍,判断本地数据是否存在,如果不存在则进行数据创建**/
void SetBook(){

    f3 = fopen("Book.db","rb");
    //[如果书名数据不存在, 创建数据]
    if(f3 == NULL){
        /**书名**/
        f = fopen("Book.db","wb+");
        if(f == NULL){
            printf("文档打开失败\n");
        }else{
            printf("书籍文档打开成功\n");
            printf("书籍名成功初始化数据\n");
            fwrite(&Book[0],sizeof(TXT),20,f);
        }
        sum = 0;
        for (int i = 0; i < 19; i++)
        {
            if(strcmp(Book[i].name,"空")!=0){
                sum++;
            }
        }
        fclose(f);
    //[如果书名数据库存在, 不创建数据库]
    }else{
        sum = 0;
        for (int i = 0; i < 19; i++)
        {
            if(strcmp(Book[i].name,"空")!=0){
                sum++;
            }
        }
        printf("\n查询到本地数据,不进行数据创建");
    }
    f4 = fopen("Author.db","rb");
    //[如果作者数据不存在, 创建本地数据]
    if(f4 == NULL){
        /**作者名**/
        f2 = fopen("Author.db","wb+");
        if(f2 == NULL){
            printf("\n文档打开失败\n");
        }else{
            printf("\n作者文档打开成功\n");
            printf("作者名成功初始化数据\n");
            fwrite(&Author[0],sizeof(TXT),20,f2);
        }
        fclose(f2);
        printf("\n查询不到本地数据,进行数据创建");
    //[如果作者数据存在, 不创建]
    }else{

    }
}
//程序结束前把内存数据写入本地数据
void setBook(){
        /**书名**/
        f = fopen("Book.db","wb+");
        if(f == NULL){
            printf("文档打开失败\n");
        }else{
            printf("书籍文档打开成功\n");
            printf("书籍名成功初始化数据\n");
            fwrite(&Book[0],sizeof(TXT),20,f);
        }
        sum = 0;
        for (int i = 0; i < 19; i++)
        {
            if(strcmp(Book[i].name,"空")!=0){
                sum++;
            }
        }
        fclose(f);
        /**作者名**/
        f2 = fopen("Author.db","wb+");
        if(f2 == NULL){
            printf("\n文档打开失败\n");
        }else{
            printf("\n作者文档打开成功\n");
            printf("作者名成功初始化数据\n");
            fwrite(&Author[0],sizeof(TXT),20,f2);
        }
        fclose(f2);
}
//修改书名
void FileBook(){
    /**书名**/
    f = fopen("Book.db","wb+");
    scanf("%s",&Book[inputNumber]);
    if(f == NULL){
        printf("| 文档打开失败\n");
        printf("+------------------------------------------------------------------------------+\n");
    }else{
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 文档打开成功\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 成功写入数据\n");
        fwrite(&Book[inputNumber],sizeof(TXT),20,f);
    }
    fclose(f);
    
}
//修改作者
void FileAuthor(){
    /**作者**/
    f2 = fopen("Author.db","wb+");
    scanf("%s",&Author[inputNumber]);
    if(f == NULL){
        printf("| 文档打开失败\n");
        printf("+------------------------------------------------------------------------------+\n");
    }else{
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 文档打开成功\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 成功写入数据\n");
        fwrite(&Author[inputNumber],sizeof(TXT),20,f);
    }
    fclose(f2);
}
/**获取图书和作者信息模块(查询)**/
void GetData(){
    f = fopen("Book.db","rb");
    f2 = fopen("Author.db","rb");
    fread(&Book[0],sizeof(TXT),20,f);
    fread(&Author[0],sizeof(TXT),20,f2);
    for (int i = 1; i < 20; i++)
    {
        printf("\n+------------------------------------------------------------------------------+");
        printf("\n");
        if(i<10){
            printf("|  0%d  |",i);
            printf(" %s |",&Author[i]);
            printf(" 《%s》 ",&Book[i]);
        }else if(i>9){
            printf("|  %d  |",i+1);
            printf(" %s |",&Author[i]);
            printf(" 《%s》 ",&Book[i]);
        }
    }
    fclose(f);
    fclose(f2);
    printf("\n+------------------------------------------------------------------------------+"); 
    printf("\n| 查询完毕.....");
    system("pause");
}
/**修改图书信息模块**/
void SetBook2(){
    printf("| 请输入要修改的图书的编号\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 输入: "); scanf("%d",&inputNumber);
    printf("+------------------------------------------------------------------------------+\n");
    if(strcmp(Author[inputNumber].name,"空")==0){
        printf("| 不可修改!,因为该编号为空\n");
        printf("+------------------------------------------------------------------------------+\n"); 
        system("pause");
    }else{
        printf("| 请选择要修改的项 (作者) (书名)\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 输入: "); scanf("%s",&input);
        printf("+------------------------------------------------------------------------------+\n");
        if(strcmp(input,"书名")==0){
            printf("| 请输入修改的数据\n");
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 输入: "); FileBook();
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 修改完毕。。。\n"); 
            printf("+------------------------------------------------------------------------------+\n");
            system("pause");
        }
        if(strcmp(input,"作者")==0){
            printf("| 请输入修改的数据\n");
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 输入: "); FileAuthor(); 
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 修改完毕。。。\n");
            printf("+------------------------------------------------------------------------------+\n");
            system("pause");
        }
    }
    setBook();
    
}
//入库书名
void sBook(){
    /**书名**/
    f = fopen("Book.db","wb+");
    scanf("%s",&Book[inputNumber]);
    if(f == NULL){
        printf("| 文档打开失败\n");
        printf("+------------------------------------------------------------------------------+\n");
    }else{
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 文档打开成功\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 成功写入数据\n");
        fwrite(&Book[inputNumber],sizeof(TXT),20,f);
    }
    fclose(f);
    
}
//入库作者名
void sAuthor(){
    /**作者**/
    f2 = fopen("Book.db","wb+");
    scanf("%s",&Author[inputNumber]);
    if(f == NULL){
        printf("| 文档打开失败\n");
        printf("+------------------------------------------------------------------------------+\n");
    }else{
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 文档打开成功\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 成功写入数据\n");
        fwrite(&Author[inputNumber],sizeof(TXT),20,f2);
    }
    fclose(f2);
}
/**图书入库模块**/
void SaveBook(){
    printf("| 请输入需要入库的图书的编号\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 输入: "); scanf("%d",&inputNumber);
    printf("+------------------------------------------------------------------------------+\n");
    if(strcmp(Author[inputNumber].name,"空")!=0){
        printf("| 该编号已被占用\n");
        printf("+------------------------------------------------------------------------------+\n");
        system("pause");
    }else{
        printf("| 请输入即将入库的书名\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 输入: "); sBook();
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 请输入即将入库的作者\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 输入: "); sAuthor();
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 正在进行入库。。。\n");
        printf("+------------------------------------------------------------------------------+\n");
        system("pause");
    }    
    setBook();
}
/**删除图书信息模块**/
void DelBook(){
    printf("| 你想要删除哪个编号的图书信息?\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 输入: "); scanf("%d",&inputNumber);
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 正在进行删除。。。\n");
    printf("+------------------------------------------------------------------------------+\n");
    if(strcmp(Book[inputNumber].name,"空")==0){
        printf("删除失败,因为它是空的");
    }else{
        Book[inputNumber] = Book[0];
        Author[inputNumber] = Author[0];    
        printf("| 删除完毕。。。\n");
        printf("+------------------------------------------------------------------------------+\n");
        system("pause");   
    }
    setBook();
}
/**查图书编号模块**/
void GetBookNumber(){
    printf("| 你想要查那个编号的书籍信息?\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 输入: "); scanf("%d",&inputNumber);
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 正在查询。。。\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| 查询完毕。。。\n");
    printf("+------------------------------------------------------------------------------+\n");
    printf("| %d 号书籍是 %s 作者:%s\n",inputNumber,&Book[inputNumber],&Author[inputNumber]);
    printf("+------------------------------------------------------------------------------+\n");
    system("pause");  
}
/**主界面**/
void Menu(){
    while(1){
        sum=0;
        SetBook();
        system("cls");
        printf("\n\n+------------------------------------------------------------------------------+\n");
        printf("|                          【 图 书 管 理 系 统 】                             |\n");
        printf("+------------------------------------------------------------------------------+\n");
        xx++;
        if(xx>1){
        printf("|              当前库中已有书籍:%d 本      请进行相关操作。                    |\n",sum);
        }else{
        printf("|              当前库中已有书籍: 本 (未查询)  请进行相关操作。                |\n");
        }
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 查询图书 | 修改图书 | 图书入库 | 删除图书 | 查图书编号 | 退出程序 | 关于程序 |\n");
        printf("+------------------------------------------------------------------------------+\n");
        printf("| 输入: "); scanf("%s",input);
        printf("+------------------------------------------------------------------------------+\n");
        if(strcmp(input,"查询图书")==0){
            printf("| 正在查询图书.....\n");
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 编号 | 作者 | 书名                                                           |");
            GetData();
            printf("\n+------------------------------------------------------------------------------+");
            printf("\n| 当前的已入库图书: %d 本\n",sum);
            printf("+------------------------------------------------------------------------------+\n\n");
        }
        if(strcmp(input,"修改图书")==0){
            printf("| 修改图书.....\n");
            printf("+------------------------------------------------------------------------------+\n");
            SetBook2();
        }
        if(strcmp(input,"图书入库")==0){
            printf("| 正在进行图书入库.....\n");
            printf("+------------------------------------------------------------------------------+\n");
            SaveBook();
        }
        if(strcmp(input,"删除图书")==0){
            printf("| 正在进行图书信息删除.....\n");
            printf("+------------------------------------------------------------------------------+\n");
            DelBook();
        }
        if(strcmp(input,"查图书编号")==0){
            GetBookNumber();
        }
        if(strcmp(input,"退出程序")==0){
            printf("| 正在退出程序.....\n");
            printf("+------------------------------------------------------------------------------+\n");
            break;
        }
        if(strcmp(input,"关于程序")==0){
            printf("| 本程序由 信息工程系 19级移动应用开发 陈杰海 完成开发.(目前已完成增删查改功能)|\n");
            printf("+------------------------------------------------------------------------------+\n");
            printf("| 是否继续使用该程序? 输入 是/否:\n"); printf("| 输入: "); scanf("%s",input);
            printf("+------------------------------------------------------------------------------+\n");
            if(strcmp(input,"否")==0){
                printf("| 正在退出程序.....\n");
                printf("+------------------------------------------------------------------------------+\n");
                break;
            }
        }
    }
    setBook();
    system("cls");
}
/**主程序**/
void main(){
    Menu();
}
文章目录