航空機票預訂c#代碼
Problem statement: Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows:
問題陳述:編寫一個程序來分配飛機上的乘客座位。 假設小型飛機的座位編號如下:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
The program should display the seat pattern, with an ‘X’ marking the seats already assigned. After displaying the seats available, the program prompts the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice.
程序應顯示座位模式,并帶有“ X”標記已分配的座位。 在顯示可用座位之后,程序會提示所需的座位,用戶鍵入座位,然后更新可用座位的顯示。 這一直持續到所有座位都裝滿或用戶發出程序結束的信號為止。 如果用戶鍵入已經分配的座位,則程序應說明該座位已被占用,并要求其他選擇。
Input Example:
輸入示例:
For example, after seats 1A, 2B, and 4C are taken, the display should look like:
例如,在坐下了座位1A,2B和4C之后,顯示屏應如下所示:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
Solution
解
The whole problem can be implemented with help of 4 major functions:
可以通過以下四個主要功能來實現整個問題:
getData()
getData()
display()
顯示()
check()
check()
update()
update()
The entire problem is discussed dividing into parts focusing on functional modularity.
討論了整個問題,分為針對功能模塊化的部分。
1. Initialize a 2-D array to represent the seat matrix
1.初始化一個二維數組以表示座位矩陣
A 2-D character array is used to represent the seat matrix where the first column have the row number of each seat & the rest of the columns have four seat A,B,C,D respectively. Thus it’s a 7X5 2-D array to represent the airplane seat matrix which looks like following:
2-D字符數組用于表示席位矩陣,其中第一列具有每個席位的行號,其余列分別具有四個席位A,B,C,D。 因此,它是一個7X5二維數組,表示飛機座椅矩陣,如下所示:
2. Take user input for seat no
2.接受用戶輸入的座位號
User is requested to input the desired seat no by giving corresponding no of seat. Like a valid seat no is 1A, 3C, 7D and so on, whereas, an invalid seat request can be 6F, 0B so on.
要求用戶通過提供相應的座位號來輸入所需的座位號。 像有效席位一樣,no是1A,3C,7D等,而無效席位請求可以是6F,0B等。
The input is taken by our function getData() which takes user input & returns the string.
輸入由我們的函數getData()獲得 ,該函數接受用戶輸入并返回字符串。
3. Check the seat no request (check() )
3.檢查座位無要求(check())
Our major function for this problem is to check the validity of the seat request & update seat matrix status as per request.
我們針對該問題的主要功能是檢查座位請求的有效性并根據請求更新座位矩陣狀態。
Firstly it checks whether the user input is in the range 1A to 7D or not. If user input is out of range a prompt out "invalid request" & continue for further request.
首先,它檢查用戶輸入是否在1A到7D范圍內。 如果用戶輸入超出范圍,則提示“無效請求”并繼續進行進一步的請求。
Check whether user input is 'N' or not. If it's 'N' then user wants to end the program. Terminate.
檢查用戶輸入是否為“ N” 。 如果為“ N”,則用戶要結束程序。 終止。
if seat request is valid but already occupied
如果座位請求有效但已被占用
Then prompt a message stating “It’s already occupied”
然后提示信息“已被占用”
This checking can be done by founding the 2-D array row & column index for the corresponding seat.
可以通過找到相應座位的二維數組行和列索引來完成此檢查。
Let,
讓,
row_index=r&column_index=c
If(seat_matrix[row_index][column_index]==’X’)row_index = r&column_index = c
如果(seat_matrix [row_index] [column_index] =='X')Seat is occupied.
座位已滿。
ELSE seat request is a valid one and not occupied still
ELSE座位請求是有效的,尚未占用
Update()
更新()
4. Update the valid entry
4.更新有效條目
If the request is to update the valid seat we simple change its value to 'X'. It can be done by finding row & column index and updating the value of seat_matrix at that location to 'X'.
如果請求更新有效席位,我們只需將其值更改為'X'即可 。 可以通過查找行和列索引并將該位置的seat_matrix值更新為'X'來完成 。
5. Special function to check whether all seats are occupied
5.特殊功能,檢查是否所有座位都被占用
The program also need to be terminated when all seats are occupied. Thus every time we keep a checking whether all the entry of seat_matrix is 'X' or not.
當所有座位都坐滿時,還需要終止該程序。 因此,每次我們都檢查一下seat_matrix的所有條目是否為“ X” 。
航空公司座位預訂問題的C ++實現 (C++ implementation for Airline Seat Reservation Problem)
#include <bits/stdc++.h>
using namespace std;
// to check whether all sits are occupied or not
int allOccupied(char arr[7][5]){
int count=0;
for(int i=0;i<7;i++){
for(int j=1;j<5;j++)
if(arr[i][j]=='X')
count++;
}
if(count==28)
return 1;
return 0;
}
//to display the sits
void display(char arr[7][5]){
for(int i=0;i<7;i++){
for(int j=0;j<5;j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return;
}
//take user data
string getData(){
string p;
cout<<"enter valid seat no to check(like 1B) or N to end: ";
cin>>p;
return p;
}
//update sit status
void update(char arr[7][5],int row,int col){
cout<<"congrats, your seat is valid. Reserved for you\n";
cout<<"updated seat status..........\n";
arr[row][col]='X';
}
//checking whether user request for
//his sit no can be processed or not
int check(char arr[7][5],string s){
//if user input is not in the range 1A to 7D
if(s[0]>'7' || s[0]<'1' || s[1]>'D' || s[1]<'A'){
cout<<"invalid seat no\n"; //invalid sit no
return 0;
}
int row=-1,col=-1;
//find the row no of the user sit
for(int i=0;i<7;i++){
if(arr[i][0]==s[0])
row=i;
}
//find the column no of user sit
for(int j=0;j<5;j++){
if(arr[row][j]==s[1])
col=j;
}
//check whether sit is already occupied or not.
if(col==-1){
cout<<"Seat is already occupied\n";
return 0;
}
else{
//if it's a valid sit & not occupied,
//process the requested & update the sit as occupied
update(arr,row,col);
}
return 1;
}
void airline(char arr[7][5]){
// user can stop process by pressing 'N'
cout<<"enter N if you are done!\n";
string s;
// continue if not interrepted by user or
//there is valid sit in unoccupied state
while(true){
s=getData(); //get user input
//if user input is to stop the process
if(s[0]=='N')
break; // break
//process the request & check according to
if(check(arr,s))
display(arr);
if(allOccupied(arr)){ //if all sits are occupied
cout<<"sorry, no more seats left!!!!!!!!!!1..."<<endl;
break; //break
}
}
cout<<"Thanks, that's all"<<endl; //end of program
}
int main()
{
//2-D array for storing sit number
char arr[7][5];
for(int i=0;i<7;i++){
//forst column is row number
arr[i][0]=i+1+'0';
for(int j=1;j<5;j++){
//to represent sit number A,B,C,D respectively
arr[i][j]='A'+j-1;
}
}
cout<<"initial seat arrangements........\n";
display(arr);
airline(arr); //airline function
return 0;
}
Output
輸出量
initial seat arrangements........
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
enter N if you are done!
enter valid seat no to check(like 1B) or N to end: 2B
congrats, your seat is valid. Reserved for you
updated seat status..........
1 A B C D
2 A X C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
enter valid seat no to check(like 1B) or N to end: 3C
congrats, your seat is valid. Reserved for you
updated seat status..........
1 A B C D
2 A X C D
3 A B X D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
enter valid seat no to check(like 1B) or N to end: 2B
Seat is already occupied
enter valid seat no to check(like 1B) or N to end: 7E
invalid seat no
enter valid seat no to check(like 1B) or N to end: 7C
congrats, your seat is valid. Reserved for you
updated seat status..........
1 A B C D
2 A X C D
3 A B X D
4 A B C D
5 A B C D
6 A B C D
7 A B X D
enter valid seat no to check(like 1B) or N to end: N
Thanks, that's all
翻譯自: https://www.includehelp.com/cpp-programs/airline-seat-reservation-problem.aspx
航空機票預訂c#代碼