实验室管理系统课程设计

时间:2022-01-13 19:39:04
主要目的:实现对实验室的硬件资源和软件资源的使用和管理。 数据信息:用户登录信息(分普通成员--管理员:信息主要有用户名、密码、性别、专业班级、学号、联系电话、QQ等) 硬件器材信息(编号、名称、数量、存放地点、所属类别、主要用途) 图书资源信息(图书编号、图书名称、图书数量、图书类别、主要用途) 主要功能: 1.登录界面:实现不同用户的登录、找回密码、注册新用户。 2。普通用户登录之后功能选择:查询资源记录、借还资源、修改个人信息。 3.管理登录可以增加新的耗材记录、修改用户信息。   #include<stdio.h> #include<string.h> #include<stdlib.h> #include<conio.h>   struct personal_message{          //个人信息  char name[10]; char num[12]; char sex[6]; char classes[15]; char QQ[12]; char Tel[12]; }p;   struct book{                          //图书信息         char name[20]; char num[10]; int member; int borrow; }books,book1[50];   struct sysuser{                          //用户名和密码  char username[16]; char password[7]; }sd,sy;   struct consumables{                  //耗材信息  char name[20]; int member; }con,con1;   void registe();                        //注册  void enter();        //登录  int validate();               //验证管理员(root)  void find_password();               //找回密码  void modification_information();     //修改信息  void modification_password();       //修改密码  void resource_record();        //查询书籍信息  void add_book();        //添加书籍  void borrow_repay();                     //书籍借还  void borrow();                  //借书  void repay();            //还书  void consumables_record();         //查询耗材  void check_consumables();          //耗材信息  void add_consumables();              // 添加耗材  void add_administrator();              //添加管理员 void star(char mima[]);                 //密码变 * void page_design1();                    //登录页面  void page_design2();         void page_design3();   void page_design4(); void page_design5();   void page_design6(); FILE *fp; FILE *fp1; FILE *fp2; FILE *fp3; int fog=0,j=0; char mima[7];   int main(void) { struct personal_message h,*o; o=&h; int n,m,flag; char st[100],s[20]; // loop:printf("1-登录\t2-注册\n");   loop:page_design1(); scanf("%d",&n); system("CLS");   switch(n) { case 1: { enter(); validate(); system("CLS"); }break;   case 2: { registe(); system("CLS"); goto loop; }break; case 3: { find_password(); system("CLS"); goto loop; }break; }   lo:if((fp=fopen("information.txt","r"))==NULL)    /*显示个人信息 */ { printf("File open error!\n"); exit(0); }   rewind(fp); fseek(fp,j*sizeof(struct personal_message),0);     fread(&h,sizeof(struct personal_message),1,fp); printf("姓名:%s\n",h.name); printf("性别:%s\n",h.sex); printf("学号:%s\n",h.num); printf("专业班级:%s\n",h.classes); printf("QQ:%s\n",h.QQ); printf("电话:%s\n",h.Tel);   if(fclose(fp)) { printf("Can not close the file!\n"); exit(0); }   //printf("1-修改个人信息\t2-查询图书资源\t3-借还资源\t4-修改密码\n");  page_design2();     if(fog==1) page_design3();     // printf("5-添加图书\t6-耗材信息\t7-添加管理员\n"); printf("\t\t\t*----------------------*\n"); printf("\t\t\t************************\n"); scanf("%d",&m); system("CLS");   switch(m) { case 1:modification_information();break; case 2:resource_record();break; case 3:borrow_repay();break; case 4:modification_password();break; case 5:add_book();break; case 6:consumables_record();break; case 7:add_administrator();break; } // printf("按1键返回首页,按2键退出\n"); page_design6(); scanf("%d",&flag); system("CLS"); if(flag==1) goto lo;   return 0; }   void registe() /*注册*/  { char c[7];     struct sysuser sa; printf("用户名:"); scanf("%s",sa.username); for(;;) { printf("密码:"); //scanf("%s",sa.password); star(sa.password); printf("确认密码:"); //scanf("%s",c); star(c); if(strcmp(c,sa.password)==0) break; } if((fp3=fopen("sysusers.txt","a"))==NULL) { printf("File open error!\n"); exit(0); } //fputs(&sa,fp3); fwrite(&sa,sizeof(struct sysuser),1,fp3);   if(fclose(fp3)) { printf("Can not close the file!\n"); exit(0); } system("CLS");   printf("姓名:"); scanf("%s",p.name); printf("性别:"); scanf("%s",p.sex); printf("学号:"); scanf("%s",p.num); printf("专业班级:"); scanf("%s",p.classes); printf("QQ:"); scanf("%s",p.QQ); printf("电话:"); scanf("%s",p.Tel);   if((fp=fopen("information.txt","a"))==NULL) { printf("File open error!\n"); exit(0); }   fwrite(&p,sizeof(struct personal_message),1,fp);   if(fclose(fp)) { printf("Can not close the file!\n"); exit(0); } printf("注册成功!\n"); }   void enter() /*登录 */ int count=0; for(;;) { printf("用户名:"); scanf("%s",sy.username); printf("密码:"); // scanf("%s",sy.password); star(sy.password); if((fp3=fopen("sysusers.txt","r"))==NULL) { printf("File open error!\n"); exit(0); } while(!feof(fp3)) { fread(&sd,sizeof(struct sysuser),1,fp3); if(strcmp(sy.username,sd.username)==0 && strcmp(sy.password,sd.password)==0) { count=1; break; } j++; }   if(fclose(fp3)) { printf("Can not close the file!\n"); exit(0); }   if(count == 1) { printf("登录成功!\n"); break; } else printf("用户名和密码不匹配!\n");   } }   int validate() /*验证是否是管理员(root) */ { int l1; l1=strlen(sd.username); if(sd.username[l1-4]=='r' && sd.username[l1-3]=='o' && sd.username[l1-2]=='o' && sd.username[l1-1]=='t') fog=1; else fog=0; return fog; }   void find_password()    //找回密码  { struct sysuser sr,sr1; int b=0; char b1[7]; lp:printf("请输入用户名:"); scanf("%s",sr1.username); if((fp3=fopen("sysusers.txt","r+"))==NULL) { printf("File open error!\n"); exit(0); } while(!feof(fp3)) {   fread(&sr,sizeof(struct sysuser),1,fp3); if(strcmp(sr.username,sr1.username)==0) break; else     b++; } if(feof(fp3)==-1) { printf("找不到该用户名"); goto lp; } else { for(;;) { printf("请输入新密码:"); // scanf("%s",sr1.password); star(sr1.password); printf("请确认新密码:"); // scanf("%s",b1); star(b1); if(strcmp(b1,sr1.password)==0) break; } rewind(fp3); strcpy(sr.password,sr1.password); fseek(fp3,b*sizeof(struct sysuser),0); fwrite(&sr,sizeof(struct sysuser),1,fp3); } if(fclose(fp3)) { printf("Can not close the file!\n"); exit(0); } }   void modification_information()  /*修改个人信息 */ { struct personal_message p1; printf("姓名:"); scanf("%s",p1.name); printf("性别:"); scanf("%s",p1.sex); printf("学号:"); scanf("%s",p1.num); printf("专业班级:"); scanf("%s",p1.classes); printf("QQ:"); scanf("%s",p1.QQ); printf("电话:"); scanf("%s",p1.Tel); if((fp=fopen("information.txt","w"))==NULL) { printf("File open error!\n"); exit(0); } rewind(fp); fseek(fp,j*sizeof(personal_message),0); fwrite(&p1,sizeof(personal_message),1,fp); if(fclose(fp)) { printf("Can not close the file!\n"); exit(0); } }   void modification_password()  //修改密码  { struct sysuser su,su1; char b[7]; for(;;) { printf("请输入新密码:"); //scanf("%s",su.password); star(su.password); if(strcmp(su.password,b)==0) break;   printf("请确认密码:"); //scanf("%s",b); star(b); if(strcmp(b,su.password)==0) break; } if((fp3=fopen("sysusers.txt","r+"))==NULL) { printf("File open error!\n"); exit(0); } rewind(fp3); fseek(fp3,j*sizeof(struct sysuser),0); fread(&su1,sizeof(struct sysuser),1,fp3); strcpy(su1.password,su.password); fseek(fp3,j*sizeof(struct sysuser),0); fwrite(&su1,sizeof(struct sysuser),1,fp3); if(fclose(fp3)) { printf("Can not close the file!\n"); exit(0); } printf("修改成功!\n"); }   void resource_record() /* 图书资源查看 */ { int w; if((fp1=fopen("resource.txt","r"))==NULL) { printf("File open error!\n"); exit(0); } fseek(fp1,0,2); //文件末      w=ftell(fp1);           //文件指针的位置  fseek(fp1,0,0);  //文件首  while(w!=ftell(fp1)) // while(!feof(fp1))               { fread(&books,sizeof(struct book),1,fp1); printf("书名:%s\n",books.name); printf("编号:%s\n",books.num); printf("数量:%d\n",books.member); printf("已借:%d\n",books.borrow); printf("\n"); } if(fclose(fp1)) { printf("Can not close the file!\n"); exit(0); } }   void add_book() /*添加图书 */ { int count; printf("请输入要添加书的本数:"); scanf("%d",&count); while(count--) { printf("书名:"); scanf("%s",books.name); printf("编号:"); scanf("%s",books.num); printf("数量:"); scanf("%d",&books.member); printf("已借:"); scanf("%d",&books.borrow); if((fp1=fopen("resource.txt","a"))==NULL) { printf("File open error!\n"); exit(0); }   fwrite(&books,sizeof(struct book),1,fp1);   if(fclose(fp1)) { printf("Can not close the file!\n"); exit(0); } if(count!=0) printf("\n"); } }   void borrow_repay() /*书籍借还 */ { int k; //printf("1-借\t2-还\n"); page_design4(); scanf("%d",&k); switch(k) { case 1:borrow();break; case 2:repay();break; } }   void borrow() /*借书 */ { int i=0; char a2[20]; printf("请输入要借的书的编号:"); scanf("%s",a2); if((fp1=fopen("resource.txt","r+"))==NULL) { printf("File open error!\n"); exit(0); } while(!feof(fp1)) { fread(&book1[i],sizeof(struct book),1,fp1); if(strcmp(a2,book1[i].num)==0) {   book1[i].member--; book1[i].borrow++; rewind(fp1); fseek(fp1,i*sizeof(struct book),0); fwrite(&book1[i],sizeof(struct book),1,fp1); break; } i++; } if(fclose(fp1)) { printf("Can not close the file!\n"); exit(0); } printf("借书成功!\n"); }   void repay() /*还书*/  { int i=0; char a3[20]; printf("请输入要还的书的编号:"); scanf("%s",a3); if((fp1=fopen("resource.txt","r+"))==NULL) { printf("File open error!\n"); exit(0); } while(!feof(fp1)) { fread(&book1[i],sizeof(struct book),1,fp1); if(strcmp(a3,book1[i].num)==0) {   book1[i].member++; book1[i].borrow--; rewind(fp1); fseek(fp1,i*sizeof(struct book),0); fwrite(&book1[i],sizeof(struct book),1,fp1); break; } i++; } if(fclose(fp1)) { printf("Can not close the file!\n"); exit(0); } printf("还书成功!\n"); }   void consumables_record() /*耗材管理 */ { int m1; //printf("1-查看耗材\t2-增加耗材\n"); page_design5(); scanf("%d",&m1); switch(m1) { case 1:check_consumables();break; case 2:add_consumables();break; } }   void check_consumables() /*查看耗材 */ { system("CLS"); int w1; if((fp2=fopen("consumables.txt","r"))==NULL) { printf("File open error!\n"); exit(0); } fseek(fp2,0,2); //文件末      w1=ftell(fp2);           //文件指针的位置  fseek(fp2,0,0); while(w1!=ftell(fp2)) // while(!feof(fp2))               { fread(&con,sizeof(struct consumables),1,fp2); printf("名称:%s\n",con.name); printf("数量:%d\n",con.member); printf("\n"); } if(fclose(fp2)) { printf("Can not close the file!\n"); exit(0); } }   void add_consumables()  /*增加耗材 */ { system("CLS"); printf("请输入增加的耗材名称:"); scanf("%s",con1.name); printf("请输入该耗材的数量:"); scanf("%d",&con1.member); if((fp2=fopen("consumables.txt","a"))==NULL) { printf("File open error!\n"); exit(0); }   fwrite(&con1,sizeof(struct consumables),1,fp2);   if(fclose(fp2)) { printf("Can not close the file!\n"); exit(0); } printf("添加成功!\n"); }   void add_administrator() /*添加管理员*/  { struct sysuser sw; char a4[16]; char a5[5]={"root"}; int l2=0,w2; loop:printf("请输入要添加成管理员的用户名:"); scanf("%s",a4); if((fp3=fopen("sysusers.txt","r+"))==NULL) { printf("File open error!\n"); exit(0); } fseek(fp3,0,2);     w2=ftell(fp3); fseek(fp3,0,0); while(!feof(fp3)) { fread(&sw,sizeof(struct sysuser),1,fp3);   if(strcmp(sw.username,a4)==0) { strcat(sw.username,a5); // rewind(fp3); // fseek(fp3,l2*sizeof(struct sysuser),0); fseek(fp3,l2*sizeof(struct sysuser),0); fwrite(&sw,sizeof(struct sysuser),1,fp3); printf("添加成功!\n"); break; } else { l2++; if(w2==ftell(fp3)) { printf("找不到该用户!\n"); goto loop; } } } if(fclose(fp3)) { printf("Can not close the file!\n"); exit(0); } }   void star(char mima[]) { for(int i=0;;i++) //以*号显示所输入密码,i并不重要,这里可以设置成无限循环。  { mima[i]=getch(); if(mima[i]=='\r') //getch()函数读到回车键则退出 { printf("\n"); mima[i]='\0'; break; } else if((mima[i]==8)&&i>0) //关于是否退格的判断 { printf("\b \b"); i=i-2; } else if(mima[i]!=8) //如果输入的不是退格键(8是退格键的Ascii码值),那么就输出星号。 { printf("*"); } } }     void page_design1() { printf("\n\n\n"); 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*---3-找回密码---------*\n"); printf("\t\t\t*----------------------*\n"); printf("\t\t\t************************\n"); }   void page_design2() { printf("\n\n"); 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*---3-借还资源---------*\n"); printf("\t\t\t*---4-修改密码---------*\n"); }   void page_design3() { printf("\t\t\t*---5-添加图书---------*\n"); printf("\t\t\t*---6-耗材信息---------*\n"); printf("\t\t\t*---7-添加管理员-------*\n"); } void page_design4() { 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*----------------------*\n"); printf("\t\t\t************************\n"); } void page_design5() { 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*----------------------*\n"); printf("\t\t\t************************\n"); }   void page_design6() { 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*----------------------*\n"); printf("\t\t\t************************\n"); }