写了一个课设,但是查询功能有BUG,希望大佬可以帮助一下。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define M 80
//客户结构体
typedef struct client
{
char name[12]; //姓名
int sex; //性别
int age; //年龄
char id[20]; //身份证号
int year; //入住时间
int month;
int date;
}CLIENT;
//客房结构体
struct Hotel
{
int roomid;
int tel; //用户的话
int price; //价格
int sign; //默认标记为空房间
CLIENT client_list; //客户信息
}room[M];
void Menu(); //目录界面
void dengji(struct Hotel room[]);
void savefile(struct Hotel room[],int i);
int readfile(struct Hotel room[]);
void tongji(struct Hotel room[]);
void chaxun(struct Hotel room[]);
void printmessage(struct Hotel room[],int n);
void xiugai(struct Hotel room[]);
//主函数
int main()
{
int n;
char reply;
Menu();
do{
printf("请输入选项对应的数字:\n");
scanf("%d",&n);
while(n!=1&&n!=2&&n!=3&&n!=4)
{
printf("请输入各选项对应的数字!\n");
scanf("%d",&n);
}
switch (n)
{
case 1:
dengji(room);
break;
case 2:
tongji(room);
break;
case 3:
chaxun(room);
break;
case 4:
xiugai(room);
break;
}
printf("是否还要进入其它选项?(回复Y或N继续)\n");
scanf(" %c",&reply);
}while (reply=='Y');
return 0;
}
//系统目录
void Menu()
{
printf("************************客房管理系统***************************\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("***************************************************************\n");
}
//登记客户信息
void dengji(struct Hotel room[])
{
int x,j,i,flag;
char answer;
do
{
i=readfile(room);
do
{
flag=0;
printf("输入您想入住的房间号:(三位数)\n");
scanf("%3d",&x);
for(j=0;j<i;j++)
{
if(room[j].roomid==x)
{
printf("该房间已经被占用!\n");
flag=1;
}
}
}while(flag>0);
room[i+1].roomid=x;
printf("请输入您的电话:\n");
scanf("%d",&room[i+1].tel);
printf("入住价格\n");
scanf("%d",&room[i+1].price);
printf("请输入您的名字\n");
scanf("%s",room[i+1].client_list.name);
printf("性别:(1表示女,0表示男)\n");
scanf("%d",&room[i+1].client_list.sex);
printf("您的年龄:\n");
scanf("%d",&room[i+1].client_list.age);
printf("您的id:\n");
scanf("%s",room[i+1].client_list.id);
printf( "请输入入住年月日(格式:2015 11 11)\n");
scanf("%4d%2d%2d",&room[i+1].client_list.year,&room[i+1].client_list.month,&room[i+1].client_list.date);
savefile(room,i+1);
printf("是否还要继续登记?(回复Y或N继续)\n");
scanf(" %c",&answer);
}while(answer=='Y');
}
//读取txt中的信息
int readfile(struct Hotel room[])
{
FILE *fp;
int i;
if((fp=fopen("room.txt","r"))==NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
for (i=0;!feof(fp);i++)
{
fscanf(fp,"%5d",&room[i].roomid);
fscanf(fp,"%6d",&room[i].price);
fscanf(fp,"%15d",&room[i].tel);
fscanf(fp,"%12s",room[i].client_list.name);
fscanf(fp,"%4d",&room[i].client_list.sex);
fscanf(fp,"%6d",&room[i].client_list.age);
fscanf(fp,"%20s",room[i].client_list.id);
fscanf(fp,"%7d",&room[i].client_list.year);
fscanf(fp,"%5d",&room[i].client_list.month);
fscanf(fp,"%5d",&room[i].client_list.date);
}
fclose(fp);
return i;
}
//保存结构体数组信息到文件
void savefile(struct Hotel room[],int i)
{
FILE *fp;
if((fp = fopen("room.txt","a"))==NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
fprintf(fp,"%5d%6d%15d%12s%4d%6d%20s%7d%5d%5d",
room[i].roomid,
room[i].price,
room[i].tel,
room[i].client_list.name,
room[i].client_list.sex,
room[i].client_list.age,
room[i].client_list.id,
room[i].client_list.year,
room[i].client_list.month,
room[i].client_list.date);
fclose(fp);
}
//统计模块
void tongji(struct Hotel room[])
{
int n,x;
int year,month,day,j,count=0;
x=readfile(room);
printf("请选择统计方式:1>统计房客数,2>统计某一天的房客数\n");
scanf("%d",&n);
switch (n)
{
case 1:
printf("客房总数为:%d\n",x);
break;
case 2:
printf("请输入具体时间:\n");
scanf("%d%d%d",&year,&month,&day);
for(j=0;j<x;j++)
{
if(room[j].client_list.year==year&&room[j].client_list.month==month&&room[j].client_list.date==day)
{
count++;
}
}
printf("%d年%d月%d号的房客数为%d",year,month,day,count);
}
}
//查询模块
void chaxun(struct Hotel room[])
{
int n,x,roomid,price,j;
char name[12],id[20];
x=readfile(room);
printf("按房间号查询请按1;按价格查询请按2;按用户姓名查询请按3;按身份证号码查询请按4\n");
scanf("%d",&n);
switch(n)
{
case 1:
printf("请输入房间号:\n");
scanf("%d",&roomid);
for(j=0;j<x;j++)
{
if(room[j].roomid==roomid)
{
printf("room,[j]");
}
}
break;
case 2:
printf("请输入价格:\n");
scanf("%d",&price);
for(j=0;j<x;j++)
{
if(room[j].price==price)
{
printf("room,[j]");
}
}
break;
case 3:
printf("请输入客户姓名:\n");
scanf("%s",name);
for(j=0;j<x;j++)
{
if(strcmp(room[j].client_list.name,name)==0)
{
printf("room,[j]");
}
}
break;
case 4:
printf("请输入身份证号码:\n");
scanf("%s",id);
for(j=0;j<x;j++)
{
if(strcmp(room[j].client_list.id,id)==0)
{
printf("room,[j]");
}
}
}
}
//打印
void print(struct Hotel room[],int j)
{
printf("%5d%6d%15d%12s%4d%6d%20s%7d%5d%5d\n",
room[j].roomid,
room[j].price,
room[j].tel,
room[j].client_list.name,
room[j].client_list.sex,
room[j].client_list.age,
room[j].client_list.id,
room[j].client_list.year,
room[j].client_list.month,
room[j].client_list.date);
}
void printmessage(struct Hotel room[],int n)
{
int j;
for(j=0;j<n;j++)
{
printf("%5d%6d%15d%12s%4d%6d%20s%7d%5d%5d\n",
room[j].roomid,
room[j].price,
room[j].tel,
room[j].client_list.name,
room[j].client_list.sex,
room[j].client_list.age,
room[j].client_list.id,
room[j].client_list.year,
room[j].client_list.month,
room[j].client_list.date);
}
}
//保存所有信息到txt文件
void saveall(struct Hotel room[],int m)
{
int j;
FILE *fp;
if((fp = fopen("room.txt","w"))==NULL)
{
printf("Failure to open score.txt!\n");
exit(0);
}
for(j=0;j<m;j++)
{
fprintf(fp,"%5d%6d%15d%12s%4d%6d%20s%7d%5d%5d",
room[j].roomid,
room[j].price,
room[j].tel,
room[j].client_list.name,
room[j].client_list.sex,
room[j].client_list.age,
room[j].client_list.id,
room[j].client_list.year,
room[j].client_list.month,
room[j].client_list.date);
}
fclose(fp);
}
//修改模块
void xiugai(struct Hotel room[])
{
int i, m, roomid;
m = readfile(room);
printf("请选择房间号:\n");
scanf("%d",&roomid);
for(i=0;i<m;i++)
{
if(room[i].roomid==roomid)
{
printf("请输入您的电话:\n");
scanf("%d",&room[i].tel);
printf("入住价格\n");
scanf("%d",&room[i].price);
printf("请输入您的名字\n");
scanf("%s",room[i].client_list.name);
printf("性别:\n");
scanf("%d",&room[i].client_list.sex);
printf("您的年龄:\n");
scanf("%d",&room[i].client_list.age);
printf("您的id:\n");
scanf("%s",room[i].client_list.id);
printf( "请输入入住年月日(格式:20150101)");
scanf("%4d%2d%2d",&room[i].client_list.year,&room[i].client_list.month,&room[i].client_list.date);
}
}
saveall(room,m);
}