#include<stdio.h>
#include<malloc.h>
#define N 10//房间等级
typedef struct Room
{
int roomlevel;
int roomnumber;
int peoplein;
int bnumber;
int bed[N];
int sex;
struct Room *next;
} Room;
struct Room*creat()
{
Room *head,*pl,*ql;
int i,m,n,h;
file://head=new(struct Room);
ql=head=(Room *)malloc(sizeof(Room));
file://q=head;
while (1)
{
printf("请输入房间等级 房间号 床位数:\n");
scanf("%d %d %d",&i,&m,&n);
file://p=new(struct Room);
pl=(Room *)malloc(sizeof(Room));
pl->roomlevel=i;
pl->roomnumber=m;
pl->bnumber=n;
pl->peoplein=0;
pl->sex=-1;//表示一个人也没有
for(h=0;h<n;h++) pl->bed[h]=0;//0表示该床未住人,1表示住人
ql->next=pl;
ql=pl;
if(i<0) break;
}
pl->next=NULL;
return(head);
}
void chushihua(struct Room *head)
file://建立房间的链表
{ struct Room *p;
int i;
p=head;
while (p!=NULL)
{
p->peoplein=0;
p->sex=-1;
for(i=0;i<p->bnumber;i++) p->bed[i]=0;
p=p->next;
}
printf("操作成功!");
}
void dingfang(struct Room *head)
{
struct Room *p;
int i,number=0,bednumber=0,s,level;
printf("性别(1为男,0为女):");
scanf("%d",&s);
printf("房间等级:");
scanf("%d",&level);
p=head;
while (p!=NULL)
{
if ((p->roomlevel==level)&&((p->sex==s)||(p->sex==-1)))
{
for(i=0;i<p->bnumber;i++)
if (p->bed[i]==0)
{
number=p->roomnumber;
bednumber=i+1;
p->bed[i]=1;
if(p->bed[i]==1)
p->peoplein++;
break;
}
if (number!=0) break;
}
p=p->next;
}
printf("房间号:%d 床号:%d 已住人: %d\n",number,bednumber,p->bnumber);
}
void tuifang(struct Room *head)
{
struct Room *p;
int number,bednumber,i;
printf("请输入房间号:");scanf("%d",&number);
printf("请输入床号:");scanf("%d",&bednumber);
p=head;
while (p!=NULL)
{
if (p->roomnumber==number)
for (i=0;i<p->roomlevel;i++) if (i+1==bednumber) {p->bed[i]=0;break;}
p=p->next;
}
printf("操作成功!\n");
}
void print(struct Room *head)
{
struct Room *p;
int i;
p=head;
while (p!=NULL)
{
printf("房间号:%4d,房间等级:%3d,已住人数:%3d,住人性别:%d",p->roomnumber,
p->roomlevel,p->peoplein,p->sex);
for(i=0;i<p->roomlevel;i++) if (p->bed[i]==1)
printf("已住人的床号:%3d",i+1);
printf("\n");
p=p->next;
}
}
void main()
{ int funcflag;
struct Room *head;
head=creat();
do{
printf("funcflag 0:对房间初始化\n");
printf("funcflag 1:订房\n");
printf("funcflag 2:退房\n");
printf("funcflag 3:显示房间分配情况\n");
printf("funcflag 4:退出系统\n");
printf("Please input the funcflag:");
scanf("%d",&funcflag);
lp:if (funcflag!=4) switch(funcflag)
{
case 0:printf("初始化将注销所有的信息,确定初始化请再次输入0:");
scanf("%d",&funcflag);
if (funcflag!=0) goto lp;
chushihua(head);break;
case 1:dingfang(head);break;
case 2:tuifang(head);break;
case 3:print(head);break;
default :printf("输入错误!从新输入:");break;
}
else break;
}while(1);
}
#include<stdio.h>
#include<malloc.h>
#define N 10//房间等级
typedef struct Room
{
int roomlevel;
int roomnumber;
int peoplein;
int bnumber;
int bed[N];
int sex;
struct Room *next;
} Room;
struct Room*creat()
{
Room *head,*pl,*ql;
int i,m,n,h;
file://head=new(struct Room);
ql=head=(Room *)malloc(sizeof(Room));
file://q=head;
while (1)
{
printf("请输入房间等级 房间号 床位数:\n");
scanf("%d %d %d",&i,&m,&n);
file://p=new(struct Room);
pl=(Room *)malloc(sizeof(Room));
pl->roomlevel=i;
pl->roomnumber=m;
pl->bnumber=n;
pl->peoplein=0;
pl->sex=-1;//表示一个人也没有
for(h=0;h<n;h++) pl->bed[h]=0;//0表示该床未住人,1表示住人
ql->next=pl;
ql=pl;
if(i<0) break;
}
pl->next=NULL;
return(head);
}
void chushihua(struct Room *head)
file://建立房间的链表
{ struct Room *p;
int i;
p=head;
while (p!=NULL)
{
p->peoplein=0;
p->sex=-1;
for(i=0;i<p->bnumber;i++) p->bed[i]=0;
p=p->next;
}
printf("操作成功!");
}
void dingfang(struct Room *head)
{
struct Room *p;
int i,number=0,bednumber=0,s,level;
printf("性别(1为男,0为女):");
scanf("%d",&s);
printf("房间等级:");
scanf("%d",&level);
p=head;
while (p!=NULL)
{
if ((p->roomlevel==level)&&((p->sex==s)||(p->sex==-1)))
{
for(i=0;i<p->bnumber;i++)
if (p->bed[i]==0)
{
number=p->roomnumber;
bednumber=i+1;
p->bed[i]=1;
if(p->bed[i]==1)
p->peoplein++;
break;
}
if (number!=0) break;
}
p=p->next;
}
printf("房间号:%d 床号:%d 已住人: %d\n",number,bednumber,p->bnumber);
}
void tuifang(struct Room *head)
{
struct Room *p;
int number,bednumber,i;
printf("请输入房间号:");scanf("%d",&number);
printf("请输入床号:");scanf("%d",&bednumber);
p=head;
while (p!=NULL)
{
if (p->roomnumber==number)
for (i=0;i<p->roomlevel;i++) if (i+1==bednumber) {p->bed[i]=0;break;}
p=p->next;
}
printf("操作成功!\n");
}
void print(struct Room *head)
{
struct Room *p;
int i;
p=head;
while (p!=NULL)
{
printf("房间号:%4d,房间等级:%3d,已住人数:%3d,住人性别:%d",p->roomnumber,
p->roomlevel,p->peoplein,p->sex);
for(i=0;i<p->roomlevel;i++) if (p->bed[i]==1)
printf("已住人的床号:%3d",i+1);
printf("\n");
p=p->next;
}
}
void main()
{ int funcflag;
struct Room *head;
head=creat();
do{
printf("funcflag 0:对房间初始化\n");
printf("funcflag 1:订房\n");
printf("funcflag 2:退房\n");
printf("funcflag 3:显示房间分配情况\n");
printf("funcflag 4:退出系统\n");
printf("Please input the funcflag:");
scanf("%d",&funcflag);
lp:if (funcflag!=4) switch(funcflag)
{
case 0:printf("初始化将注销所有的信息,确定初始化请再次输入0:");
scanf("%d",&funcflag);
if (funcflag!=0) goto lp;
chushihua(head);break;
case 1:dingfang(head);break;
case 2:tuifang(head);break;
case 3:print(head);break;
default :printf("输入错误!从新输入:");break;
}
else break;
}while(1);
}
}
不知道为什么不可以调用函数creat();那位高手知道的帮忙改一下!小妹在此先说谢谢了