C語言實現宿舍管理系統
本文實例為大傢分享瞭C語言實現宿舍管理系統的具體代碼,供大傢參考,具體內容如下
本次大一課設的作品,從空項目開始寫的,全部在txt文件中增改刪。變量命名太土瞭,代碼格式還有待優化,望大佬指點。程序有個缺點就是不能把字符串互相對齊,看著不美觀。mysubstr是參考瞭論壇某位大佬的代碼,再此感謝。
主界面是這樣的
以下為源代碼:
#include<stdio.h> #include<string.h> #include<process.h> #include<conio.h> #define MAX_LINE 1024 #include<malloc.h> #include <assert.h> char* mysubstr(char* srcstr, int offset, int length) { assert(length > 0); assert(srcstr != NULL); int total_length = strlen(srcstr); int real_length = ((total_length - offset) >= length ? length : (total_length - offset)) + 1; char *tmp; if (NULL == (tmp=(char*) malloc(real_length * sizeof(char)))) { printf("Memory overflow . \n"); exit(0); } strncpy(tmp, srcstr+offset, real_length - 1); tmp[real_length - 1] = '\0'; return tmp; } //首先獲取srcstr的長度 //判斷srcstr的長度減去需要截取的substr開始位置之後,剩下的長度 //是否大於指定的長度length,如果大於,就可以取長度為length的子串 //否則就把從開始位置剩下的字符串全部返回。 int open=0,n,house=0;//open為判斷是否登錄的變量,登錄為1否則為0 void menu(void); void admin(void); void student(void); void house1(); void search(void); void clean(void); void delete1(void); void sushe(void); void weisheng(void); void searweisheng(void); void checkout(void); struct student { int xuehao; int sushe; char name[100]; char zhuanye[100]; }; struct clean { int sushe; char days[100]; char time[20]; char level; char fial[20]; }; int main() { int flag=0; menu(); do { flag=1; switch(getch()) { case '1' : system("cls");admin();break; /*管理員權限*/ case '2' : system("cls");student();break; /*學生錄入*/ case '3' : system("cls");house1();break; /*宿舍總覽*/ case '4' :system("cls");weisheng();break; /*衛生總覽*/ case '5' : system("cls");search();break; /*查詢系統*/ case '6' : system("cls");clean();break; /*衛生錄入*/ case '7' : system("cls"); student();break; /*添加學生*/ case '8' : system("cls");delete1();break; /*刪除學生*/ case '9' : system("cls");checkout();break; /*退宿記錄*/ case '0' :exit(0); /*退出程序*/ default : printf("輸入有誤\n"); } printf("\n"); printf("按任意鍵繼續\n"); getch(); menu(); }while(flag==1); } void admin() { int account=0,password=0,password_1=0,a=0; FILE *fread=fopen("amdin.txt","r"); FILE *fwrite=fopen("amdin.txt","a+"); ; fscanf(fread,"%*[^\n]"); fscanf(fread,"%d\t%d",&account,&password); if(account==0)//fwrite==null { printf("未查找到賬號,請創建賬號"); fprintf(fwrite,"賬號\t密碼\n"); loop:a=scanf("%d%d",&account,&password_1); if(a==2){ printf("創建成功"); fprintf(fwrite,"%d\t%d",account,password_1); }else{ printf("創建失敗"); goto loop; } }else{ fscanf(fwrite,"%*[^\n]"); printf("賬號:"); fscanf(fwrite,"%d\t",&account); printf("%d",account); printf("請輸入密碼"); scanf("%d",&password); fscanf(fwrite,"%d",&password_1); if(password_1==password) { open=1; printf("登錄成功"); } else printf("登錄失敗,請檢查密碼是否錯誤"); } fclose(fread); fclose(fwrite); } void menu() { system("cls"); if(open==1) printf(" 當前狀態:已登錄 \n"); else printf(" 當前狀態:未登錄 \n"); printf("***************************歡迎進入宿舍管理系統****************************\n"); printf(" 菜單 \n"); printf("|1--------------------------管理員權限-----------------------------------| \n"); printf("|2---------------------------學生錄入------------------------------------| \n"); printf("|3---------------------------宿舍總覽------------------------------------| \n"); printf("|4---------------------------衛生總覽------------------------------------| \n"); printf("|5---------------------------查詢系統------------------------------------| \n"); printf("|6---------------------------衛生錄入------------------------------------| \n"); printf("|7---------------------------添加學生------------------------------------| \n"); printf("|8---------------------------刪除學生------------------------------------| \n"); printf("|9---------------------------退宿記錄------------------------------------| \n"); printf("|0---------------------------退出系統------------------------------------| \n"); printf("***************************************************************************\n"); printf("--------------------------請輸入您的選擇-----------------------------------\n"); } void student() { if(open==1) { int n,i,k=0; char Q; printf("請輸入需錄入的學生個數(學號為八位數!):"); scanf("%d",&n); struct student stu1[n]; printf("學號\t\t學生姓名\t專業班級\t宿舍號\n"); for(int i=0;i<n;i++) { scanf("%d",&stu1[i].xuehao); scanf("%s",stu1[i].name); scanf("%s",stu1[i].zhuanye); scanf("%d",&stu1[i].sushe); } printf("是否需要保存數據?(Y\\N)"); scanf("%s",&Q); if(Q=='Y') { FILE*baocun=fopen("student.txt","ab+");//保存數據 //fscanf(baocun,"%*[^\n]"); fscanf(baocun,"%d",&k); fseek(baocun,0,SEEK_SET); if(k==0) { k=1; fprintf(baocun,"%d\n",k); fprintf(baocun,"學號\t學生姓名\t專業班級\t宿舍號\n"); for(i=0;i<n;i++) fprintf(baocun,"%d\t%s\t%s\t%d\n",stu1[i].xuehao,stu1[i].name,stu1[i].zhuanye,stu1[i].sushe); }else{ for(i=0;i<n;i++) fprintf(baocun,"%d\t%s\t%s\t%d\n",stu1[i].xuehao,stu1[i].name,stu1[i].zhuanye,stu1[i].sushe); } fclose(baocun); printf("錄入成功!"); } if(Q=='N') printf ("錄入失敗!"); }else{ printf("請先登錄,你還沒有權限!"); } } void house1() { int i,sushe; char cx[100]; FILE*check=fopen("student.txt","r"); fscanf(check,"%*[^\n]"); while(fscanf(check,"%[^\n]",cx)!=EOF) { printf("%s\n",cx); fgetc(check); } fclose(check); } void sushe() { int p=0; char *linshi; char sushe[20];//搜索的宿舍號碼 char buf[1024];//每次搜索的整行 char *chuxu=(char*)malloc(10);//需對比的前三項 FILE* search=fopen("student.txt","r"); if(fgets(buf,1024,search)==NULL) { printf("你還沒有錄入數據,請先錄入!"); }else{ printf("請輸入要搜索的宿舍號"); scanf("%s",&sushe); printf("學號\t學生姓名\t專業班級\t宿舍號\n");//str[]="deheve" while(fgets(buf,1024,search)!=NULL) { p=strlen(buf); linshi= mysubstr(buf, p-4, 3); if(strcmp(linshi,sushe)==0)//開始對比 { printf("%s",buf); } } fclose(search); } } void clean() { int n1,k=0; char Q; if(open==1) { printf("請輸入需要錄入的宿舍個數:\n"); scanf("%d",&n1); struct clean cleanning[n1]; printf("宿舍號 日期 具體時間段 衛生等級 是否消毒\n");//等級為ABCD for(int n=0;n<n1;n++) { scanf("%d",&cleanning[n].sushe); scanf("%s",cleanning[n].days); scanf("%s",cleanning[n].time); scanf("%s",&cleanning[n].level); scanf("%s",cleanning[n].fial); } printf("\n"); printf("是否需要保存數據(Y\\N)?"); scanf("%s",&Q); if(Q=='Y') { FILE * clean=fopen("clean.txt","ab+"); fscanf(clean,"%d",&k); fseek(clean,0,SEEK_SET); // printf("%d\n",k); if(k==0) { k=1; fprintf(clean,"%d\n",k); fprintf(clean,"宿舍號 日期 具體時間段 衛生等級 是否消毒\n"); for(int i=0;i<n1;i++) fprintf(clean,"%d %s %s %c %s\n",cleanning[i].sushe,cleanning[i].days,cleanning[i].time,cleanning[i].level,cleanning[i].fial); printf("錄入成功!"); }else{ fseek(clean,0,SEEK_SET); for(int i=0;i<n1;i++) fprintf(clean,"%d %s %s %c %s\n",cleanning[i].sushe,cleanning[i].days,cleanning[i].time,cleanning[i].level,cleanning[i].fial); printf("錄入成功!"); } fclose(clean); } if(Q=='N') { printf ("錄入失敗!"); } }else{ printf("你還沒有權限,請先登錄!"); } } void delete1() { if(open==1) { FILE *fp,*fp2,*fp3; char buf[1024]; char out[10]; char xuehao[8]; printf("請輸入要刪除的學生學號:"); scanf("%s",xuehao); fp=fopen("student.txt","ab+"); fp2=fopen("student2.txt","ab+"); fp3=fopen("quit.txt","ab+"); while(fgets(buf,1024,fp)!=NULL) { memcpy(out,buf,9);//取buf字符串的前九位再把值賦給out if(strstr(out,xuehao)) { fprintf(fp3,buf); continue; }else{ fprintf(fp2,buf); } } fclose(fp); fclose(fp2); fclose(fp3); remove("student.txt"); rename("student2.txt","student.txt"); }else{ printf("你還沒有權限,請先登錄"); } } void search() { system("cls"); printf("*********歡迎進入宿舍管理系統**************\n"); printf(" 菜單 \n"); printf("搜索宿舍-------------------------------------1\n"); printf("搜索衛生-------------------------------------2\n"); switch(getch()) { case '1' : system("cls");sushe();break; case '2' : system("cls");searweisheng();break; default : printf("輸入有誤\n"); } } void weisheng() { int i,sushe; char cx[100]; FILE*check=fopen("clean.txt","r"); fscanf(check,"%*[^\n]"); while(fscanf(check,"%[^\n]",cx)!=EOF) { printf("%s\n",cx); fgetc(check); } fclose(check); } void searweisheng() { int p=0; char *linshi; char sushe[20]; char buf[1024]; char *chuxu=(char*)malloc(10); FILE* search=fopen("clean.txt","r"); if(fgets(buf,1024,search)==NULL) { printf("你還沒有錄入數據,請先錄入!"); }else{ printf("請輸入要搜索的宿舍號"); scanf("%s",&sushe); printf("宿舍號 日期 具體時間段 衛生等級 是否消毒\n"); while(fgets(buf,1024,search)!=NULL) { p=strlen(buf); linshi= mysubstr(buf, 0, 3); if(strcmp(linshi,sushe)==0)//開始對比 { printf("%s",buf); } } fclose(search); } } void checkout() { char cx[200],buf[1024]; FILE*check=fopen("quit.txt","r"); fseek(check,0,SEEK_CUR); printf("學號 學生姓名 專業班級 宿舍號\n"); while(fgets(buf,1024,check)) { printf("%s",buf); } fclose(check); }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。