本文实例为大家分享了C语言实现宿舍管理的具体代码,供大家参考,具体内容如下
和本编前几个程序结构差不多,比较简单直观,希望可以给你带来帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<Windows.h>
typedef struct student
{
int might1;
int might2;
char user[20];//账号
char mima[20];//密码
char name[20];//姓名
char sex[5];//性别
char num[20];//学号
char classs[20];//班级
char floor[20];//楼号
char dorm[20];//宿舍号
struct student *next;
}LinkList;
void menu3(struct student *p,char us[20]);
void menu1(struct student *p);
void menu2(struct student *p);
void menu33(struct student *p);
void menu22(struct student *p);
void savestudentFile(struct student *L);
void Look(struct student *p)
{
struct student *q=p->next;
char us[20];
int flag = 0,i;
system("cls");
printf("\t请输入你要打印的学生的账号:\n");
scanf("%s",us);
while (q!= NULL)
{
if(strcmp(q->user,us)==0)
{
printf("账号:%s,密码:%s,姓名:%s,楼号:%s,宿舍号:%s\n",q->user,q->mima,q->name,q->floor,q->dorm);
flag=1;
}
q = q->next;
}
if (flag == 0)
{
printf("\t无该学生信息\n");
}
printf("按回车返回菜单!\n");
getchar();
getchar();
menu2(p);
}
void ADD(struct student *p)
{
struct student *q, *n = p;
struct student *h = p->next;
int flag = 0;
system("cls");
q = (struct student *)malloc(sizeof(struct student));
q->next = NULL;
q->might1=0;
q->might2=2;
printf("请输入选择添加的学生:\n");
printf("账号 :");
scanf("%s",q->user);
printf("密码 :");
scanf("%s",q->mima);
printf("姓名 :");
scanf("%s", q->name);
printf("性别 :");
scanf("%s",q->sex);
printf("班级 :");
scanf("%s",q->classs);
printf("学号 :");
scanf("%s",q->num);
printf("楼号 :");
scanf("%s",q->floor);
printf("宿舍号 :");
scanf("%s",q->dorm);
getchar();
while (h != NULL)
{
if (strcmp(h->user,q->user)==0)
{
flag = 1;
printf("\t该学号已存在\n");
Sleep(1000);
ADD(p);
break;
}
h = h->next;
}
while (n->next != NULL)
{
n = n->next;
}
n->next=q;
if (flag == 0)
{
printf("\t添加成功\n");
printf("\t按任意键返回菜单\n");
}
else
printf("\t添加失败\n");
printf("按回车返回菜单!\n");
getchar();
menu2(p);
}
void Dele(struct student *p)
{
struct student *q = p->next;
struct student *n = p;
char us[20];
int flag = 0;
system("cls");
printf("\t请输入你要删除的学生学号\n");
scanf("%s", us);
while (q != NULL)
{
if (strcmp(q->user,us)==0)
{
flag = 1;
n->next = q->next;
break;
}
n = q;
q = q->next;
}
if (flag != 1)
{
printf("\t无该学生信息\n");
}
else
{
printf("\t删除成功\n");
}
printf("按回车返回菜单!\n");
getchar();
getchar();
menu2(p);
}
void Quan(struct student *p)
{
int i,m,j;
char us[20],mi[20],na[20],se[5],nu[20],cla[20],flo[20],dar[20];
struct student *q=p->next->next;
printf("请输入要修改的学生账户:");
scanf("%s",us);
while(q!=NULL)
{
if(strcmp(q->user,us)==0)
{
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("10.解冻账号\n");
printf("11.返回上一层\n");
scanf("%d",&i);
switch(i)
{
case 1:printf("新修改的姓名\n");scanf("%s",na);j=1;strcpy(q->name,na);break;
case 2:printf("新修改的性别\n");scanf("%s",se);j=1;strcpy(q->sex,se);break;
case 3:printf("新修改的学号\n");scanf("%s",nu);j=1;strcpy(q->num,nu);break;
case 4:printf("新修改的班级\n");scanf("%s",cla);j=1;strcpy(q->classs,cla);break;
case 5:printf("新修改的楼号\n");scanf("%s",flo);j=1;strcpy(q->floor,flo);break;
case 6:printf("新修改的宿舍号\n");scanf("%s",dar);j=1;strcpy(q->dorm,dar);break;
case 7:printf("新修改的账号\n");scanf("%s",us);j=1;strcpy(q->user,us);break;
case 8:printf("新修改的密码\n");scanf("%s",mi);j=1;strcpy(q->mima,mi);break;
case 9:j=1;q->might2=3;break;
case 10:j=1;q->might2=2;break;
case 11:menu2(p);break;
default:printf("请输入合法量!\n");break;
}
}
q=q->next;
}
if(j==1)
{
printf("修改成功!\n");
}
if(j==0)
{
printf("修改失败!\n");
}
printf("按回车返回菜单!\n");
getchar();
getchar();
menu2(p);
}
void Browse(struct student *p,char us[20])
{
struct student *q = p->next;
int flag = 0;
system("cls");
{
while (q != NULL)
{
if (strcmp(q->user,us)==0)
{
flag = 1;
printf("\t该学生信息 姓名:%s 学号:%d 性别:%s 班级:%s 楼号:%s 寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor,q->dorm);
}
q = q->next;
}
if (flag == 0)
{
printf("\t你无此权限\n");
}
printf("按回车返回菜单!\n");
getchar();
getchar();
menu3(p,us);
}
}
void modify(struct student *p,char us[20])
{
struct student *q=p->next;
char mi[20],na[20],se[5],nu[20],cla[20],flo[20],dar[20];
int j,i;
system("cls");
printf("\n\n\n**********修改学生住宿信息**********\n");
while(q!=NULL)
{
if(strcmp(q->user,us)==0)
{
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");
scanf("%d",&i);
switch(i)
{
case 1:printf("新修改的姓名\n");scanf("%s",na);j=1;strcpy(q->name,na);break;
case 2:printf("新修改的性别\n");scanf("%s",se);j=1;strcpy(q->sex,se);break;
case 3:printf("新修改的学号\n");scanf("%s",nu);j=1;strcpy(q->num,nu);break;
case 4:printf("新修改的班级\n");scanf("%s",cla);j=1;strcpy(q->classs,cla);break;
case 5:printf("新修改的楼号\n");scanf("%s",flo);j=1;strcpy(q->floor,flo);break;
case 6:printf("新修改的宿舍号\n");scanf("%s",dar);j=1;strcpy(q->dorm,dar);break;
case 7:printf("新修改的账号\n");scanf("%s",us);j=1;strcpy(q->user,us);break;
case 8:printf("新修改的密码\n");scanf("%s",mi);j=1;strcpy(q->mima,mi);break;
case 9:menu2(p);break;
default:printf("请输入合法量!\n");break;
}
}
q=q->next;
}
if(j==1)
{
printf("修改成功!\n");
}
if(j==0)
{
printf("修改失败!\n");
}
printf("按回车返回菜单!\n");
getchar();
getchar();
menu3(p,us);
}
void inquire(struct student *p,char us[20])
{
struct student *q = p->next;
char nu[20];
char name[20];
char flo[20];
char dor[20];
int k;
int flag=0;
system("cls");
printf("\n\n\t\t\t1.按学号查询\n");
printf("\t\t\t2.按姓名查询\n");
printf("\t\t\t3.按楼号统计\n");
printf("\t\t\t4.按楼号+寝室号查询\n");
printf("\t\t\t0.结束查询返回上一层\n");
printf("请选择:");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n\n\n**********按学号查询**********\n");
printf("\n 请输入要查询的学号:");
scanf("%s",nu);
while (q != NULL)
{
if (strcmp(q->num,nu)==0)
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 2:
printf("\n\n\n**********按姓名查询**********\n");
printf("\n 请输入要查询的姓名:");
scanf("%s",name);
while (q!=NULL)
{
if (strcmp(name,q->name)==0)
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 3:
printf("\n\n\n**********按楼号查询**********\n");
printf("\n 请输入要查询的楼号:");
scanf("%s",flo);
getchar();
while (q != NULL)
{
if (strcmp(flo,q->floor)==0)
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 4:
printf("\n\n\n**********按楼号+寝室号查询**********\n");
printf("\n 请输入要查询的楼号+寝室号:");
scanf("%s %s",flo,dor);
while (q != NULL)
{
if ((strcmp(flo,q->floor)==0)&&(strcmp(dor,q->dorm)==0))
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 0:menu3(p,us);break;
}
if(flag==0)
{
printf("无此信息!");
Sleep(1500);
menu3(p,us);
}
if(flag==1)
{
printf("查询成功!\n");
printf("按回车返回菜单!\n");
getchar();
menu3(p,us);
}
}
void statistics(struct student *p,char us[20])
{
struct student *q = p->next;
char cla[20];
char name[20];
char flo[20];
char dor[20];
int flag=0,k;
system("cls");
printf("\n\n\t\t\t1.按班级统计\n");
printf("\t\t\t2.按楼号统计\n");
printf("\t\t\t3.按楼号+寝室号统计\n");
printf("\t\t\t0.结束查询返回上一层\n");
printf("请选择:");
scanf("%d",&k);
switch(k)
{
case 1:
printf("\n\n\n**********按班级查询**********\n");
printf("\n 请输入要查询的班级:");
scanf("%s",cla);
while (q != NULL)
{
if (strcmp(q->classs,cla)==0)
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 2:
printf("\n\n\n**********按楼号查询**********\n");
printf("\n 请输入要查询的楼号:");
scanf("%s",flo);
while (q != NULL)
{
if (strcmp(flo,q->floor)==0)
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 3:
printf("\n\n\n**********按楼号+寝室号查询**********\n");
printf("\n 请输入要查询的楼号+寝室号:");
scanf("%s%s",flo,dor);
while (q != NULL)
{
if ((strcmp(flo,q->floor)==0)&&(strcmp(dor,q->dorm)==0))
{
flag=1;
printf("\t该学生信息\n姓名:%s \n学号:%d \n性别:%s \n班级:%s \n楼号:%s \n寝室号:%s\n", q->name, q->num, q->sex, q->classs, q->floor, q->dorm);
}
q = q->next;
}
break;
case 0:menu3(p,us);break;
}
if(flag==0)
{
printf("无此信息!\n");
Sleep(1500);
menu3(p,us);
}
if(flag==1)
{
printf("查询成功!");
printf("按回车返回菜单!\n");
getchar();
menu3(p,us);
}
}
void amend(LinkList *p)
{
struct student *q=p->next;
char acc[20],poss[20],ac[20];
int flag=0,i;
system("cls");
printf("请输入要修改的账号:");
scanf("%s",acc);
while(q!=NULL)
{
if(strcmp(q->user,acc)==0)
{
flag=1;
printf("1.仅修改密码\n2.密码账户均修改\n");
scanf("%d",&i);
if(i==1)
{
printf("请输入新密码:");
scanf("%s",poss);
strcpy(q->mima,poss);break;
}
if(i==2)
{
printf("请输入新账号:");
scanf("%s",ac);
getchar();
printf("请输入新密码:");
scanf("%s",poss);
strcpy(q->user,ac);
strcpy(q->mima,poss);break;
}
}
q=q->next;
}
if(flag==0)
{
printf("账户错误!\n");
printf("\t请按回车键返回菜单\n");
}
if(flag==1)
{
printf("修改成功!\n");
printf("\t请按回车键返回菜单\n");
}
getchar();
getchar();
menu2(p);
}
void menu33(struct student *p)
{
int i=0;
struct student *h=p->next->next;
struct student *t;
char a[20],b[20];
system("cls");
printf("请输入账户:");
scanf("%s",a);
getchar();
printf("请输入密码:");
scanf("%s",b);
getchar();
while(h!=NULL)
{
if((strcmp(h->user,a)==0)&&(strcmp(h->mima,b)==0))
{
t=h;
if(h->might2==2)
{
i=1;
menu3(p,a);break;
}
}
h=h->next;
}
if(t->might2==3)
{
printf("该账号已被冻结");
Sleep(2000);
menu1(p);
}
if(i==0)
{
printf("密码或账号错误!\n");
Sleep(2000);
menu1(p);
}
}
void menu3(struct student *p,char us[20])
{
int i;
system("cls");
while(1)
{
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(" *****************************************\n");
scanf("%d",&i);
switch(i)
{
case 1:Browse(p,us);break;//个人信息浏览
case 2:modify(p,us);break;//个人信息修改
case 3:inquire(p,us);break;//宿舍信息查询
case 4: statistics(p,us);break;//宿舍信息统计
case 5:menu1(p);break;
case 6:savestudentFile(p);exit(0);
default:printf("\t输入不合法量\n");
}
}
}
void menu22(struct student *p)
{
struct student *h=p->next;
char a[20],b[20];
int i=0;
system("cls");
printf("请输入账户:");
scanf("%s",a);
getchar();
printf("请输入密码:");
scanf("%s",b);
getchar();
while(h!=NULL)
{
if((strcmp(h->user,a)==0)&&(strcmp(h->mima,b)==0))
{
i=1;
menu2(p);break;
}
h=h->next;
}
if(i==0)
{
printf("密码或账号错误!\n");
Sleep(2000);
menu1(p);
}
}
void menu2(struct student *p)
{
int c;
system("cls");
while(1)
{
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(" ****************************************\n");
scanf("%d",&c);
switch(c)
{
case 1:Look(p);break;//用户资料打印
case 2:ADD(p);break;//添加用户
case 3:Dele(p);break;//删除用户
case 4:Quan(p);break;//修改用户权限
case 5:menu1(p);break;
case 6:amend(p);break;
case 7:savestudentFile(p);exit(0);
default:printf("请输入合法量!\n");
}
}
}
void menu1(struct student *p)
{
int a;
system("cls");
while(1)
{
printf(" *****************************************\n");
printf(" ***** *****\n");
printf(" ***** 欢迎使用宿舍管理系统 *****\n");
printf(" ***** *****\n");
printf(" ***** 1.管理员登陆 *****\n");
printf(" ***** *****\n");
printf(" ***** 2.用户登陆 *****\n");
printf(" ***** *****\n");
printf(" ***** *****\n");
printf(" *****************************************\n");
scanf("%d",&a);
switch(a)
{
case 1:menu22(p);break;
case 2:menu33(p);break;
case 3:exit(0);break;
default:printf("请输入合法量!\n");
}
}
}
void savestudentFile(struct student *L)
{
FILE *fp;
struct student *p;
p=L->next;
fp=fopen("dorm.txt","w");
if(fp==NULL)
{
printf("无法打开此文件\n");
exit(0);
}
while(p)
{
fprintf(fp,"%d %d %s %s %s %s %s %s %s %s \n",p->might1,p->might2,p->user,p->mima,p->name,p->sex,p->num,p->classs,p->floor,p->dorm);
p=p->next;
}
fclose(fp);
}
void ListInsert(struct student *L,struct student *n)
{
struct student *q=L;
n->next=NULL;
while(q->next!=NULL)
{
q=q->next;
}
q->next=n;
}
void readstudentput(struct student *L)
{
FILE *fp;
struct student *n;
struct student *m;
char us[20],mi[20],na[20],se[5],nu[20],cla[20],flo[20],dar[20],a[20],b[20];
int mi1,mi2;
fp=fopen("dorm.txt","r");
if(fp==NULL)
{
fp=fopen("dorm.txt","w");
exit(0);
}
else
{
if(fgetc(fp)==-1)
{
printf("此账号初次使用:\n请输入初始账号:");
scanf("%s",a);
getchar();
printf("请输入初始密码:");
scanf("%s", b);
getchar();
m=(struct student *)malloc(sizeof(LinkList));
strcpy(m->user,a);//初始账户
strcpy(m->mima,b);//初始密码
strcpy(m->name ,"0");
strcpy(m->sex ,"0");
strcpy(m->num ,"0");
strcpy(m->classs ,"0");
strcpy(m->floor ,"0");
strcpy(m->dorm,"0");
m->might1=1;
m->might2=0;
m->next=NULL;
L->next=m;
}
if(fgetc(fp)!=-1)
{
fseek(fp,-2L,1);
}
while(!feof(fp))
{
fscanf(fp,"%d %d %s %s %s %s %s %s %s %s ",&mi1,&mi2,us,mi,na,se,nu,cla,flo,dar);
n=(struct student *)malloc(sizeof(struct student));
strcpy(n->user,us);
strcpy(n->mima,mi);
strcpy(n->name ,na);
strcpy(n->sex ,se);
strcpy(n->num ,nu);
strcpy(n->classs ,cla);
strcpy(n->floor ,flo);
strcpy(n->dorm,dar);
n->might1=mi1;
n->might2=mi2;
ListInsert(L,n);
}
}
fclose(fp);
}
int main()
{
struct student *p;
p = (struct student *)malloc(sizeof(struct student));
p->next = NULL;
readstudentput(p);
menu1(p);
system("pause");
return 0;
}