看谁能把这个C程序改写成C++格式的程序!!(程序分两部分发)

lybjust 2007-10-21 09:13:25
程序其中可能部分还有错误~~
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<iostream.h>
#include<string.h>
#define NUM 100 //假设该旅店最多容纳100人
struct Client{
char Name[20];
int Sex;
int Age;
char ID_card[20];//居民身份证;
};
struct Hotel{
int Room_ID;//房间号
int Price;
bool Sign;//标记房间是否为空房间;
struct Client Client_list;//实现两个结构体的嵌套
}Room[NUM];
#define LENGTH sizeof(struct Hotel)
static int count=0,recount=0;//被使用的房间的统计数目和当前记录数
void Query_Client_List();
void Read_file(struct Hotel);
void Handle_menu();
void Delete_room(struct Hotel);
void Add_new_client(struct Hotel);
void Show_All_Client(struct Hotel,int);
void Query_Room_ID(struct Hotel);
void Query_room_Empty(struct Hotel Room[]);
void Write_file(struct Hotel);
void Initialize_room_ID(struct Hotel);
void main()
{
Read_file(struct Hotel Room[]);//能不能把这个函数设置为
Initialize_room_ID(struct Hotel Room[]);
Handle_menu();
}
//从硬盘读取文件,以便对下面进行操作,这里是程序的关键
void Read_file(struct Hotel Room[])
{
FILE *fp;
int i;
if((fp=fopen("Client_table","rb"))==NULL)
//检测文件是不是能打开,不能打开,将创建一个新的文件
{
if((fp=fopen("Client_table","wb"))==NULL)
{
cout<<"\n 文件不能被打开!"<<endl;
exit(1);
}
fclose(fp);
}
if((fp=fopen("Client_table","rb))==NULL)
{
cout<<"\n 文件出现问题,不能被打开,请重试!!!"<<endl;
exit(1);
}
i=0;
count=0;
recount;
while(!feof(fp))
//读文件一直到当前记录
{
if(1!=fread(&Room[i],LENGTH,1,fp)
break;
++recount;
++count;
i++;
}
fclose(fp);
cout<<"\n 总共记录条数:"<<count <<endl;
}
//这个函数是写文件操作,当有新的记录时,将记录写入文件
//初始化旅店的函数,但是我们也遇到了麻烦
void Initialize_room_ID(struct Hotel Room[])
{
int ID=100,mark=0;
//,k=0;
//动态分配存储空间
Room=(struct Hotel *)malloc(100*sizeof(struct Hotel));
//以下便是初始化过程
for(int i=1;i<=5;i++)
for(int j=1;j<=20;j++)
{
Room[mark].Room_ID=ID*i;
Room[mark].Price=100;
Room[mark].Sign=false;
//Room[mark].Tel=8500+k;
//k++;
}
}
//在每次运行该函数的时候,需要解决一些问题
//保存文件函数
void Write_file(struct Hotel Room[])
{
FILE *fp;
int i;
if((fp=fopen("Client_table","wb"))==NULL)
{
cout>>"\n不能打开该文件!"<<endl;
exit(1);
}
//以下部分也就是实现写操作
for(i=0;i<count;i++)
if(fwrite(&Room[i],LENGTH,1,fp)
cout<<"\n写文件发生错误"<<endl;
fclose(fp);
}
int Handle_menu()
{int Selection;
//读文件的过程
cout<<"\n *********欢迎进入客房管理信息系统********* "<<endl;

cout<<"\n 1、顾客登记入住 "<<endl;
cout<<"\n 2、信息查询 "<<endl;
cout<<"\n 3、订房 "<<endl;
cout<<"\n 4、退房 "<<endl;
cout<<"\n 0、退出 "<<endl;
cout<<"\n ****************************************** "<<endl;
cin>>Selection;
while(1)
{
switch(Selection)
{

case 1: Add_new_client(struct Hotel Room[]);Write_file(struct Hotel Room[]);break;
case 2: Query_Client_List();break;
case 3: Room_Book(struct Hotel Room[]);break;
case 4: Delete_room(struct Hotel Room[]);Write_file(struct Hotel Room[]);break;
case 0: exit(0);break;
defaut: cout<<"\n 请按任意键继续"<<endl;
}
}
}
...全文
84 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lybjust 2007-10-21
  • 打赏
  • 举报
回复
//*******************************顾客登记入住******************************************
int Add_new_client(struct Hotel Room[])
{
char result;
int i=0;
//将结构体中涉及的内容全部注册就可以了
//先检查空房间,再让顾客入住……
while(Room[i].Sign)
i++;
cout<<"\n 房间是空的,您可以住在这个房间"<<Room[i].Room_ID<<endl;
cout<<"\n 请输入您本人的相关信息:"<<endl;
cout<<"\n 请输入您的姓名:"<<endl;
cin>>Room[i].Client_list.Name;
cout<<"\n "<<endl;
cout<<"\n 请输入性别(0->男;1->女):"<<endl;
cin>>Room[i].Client_list.Sex;
cout<<"\n "<<endl;
cout<<"\n 请输入您的年龄:"<<endl;
cin>>Room[i].Client_list.Age;
cout<<"\n 请输入您的身份证号:"<<endl;
cin>>Room[i].Client_list.ID_card;
cout<<"\n"<<endl;
cout<<"\n 确认该用户登记吗?(是->Y;否->N) "<<endl;
cin>>result;
if(result==Y)
{
Room[i].Sign=true;
cout<<"\n 该用户登记成功! "<<endl;
count=count+1;
exit(1);
}
else
{
Room[i].Sign=false;
cout<<"\n 该用户登记已经取消! "<<endl;
exit(0);
}
//旅店的人数已经自增了1
}



//*********************以下是实现查询功能******************************************
int Query_Client_List()
{
int Selection;
cout<<"\n 1.按房间号查询 "<<endl;
cout<<"\n 2.空房间查询 "<<endl;
cout<<"\n 请选择: "<<endl;
cin>>Selection;
while(1)
{
switch(Selection)
{
case 1: Query_Room_ID(struct Hotel Room[]);break;
case 2: Query_Room_Empty();break;
case 3: Show_All_Client(struct Hotel Room[],int count);break;
default: printf("输入错误,请继续");
}
}
}

//**************************按照房间号实现查询的函数********************************
void Query_Room_ID(struct Hotel Room[])
{ int room_id;
cout<<"\n 请输入房间号: "<<endl;
cin>>room_id;
for(int i=0;i<NUM;i++;)
if(Room[i].Room_ID==room_id&&Room[i].Sign==true)
{
cout<<"\n 您要查询的客户信息如下: "<<endl;
cout<<"\n 姓名:"<<Room[i].Client_list.Name <<endl;
cout<<"\n 性别:"<<Room[i].Client_list.Sex <<endl;
cout<<"\n 0->(男);1->(女) "<<endl;
cout<<"\n 身份证号: "<<Room[i].Client_list.ID_card <<endl;
cout<<"\n 房间号: "<<Room[i].Room_ID <<endl;
break;
}
else
cout<<"\n 对不起,没有该记录"<<endl;
}
/*******************查询空房间****************************************
void Query_room_Empty(struct Hotel Room[])
{
cout<<"\n 以下是为您查询到的空的房间:"<<endl;
for(j=0;j<NUM;j++)
if(Room[j].Sign==false)
{
cout<<"\n 空房间的ID是: "<<Room[j].Room_ID<<endl;
}
}
*/
//****************************查询全部信息***************************************
void Show_All_Client(struct Hotel Room[],int count)
{
int Client_all=count;
cout<<"\n ****************************************************** "<<endl;
cout<<"\n 以下是所有顾客的入住信息 "<<endl;
cout<<"\n ****************************************************** "<<endl;
cout<<"\n 0->男;1->女"): "<<endl;
cout<<"\n 姓名******性别***年龄**身份证号***************房间号*** "<<endl;
for(int i=0;i<NUM;i++)
if(Room[i].Sign==true)
cout<<"\n "<< Room[i].Client_list.Name, Room[i].Client_list.Sex,Room[i].Client_list.Age,Room[i].Client_list.ID_card,Room[i].Room_ID <<endl;
else
continue;
}


//**************实现顾客订房********************************
void Room_Book(struct Hotel Room[])
{
printf("以下是为您查询到的空的房间:");
for(j=0;j<NUM;j++)
if(Room[j].Sign==false)
{
cout<<"\n 空房间ID是:<<Room[j].Room_ID,j "<<endl;
cout<<"\n 请输入您本人的相关信息:"<<endl;
cout<<"\n 请输入您的姓名:"<<endl;
cin>>Room[i].Client_list.Name;
cout<<"\n "<<endl;
cout<<"\n 请输入性别(0->男;1->女)"<<endl;
cin>>Room[i].Client_list.Sex;
cout<<"\n "<<endl;
cout<<" 请输入您的年龄:"<<endl;
cin>>Room[i].Client_list.Age;
cout<<"\n 请输入您的身份证号:"<<endl;
cin>>Room[i].Client_list.ID_card;
cout<<"\n 确认预订吗?(是->Y;否->N) "<<endl;
cin>>result;
if(result==Y)
{
Room[i].Sign=true;
cout<<"\n 预订房间成功! "<<endl;
count=count+1;
exit(1);
}
else
{
Room[i].Sign=false;
cout<<"\n 预订已经取消! "<<endl;
exit(0);
}
}
}
}

//***********************实现退房间函数***************************
int Delete_room(struct Hotel Room[])
{
char *name;
cout<<"\n 请输入您的姓名: "<<endl;
cin>>name;
for(int i=0;i<100;i++)
if(Room[i].Name==name)
break;
Room[i].Sign=false;
}

64,653

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧