學生宿舍管理系統


運用了結構體,文件指針。其實寫起來不算難,主要是文件的指針很少用到,所以花了很長時間不斷的調試。最終的程序還是有一點BUG的,比如雙人互換宿舍必須在單人調換宿舍前面使用,不然后面的讀取數據會出錯,還有互換宿舍時A同學的記錄必須在B同學的前面,否則會出錯。這都是文件指針的問題,還有待改善。盡管花了很長時間,老師並不認可這樣的作業,因為沒有可視化界面,他說,一個程序員必須考慮用戶使用的感受,所以只要是沒有可視化界面的,無論你寫的多么好,都不會得到好的分數。

#include<stdio.h>
#include
<stdlib.h>
#include
<string.h>

typedef
struct{
char ID[20]; //學號
char name[20]; //姓名
char gender[10]; //性別
char dormitory[4]; //宿舍號
}Student;
typedef
struct{
char ID[4]; //宿舍號
int max_num; //可容納人數
int current_num; //已入住人數
char gender[10]; //男舍或女舍
}Dormitory;

char filename[20];
FILE
*fp; //學生信息文件指針
FILE *_fp; //宿舍信息文件指針

void create(){
printf(
"\n請輸入新增樓棟名稱:\n");
scanf(
"%s",filename);
_fp
=fopen(filename,"rb+");
if( _fp==NULL){ //如果文件不存在則建立文件否則提示文件已存在
_fp=fopen(filename,"wb+");
fp
=fopen(strcat( filename, "學生"),"wb+");
if(fp==NULL || _fp==NULL){
printf(
"File not open!\n");
exit(
-1);
}
}
else
printf(
"文件已存在!\n");
fclose(fp);
fclose(_fp);
}

void open(){
printf(
"請輸入學生宿舍所在樓棟:\n");
scanf(
"%s",filename);
_fp
=fopen(filename,"rb+");
fp
=fopen(strcat( filename, "學生"),"rb+");
if(fp==NULL || _fp==NULL){
printf(
"File not open!\n");
exit(
-1);
}
}

void insert_dorm(){
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入宿舍號,可容納學生數,已容納學生數,男舍/女舍(空格隔開,‘#’結束):\n");
scanf(
"%s",d->ID);
while(strcmp(d->ID,"#")){
scanf(
"%d %d %s",&d->max_num,&d->current_num,d->gender);
fwrite(d,
sizeof(Dormitory),1,_fp);
scanf(
"%s",d->ID);
}
fclose(_fp);
fclose(fp);
}

void insert_stu(){
long locate,pre_locate;
int flag=0;
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));

open();
printf(
"\n請輸入學號、姓名、性別、宿舍號(空格隔開,‘#’結束):\n");
scanf(
"%s",s->ID);
while(strcmp(s->ID,"#")){ //不相等繼續讀取,相等為0便退出循環不再讀取數據
scanf("%s %s %s",s->name,s->gender,s->dormitory);
locate
=ftell(_fp);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(d->ID,s->dormitory)){ //每增加一個學生,相應寢室的實際人數應當增加一個
fseek(_fp,locate,SEEK_SET);
d
->current_num++;
fwrite(d,
sizeof(Dormitory),1,_fp);
flag
=1;
break;
}
locate
=ftell(_fp);
}
if(flag==0){
printf(
"不存在該宿舍!\n");
exit(
-1);
}
fwrite(s,
sizeof(Student),1,fp);
rewind(_fp);
scanf(
"%s",s->ID);
}
fclose(_fp);
fclose(fp);
}

void dormnumsearch(){
int flag=0,tag=0;
char input_dormnum[4];
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"請輸入需要搜索的宿舍號:\n");
scanf(
"%s",input_dormnum);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(input_dormnum,d->ID)){
flag
=1;
printf(
"\n寢室信息如下:\n%-20s%-20s%-20s%-20s\n","宿舍號","可容納學生數","已容納學生數","男舍/女舍");
printf(
"%-20s%-20d%-20d%-20s\n",d->ID,d->max_num,d->current_num,d->gender);
break;
}
}
if(flag){
printf(
"該宿舍學生信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
while(fread(s,sizeof(Student),1,fp)){
if(!strcmp(input_dormnum,s->dormitory)){
tag
=1;
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
}
}
if(tag==0)
printf(
"\n該宿舍沒有學生入住!\n");
}
else
printf(
"不存在該宿舍記錄!!\n");
fclose(_fp);
fclose(fp);
}

void change(){
int flag=0,tag=0;
long locate,locate_b,locate_c;
char input_stunum[20];
char input_dormnum[4];
char previous_dormnum[4];
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入需要調換宿舍學生學號:\n");
scanf(
"%s",input_stunum);

locate
=ftell(fp);
while(fread(s,sizeof(Student),1,fp)){
if(!strcmp(input_stunum,s->ID)){
flag
=1;
printf(
"該學生信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
strcmp(previous_dormnum,s
->dormitory);
printf(
"\n請輸入新宿舍的宿舍號:\n");
scanf(
"%s",input_dormnum);
locate_b
=ftell(_fp);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(input_dormnum,d->ID)){
tag
=1;
if(d->current_num==d->max_num)
printf(
"該宿舍已滿員!");
else{
//更換學生宿舍信息
fseek(fp,locate,SEEK_SET);
strcpy(s
->dormitory,input_dormnum);
fwrite(s,
sizeof(Student),1,fp);

//更換新宿舍信息
fseek(_fp,locate_b,SEEK_SET);
d
->current_num++;
fwrite(d,
sizeof(Dormitory),1,_fp);
rewind(_fp);

//更換舊宿舍信息
locate_c=ftell(_fp);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(previous_dormnum,d->ID)){
fseek(_fp,locate_c,SEEK_SET);
d
->current_num--;
fwrite(d,
sizeof(Dormitory),1,_fp);
break;
}
locate_c
=ftell(_fp);
}
}
break;
}
locate_b
=ftell(_fp);
}
if(tag==0){
printf(
"不存在該宿舍!");
exit(
-1);
}
break;
}
locate
=ftell(fp);
}
if(flag==0)
printf(
"\n不存在該學生!\n");
fclose(_fp);
fclose(fp);
}

void namesearch(){
int flag=0,tag=0;
long locate;
char input_name[20];
char dor_num[4];
char ID[20];

Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入需要搜索的姓名:\n");
scanf(
"%s",input_name);


while(fread(s,sizeof(Student),1,fp)){
locate
=ftell(fp); //記錄下按名字搜索到達的位置
if(!strcmp(input_name,s->name)){
flag
=1;
printf(
"\n學生信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
strcpy(dor_num,s
->dormitory);
strcpy(ID,s
->ID);
rewind(fp);
//指針重置於文件頭,查找室友
printf("\n其室友信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");

while(fread(s,sizeof(Student),1,fp)){ //室友宿舍號相同,學號不同
if( (!strcmp(dor_num,s->dormitory)) && (strcmp(s->ID,ID)) ){
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
tag
=1;
}
}
if(tag==0)
printf(
"\n沒有室友!\n");
tag
=0;
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(dor_num,d->ID)){
printf(
"\n該學生所在寢室信息如下:\n%-20s%-20s%-20s%-20s\n","宿舍號","可容納學生數","已容納學生數","男舍/女舍");
printf(
"%-20s%-20d%-20d%-20s\n",d->ID,d->max_num,d->current_num,d->gender);
break;
}
}
//break;
}
//同名情況
if(flag)
fseek(fp,locate,SEEK_SET);

}
if(flag==0)
printf(
"\n沒有符合條件的學生的記錄!\n");
fclose(_fp);
fclose(fp);
}

void check_out(){
char stu_num[20];
char dorm_num[20];
long locate,locate_b;
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入退宿學生學號:\n");
scanf(
"%s",stu_num);

locate
=ftell(fp);
while(fread(s,sizeof(Student),1,fp)){
// locate=ftell(fp);
if(!strcmp(s->ID,stu_num)){
strcpy(dorm_num,s
->dormitory);
fseek(fp,locate,SEEK_SET);
memset(s,
0,sizeof(Student));
fwrite(s,
sizeof(Student),1,fp);
break;
}
locate
=ftell(fp);
}
locate_b
=ftell(_fp);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(d->ID,dorm_num)){
fseek(_fp,locate_b,SEEK_SET);
d
->current_num--;
//printf("111\n");
//printf("%d\n",d->current_num);
fwrite(d,sizeof(Dormitory),1,_fp);
printf(
"刪除成功!\n");
break;
}
locate_b
=ftell(_fp);
}
fclose(_fp);
fclose(fp);
}

void numsearch(){
int flag=0,tag=0;
char input_num[20];
char dor_num[4];
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入需要搜索的學號:\n");
scanf(
"%s",input_num);
while(fread(s,sizeof(Student),1,fp)){
if(!strcmp(input_num,s->ID)){
flag
=1;
printf(
"\n該學生信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
break;
}
}
strcpy(dor_num,s
->dormitory);
if(flag){
rewind(fp);
printf(
"\n室友信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
while(fread(s,sizeof(Student),1,fp)){
if( (!strcmp(dor_num,s->dormitory)) && strcmp(input_num,s->ID) ){
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
tag
=1;
}
}
if(tag==0)
printf(
"\n沒有室友!\n");
while(fread(d,sizeof(Dormitory),1,_fp)){
if(!strcmp(dor_num,d->ID)){
printf(
"\n該學生所在寢室信息如下:\n%-20s%-20s%-20s%-20s\n","宿舍號","可容納學生數","已容納學生數","男舍/女舍");
printf(
"%-20s%-20d%-20d%-20s\n",d->ID,d->max_num,d->current_num,d->gender);
break;
}
}
}
else
printf(
"\n沒有該學生的記錄!\n");
fclose(_fp);
fclose(fp);
}

void exchange(){
int flag=0,tag=0;
long locate_a,locate_b;
char input_ID_a[20];
char input_ID_b[20];
char dor_num[4];
Student
*s=(Student*)malloc(sizeof(Student));
Student
*_s=(Student*)malloc(sizeof(Student));
open();
printf(
"\n請輸入需要互換宿舍學生A的學號:\n");
scanf(
"%s",input_ID_a);
printf(
"\n請輸入需要互換宿舍學生B的學號:\n");
scanf(
"%s",input_ID_b);

locate_a
=ftell(fp);
while(fread(s,sizeof(Student),1,fp)){
if(!strcmp(input_ID_a,s->ID)){
printf(
"\n學生A信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
flag
=1;
break;
}
locate_a
=ftell(fp);
}
rewind(fp);
locate_b
=ftell(fp);
while(fread(_s,sizeof(Student),1,fp)){
if(!strcmp(input_ID_b,_s->ID)){
printf(
"\n學生B信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",_s->ID,_s->name,_s->gender,_s->dormitory);
tag
=1;
break;
}
locate_b
=ftell(fp);
}


if(flag==0 || tag==0){
printf(
"不存在A同學或B同學記錄!");
exit(
-1);
}
else{
strcpy(dor_num,s
->dormitory);
strcpy(s
->dormitory,_s->dormitory);
strcpy(_s
->dormitory,dor_num);
//locate_a=locate_a-sizeof(Student);
fseek(fp,locate_a,0);
fwrite(s,
sizeof(Student),1,fp);
// locate_b=locate_b-sizeof(Student);
fseek(fp,locate_b,0);
fwrite(_s,
sizeof(Student),1,fp);
}
printf(
"更換宿舍后:\n");
rewind(fp);

locate_a
=ftell(fp);
while(fread(s,sizeof(Student),1,fp)){
if(!strcmp(input_ID_a,s->ID)){
printf(
"\n學生A信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",s->ID,s->name,s->gender,s->dormitory);
break;
}
locate_a
=ftell(fp);
}

locate_b
=ftell(fp);
while(fread(_s,sizeof(Student),1,fp)){
if(!strcmp(input_ID_b,_s->ID)){
printf(
"\n學生B信息如下:\n%-20s%-20s%-20s%-20s\n","學號","姓名","性別","宿舍號");
printf(
"%-20s%-20s%-20s%-20s\n",_s->ID,_s->name,_s->gender,_s->dormitory);
break;
}
locate_b
=ftell(fp);
}

fclose(_fp);
fclose(fp);
}

void display(){
long locate;
Student
*s=(Student*)malloc(sizeof(Student));
Dormitory
* d=(Dormitory*)malloc(sizeof(Dormitory));
open();
printf(
"\n請輸入學號、姓名、性別:\n");
scanf(
"%s %s %s",s->ID,s->name,s->gender);

locate
=ftell(_fp);
while(fread(d,sizeof(Dormitory),1,_fp)){
if(d->max_num=d->current_num){
strcpy(s
->dormitory,d->ID);
fwrite(s,
sizeof(Student),1,fp);
fseek(_fp,locate,
0);
d
->current_num++;
fwrite(d,
sizeof(Dormitory),1,_fp);
printf(
"分配的宿舍是:");
printf(
"%s\n",s->dormitory);
break;
}
locate
=ftell(fp);
}
fclose(_fp);
fclose(fp);
}


int main(){
int select,flag=1; //flag為0則退出程序
while(flag){
printf(
"\t\t------------------------------------------------------\n");
printf(
"\t\t\t 學生宿舍管理系統\n\n");
printf(
"\t\t\t 0 ---------------- 新增樓棟\n");
printf(
"\t\t\t 1 ---------------- 新增宿舍信息\n");
printf(
"\t\t\t 2 ---------------- 新增學生住宿信息\n");
printf(
"\t\t\t 3 ---------------- 互換宿舍\n");
printf(
"\t\t\t 4 ---------------- 調換宿舍\n");
printf(
"\t\t\t 5 ---------------- 學生畢業退宿\n");
printf(
"\t\t\t 6 ---------------- 智能分配宿舍\n");
printf(
"\t\t\t 7 ---------------- 學號查詢\n");
printf(
"\t\t\t 8 ---------------- 姓名查詢\n");
printf(
"\t\t\t 9 ---------------- 房號查詢\n");
printf(
"\t\t\t 10 ---------------- 退出系統\n");
printf(
"\t\t------------------------------------------------------\n");
printf(
"請輸入你的操作編號(0~10):");
scanf(
"%d",&select);
switch (select){
case 0:
create();
break;
case 1:
insert_dorm();
break;
case 2:
insert_stu();
break;
case 3:
exchange();
break;
case 4:
change();
break;
case 5:
check_out();
break;
case 6:
display();
break;
case 7:
numsearch();
break;
case 8:
namesearch();
break;
case 9:
dormnumsearch();
break;
case 10:
return 0;
defaut:
break;
}
}
return 0;
}

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com