本文实例为大家分享了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);
}