You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ssq/图书管理系统.cpp

725 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int prince=10;
int time=3;
struct book_class
{
char num[10]; //编号
char name[10]; //书名
char author[10]; //作者
int Price; //金额
};
struct book
{
char num[10]; //编号 和书类的编号一致
char name[10] ; //书名 和书类的书名一致
int price; //价格 和书类的价格一致
};
struct person
{
char number[10] ; //借书证号 和上面一致
char name [10]; //姓名
char password[10]; //密码
char class_[15]; //班级
char num[10]; //学号;
};
void register_m() //管理员注册
{
struct person p1;
FILE *fp;
int flag=1,x;
while(flag==1){
system("cls");
if((fp=fopen("管理员信息.txt","a"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
printf("请输入用户名:\n");
scanf("\n%s",p1.number);
fprintf(fp,"\n%s ",p1.number);
printf("请输入密码:\n");
scanf(" %s",p1.password);
fprintf(fp,"%s ",p1.password);
printf("请输入姓名:\n");
scanf(" %s",p1.name);
fprintf(fp,"%s ",p1.name);
printf("请输入班级:\n");
scanf(" %s",p1.class_);
fprintf(fp,"%s ",p1.class_);
printf("请输入学号:\n");
scanf(" %s",p1.num);
fprintf(fp,"%s ",p1.num);
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit (0);
}
printf("注册成功!\n");
printf("按1继续注册按0退出\n\n");
printf("下步操作:");
scanf("%d",&flag);
}
return ;
}
void register_y() //用户注册
{
struct person p1;
FILE *fp;
int flag=1,x;
while(flag==1){
system("cls");
if((fp=fopen("管理员信息.txt","a"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
printf("请输入用户名:\n");
scanf("\n%s",p1.number);
fprintf(fp,"\n%s ",p1.number);
printf("请输入密码:\n");
scanf(" %s",p1.password);
fprintf(fp,"%s ",p1.password);
printf("请输入姓名:\n");
scanf(" %s",p1.name);
fprintf(fp,"%s ",p1.name);
printf("请输入班级:\n");
scanf(" %s",p1.class_);
fprintf(fp,"%s ",p1.class_);
printf("请输入学号:\n");
scanf(" %s",p1.num);
fprintf(fp,"%s ",p1.num);
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit (0);
}
printf("注册成功!\n");
printf("按1继续注册按0退出\n\n");
printf("下步操作:");
scanf("%d",&flag);
}
return ;
} //用户注册
int cheakUserValid_m(struct person *p1) //用户登录时检测密码是否正确
{
FILE *fp;
struct person p2;
char usr[10],usr1[10],pwd[10],pwd1[10];
int check=0;
strcpy(usr1,p1->number);
strcpy(pwd1,p1->password);
strcat(usr1,pwd1);
if((fp=fopen("管理员信息.txt","r"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s%s%d",p2.number,p2.password,p2.name,p2.class_,p2.num);
strcpy(usr,p2.number);
strcpy(pwd,p2.password);
strcat(usr,pwd);
if(strcmp(usr,usr1)==0)
{
check=1;break;
}
}
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit (0);
}
return check;
}
void jiashu() //书本信息添加
{
FILE *fp;
struct book_class b1;
int flag=1;
while(flag==1){
system("cls");
if ((fp=fopen("book_class.txt","a"))==NULL)
{
printf("文件打开错误\n");
exit(0);
}
printf("请输入编号:\n");
scanf("\n%s",b1.num);
fprintf(fp,"\n%s ",b1.num);
printf("请输入书名:\n");
scanf(" %s",b1.name);
fprintf(fp,"%s ",b1.name);
printf("请输入作者:\n");
scanf(" %s",b1.author);
fprintf(fp,"%s ",b1.author);
printf("请输入金额:\n");
scanf(" %d",&b1.Price);
fprintf(fp,"%d ",b1.Price);
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit(0);
}
printf("继续添加请按1返回界面请按0:\n");
scanf("%d",&flag);
}
return ;
}
void chashu() //书本信息查询
{
FILE *fp;
struct book_class b1;
char a[10];
int temp=0,flag=1;
while(flag==1){
system("cls");
printf("请输入书名:\n");
scanf("%s",a);
if((fp=fopen("book_class.txt","r"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s%%d%",b1.num,b1.name,b1.author,&b1.Price);
if(strcmp(a,b1.name)==0)
{
printf("编号 书名 作者 出版社 出版日期 金额 类别 总入库数量 现存数量 已经借出数量\n");
printf("%s %s %s %d\n",b1.num,b1.name,b1.author,b1.Price);
temp=1;
break;
}
}
if(temp==0)
{
printf("无此书籍\n");
}
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit (0);
}
printf("继续查询请按1返回界面请按0:\n");
scanf("%d",&flag);
}
return ;
}
void shanshu() //书本信息删除
{
FILE *fp,*fp1;
struct book_class b1;
char a[10];
int flag=1,temp=0;
while(flag==1){
system("cls");
printf("请输入要删除的书名:\n");
scanf("%s",a);
if((fp=fopen("book_class.txt","r"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
if((fp1=fopen("bbookk.txt","w"))==NULL)
{
printf("文件打开错误!\n");
exit (0);
}
do
{
fscanf(fp,"%s%s%s%d",b1.num,b1.name,b1.author,&b1.Price);
if(strcmp(b1.name,a)!=0)
{
fprintf(fp1,"\n%s %s %s %d",b1.num,b1.name,b1.author,b1.Price);
}
else
temp=1;
}while(!feof(fp));
if(temp==0)
{
printf("无此书籍\n");
}
else
printf("删除成功\n");
if(fclose(fp))
{
printf("文件无法关闭!\n");
exit (0);
}
if(fclose(fp1))
{
printf("文件无法关闭!\n");
exit (0);
}
remove("book_class.txt");
rename("bbookk.txt","book_class.txt");
printf("继续删除请按1返回界面请按0:\n");
scanf("%d",&flag);
}
return ;
}
void jieyuegl() //借阅管理界面
{
int a;
while(1)
{
system("cls");
printf("********** 借阅资料管理 **********\n");
printf("\n\n\n");
printf("********** 1.添加 **********\n");
printf("********** 2.查找 **********\n");
printf("********** 3.删除 **********\n");
printf("********** 0.返回 **********\n");
printf("\n\n\n");
printf("请输入操作(0-3):");
scanf("%d",&a);
if(a==0)
break;
switch(a)
{
case 1:jiashu();break;
case 2:chashu();break;
case 3:shanshu();break;
}
}
}
void guandlscreen() //管理员登录界面
{
int x;
while(1)
{
system("cls");
printf("********** 管理员登录 **********\n");
printf("\n\n\n");
printf("********** 1.借阅资料管理 **********\n");
printf("********** 0.退出登录 **********\n");
printf("\n\n\n");
printf("请输入操作(0-1):");
scanf("%d",&x);
if(x==0)
break;
switch(x)
{
case 1:jieyuegl();break;
}
}
return ;
}
void denglu_m() //管理员登录
{
struct person p1;
system("cls");
printf("请输入用户名(借书证号):\n");
scanf("%s",p1.number);
printf("请输入密码:\n");
scanf("%s",p1.password);
if(cheakUserValid_m(&p1)==1)
{
printf("登录成功\n");
guandlscreen();
}
else
{
printf("密码错误\n");
system("pause");
}
}
void lend_out(struct person p2,int choice) //借阅
{
system("cls");
struct book_class *su;
int i,year,month,day,year1=0,month1=0,day1=0; //定义今天日期和归还日期
char name[20]; //书名
printf("请输入要借阅的书名\n");
scanf("%s",name);
printf("请输入今天的日期xxxx xx xx\n");
scanf("%d%d%d",&year ,&month ,&day);
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("book_class.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp2 = fopen("book_class1.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp3=fopen("bookone.txt","a"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
while(!feof(fp1))
{
su=(struct book_class *)malloc(sizeof(struct book_class));
fscanf(fp1,"%s%s%s%d",su->num,su->name,su->author,&su->Price);
if(!strcmp(name,su->name))
{
//修改借阅信息
fprintf(fp2,"\n%s %s %s %d",su->num,su->name,su->author,su->Price);
FILE *fp5,*fp6; //罚款信息写入用户信息
switch(choice)
{
case 0:
{
if((fp5=fopen("教师信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp6=fopen("教师信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
case 1:
{
if((fp5=fopen("学生信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp6=fopen("学生信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
}
while(!feof(fp5))
{
struct person *p3;
p3=(struct person *)malloc(sizeof(struct person));
fscanf(fp5,"%s%s%s%s%s%d",p3->number,p3->name,p3->password,p3->class_,p3->num);
if(!strcmp(p3->number,p2.number))
{
fprintf(fp6,"\n%s %s %s %s %s %d",p3->number,p3->name,p3->password,p3->class_,p3->num);
}
else
fprintf(fp6,"\n%s %s %s %s %s %d",p3->number,p3->name,p3->password,p3->class_,p3->num);
}
FILE *FP1,*FP2,*FP3,*FP4,*FP5,*FP6; //复制回去
if((FP1=fopen("book_class1.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP2=fopen("book_class.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP3=fopen("bookone1.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP4 = fopen("bookone.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
switch(choice)
{
case 0:
{
if((FP5=fopen("教师信息2.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP6=fopen("教师信息.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
case 1:
{
if((FP5=fopen("学生信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP6=fopen("学生信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
}
while(1)
{
char ch;
ch=getc(FP1);
if(feof(FP1)) break;
putc(ch,FP2);
}
while(1)
{
char ch;
ch=getc(FP3);
if(feof(FP3)) break;
putc(ch,FP4);
}
while(1)
{
char ch;
ch=getc(FP5);
if(feof(FP5)) break;
putc(ch,FP6);
}
system("pause");
fclose(FP1);
fclose(FP2);
fclose(FP3);
fclose(FP4);
fclose(FP5);
fclose(FP6);
return ;
}
}
}
void lend_back(struct person p2,int choice) //还书
{
system("cls");
struct book_class *su;
int i,year,month,day; //定义归还日期
char name[20]; //书名
printf("\t\t请输入要归还的书名\n");
scanf("%s",name);
FILE *fp1,*fp2,*fp3,*fp4;
if((fp1=fopen("book_class.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp2 = fopen("book_class1.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
while(!feof(fp1))
{
su=(struct book_class *)malloc(sizeof(struct book_class));
fscanf(fp1,"%s%s%s%d",su->num,su->name,su->author,&su->Price);
if(!strcmp(name,su->name))
{
//修改借阅信息
fprintf(fp2,"\n%s %s %s %d",su->num,su->name,su->author,su->Price);
if((fp3=fopen("bookone.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp4=fopen("bookone1.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
struct book bacbook;
while(!feof(fp3))
{
fscanf(fp3,"%s%s%d",bacbook.num,bacbook.name,&bacbook.price);
if((!strcmp(bacbook.name,name))&&(!strcmp(bacbook.num,p2.number)))
{
printf("还书成功\n");
system("pause");
}
else
fprintf(fp4,"\n%s %s %d",bacbook.num,bacbook.name,bacbook.price);
}
fclose(fp3);
fclose(fp4);
}
else
fprintf(fp2,"\n%s %s %s %d",su->num,su->name,su->author,su->Price);
}
fclose(fp1);
fclose(fp2);
FILE *fp5,*fp6; //罚款信息写入用户信息
switch(choice)
{
case 0:
{
if((fp5=fopen("教师信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp6=fopen("教师信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
case 1:
{
if((fp5=fopen("学生信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((fp6=fopen("学生信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
}
while(!feof(fp5))
{
struct person *p3;
p3=(struct person *)malloc(sizeof(struct person));
fscanf(fp5,"%s%s%s%s%s",p3->number,p3->name,p3->password,p3->class_,p3->num);
if(!strcmp(p3->number,p2.number))
{
fprintf(fp6,"\n%s %s %s %s %s",p3->number,p3->name,p3->password,p3->class_,p3->num);
}
else
fprintf(fp6,"\n%s %s %s %s %s",p3->number,p3->name,p3->password,p3->class_,p3->num);
}
FILE *FP1,*FP2,*FP3,*FP4,*FP5,*FP6; //复制回去
if((FP1=fopen("book_class1.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP2=fopen("book_class.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP3=fopen("bookone1.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP4 = fopen("bookone.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
switch(choice)
{
case 0:
{
if((FP5=fopen("教师信息2.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP6=fopen("教师信息.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
case 1:
{
if((FP5=fopen("学生信息.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
if((FP6=fopen("学生信息2.txt","w"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
}
break;
}
while(1)
{
char ch;
ch=getc(FP1);
if(feof(FP1)) break;
putc(ch,FP2);
}
while(1)
{
char ch;
ch=getc(FP3);
if(feof(FP3)) break;
putc(ch,FP4);
}
while(1)
{
char ch;
ch=getc(FP5);
if(feof(FP5)) break;
putc(ch,FP6);
}
system("pause");
fclose(FP1);
fclose(FP2);
fclose(FP3);
fclose(FP4);
fclose(FP5);
fclose(FP6);
return;
}
void mana(struct person p2,int choice) //用户功能界面
{while(1)
{
system("cls");
printf("\t\t\t =======================================================\n");
printf("\t\t\t| |\n");
printf("\t\t\t| (1)借阅 |\n");
printf("\t\t\t| (2)还书 |\n");
printf("\t\t\t| (0)返回主页面 |\n");
printf("\t\t\t| |\n");
printf("\t\t\t =======================================================\n");
int n;
scanf("%d",&n);
if(n==0)break;
switch(n)
{
case 1:lend_out(p2,choice);break;
case 2:lend_back(p2,choice);break;
}
}
}
int main(){
int x;
while(1)
{
system("cls");
system("color 2e");
printf(" 图书管理系统 \n");
printf("\n\n\n");
printf(" 1.管理员登录 \n");
printf(" 2.管理员注册 \n");
printf(" 3.用户登录 \n");
printf(" 4.用户注册 \n");
printf(" 0.退出系统 \n");
printf("\n\n\n");
printf("请输入操作(0-4):");
scanf("%d",&x);
if(x==0)
break;
switch(x)
{
case 1:denglu_m();break;
case 2:register_m();break;
case 4:register_y();break;
}
}
}