呃 楼上说的问题我改好了
如下
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <ctime>
#include <cstdlib>
void choose(char);
void menu();
void money(int);
int montoday(int,int,int);
int yeartoday(int,int);
int daytoday(int,int);
int inday(int,int,int);
bool choose_a(char *);
const int LENname=8;
const int LENIDc=20;
const int LENsex=4;
const int LENnat=8;
const int MAXroom=500;
const int LENphone=14;
class GuestDate{
char name[LENname];
char sex[LENsex];
char IDc[LENIDc];
char nat[LENnat];
char phone[LENphone];
int roomID;
int delTag;
public:
GuestDate() {delTag=0;}
int year;
int mon;
int day;
char *getname() {return name;}
char *getsex() {return sex;}
char *getIDc() {return IDc;}
char *getnat() {return nat;}
char *getphone() {return phone;}
int getdelTag() {return delTag;}
int getroomID() {return roomID;}
void setname(char na[]) {strcpy(name,na);}
void setsex(char na[]) {strcpy(sex,na);}
void setIDc(char na[]) {strcpy(IDc,na);}
void setnat(char na[]) {strcpy(nat,na);}
void setphone(char na[]) {strcpy(phone,na);}
void setroomID(int na) {roomID=na;}
void del() {delTag=0;}
void unDel() {delTag=1;}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class GuestWay{
public:
GuestDate guest[MAXroom];
void showdate(int);
void showdate();
void save();
void open();
void newfile();
void del(int roomID)
{
guest[roomID].del();
}
int roomIDscan(int roomID){
if(1==guest[roomID].getdelTag()) return 1;
return 0;
}
void IDcscan(char []);
} guest ;
void GuestWay::showdate(int i)
{
cout.flags(ios::left);
cout<<setw(8)<<guest[i].getname()
<<setw(6)<<guest[i].getsex()
<<setw(20)<<guest[i].getIDc()
<<setw(8)<<guest[i].getnat()
<<setw(14)<<guest[i].getphone()
<<setw(6)<<guest[i].getroomID()
<<guest[i].year<<"."
<<guest[i].mon<<"."
<<setw(5)<<guest[i].day
<<setw(3)<<inday(guest[i].year,guest[i].mon,guest[i].day)
<<endl;
}
void GuestWay::showdate()
{
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
for(int i=1;i<MAXroom;i++)
if(guest[i].getdelTag()==1)
showdate(i);
}
void GuestWay::save()
{
fstream f("GusetDate.bin",ios::out|ios::binary);
for(int roomID=1;roomID<=MAXroom;roomID++)
f.write((char*)&guest[roomID],sizeof(guest[roomID]));
f.close();
}
void GuestWay::open()
{
fstream f("GusetDate.bin",ios::in|ios::binary);
int roomID=0;
for(;;)
{
if(roomID>MAXroom)
break;
roomID++;
f.seekg((roomID-1)*sizeof(guest[roomID]));
f.read((char*)&guest[roomID],sizeof(guest[roomID]));
}
f.close();
}
void GuestWay::IDcscan(char IDc[])
{
for(int i=1;i<=MAXroom;i++)
{
if (0==strcmp(guest[i].getIDc(),IDc)) {
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
GuestWay::showdate(i);
break;
}
else if(i==MAXroom) cout<<"没有入住该旅客"<<endl;
}
}
void GuestWay::newfile()
{
int roomID;
char name[LENname];
char sex[LENsex];
char IDc[LENIDc];
char nat[LENnat];
char phone[LENphone];
for(;;){time_t t=time(0);
cout<<"输入房间"<<endl;
cin>>roomID;
if(0==GuestWay::roomIDscan(roomID) && roomID<MAXroom)
guest[roomID].setroomID(roomID);
else {
cout<<"该房间已有人"<<endl;
break;
}
cout<<"输入姓名"<<endl;
cin>>name; guest[roomID].setname(name);
cout<<"输入性别"<<endl;
cin>>sex;
guest[roomID].setsex(sex);
cout<<"输入身份证"<<endl;
cin>>IDc;
guest[roomID].setIDc(IDc);
cout<<"输入籍贯"<<endl;
cin>>nat;
guest[roomID].setnat(nat);
cout<<"输入手机号"<<endl;
cin>>phone;
guest[roomID].setphone(phone);
guest[roomID].year=localtime(&t)->tm_year+1900;
guest[roomID].mon=localtime(&t)->tm_mon+1;
guest[roomID].day=localtime(&t)->tm_mday;
guest[roomID].unDel();
break;
}
for(int i=1;i<roomID;i++){
if(1==guest[i].getdelTag() && guest[roomID].getroomID() < guest[i].getroomID()){
GuestDate temp;
temp=guest[i];
guest[i]=guest[roomID];
guest[roomID]=temp;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void menu()
{
cout<<">>>>>按1入住"<<" "
<<"按2退房"<<" "
<<"按3显示住房信息"<<" "
<<"按4房间查询"<<" "
<<"按5身份证查询"<<" "
<<"按q退出<<<<<"<<" "<<endl;
}
void choose(char key){
switch(key){
case 49 :
cout<<endl;
guest.newfile();
break;
case 50 :
cout<<endl;
cout<<"输入房间"<<endl;
int roomID;
cin>>roomID;
if(0==guest.roomIDscan(roomID)) {
cout<<"该房间没有人"<<endl;
break;
}
else {
money(inday(guest.guest[roomID].year,guest.guest[roomID].mon,guest.guest[roomID].day));
cout<<"确定支付? 1.确定 2.返回"<<endl;
char c[10];
cin>>c;
if (choose_a(c))
switch(c[0])
{
case 49 : cout<<"选择支付"<<endl; guest.del(roomID); break;
case 50 : cout<<"选择返回"<<endl; break;
default : cout<<"输入错误,重新输入"<<endl; break;
}
else cout<<"输入错误,重新输入"<<endl;
}
break;
case 51 :
cout<<endl;
guest.showdate();
break;
case 52 :
cout<<endl;
cout<<"输入房间"<<endl;
cin>>roomID;
if(0==guest.roomIDscan(roomID)) {
cout<<"该房间没有人"<<endl;
break;
}
else {
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
guest.showdate(roomID);
break;
}
case 53 :
cout<<endl;
char IDc[18];
cout<<"输入身份证"<<endl;
cin>>IDc;
guest.IDcscan(IDc);
break;
case 113:
guest.save();
exit(0);
default : cout<<"输入错误,重新输入"<<endl;
}
}
bool choose_a(char *p){
int k=0;
while(*p) {p++;k++;}
if (k==1) return true;
else return false;
}
int montoday(int year,int mon,int tempmon){
int n=tempmon-mon;
int day=0;
for(int i=0;i<n;i++,mon++){
switch(mon){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12:
day+=31;break;
case 4 :
case 6 :
case 9 :
case 11:
day+=30;break;
case 2 :
if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
day+=29;
else
day+=28;
}
}
return day;
}
int yeartoday(int year,int tempyear)
{
int n=tempyear-year;
int day=0;
for (int i=0;i<n;i++,year++)
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) day+=366;
else day+=365;
}
return day;
}
int daytoday(int day,int tempday)
{
if(day>tempday) {return day-tempday;}
else if(tempday>day) {return tempday-day;}
else return 0 ;
}
int inday(int year,int mon,int day)
{
int tempyear,tempmon,tempday;
time_t t=time(0);
tempyear=localtime(&t)->tm_year+1900;
tempmon=localtime(&t)->tm_mon+1;
tempday=localtime(&t)->tm_mday;
int py=yeartoday(year,tempyear);
int pm=montoday(year,mon,tempmon);
int pd=daytoday(day,tempday);
return (py+pm+pd)>1?(py+pm+pd):1 ; //不足一天算一天
}
void money(int p)
{
cout<<"应付:"<<p*120<<endl; //120一天
}
void main()
{ guest.open();
menu();
while(1)
{
char key[10];
cin>>key;
if (choose_a(key))
{
choose(key[0]);
cout<<endl;
menu();
}
else {cout<<"输入错误"<<endl<<endl;menu();}
}
}
如下
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <ctime>
#include <cstdlib>
void choose(char);
void menu();
void money(int);
int montoday(int,int,int);
int yeartoday(int,int);
int daytoday(int,int);
int inday(int,int,int);
bool choose_a(char *);
const int LENname=8;
const int LENIDc=20;
const int LENsex=4;
const int LENnat=8;
const int MAXroom=500;
const int LENphone=14;
class GuestDate{
char name[LENname];
char sex[LENsex];
char IDc[LENIDc];
char nat[LENnat];
char phone[LENphone];
int roomID;
int delTag;
public:
GuestDate() {delTag=0;}
int year;
int mon;
int day;
char *getname() {return name;}
char *getsex() {return sex;}
char *getIDc() {return IDc;}
char *getnat() {return nat;}
char *getphone() {return phone;}
int getdelTag() {return delTag;}
int getroomID() {return roomID;}
void setname(char na[]) {strcpy(name,na);}
void setsex(char na[]) {strcpy(sex,na);}
void setIDc(char na[]) {strcpy(IDc,na);}
void setnat(char na[]) {strcpy(nat,na);}
void setphone(char na[]) {strcpy(phone,na);}
void setroomID(int na) {roomID=na;}
void del() {delTag=0;}
void unDel() {delTag=1;}
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class GuestWay{
public:
GuestDate guest[MAXroom];
void showdate(int);
void showdate();
void save();
void open();
void newfile();
void del(int roomID)
{
guest[roomID].del();
}
int roomIDscan(int roomID){
if(1==guest[roomID].getdelTag()) return 1;
return 0;
}
void IDcscan(char []);
} guest ;
void GuestWay::showdate(int i)
{
cout.flags(ios::left);
cout<<setw(8)<<guest[i].getname()
<<setw(6)<<guest[i].getsex()
<<setw(20)<<guest[i].getIDc()
<<setw(8)<<guest[i].getnat()
<<setw(14)<<guest[i].getphone()
<<setw(6)<<guest[i].getroomID()
<<guest[i].year<<"."
<<guest[i].mon<<"."
<<setw(5)<<guest[i].day
<<setw(3)<<inday(guest[i].year,guest[i].mon,guest[i].day)
<<endl;
}
void GuestWay::showdate()
{
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
for(int i=1;i<MAXroom;i++)
if(guest[i].getdelTag()==1)
showdate(i);
}
void GuestWay::save()
{
fstream f("GusetDate.bin",ios::out|ios::binary);
for(int roomID=1;roomID<=MAXroom;roomID++)
f.write((char*)&guest[roomID],sizeof(guest[roomID]));
f.close();
}
void GuestWay::open()
{
fstream f("GusetDate.bin",ios::in|ios::binary);
int roomID=0;
for(;;)
{
if(roomID>MAXroom)
break;
roomID++;
f.seekg((roomID-1)*sizeof(guest[roomID]));
f.read((char*)&guest[roomID],sizeof(guest[roomID]));
}
f.close();
}
void GuestWay::IDcscan(char IDc[])
{
for(int i=1;i<=MAXroom;i++)
{
if (0==strcmp(guest[i].getIDc(),IDc)) {
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
GuestWay::showdate(i);
break;
}
else if(i==MAXroom) cout<<"没有入住该旅客"<<endl;
}
}
void GuestWay::newfile()
{
int roomID;
char name[LENname];
char sex[LENsex];
char IDc[LENIDc];
char nat[LENnat];
char phone[LENphone];
for(;;){time_t t=time(0);
cout<<"输入房间"<<endl;
cin>>roomID;
if(0==GuestWay::roomIDscan(roomID) && roomID<MAXroom)
guest[roomID].setroomID(roomID);
else {
cout<<"该房间已有人"<<endl;
break;
}
cout<<"输入姓名"<<endl;
cin>>name; guest[roomID].setname(name);
cout<<"输入性别"<<endl;
cin>>sex;
guest[roomID].setsex(sex);
cout<<"输入身份证"<<endl;
cin>>IDc;
guest[roomID].setIDc(IDc);
cout<<"输入籍贯"<<endl;
cin>>nat;
guest[roomID].setnat(nat);
cout<<"输入手机号"<<endl;
cin>>phone;
guest[roomID].setphone(phone);
guest[roomID].year=localtime(&t)->tm_year+1900;
guest[roomID].mon=localtime(&t)->tm_mon+1;
guest[roomID].day=localtime(&t)->tm_mday;
guest[roomID].unDel();
break;
}
for(int i=1;i<roomID;i++){
if(1==guest[i].getdelTag() && guest[roomID].getroomID() < guest[i].getroomID()){
GuestDate temp;
temp=guest[i];
guest[i]=guest[roomID];
guest[roomID]=temp;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void menu()
{
cout<<">>>>>按1入住"<<" "
<<"按2退房"<<" "
<<"按3显示住房信息"<<" "
<<"按4房间查询"<<" "
<<"按5身份证查询"<<" "
<<"按q退出<<<<<"<<" "<<endl;
}
void choose(char key){
switch(key){
case 49 :
cout<<endl;
guest.newfile();
break;
case 50 :
cout<<endl;
cout<<"输入房间"<<endl;
int roomID;
cin>>roomID;
if(0==guest.roomIDscan(roomID)) {
cout<<"该房间没有人"<<endl;
break;
}
else {
money(inday(guest.guest[roomID].year,guest.guest[roomID].mon,guest.guest[roomID].day));
cout<<"确定支付? 1.确定 2.返回"<<endl;
char c[10];
cin>>c;
if (choose_a(c))
switch(c[0])
{
case 49 : cout<<"选择支付"<<endl; guest.del(roomID); break;
case 50 : cout<<"选择返回"<<endl; break;
default : cout<<"输入错误,重新输入"<<endl; break;
}
else cout<<"输入错误,重新输入"<<endl;
}
break;
case 51 :
cout<<endl;
guest.showdate();
break;
case 52 :
cout<<endl;
cout<<"输入房间"<<endl;
cin>>roomID;
if(0==guest.roomIDscan(roomID)) {
cout<<"该房间没有人"<<endl;
break;
}
else {
cout.flags(ios::left);
cout<<setw(8)<<"姓名"
<<setw(6)<<"性别"
<<setw(20)<<"身份证"
<<setw(8)<<"籍贯"
<<setw(14)<<"手机号"
<<setw(6)<<"房号"
<<setw(12)<<"入住时间"
<<setw(3)<<"天数"
<<endl;
guest.showdate(roomID);
break;
}
case 53 :
cout<<endl;
char IDc[18];
cout<<"输入身份证"<<endl;
cin>>IDc;
guest.IDcscan(IDc);
break;
case 113:
guest.save();
exit(0);
default : cout<<"输入错误,重新输入"<<endl;
}
}
bool choose_a(char *p){
int k=0;
while(*p) {p++;k++;}
if (k==1) return true;
else return false;
}
int montoday(int year,int mon,int tempmon){
int n=tempmon-mon;
int day=0;
for(int i=0;i<n;i++,mon++){
switch(mon){
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12:
day+=31;break;
case 4 :
case 6 :
case 9 :
case 11:
day+=30;break;
case 2 :
if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
day+=29;
else
day+=28;
}
}
return day;
}
int yeartoday(int year,int tempyear)
{
int n=tempyear-year;
int day=0;
for (int i=0;i<n;i++,year++)
{
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) day+=366;
else day+=365;
}
return day;
}
int daytoday(int day,int tempday)
{
if(day>tempday) {return day-tempday;}
else if(tempday>day) {return tempday-day;}
else return 0 ;
}
int inday(int year,int mon,int day)
{
int tempyear,tempmon,tempday;
time_t t=time(0);
tempyear=localtime(&t)->tm_year+1900;
tempmon=localtime(&t)->tm_mon+1;
tempday=localtime(&t)->tm_mday;
int py=yeartoday(year,tempyear);
int pm=montoday(year,mon,tempmon);
int pd=daytoday(day,tempday);
return (py+pm+pd)>1?(py+pm+pd):1 ; //不足一天算一天
}
void money(int p)
{
cout<<"应付:"<<p*120<<endl; //120一天
}
void main()
{ guest.open();
menu();
while(1)
{
char key[10];
cin>>key;
if (choose_a(key))
{
choose(key[0]);
cout<<endl;
menu();
}
else {cout<<"输入错误"<<endl<<endl;menu();}
}
}
网络幽灵.....