溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng)

發(fā)布時(shí)間:2021-04-20 17:33:32 來源:億速云 閱讀:303 作者:Leah 欄目:編程語言

這篇文章給大家介紹使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

C語言是什么

C語言是一門面向過程的、抽象化的通用程序設(shè)計(jì)語言,廣泛應(yīng)用于底層開發(fā),使用C語言可以以簡易的方式編譯、處理低級(jí)存儲(chǔ)器。

問題提出

用C語言實(shí)現(xiàn)門禁系統(tǒng)功能。

功能簡析

1、系統(tǒng)登錄分為管理員登錄和學(xué)生登錄,輸入相應(yīng)字符即可選擇登錄;

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

2、學(xué)生登錄,學(xué)生使用自己的賬號(hào)密碼登錄系統(tǒng),選擇開啟的門,即可開啟門禁;此外系統(tǒng)還會(huì)記錄下學(xué)生登錄的信息,如登錄賬號(hào)、登錄時(shí)間、開啟哪間門;

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

3、管理員登錄,管理員登錄也是要輸入特定的賬號(hào)密碼才能登錄;登錄之后才能進(jìn)入管理員操作界面進(jìn)行相應(yīng)操作,如添加學(xué)生賬號(hào)、刪除學(xué)生賬號(hào)、查看學(xué)生基本信息、查看學(xué)生登錄情況……輸入相應(yīng)字符即可選擇執(zhí)行相應(yīng)的功能。

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

4、管理員權(quán)限一:添加學(xué)生賬號(hào)。輸入學(xué)生姓名(即為賬號(hào))、密碼、專業(yè)班級(jí)等基本信息,可用于后續(xù)查詢,回車完成添加。查看系統(tǒng)中學(xué)生信息,該賬號(hào)已存在

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

5、管理員權(quán)限二:刪除學(xué)生賬號(hào)。輸入需要?jiǎng)h除的賬號(hào),回車完成刪除操作,查看系統(tǒng)數(shù)據(jù)庫中已無該學(xué)生信息。

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

6、管理員權(quán)限三:查看所以學(xué)生基本信息。輸入相應(yīng)字符,即可查詢所以信息

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

7、管理員權(quán)限四:查看學(xué)生登錄門禁系統(tǒng)情況。輸入相應(yīng)相應(yīng)字符,可按學(xué)生使用門禁系統(tǒng)時(shí)間先后順序一次顯示學(xué)生賬號(hào)、登錄時(shí)間,選擇開啟的門。

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng) 

8、管理員權(quán)限五:修改存在于系統(tǒng)的學(xué)生信息。輸入需要修改的學(xué)生賬號(hào),并輸入修改后的賬號(hào)、密碼、專業(yè)班級(jí)等基本信息。

使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng)

實(shí)現(xiàn)關(guān)鍵

首先考慮到要對學(xué)生賬號(hào)等基本數(shù)據(jù)要重復(fù)使用,且易更改,下次使用時(shí)上次修改的信息還會(huì)存在而不是對代碼進(jìn)行修改,所以考慮使用對文本進(jìn)行操作。將學(xué)生賬號(hào)密碼等數(shù)據(jù)信息全部儲(chǔ)存在一個(gè)txt或dat文本中,調(diào)用函數(shù)對文本進(jìn)行相應(yīng)操作,對數(shù)據(jù)的操作即是對文件的操作。

代碼實(shí)現(xiàn)

#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);  //函數(shù)[1]管理員添加學(xué)生賬號(hào),使用門禁系統(tǒng) 
void delete_student(char names[10]);//函數(shù)[2]管理員員刪除學(xué)生賬號(hào),解除使用門禁系統(tǒng)權(quán)限 
void administrator_login();  //函數(shù)[3]管理員登陸 
void administrator_management(); //函數(shù)[4]管理員操作界面,添加學(xué)生賬號(hào),刪除學(xué)生賬號(hào),查看學(xué)生使用門禁情況 
void student_login();   //函數(shù)[5]學(xué)生登陸,用于打開門禁 
void time ();     //函數(shù)[6]時(shí)間函數(shù),用于顯示登陸時(shí)間 
void All_output();    //函數(shù)[7]全部學(xué)生信息 
void the_log(information a,char num[1]); //函數(shù)[8]錄入學(xué)生使用門禁情況 
void log_output();       //函數(shù)[9]輸出學(xué)生使用門禁情況 
void replace_massage(char names[10]);  //函數(shù)[10]更改學(xué)生信息,只有管理員有權(quán)限 
int main()
{
 int n;
 printf("   *********************************************\n");
 printf("   *   歡迎使用四海門禁系統(tǒng)?。?!  *\n");
 printf("   *********************************************\n");
 printf("\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
 printf("   *輸入0:退出系統(tǒng); 輸入1:管理員登陸; 輸入2:學(xué)生登陸*\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("   *   歡迎使用四海門禁系統(tǒng)?。?!  *\n");
  printf("   *********************************************\n");break;
 }
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
 printf("   *輸入0:退出系統(tǒng); 輸入1:管理員登陸; 輸入2:學(xué)生登陸*\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*\n");
  printf(" ");
  scanf("%d",&n);
 }
  printf("   *********************************************\n");
  printf("   *   歡迎使用四海門禁系統(tǒng)?。?!  *\n");
  printf("   *********************************************\n");
 return 0;
}
void administrator_login()   //管理員登錄 
{
 information a;

 printf(" 請輸入管理員賬號(hào):");
 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( "    管理員登陸失敗?。?!請檢查仔細(xì)核對賬號(hào)、密碼\n"); 
  }

}
void administrator_management() //管理員操作臺(tái) 
{
 int num;
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
 printf("   *輸入1:注冊學(xué)生賬號(hào)   輸入2:刪除賬號(hào)  輸入3:顯示所有學(xué)生信息*\n"); 
 printf("   *輸入4;查看學(xué)生使用門禁情況 輸入5:修改學(xué)生信息 輸入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("   請輸入賬號(hào):");
  scanf("%s",a.name);
  printf("   請輸入密碼:");
  scanf("%s",a.password);
  printf("  請輸入專業(yè)班級(jí):");
  scanf("%s",a.profession);
   add_student(a);
  fclose(fp);
  }
   if(num==2)
  {
  char names[10];
  printf(" 請輸入要?jiǎng)h除的賬號(hào):");
  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(" 請輸入需要修改的賬號(hào):");
    scanf("%s",names);
    replace_massage(names);
    fclose(fp);
   }
   if(num==6)
   {
   system("cls");
   }
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
 printf("   *輸入1:注冊學(xué)生賬號(hào)   輸入2:刪除賬號(hào)  輸入3:顯示所有學(xué)生信息*\n"); 
 printf("   *輸入4;查看學(xué)生使用門禁情況 輸入5:修改學(xué)生信息 輸入6:刷新屏幕  *\n");
 printf("   */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/**/*/*/*\n");
  printf(" ");
  scanf("%d",&num);
 }
}
void student_login()    //學(xué)生登錄 
{
 information a;
 information b;
 fp=fopen("student_Account.txt","r");
 printf(" 請輸入賬號(hào):");
 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(" 請選擇要打開幾號(hào)門\n");
  printf(" ");
  scanf("%s",num);

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

  }
  fread(&b,sizeof(struct information),1,fp);
 }
 if(i==0)
  printf(" 登陸賬號(hào)不存在或密碼錯(cuò)誤!?。∪魺o賬號(hào)請聯(lián)系管理員分配賬號(hào)\n");
 fclose(fp);
}
void delete_student(char names[10])  //管理員刪除學(xué)生賬號(hào),解除使用門禁權(quán)限 
{ 
 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(" 賬號(hào)%s不在系統(tǒng)中\(zhòng)n",names);
  else
  printf(" 賬號(hào)%s已成刪除\n",names);
 fclose(fp);
 fclose(p);
 remove("student_Account.txt");
 rename("student.txt","student_Account.txt");
}
void add_student(information a)  //管理員添加學(xué)生信息,分配賬號(hào),授權(quán)使用門禁系統(tǒng) 
{

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


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

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

 fread(&a,sizeof(struct information),1,fp);
 while(feof(fp)==0)
 {
  printf("      * * * * * * * * * * * * * * * * * * * * * * *\n");
  printf("      *    學(xué)生賬號(hào):%-20s*\n",a.name);
  printf("      *    學(xué)生密碼:%-20s*\n",a.password);
  printf("      *    專業(yè)班級(jí):%-20s*\n",a.profession);
  printf("      * * * * * * * * * * * * * * * * * * * * * * *\n");
  printf("\n");
  i++;
 fread(&a,sizeof(struct information),1,fp);
 } 
 if(i==0)
 printf("    系統(tǒng)中無學(xué)生賬號(hào)!??!\n\n");

}
void the_log(information a,char num[1])    //實(shí)時(shí)保存學(xué)生使用門禁系統(tǒng)情況 
{
 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()        //輸出學(xué)生使用門禁系統(tǒng)情況 
{ 
 Thelog a;

 fread(&a,sizeof(struct Thelog),1,fp);
 while(feof(fp)==0)
 {
  printf("%s",a.times);
  printf("學(xué)生%s登錄了門禁系統(tǒng),并開啟了%s號(hào)門\n",a.name,a.num);
 fread(&a,sizeof(struct Thelog),1,fp);
 }

}
void replace_massage(char names[10])   //更改學(xué)生信息 
{
 information a;
 information b;
 fread(&a,sizeof(struct information),1,fp);
 if(strcmp(names,a.name)==0)
 {
 printf("  請輸入更改后的賬號(hào):");
 scanf("%s",b.name);
 printf("  請輸入修改后的密碼:");
 scanf("%s",b.password);
 printf(" 請輸入修改后的專業(yè)班級(jí):");
 scanf("%s",b.profession);
 fseek(fp,-sizeof(struct information),1);//文件指針退回原結(jié)構(gòu)體頭 
 fwrite(&b,sizeof(struct information),1,fp);//更新聯(lián)系人信息 
 printf("   信息更改成功!?。n\n");
 }
 else if(feof(fp)==0)
  replace_massage(names);//遞歸調(diào)用直到找到需要更改的姓名,并完成更改 
 else
  printf("%s不在系統(tǒng)中?。?!\n\n",names);

}

關(guān)于使用C語言怎么實(shí)現(xiàn)一個(gè)門禁系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI