简单介绍
上图表明了我做这个项目要实现的基本功能,包括各个模块的增删改查,以及用户名
登陆,三次后跳出什么的,因为还学习C语言没有多久,还做得比较的粗糙,后续应该
会做的更好,有兴趣的同学可以点个关注,一起成长。
下面是关于其中一些代码的部分:
结构体的申明部分以及一些全局变量的申明
#ifndef _INFO_H__
2 #define _INFO_H__
3
4 #include<stdio.h>
5 #include<stdlib.h>
6 #include<string.h>
7
8 #define NAME_LEN 40
9 #define PASS_LEN 40
10
11 #define FILE_ADMIN "adminSys.bin"
12 #define FILE_BOOK "bookSys.bin"
13 #define FILE_VIP "vipSys.bin"
14
15
16 struct sAdmin{
17 char name_supAdmin[NAME_LEN];
18 char pass_supAdmin[PASS_LEN];
19 };
20
21 typedef struct adminInfo{
22 int id_admin;
23 char name_admin[NAME_LEN];
24 char user_admin[NAME_LEN];
25 char pass_admin[PASS_LEN];
26 }Admin;
27
28 extern Admin *padmin;
29 extern size_t size_admin ;
30 extern size_t cnt_admin;
31 extern int madeId_admin;
32 #define SIZE_ADMIN sizeof(Admin)
33
34 typedef struct bookInfo{
35 int id_book;
36 char name_book[NAME_LEN];
37 char author_book[NAME_LEN];
38 int usePrice;
39 int amount;
40 int bowTimes;
41 }Book;
42
43 extern Book *pbook;
44 extern size_t size_book;
45 extern size_t cnt_book;
46 extern int madeId_book;
47 #define SIZE_BOOK sizeof(Book)
48
49 typedef struct vipInfo{
50 int id_vip;
51 char name_vip[NAME_LEN];
52 char user_vip[NAME_LEN];
53 char pass_vip[PASS_LEN];
54 int balance;
55 char state[NAME_LEN];
56 }Vip;
57
58 extern Vip *pvip;
59 extern size_t size_vip;
60 extern size_t cnt_vip;
61 extern int madeId_vip;
62 #define SIZE_VIP sizeof(Vip)
63
64 enum RET_CODE{
65 SUCCESS = 0,
66 ARG_ERR,
67 OPEN_ERR,
68 FILE_NOT_EXISTS,
69 WRITE_ERR
70 };
71
72 #endif
main函数以及其中的已经声明了的run函数
1 #include "view.h"
2 #include "bigRun.h"
3
4 Admin *padmin = NULL;
5 size_t size_admin = 1000;
6 size_t cnt_admin = 0;
7 int madeId_admin = 1;
8
9 Book *pbook = NULL;
10 size_t size_book = 1000;
11 size_t cnt_book = 0;
12 int madeId_book = 1;
13
14 Vip *pvip = NULL;
15 size_t size_vip = 1000;
16 size_t cnt_vip = 0;
17 int madeId_vip = 2019000;
18
19 int main(int argc,int *argv[]){
20 load_admin(FILE_ADMIN);
21 load_book(FILE_BOOK);
22 load_vip(FILE_VIP);
23 run();
24 }
下面是run()函数
1 #include "bigRun.h"
2 #include "Info.h"
3 void run(){
4 struct sAdmin sp={"123","123"};
5 int opt = 0;
6 printf("-----欢迎进入图书管理系统-----\n");
7 printf("1.超级管理员模式\n");
8 printf("2.普通管理员模式\n");
9 printf("3.会员模式\n");
10 printf("0.退出系统\n");
11 printf("请选择:");
12 while(1){
13 scanf("%d",&opt);
14 switch(opt){
15 case 1:
16 run1(sp);
17 break;
18 case 2:
19 run2();
20 break;
21 case 3:
22 run3();
23 break;
24 case 0:
25 save_admin(FILE_ADMIN);
26 save_book(FILE_BOOK);
27 save_vip(FILE_VIP);
28 free(padmin);
29 free(pbook);
30 free(pvip);
31 exit(0);
32 default:
33 printf("Error!\n");
34 }
35 }
36 }
以上的 "xxxx.h"是函数声明,方便在其他函数中使用。
将读写文件程序
1 #include "file_admin.h"
2 #include<unistd.h>
3 #include "oper.h"
4
5 int load_admin(const char * path){
6 if(path == NULL){
7 return ARG_ERR;
8 }
9 if(access(path,F_OK)!=0){
10 return FILE_NOT_EXISTS;
11 }
12 FILE *fp = fopen(path,"r");
13 if(fp == NULL){
14 return OPEN_ERR;
15 }
16 fread(&madeId_admin,4,1,fp);
17
18 Admin a = {};
19 while(fread(&a,SIZE_ADMIN,1,fp)>0){
20
21 int ret = addAdmin(&a);
22 if(ret != 1){
23 fclose(fp);
24 return ret;
25 }
26 }
27 fclose(fp);
28 return SUCCESS;
29 }
30
31
32 int save_admin(const char * path){
33 if(path == NULL){
34 return ARG_ERR;
35 }
36 FILE *fp = fopen(path,"w");
37 if(fp == NULL){
38 return OPEN_ERR;
39 }
40 fwrite(&madeId_admin,4,1,fp);
41
42 if(fwrite(padmin,SIZE_ADMIN,cnt_admin,fp)<cnt_admin){
43 fclose(fp);
44 return WRITE_ERR;
45 }
46
47 fclose(fp);
48 return SUCCESS;
49 }
超级管理模式下增加管理员的方式
1 #include "oper.h"
2
3 int addAdmin(Admin* pa){
4 if(pa == NULL){
5 return -1;
6 }
7 if(padmin == NULL){
8 padmin = malloc(SIZE_ADMIN*size_admin);
9 }
10 if(padmin != NULL && size_admin == cnt_admin){
11 size_admin = 2*size_admin;
12 padmin = realloc(padmin,SIZE_ADMIN*size_admin);
13 }
14 if(padmin == NULL){
15 return -1;
16 }
17 padmin[cnt_admin] = *pa;
18 ++cnt_admin;
19 return 1;
20 }
21
然后是主函数
50 void add_Admin(){
51 Admin a = {};
52 a.id_admin = madeId_admin++;
53 printf("Add Admin\n");
54 printf("请输入姓名:");
55 scanf("%s",a.name_admin);
56 size_t i;
57 int flag;
58 do{
59 flag = 0;
60 printf("请输入用户名:");
61 scanf("%s",a.user_admin);
62 for(i=0;i<cnt_admin;i++){
63 if(strcmp(padmin[i].user_admin,a.user_admin)==0){
64 printf("用户名重复,请重新尝试!\n");
65 flag = 1;
66 }
67 }
68 }while(flag);真则继续循环,假则跳出循环,用do{}while()的方式至少进行一次
69 printf("请输入密码:");
70 scanf("%s",a.pass_admin);
71 addAdmin(&a);
72 showAdmin(&a);
73 }
以上为本项目的部分代码,后面还有完整的项目