【C语言】简单图书管理系统


*/

/*
程序不完美,输出书籍信息会出现乱码,也许是  booknum    在从文件读入和直接录入书籍信息处重复赋值了 
找不出来    暂时没改


编译器  c free 5.0    在达到 exit(0) 这类时会停顿  但在其它编译器如 dev  则不会
一些地方没有用 sleep()  用其他编译器可能效果不佳


尽可能地达到了不看代码能够直接使用程序
*/


/*
使用     :          管理员 账号   leay 
密码   110
用户 账号   hqweay 
密码   520
*/ 


#include<stdio.h>
#include<stdlib.h>
#include<string.h>          //调用字符串比较函数strcmp 
#include<windows.h>
#define N 10
int booknum = 0;                    //书的编号 代表书的数量       从文件读入结构体有用 
int type;                   //定义一个全局变量,返回值对应用户类型 


/*
函数作用 一个并没有什么用的字符动画 提升界面可读性
*/ 


int welcome()
{
        printf("\b\\");
        Sleep(500);
        printf("\b/");
        Sleep(500);
        printf("\b-");
        Sleep(500);
}

/*
函数作用   实现登录界面,并向下返回用户类型
           type = 0;普通用户
  type = 1;管理员
  
  实现了  密码错误三次  退出 
*/ 


int login()
{
int i;
char id[] = "hqweay";
char vip[] = "leay";
char idpass[] = "520";
char vippass[] = "110";
char user[20];
char passage[20];
for(i = 0; i < 3; i++)
{
printf("\n请输入您的用户名\n**************\n");
gets(user);
printf("请输入您的密码\n**************\n");
gets(passage);
if(strcmp(user, id) == 0&&strcmp(passage, idpass) == 0)
{
printf("您是普通用户\n");
return type = 0;
}
if(strcmp(user, vip) == 0&& strcmp(passage, vippass) == 0)
{
printf("您是尊贵的管理员`(*∩_∩*)′\n");
return type = 1;
}
else
{
printf("您的用户名或者密码错误,请重新输入\n**************\n"); 
}
if(i == 2)
{
printf("您已经输错三次,我们怀疑您是不法分子。\n程序将退出...\n");
exit(0);
}
}
}


/*
函数作用 检测D盘下有无书籍信息 并返回相应信息
*/ 


int banfile()
{
FILE *fp;
if((fp = fopen("D://book.txt", "r")) == NULL)             //如果在D盘下没有发现文件book.txt 
{
printf("抱歉 未查询到书本信息\n机智的我将会在D盘下建立一个空文本并命名为book.txt\n\n*********\n\n");
FILE *fp = fopen("D://book.txt", "w");
Sleep(5000);
exit(0);
}
else
{
if(type  == 0)
{
printf("请联系管理员添加书本信息\n");
if(feof(fp)==1) exit(0); 
}
printf("*************************\n");
if(type == 1)
printf("请录入书籍信息\n");
printf("******************\n");
}
}


/*
定义结构体
*/
 
struct bookdate
{
char booka[20];
char writer[20];
int ID;
int stock;
int price; 
};

/*
录入书籍信息
*/
 
int Inbook(struct bookdate *hello, int niu)
{
int i;
int m;
for(i = 0; i < niu; i++)
{ 

printf("书名:\n");
scanf("%s",&hello[i].booka);
printf("作者:\n");
scanf("%s",&hello[i].writer);
printf("编号:\n");
scanf("%d",&hello[i].ID);
printf("库存:\n");
scanf("%d",&hello[i].stock);
printf("价格:\n");
scanf("%d",&hello[i].price);
  printf("--------------------------------------------------\n");
  printf("|书名|作者|编号|库存|价格|\n");
  printf("--------------------------------------------------\n");
  for (m = 0; m <= i; m++)
  {
  printf("|%-4s|%-4s|%-4d|%-4d|%-4d|\n",hello[m].booka, hello[m].writer, hello[m].ID, hello[m].stock, hello[m].price);
  }
  printf("--------------------------------------------------\n");
  booknum++;                                                            //每录一本书  booknum值加1 
} 

}


/*
输出书籍信息 
*/


int Putbook(struct bookdate *hello)
{
int m;
  printf("--------------------------------------------------\n");
  printf("|书名|作者|编号|库存|价格|\n");
  printf("--------------------------------------------------\n");
  for (m = 0; m <= booknum; m++)
  {
  printf("|%-4s|%-4s|%-4d|%-4d|%-4d|\n",hello[m].booka, hello[m].writer, hello[m].ID, hello[m].stock, hello[m].price);
  }
  printf("--------------------------------------------------\n");
}


/* 
把书籍信息存入文件 book.txt
*/ 


int Indoc(struct bookdate *hello, int booknum)
{
int m;
FILE *fp;
if((fp = fopen("D://book.txt", "ab")) == NULL)
{
printf("\nerror!\n");
exit(0);
}
fwrite(hello, sizeof(struct bookdate), booknum, fp);
fclose(fp); 
}


/* 
当管理员对书籍信息进行删除等操作后  重新把信息写入文件 book.txt


即打开文件时  用 w实现
 
*/ 


int Gaidoc(struct bookdate *hello, int booknum)
{
int m;
FILE *fp;
if((fp = fopen("D://book.txt", "wb")) == NULL)
{
printf("\nerror!\n");
exit(0);
}
fwrite(hello, sizeof(struct bookdate), booknum, fp);
fclose(fp);
}


/*
删除  书籍
*/


int Delete(struct bookdate *hello) 
{
int dele;
int u;
int v;
int tem;
char a[20]; 
printf("输入您要删除的书籍的编号");
scanf("%d", &dele);
printf("正在查找哦、、\n");
for(u = 0; u <= booknum; u++)
{
if(dele == hello[u].ID)
break;
}
while(u != booknum)
{
hello[u] = hello[u + 1];
u++;
}
booknum--;
}


/*
从文件读取书籍信息  非常重要的一个函数 (花时最久) 
*/


int Outdoc(struct bookdate *nihao)
{
int m;
FILE *fp;
if((fp = fopen("D://book.txt", "rb")) == NULL)
{
printf("\nerror!\n");
exit(0);
}
for(m = 0; !feof(fp); m++)
{
fread(&nihao[m], sizeof(struct bookdate), 1, fp);
booknum++;                                                       //这一步困扰了我将近一天 
}
booknum = booknum - 2;
fclose(fp);
} 


/*
查询函数      虽然取的数字比较小  写这个有点鸡肋了   但还是写一下
*/
int Findbook(struct bookdate *hello)
{
char g;
char bookn[N];
int q;
Ret1: printf("\n输入您想查询的书籍的名字\n");
scanf("%s", bookn);
for(q = 0; q < N; q++)
{
if(strcmp(hello[q].booka, bookn) == 0)
{
  printf("--------------------------------------------------\n");
  printf("|书名|作者|编号|库存|价格|\n");
  printf("--------------------------------------------------\n");
  printf("|%-4s|%-4s|%-4d|%-4d|%-4d|\n", hello[q].booka, hello[q].writer, hello[q].ID, hello[q].stock, hello[q].price);
  printf("--------------------------------------------------\n");
printf("\n输入   5    返回上一级\n");
scanf("%c", &g);
if(g == '5')   goto Ret1;
   break;
}
}
if(q == N)
{
printf("\n抱歉啊亲  我们没有这本书 联系管理员添加哦\n"); 
printf("\n输入   5    返回上一级\n");
scanf("%c", &g);
if(g == '5')   goto Ret1;
}

} 


/*
会员 借书函数


实现了 当库存为0时  删除书籍信息 
*/


int Lendbook(struct bookdate *hello)
{
int i;
int fin; 
int u; 
printf("\n亲 借书前先通过查询找到书籍编号哦 请输入: ");
scanf("%d", &fin);
for(i = 0; i < N; i++)
{
if(hello[i].ID == fin)
{
hello[i].stock--;
break;
}
else
{
printf("\n没有这本书诶\n");
break;
}
}
printf("\n**************\n");
if(hello[i].stock == 0)
{
for(u = 0; u <= booknum; u++)
{
while(u != booknum)
{
hello[u] = hello[u + 1];
u++;
}
}
booknum--;
}
}


//                    图书管理系统 


int main()
{
struct bookdate hello[N];
// struct bookdate nihao[N];
int niu;                                             //录入书籍信息时调用 录入几本书 
char g;
char c; //判断是否继续录入信息 
char d;
welcome();                                             //欢迎函数   
login();
banfile(); 
if( type == 1)
{
printf("\n您是否想输入书籍信息?(y/n)***************\n若否将进入查看本地图书信息\n");
printf("\n***********************\n");
scanf("%c", &c);
while(c == 'Y'||c == 'y')
{
printf("\n您想录入几本\n");
scanf("%d", &niu);
Inbook(hello,niu);
Indoc(hello, booknum);
printf("\n您是否想继续输入书籍信息?(y/n)***************\n若否将进入查看本地图书信息\n ");
scanf(" %c", &c);
    }
if(c == 'N'||c == 'n')
{
Outdoc(hello);                         //booknum1
    Putbook(hello);                        //booknum1
    printf("%d", booknum) ;
}
Ret:   printf("\n在键盘按下  1   ,进入删除系统\n");
    printf("\n在键盘按下  2   ,进入查询系统\n");
    scanf(" %c", &d);
switch(d) 
{
case '1' : 
Delete(hello);
Putbook(hello);
Gaidoc(hello, booknum);
printf("\n输入   5    返回上一级\n");
scanf(" %c", &g);
if(g == '5')   goto Ret;
break;
case '2' : 
Findbook(hello);
printf("\n输入   5    返回上一级\n");
scanf(" %c", &g);
if(g == '5')   goto Ret;
break;
dafault : ;
}
}
if( type == 0 )
{
Outdoc(hello);
    Putbook(hello);
Ret2: printf("\n在键盘按下  1   ,进入查询系统\n");
printf("\n在键盘按下  2   ,进入借书系统\n");
scanf(" %c", &c);
switch(c)
{
case '1' :
Findbook(hello);
printf("\n若要借书 请记住书籍的编号\n") ;
printf("\n输入   5    返回上一级\n");
scanf("%c", &g);
if(g == '5')   goto Ret2;
break;
case '2' :
Lendbook(hello);
Gaidoc(hello, booknum);
Putbook(hello);
printf("\n输入   5    返回上一级\n");
scanf(" %c", &g);
if(g == '5')   goto Ret2;
break; 
} 
}
}


  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值