#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
const int ROWS = 14;
const int COL = 7;
void initPlane(char plane[ROWS][COL])
{
plane[14][7]={"ABCDEF","**X*XX","*X*X*X",
"3**XX*X","4X*X*XX","*X*X**",
"*X***X","X***XX","*X*XX*",
"X*XX*X","*X*XXX","**X*X*",
"**XX*X","****X*"};
}
//飞机座位布局打印
//输出,每个座位显示一个X
void printPlane(char msg[], char plane[ROWS][COL])
{
cout << msg << endl;
int Row,col;
for ( Row = 0; Row < ROWS ; Row ++)
{
cout << "Row" << " " << Row;
for (col = 0; col < COL; col++)
{
cout << setw(5) << plane [Row][col] << " ";
}
cout << endl;
}
}
int main()
{
int seatsTaken = 0;
int seats = ROWS * COL;
char plane[ROWS][COL];
char keep = 'y';
int row = 0;
char seat;
int rowIndex, seatIndex;
initPlane(plane);
cout << "请选择你想要的座位!" << endl;
while (seatsTaken < seats && keep == 'y')
{
//显示布局并获得座位选择
printPlane("*-可选\nX-已购\n\n当前座位表如下:", plane);
cout << "Row 1 和Row 2是头等舱。" << endl;
cout << "Row 3 到 Row 7是商务舱。" << endl;
cout << "Row 8 到 Row 13是经济舱。" << endl;
cout << "机票类型(头等舱、商务舱或经济舱):" ;
cin >> row;
cout << "座位数(A、B、C、D、E、F):" ;
cin >> seat;
// 调整输入
rowIndex = row;
if ( seat == 'A' || seat == 'a' )
seatIndex = 1;
else if ( seat == 'B' || seat == 'b' )
seatIndex = 2;
else if ( seat == 'C' || seat == 'c' )
seatIndex = 3;
else if ( seat == 'D' || seat == 'd' )
seatIndex = 4;
else if ( seat == 'E' || seat == 'e' )
seatIndex = 5;
else if ( seat == 'F' || seat == 'f' )
seatIndex = 6;
//检查座位是否有人坐
if (plane[rowIndex][seatIndex] == 'X')
{
cout << "对不起, " << row << " " << seat << "已经被他人预定" << endl;
}
else
{
cout << "好的,您预定" << row << " " << seat << "成功" << endl;
plane[rowIndex][seatIndex] = 'X';
seatsTaken++;
}
// 如果还有座位,继续往下运行
if (seatsTaken < seats)
{
cout << "是否选择其他座位? (yes-Y/no-N) ";
cin >> keep;
cout << endl << endl;
}
else
{
cout << "非常抱歉,该航班机票已被预定完,请您预定另外航班机票" << endl;
}
}
printPlane("最终座位表", plane);
system("pause");
system("cls");
return 0;
}
问题1:plane[14][7]这是指数组的行下标为4,列下标为7的元素,
问题2:"3*XXX", "4XXXX",这两个元素有7个字符,那么列长度就应该为8
void initPlane(char plane[][COL]) {
char s[14][7] = {"ABCDEF", "**X*XX", "*X*X*X",
"**XX*X", "X*X*XX", "*X*X**",
"*X***X", "X***XX", "*X*XX*",
"X*XX*X", "*X*XXX", "**X*X*",
"**XX*X", "****X*"
};
for(int i=0;i<14;i++){
strcpy(plane[i],s[i]);
}
}