我尝试创建一个影院系统。您添加影院(给出尺寸--2d数组),并且首先它们是空的(在数组中标记为'_'
)。此外,您可以预订座位,您可以显示所有影院并删除其中任何一个。我在预订时遇到了麻烦。
我给你我的代码:
theatre
class theatre
{
int id,seats,x,y;
int arr[100];
public:
void addTheatre();
void showdata() const;
int idTheatre() const;
void reserveSeats(int n) const;
};
Add
void theatre::addTheatre()
{
cout << "Please enter the id of the theatre:" << endl;
cin >> id;
cout << "Please enter the number of the seats in the theatre:" << endl;
cin >> seats;
cout << "Please enter the dimensions:" << endl;
cin >> x >> y;
}
// i use a function for write in the file
void write_theatre()
{
theatre th;
ofstream outFile;
outFile.open("theatre.txt",ios::binary|ios::app);
th.addTheatre();
outFile.write(reinterpret_cast<char *> (&th), sizeof(theatre));
outFile.close();
cout << "\n\nTheatre has been added succesfully ";
cin.ignore();
cin.get();
}
Display
void theatre::showdata() const
{
int k=x+1;
char** t = new char*[k];
for ( int i = 0 ; i < k ; i++ )
t[i] = new char[y];
for ( int i = 0 ; i < k ; i++ )
cout << i << '\t';
cout << '\n' ;
for ( int i = 0 ; i < k ; i++ )
{
for ( int j =0 ; j < y ; j++ )
{
t[0][j]=char('a' + j);
t[i][j]='_';
}
}
for ( int j = 0 ; j <y ; j++ )
{
for ( int i = 0 ; i < k ; i++ )
{
cout << t[i][j] << '\t';
}
cout << '\n';
}
for ( int i = 0 ; i < x ; i++ )
delete[] t[i];
delete[] t;
}
//function to write in the file
void display_all_theatres()
{
theatre th;
ifstream inFile;
inFile.open("theatre.txt",ios::binary);
if(!inFile)
{
cout<<"File could not be open !! Press any Key...";
cin.ignore();
cin.get();
return;
}
cout << "\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
while(inFile.read(reinterpret_cast<char *> (&th), sizeof(theatre)))
{
th.showdata();
cout << "\n\n====================================\n";
}
inFile.close();
cin.ignore();
cin.get();
}
Id
int theatre::idTheatre() const
{
return id;
}
void theatre::reserveSeats(int n) const
{
int k,m;
char c;
theatre th;
bool found=false;
fstream File;
File.open("theatre.txt",ios::binary|ios::in|ios::out);
if(!File)
{
cout<<"File could not be open !! Press any Key...";
cin.ignore();
cin.get();
return;
}
while(!File.eof() && found==false)
{
File.read(reinterpret_cast<char *> (&th), sizeof(theatre));
if(th.idTheatre()==n)
{
cout << "How many seats do you want to reserve?" << endl;
cin >> k;
for(int i=0;i<k;i++)
{
cout << "Enter the number of "<<i+1<<"seat:" << endl;
cin >> m;
}
int pos=(-1)*static_cast<int>(sizeof(th));
File.seekp(pos,ios::cur);
File.write(reinterpret_cast<char *> (&th), sizeof(theatre));
cout << "\n\n\t Record Updated";
found = true;
}
}
File.close();
if(found==false)
cout<<"Record Not Found ";
cin.ignore();
cin.get();
}
根据预订,我想向用户输入他想要的座位号(例如B5
),而在数组中而不是'_'
,它将显示为'X'
。我不知道如何改变特定剧院的阵列。