下面是我用c语言在linux环境下写的一个学生宿舍管理系统,数据结构是 用链表。不完整,只有输入学生信息和 计算机给学生自动分配宿舍,没有考虑男女宿舍,我 是 这样想的:
对每栋宿舍都作标记,0表示女生宿舍,1表示男生宿舍,但我又是以一个宿舍作为链表的一个节点的.所以实现起来比较难啊。
有人能帮我想想在 不改变数据结构的 情况下有什么好 的 办法没有?
/*******头文件*****/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*定义数据结构类型*/
typedef struct stud{
char id[9];
char name[16];
char sex[2];
int age;
char department[12];
char speciality[12];
int grade;
int _class;
int b_no;
int r_no;
int bed;
struct stud *next;
}STUD;
typedef struct{
STUD *head, *tail;
int len;
} SList;
typedef struct room{
int b_no;
int r_no;
char beds[6][8];
struct room *next;
}ROOM;
typedef struct{
ROOM *head, *tail;
int len;
}RList;
void init(RList *L, int b, int f, int r, int d) /*初始化宿舍为空*/
{
int i, j, k, t;
ROOM *p, *q;
p = q = (ROOM *)malloc(sizeof(ROOM));
L->head = p;
q->next = NULL;
for(i = 1; i <= b; i ++ ){
for(j = 1; j <= f; j++ ){
for(k = 1; k <= r; k++ ){
p = (ROOM *)malloc(sizeof(ROOM));
for(t = 0; t < d; t++ ){
p->b_no = i;
p->r_no = j*100 + k;
strcpy(p->beds[t], "0");
}
p->next = q->next;
q->next = p;
q = p;
}
}
}
L->len = b*f*r;
}
int s_input(SList *L) /*从终端读取学生信息*/
{
STUD *p, *q;
int n = 0;
q = p = (STUD *)malloc(sizeof(STUD));
system("clear");
printf("/t/nPlease input student ID, when ID ='#' end input :");
scanf("%s", p->id);
if(p->id[0] == '#')
return 0;
printf("name: ");
scanf("%s", p->name);
printf("sex(M/F): ");
scanf("%s", p->sex);
printf("age: ");
scanf("%d", &p->age);
printf("department: ");
scanf("%s", p->department);
printf("speciality: ");
scanf("%s", p->speciality);
printf("grade: ");
scanf("%d", &p->grade);
printf("class: ");
scanf("%d", &p->_class);
L->head = p;
q->next = NULL;
n++;
p = (STUD *)malloc(sizeof(STUD));
while(1){
printf("/t/nPlease input student ID, when ID ='#' end input :");
scanf("%s", p->id);
if(p->id[0] == '#')
break;;
printf("name: ");
scanf("%s", p->name);
printf("sex(M/F): ");
scanf("%s", p->sex);
printf("age: ");
scanf("%d", &p->age);
printf("department: ");
scanf("%s", p->department);
printf("speciality: ");
scanf("%s", p->speciality);
printf("grade: ");
scanf("%d", &p->grade);
printf("class: ");
scanf("%d", &p->_class);
p->next = q->next;
q->next = p;
q = p;
p = (STUD *)malloc(sizeof(STUD));
n++;
}
L->len = n;
return n;
}
/*计算机自动分配宿舍*/
void admeasure(RList *R, SList *S, int N)
{
STUD *p;
ROOM *q;
int i=0;
p = S->head;
q = R->head->next;
while(p != NULL && q != NULL){
while(i < N){
if(strcmp(q->beds[i] , "0") == 0){
p->b_no = q->b_no;
p->r_no = q->r_no;
strcpy(q->beds[i], p->id);
p->bed = i;
p = p->next;
}
if(p == NULL)
break;
i++;
}
i = 0;
q = q->next;
}
if(p != NULL && q == NULL)
printf("/nToo many students and too few room");
}
int search(SList *S, char *c, int flag)
{
STUD *p;
int n = 0;
p = S->head;
while(p != NULL){
if(strcmp(p->id, c) != 0){
p = p->next;
}
else{
printf("/n%-12s%-17s%4s%3d%12s%12s%3d",
p->id, p->name, p->sex, p->age, p->department, p->speciality, p->bed);
if(flag == 1)
p = p->next;
n = 1;
}
}
return n;
}
void output(RList *R, SList *S, int N)
{
ROOM *p;
int i;
p = R->head->next;
printf("===================== THE OUTPUT =========================");
while(p != NULL ){
printf("/nBUILD NO:%d ROOM NO:%d ", p->b_no, p->r_no);
printf("/n====ID==========name=======sex==age==department==speciality==bed===");
for(i = 0; i < N; i++){
if( 0 == search(S, p->beds[i], 1))
printf("/n--------------------------------------------------------------------");
}
getchar();
p = p->next;
}
}
int main(void)
{
SList *S=(SList *)malloc(sizeof(SList));
RList *R=(RList *)malloc(sizeof(RList));
char c, ch='*';
FILE *fp;
struct abc{
int b_no;
int f_no;
int r_no;
int num;
}ss;
system("clear");
printf("/t/t/n/nTHIS IS A MIB OF SCHOOL STUDENTS' ROOM/n");
printf("============================================================/n");
if( (fp = fopen("abc", "wt")) == NULL){
printf("/ncan not open file press any key to exit....");
getchar();
exit(0);
}
printf("/nIs the frist time of you use this MIB(y/n)/n");
c = getchar();
if(c == 'y'){
printf("/nEnter the number of build:");
scanf("%d", &ss.b_no);
printf("/nEnter the number of floor:");
scanf("%d", &ss.f_no);
printf("/nEnter the number of room:");
scanf("%d", &ss.r_no);
printf("/nEnter the number of students a room can have:");
scanf("%d", &ss.num);
}
if(fwrite(&ss, sizeof(struct abc), 1, fp) != 1){
printf("write file error press any key to exit....");
getchar();
exit(0);
}
init(R, ss.b_no, ss.f_no, ss.r_no, ss.num);
printf("/n/t/tPress any key to the main menu/n");
getchar();
do{
system("clear");
printf("/n=========================================================================/n");
printf("/n/t1: input students' information/n");
printf("/n/t8: output dormitory and students' information/n");
printf("/n/t0: exit this MIB/n ");
ch = getchar();
switch(ch){
case '1': s_input(S);
admeasure(R, S, ss.num);
break;
case '8': output(R, S, ss.num);
break;
case '0': exit(0);
}
}while(1);
return 0;
}
/*******头文件*****/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*定义数据结构类型*/
typedef struct stud{
char id[9];
char name[16];
char sex[2];
int age;
char department[12];
char speciality[12];
int grade;
int _class;
int b_no;
int r_no;
int bed;
struct stud *next;
}STUD;
typedef struct{
STUD *head, *tail;
int len;
} SList;
typedef struct room{
int b_no;
int r_no;
char beds[6][8];
struct room *next;
}ROOM;
typedef struct{
ROOM *head, *tail;
int len;
}RList;
void init(RList *L, int b, int f, int r, int d) /*初始化宿舍为空*/
{
int i, j, k, t;
ROOM *p, *q;
p = q = (ROOM *)malloc(sizeof(ROOM));
L->head = p;
q->next = NULL;
for(i = 1; i <= b; i ++ ){
for(j = 1; j <= f; j++ ){
for(k = 1; k <= r; k++ ){
p = (ROOM *)malloc(sizeof(ROOM));
for(t = 0; t < d; t++ ){
p->b_no = i;
p->r_no = j*100 + k;
strcpy(p->beds[t], "0");
}
p->next = q->next;
q->next = p;
q = p;
}
}
}
L->len = b*f*r;
}
int s_input(SList *L) /*从终端读取学生信息*/
{
STUD *p, *q;
int n = 0;
q = p = (STUD *)malloc(sizeof(STUD));
system("clear");
printf("/t/nPlease input student ID, when ID ='#' end input :");
scanf("%s", p->id);
if(p->id[0] == '#')
return 0;
printf("name: ");
scanf("%s", p->name);
printf("sex(M/F): ");
scanf("%s", p->sex);
printf("age: ");
scanf("%d", &p->age);
printf("department: ");
scanf("%s", p->department);
printf("speciality: ");
scanf("%s", p->speciality);
printf("grade: ");
scanf("%d", &p->grade);
printf("class: ");
scanf("%d", &p->_class);
L->head = p;
q->next = NULL;
n++;
p = (STUD *)malloc(sizeof(STUD));
while(1){
printf("/t/nPlease input student ID, when ID ='#' end input :");
scanf("%s", p->id);
if(p->id[0] == '#')
break;;
printf("name: ");
scanf("%s", p->name);
printf("sex(M/F): ");
scanf("%s", p->sex);
printf("age: ");
scanf("%d", &p->age);
printf("department: ");
scanf("%s", p->department);
printf("speciality: ");
scanf("%s", p->speciality);
printf("grade: ");
scanf("%d", &p->grade);
printf("class: ");
scanf("%d", &p->_class);
p->next = q->next;
q->next = p;
q = p;
p = (STUD *)malloc(sizeof(STUD));
n++;
}
L->len = n;
return n;
}
/*计算机自动分配宿舍*/
void admeasure(RList *R, SList *S, int N)
{
STUD *p;
ROOM *q;
int i=0;
p = S->head;
q = R->head->next;
while(p != NULL && q != NULL){
while(i < N){
if(strcmp(q->beds[i] , "0") == 0){
p->b_no = q->b_no;
p->r_no = q->r_no;
strcpy(q->beds[i], p->id);
p->bed = i;
p = p->next;
}
if(p == NULL)
break;
i++;
}
i = 0;
q = q->next;
}
if(p != NULL && q == NULL)
printf("/nToo many students and too few room");
}
int search(SList *S, char *c, int flag)
{
STUD *p;
int n = 0;
p = S->head;
while(p != NULL){
if(strcmp(p->id, c) != 0){
p = p->next;
}
else{
printf("/n%-12s%-17s%4s%3d%12s%12s%3d",
p->id, p->name, p->sex, p->age, p->department, p->speciality, p->bed);
if(flag == 1)
p = p->next;
n = 1;
}
}
return n;
}
void output(RList *R, SList *S, int N)
{
ROOM *p;
int i;
p = R->head->next;
printf("===================== THE OUTPUT =========================");
while(p != NULL ){
printf("/nBUILD NO:%d ROOM NO:%d ", p->b_no, p->r_no);
printf("/n====ID==========name=======sex==age==department==speciality==bed===");
for(i = 0; i < N; i++){
if( 0 == search(S, p->beds[i], 1))
printf("/n--------------------------------------------------------------------");
}
getchar();
p = p->next;
}
}
int main(void)
{
SList *S=(SList *)malloc(sizeof(SList));
RList *R=(RList *)malloc(sizeof(RList));
char c, ch='*';
FILE *fp;
struct abc{
int b_no;
int f_no;
int r_no;
int num;
}ss;
system("clear");
printf("/t/t/n/nTHIS IS A MIB OF SCHOOL STUDENTS' ROOM/n");
printf("============================================================/n");
if( (fp = fopen("abc", "wt")) == NULL){
printf("/ncan not open file press any key to exit....");
getchar();
exit(0);
}
printf("/nIs the frist time of you use this MIB(y/n)/n");
c = getchar();
if(c == 'y'){
printf("/nEnter the number of build:");
scanf("%d", &ss.b_no);
printf("/nEnter the number of floor:");
scanf("%d", &ss.f_no);
printf("/nEnter the number of room:");
scanf("%d", &ss.r_no);
printf("/nEnter the number of students a room can have:");
scanf("%d", &ss.num);
}
if(fwrite(&ss, sizeof(struct abc), 1, fp) != 1){
printf("write file error press any key to exit....");
getchar();
exit(0);
}
init(R, ss.b_no, ss.f_no, ss.r_no, ss.num);
printf("/n/t/tPress any key to the main menu/n");
getchar();
do{
system("clear");
printf("/n=========================================================================/n");
printf("/n/t1: input students' information/n");
printf("/n/t8: output dormitory and students' information/n");
printf("/n/t0: exit this MIB/n ");
ch = getchar();
switch(ch){
case '1': s_input(S);
admeasure(R, S, ss.num);
break;
case '8': output(R, S, ss.num);
break;
case '0': exit(0);
}
}while(1);
return 0;
}