航空機票預訂c#代碼_航空公司座位預訂問題的C ++程序

航空機票預訂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:

可以通過以下四個主要功能來實現整個問題:

  1. getData()

    getData()

  2. display()

    顯示()

  3. check()

    check()

  4. 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二維數組,表示飛機座椅矩陣,如下所示:

airline reseravtion system in C++

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#代碼

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541825.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541825.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541825.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux命令之which

which這個命令可以說并不常用&#xff0c;它的作用是查看可執行文件的位置&#xff0c;并返回第一個搜索結果。可執行文件也就是指的某個系統命令&#xff0c;但是這個命令的位置必須是在PATH路徑里存在的。截圖中 &#xff0c;pwd的位置在/bin/pwd,當然&#xff0c;這個路徑是…

線性代數向量乘法_向量的標量乘法| 使用Python的線性代數

線性代數向量乘法Prerequisite: Linear Algebra | Defining a Vector 先決條件&#xff1a; 線性代數| 定義向量 Linear algebra is the branch of mathematics concerning linear equations by using vector spaces and through matrices. In other words, a vector is a mat…

sonar掃描普通JAVA執行,SonarQube掃描源代碼的方法

SonarQube掃描源代碼的方法雷建鋒一、分析源代碼綜述一旦成功安裝了SonarQube平臺&#xff0c;您就可以開始安裝一個分析器并開始創建項目了。在第一次分析時&#xff0c;該平臺會自動創建一個項目。如果您需要在第一個分析之前在項目上設置一些配置&#xff0c;那么您可以選擇…

html的學習思維導圖

轉載于:https://www.cnblogs.com/lingdublog/p/6438088.html

php語言冒泡法,PHP實現冒泡排序算法的案例

PHP實現冒泡排序算法的案例發布時間&#xff1a;2020-10-23 17:39:38來源&#xff1a;億速云閱讀&#xff1a;84作者&#xff1a;小新這篇文章主要介紹PHP實現冒泡排序算法的案例&#xff0c;文中介紹的非常詳細&#xff0c;具有一定的參考價值&#xff0c;感興趣的小伙伴們一定…

線性代數分塊矩陣求逆矩陣_單位矩陣屬性(AI = A)| 使用Python的線性代數

線性代數分塊矩陣求逆矩陣Prerequisites: 先決條件&#xff1a; Defining Matrix 定義矩陣 Identity matrix 身份矩陣 numpy.matmul( ) matrix multiplication numpy.matmul()矩陣乘法 In linear algebra, the identity matrix, of size n is the n n square matrix with one…

MySQL5.7.17的簡單配置文件

#編譯安裝mysql5.7.17 [rootweb_1 data]# cat ../my.cnf [client]port3307socket/data/3307/mysql.sock[mysqld]user mysqlbasedir /usr/local/mysqldatadir /data/3307/dataport3307server-id 1socket/data/3307/mysql.sockcharacter-set-server utf8log-error /data/33…

cubic-bezier_帶CSS中的示例的cube-bezier()函數

cubic-bezierIntroduction: 介紹&#xff1a; How many times have we come across the word function? Well, it would not be wrong to say a lot. The fact that functions are used in web development while developing a website or web page is very important. There…

php時間調用最簡單的,PHP調用時間通過引用不可避免?

給定以下接口:interface ISoapInterface {public static function registerSoapTypes( &$wsdl );public static function registerSoapOperations( &$server );}以及以下代碼:$soapProvider array( "FilePool", "UserList" );foreach( $soapProvi…

上手Caffe(一)

author&#xff1a;oneBite 本文記錄編譯使用caffe for windows 使用環境 VS2013 ultimate,win7 sp1,caffe-windows源碼&#xff08;從github上下載caffe的windows分支&#xff0c;下載解壓之后&#xff0c;不要改變原有的目錄結構,因為solution rebuild時會使用文件的相對路徑…

使用JavaScript的圖像識別游戲

Today we are going to develop a fully functional image recognition game using JavaScript. JavaScript is the best fit choice since it is a web-based game. The game is totally based on event handling and event objects. 今天&#xff0c;我們將使用JavaScript開發…

php 判斷 in,tinkphp常用判斷條件in、notin、between、AND、OR

越來越多的人使用thinkphp框架開發應用&#xff0c;容易上手開發周期短&#xff0c;接下來吾愛編程為大家分享一下tinkphp常用判斷條件in、notin、between、AND、OR&#xff0c;有需要的小伙伴可以參考一下&#xff1a;in&#xff1a;{in name"Think.get.level" valu…

關于設置不同linux主機之間ssh免密登錄簡易方法

2019獨角獸企業重金招聘Python工程師標準>>> 在linux日常中&#xff0c;經常會有ssh鏈接其他主機服務器的action,也學習過大家日常用配置ssh免密登錄的方法。 小編今天在這里給大家介紹一種比較簡單的配置linux主機ssh免密登錄的方法。 兩臺主機的IP地址&#xff1a…

c語言指針++_C ++此指針| 查找輸出程序| 套裝1

c語言指針Program 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int A 10;this* ptr;ptr &A;*ptr 0;cout << *ptr << endl;return 0;}Output: 輸出&#xff1a; main.cpp: In function ‘int main()’:main.cpp:7:5: e…

java自定義線程池池,線程池使用及自定義線程池

一 案例引申編寫代碼同時只允許五個線程并發訪問(以下文的函數為例子)private static void method() {System.out.println("ThreadName" Thread.currentThread().getName() "進來了");Thread.sleep(2000);System.out.println("ThreadName" Th…

long類型20位示例_Java Long類reverseBytes()方法與示例

long類型20位示例長類reverseBytes()方法 (Long class reverseBytes() method) reverseBytes() method is available in java.lang package. reverseBytes()方法在java.lang包中可用。 reverseBytes() method is used to returns the value generated by reversing the order o…

impala和mysql語法,impala CREATE TABLE語句

CREATE TABLE語句用于在Impala中的所需數據庫中創建新表。 創建基本表涉及命名表并定義其列和每列的數據類型。語法以下是CREATE TABLE語句的語法。 這里&#xff0c;IF NOT EXISTS是一個可選的子句。 如果使用此子句&#xff0c;則只有在指定數據庫中沒有具有相同名稱的現有表…

Guava翻譯系列之EventBus

EventBus 類解析 當我們開發軟件時&#xff0c;各個對象之間的數據共享和合作是必須的。 但是這里比較難做的是 怎樣保證消息之間的傳輸高效并且減少各個模塊之間的耦合。 當組件的職責不清楚時&#xff0c;一個組件還要承擔另一個組件的職責&#xff0c;這樣的系統我們就認為是…

Java PipedOutputStream close()方法與示例

PipedOutputStream類close()方法 (PipedOutputStream Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to close this PipedOutputStream and free all system resources linked with this str…

Java二維數組谷電,java二維數組遍歷的2種代碼

二維數組遍歷&#xff1a;思想&#xff1a;1.先將二維數組中所有的元素拿到2.再將二維數組中每個元素進行遍歷&#xff0c;相當于就是在遍歷一個一維數組第一種方法&#xff1a;雙重for循環//遍歷二維數組public class Traverse_a_two_dimensional_array {public static void m…