C語言簡單實現門禁系統

這篇文章主要爲大家詳細介紹了C語言簡單實現門禁系統,具有一定的參考價值,感興趣的小夥伴們可以參考一下

問題提出

用C語言實現門禁系統功能。

功能簡析

1、系統登錄分爲管理員登錄和學生登錄,輸入相應字符即可選擇登錄;

這裏寫圖片描述 

2、學生登錄,學生使用自己的賬號密碼登錄系統,選擇開啓的門,即可開啓門禁;此外系統還會記錄下學生登錄的信息,如登錄賬號、登錄時間、開啓哪間門;

這裏寫圖片描述 

3、管理員登錄,管理員登錄也是要輸入特定的賬號密碼才能登錄;登錄之後才能進入管理員操作界面進行相應操作,如添加學生賬號、刪除學生賬號、查看學生基本信息、查看學生登錄情況……輸入相應字符即可選擇執行相應的功能。

這裏寫圖片描述 

4、管理員權限一:添加學生賬號。輸入學生姓名(即爲賬號)、密碼、專業班級等基本信息,可用於後續查詢,回車完成添加。查看系統中學生信息,該賬號已存在

這裏寫圖片描述 

5、管理員權限二:刪除學生賬號。輸入需要刪除的賬號,回車完成刪除操作,查看系統數據庫中已無該學生信息。

這裏寫圖片描述 

6、管理員權限三:查看所以學生基本信息。輸入相應字符,即可查詢所以信息

這裏寫圖片描述 

7、管理員權限四:查看學生登錄門禁系統情況。輸入相應相應字符,可按學生使用門禁系統時間先後順序一次顯示學生賬號、登錄時間,選擇開啓的門。

這裏寫圖片描述 

8、管理員權限五:修改存在於系統的學生信息。輸入需要修改的學生賬號,並輸入修改後的賬號、密碼、專業班級等基本信息。

這裏寫圖片描述

實現關鍵

首先考慮到要對學生賬號等基本數據要重複使用,且易更改,下次使用時上次修改的信息還會存在而不是對代碼進行修改,所以考慮使用對文本進行操作。將學生賬號密碼等數據信息全部儲存在一個txt或dat文本中,調用函數對文本進行相應操作,對數據的操作即是對文件的操作。

代碼實現

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
struct information
{
 char name[10];
 char password[10];
 char profession[20];


};
struct Thelog
{
 char name[10];
 char times[24];
 char num[1];
};
FILE *fp;   //文件指針 
information *head;//鏈表頭指針 
void add_student(information a);  //函數[1]管理員添加學生賬號,使用門禁系統 
void delete_student(char names[10]);//函數[2]管理員員刪除學生賬號,解除使用門禁系統權限 
void administrator_login();  //函數[3]管理員登陸 
void administrator_management(); //函數[4]管理員操作界面,添加學生賬號,刪除學生賬號,查看學生使用門禁情況 
void student_login();   //函數[5]學生登陸,用於打開門禁 
void time ();     //函數[6]時間函數,用於顯示登陸時間 
void All_output();    //函數[7]全部學生信息 
void the_log(information a,char num[1]); //函數[8]錄入學生使用門禁情況 
void log_output();       //函數[9]輸出學生使用門禁情況 
void replace_massage(char names[10]);  //函數[10]更改學生信息,只有管理員有權限 
int main()
{
 int n;
 printf("   *********************************************\n");
 printf("   *   歡迎使用四海門禁系統!!!  *\n");
 printf("   *********************************************\n");
 printf("\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
 printf("   *輸入0:退出系統; 輸入1:管理員登陸; 輸入2:學生登陸*\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
 printf(" ");
 scanf("%d",&n);
 while(n!=0)
 {
 if(n==1)
  administrator_login();
 else if(n==2)
  student_login();

 else if(n>10||n<0)
 {
  printf("   *********************************************\n");
  printf("   *   歡迎使用四海門禁系統!!!  *\n");
  printf("   *********************************************\n");break;
 }
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
 printf("   *輸入0:退出系統; 輸入1:管理員登陸; 輸入2:學生登陸*\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
  printf(" ");
  scanf("%d",&n);
 }
  printf("   *********************************************\n");
  printf("   *   歡迎使用四海門禁系統!!!  *\n");
  printf("   *********************************************\n");
 return 0;
}
void administrator_login()   //管理員登錄 
{
 information a;

 printf(" 請輸入管理員賬號:");
 scanf("%s",a.name);
 printf(" 請輸入管理員密碼:");
 scanf("%s",a.password);
 if(strcmp(a.name,"管理員")==0&&strcmp(a.password,"123456789")==0)
  {
  printf("\n");
  printf(" *********管理員登陸成功!!!*********\n\n");
  administrator_management();
  }
  else
  {
  printf( "    管理員登陸失敗!!!請檢查仔細覈對賬號、密碼\n"); 
  }

}
void administrator_management() //管理員操作檯 
{
 int num;
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
 printf("   *輸入1:註冊學生賬號   輸入2:刪除賬號  輸入3:顯示所有學生信息*\n"); 
 printf("   *輸入4;查看學生使用門禁情況 輸入5:修改學生信息 輸入6:刷新屏幕  *\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
 printf(" ");
 scanf("%d",&num);
 while(num!=-1)
 {
  if(num==0||num>10||num<0)
  {
   printf("  管理員身份已註銷\n");
   break;
  }
  if(num==1)
  {
  fp=fopen("student_Account.txt","a");
  information a;
  printf("   請輸入賬號:");
  scanf("%s",a.name);
  printf("   請輸入密碼:");
  scanf("%s",a.password);
  printf("  請輸入專業班級:");
  scanf("%s",a.profession);
   add_student(a);
  fclose(fp);
  }
   if(num==2)
  {
  char names[10];
  printf(" 請輸入要刪除的賬號:");
  scanf("%s",names);
  delete_student(names);

  }
   if(num==3)
   {
   fp=fopen("student_Account.txt","r");
   All_output();
   fclose(fp);
   }
  if(num==4)
   {
   fp=fopen("Thelog.txt","r");
   log_output();
   fclose(fp);
   }
   if(num==5)
   {
   char names[10];
   fp=fopen("student_Account.txt","r+");
    printf(" 請輸入需要修改的賬號:");
    scanf("%s",names);
    replace_massage(names);
    fclose(fp);
   }
   if(num==6)
   {
   system("cls");
   }
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
 printf("   *輸入1:註冊學生賬號   輸入2:刪除賬號  輸入3:顯示所有學生信息*\n"); 
 printf("   *輸入4;查看學生使用門禁情況 輸入5:修改學生信息 輸入6:刷新屏幕  *\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
  printf(" ");
  scanf("%d",&num);
 }
}
void student_login()    //學生登錄 
{
 information a;
 information b;
 fp=fopen("student_Account.txt","r");
 printf(" 請輸入賬號:");
 scanf("%s",a.name);
 printf(" 請輸入密碼:");
 scanf("%s",a.password);
 fread(&b,sizeof(struct information),1,fp);
 int i=0;
 while(feof(fp)==0)
 {
  if(strcmp(a.name,b.name)==0&&strcmp(a.password,b.password)==0)
  {
  char num[1];
  printf(" 請選擇要打開幾號門\n");
  printf(" ");
  scanf("%s",num);

   time();
   printf(" 親愛的%s童鞋,你成功登陸本系統,已開啓%s號門\n",a.name,num);
   i++;
  the_log(a,num);

  }
  fread(&b,sizeof(struct information),1,fp);
 }
 if(i==0)
  printf(" 登陸賬號不存在或密碼錯誤!!!若無賬號請聯繫管理員分配賬號\n");
 fclose(fp);
}
void delete_student(char names[10])  //管理員刪除學生賬號,解除使用門禁權限 
{ 
 FILE *p;
 information a;
 int i=0;
 fp=fopen("student_Account.txt","r");
 p=fopen("student.txt","w");
 fread(&a,sizeof(struct information),1,fp);
 while(feof(fp)==0)
 {
 if(strcmp(a.name,names)==0)
  {fread(&a,sizeof(struct information),1,fp);i=1;}
 else
  {
   fwrite(&a,sizeof(struct information),1,p);
  }
 fread(&a,sizeof(struct information),1,fp);
 } 
 if(strcmp(a.name,names)!=0)
  fwrite(&a,sizeof(struct information),1,p);
 else
  i=1;
 if(i==0)
  printf(" 賬號%s不在系統中\n",names);
  else
  printf(" 賬號%s已成刪除\n",names);
 fclose(fp);
 fclose(p);
 remove("student_Account.txt");
 rename("student.txt","student_Account.txt");
}
void add_student(information a)  //管理員添加學生信息,分配賬號,授權使用門禁系統 
{

   fseek(fp,-sizeof(struct information),1);
   fwrite(&a,sizeof(struct information),1,fp);
   printf("  添加學生%s成功!!!\n\n",a.name);


}
void time()       //顯示本地時間 
{
 time_t rawtime;
 struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime (&rawtime );
printf(" %s",asctime (timeinfo));
}
void All_output()     //輸出所有學生信息 
{
 int i=0;
 information a;

 fread(&a,sizeof(struct information),1,fp);
 while(feof(fp)==0)
 {
  printf("      * * * * * * * * * * * * * * * * * * * * * * *\n");
  printf("      *    學生賬號:%-20s*\n",a.name);
  printf("      *    學生密碼:%-20s*\n",a.password);
  printf("      *    專業班級:%-20s*\n",a.profession);
  printf("      * * * * * * * * * * * * * * * * * * * * * * *\n");
  printf("\n");
  i++;
 fread(&a,sizeof(struct information),1,fp);
 } 
 if(i==0)
 printf("    系統中無學生賬號!!!\n\n");

}
void the_log(information a,char num[1])    //實時保存學生使用門禁系統情況 
{
 FILE *p;
 Thelog c;
 strcpy(c.name,a.name);
 time_t rawtime;
 struct tm * timeinfo;
 time ( &rawtime );
 timeinfo =localtime ( &rawtime );
 strcpy(c.times,asctime (timeinfo));
 strcpy(c.num,num);
 p=fopen("Thelog.txt","a");
 fwrite(&c,sizeof(struct Thelog),1,p); 
 fclose(p);
}
void log_output()        //輸出學生使用門禁系統情況 
{ 
 Thelog a;

 fread(&a,sizeof(struct Thelog),1,fp);
 while(feof(fp)==0)
 {
  printf("%s",a.times);
  printf("學生%s登錄了門禁系統,並開啓了%s號門\n",a.name,a.num);
 fread(&a,sizeof(struct Thelog),1,fp);
 }

}
void replace_massage(char names[10])   //更改學生信息 
{
 information a;
 information b;
 fread(&a,sizeof(struct information),1,fp);
 if(strcmp(names,a.name)==0)
 {
 printf("  請輸入更改後的賬號:");
 scanf("%s",b.name);
 printf("  請輸入修改後的密碼:");
 scanf("%s",b.password);
 printf(" 請輸入修改後的專業班級:");
 scanf("%s",b.profession);
 fseek(fp,-sizeof(struct information),1);//文件指針退回原結構體頭 
 fwrite(&b,sizeof(struct information),1,fp);//更新聯繫人信息 
 printf("   信息更改成功!!!\n\n");
 }
 else if(feof(fp)==0)
  replace_massage(names);//遞歸調用直到找到需要更改的姓名,並完成更改 
 else
  printf("%s不在系統中!!!\n\n",names);

}

後期分析

後期可利用單片機實現刷卡登錄的門禁系統,利用qt 設計可視化程序,在學生登錄成功後觸發相應的門開啓或關閉。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章