1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
const int MAX_ROWS = 10;
const int MAX_COLS = 3;
//void printForm(char ray[][], int row, char column);
void getBC (int ray[MAX_ROWS][MAX_COLS]);
void getFC (int ray[MAX_ROWS][MAX_COLS]);
void seatmap(int ray[MAX_ROWS][MAX_COLS]);
int get_int (int min, int max)
{ int num;
while (true)
{ cin >> num;
if (num >= min && num <= max)
return num;
cout << "Invalid number" << endl;
}
}
int main()
{ int ray[MAX_ROWS][MAX_COLS] = { 0 };
char choice;
int numSeatsFC, numSeatsBC;
int totalfc = 9, totalbc = 21;
cout << "The airplane has 10 rows, with 3 seats in each row.\n";
cout << "0 indicates that the seat is still available,\n";
cout << "1 indicates that the seat has already occupied.\n";
seatmap (ray);
do
{ cout << "Enter the class of the ticket.\n";
cout << "1/F - First class<Row 1 to 3>\n";
cout << "2/B - Business class<Row 4 to 10>\n";
cin >> choice;
if (choice == '1' || choice == 'F')
{ cout << "There are " << totalfc << " seats available.\n";
cout << "How many you want to purchase?";
numSeatsFC = get_int (1, totalfc);
for (int k = 0; k < numSeatsFC; k++)
{ getFC (ray);
}
totalfc -= numSeatsFC;
break;
}
else if (choice == '2' || choice == 'B')
{ cout << "There are " << totalbc << " business class seats available.\n";
cout << "How many you want to purchase?";
numSeatsBC = get_int (1, totalbc);
for (int k = 0; k < numSeatsBC; k++)
{ getBC (ray);
}
totalbc -= numSeatsBC;
break;
}
} while (! (choice == '1' || choice == 'F' || choice == '2' || choice == 'B'));
cout << "Number of tickets left:\n";
cout << "First class" << totalfc << endl;
cout << "Business class" << totalbc << endl;
cout << "Select one of the options.\n";
cout << "0/X - Exit Program\n";
cout << "1/F - Plane Depart\n";
cout << "2/M - Show Seat Map";
cout << "Any other key - next customer\n";
cin >> choice;
if (choice == '2' || choice == 'M')
{ seatmap(ray);
}
system ("pause");
return 0;
}
bool reserve_seat (int ray[MAX_ROWS][MAX_COLS], int row, int col)
{ if (ray[row-1][col-1])
{ cout << "Seat is occupied" << endl;
return false;
}
ray[row-1][col-1] = 1;
return true;
}
void getBC(int ray[MAX_ROWS][MAX_COLS])
{ int row, col;
do
{ cout << "Enter the row you want to sit<From row 4 to 10> " << endl;
row = get_int (4, 10);
cout << "Enter the column number (from 1 to 3). " << endl;
col = get_int (1,3);
} while (! reserve_seat (ray, row, col));
}
void getFC (int ray[MAX_ROWS][MAX_COLS])
{ int row, col;
do
{ cout << "Enter the row you want to sit<From row 1 to 3> " << endl;
row = get_int (1, 3);
cout << "Enter the column number (from 1 to 3). " << endl;
col = get_int (1, 3);
} while (! reserve_seat (ray, row, col));
}
void seatmap(int ray[MAX_ROWS][MAX_COLS])
{ for (int i = 0; i < MAX_ROWS; i++)
{ cout << endl;
cout << "Row" << setw(2) << setprecision(4) << "#" << (i + 1) << setw(3);
for (int j = 0; j < MAX_COLS; j++)
{ cout << setw(4) << ray[i][j];
}
}
}
|