Questions and codes:
Design and implement a flight information query and retrieval system. Requirements: To sort and find the flight information of the aircraft, according to the flight number, starting point, Arrival station, departure time and arrival time and other information to inquire. The Flight information table is styled as follows:
Flight number |
Starting point |
Terminal |
Schedule |
Departure time |
Arrival time |
Models |
Fare |
ca1544 |
Hefei |
Beijing |
1.2.4.5 |
10:55 |
12:40 |
733 |
960 |
mu5341 |
Shanghai |
Guangzhou |
daily |
14:20 |
16:15 |
m90 |
1280 |
CZ3869 |
Chongqing |
Shenzhen |
2.4.6 |
08:55 |
10:35 |
733 |
1010 |
One of the flight numbers is in the format: the first two uppercase letters indicate the name of the airline, after 4 a bit is the flight number, for example: CA1544 , CA indicates the name of the airline, 1544 the flight number.
/** Copyright (c) 2015, Yantai University School of Computer and control engineering *all rights reserved.** file name: flight.cpp* file ID: None * Content Summary: This code is used to get the first file that meets the suffix requirements * Other instructions: None * Current version: v1.0* Author: Tan Xinxin * Completion date: June 22, 2016 * * Modified record: * Modified Date: None. * Version Number: * Modified by: Tan Xinxin * Modified: * * #include <iostream> #include <cstring> #include <cstdio> #include <fstream >//action file must write # include <cstdlib>using namespace std;const int maxn=10000;const int p=5000;//hash Table Maximum length const string Nullkey= "Nullkey";//define NULL keyword value const string delkey= "Delkey";//define deleted keyword value string x[maxn];//enter flight number int hashlen=0;// Hash table length Ifstream infile ("Read.txt", ios::in); typedef struct{String departure;//starting point string terminal;//terminal string Sche dule;//Schedule String start;//take-off time string arrival;//arrival time string type;//model double price;//fare} info;//description information type typedef s truct{string number;//Flight number info data;//description information int cnt;//probe} hash;//hash table type hash ha[maxn];//define a hash table int work ();//Feature Selection table Single int getnum (string k);//Check out the numeric part of the flight number int Search (string key);//Find the flight number int Delete (string str) in the hash table;//Delete the specified flight information void Insert1 ( String key);/Automatically insert flight information void Insert2 (string key);//Manually insert flight information void Create1 ();//file automatically read void Create2 ();//keyboard manually enter void number (string key);// Check flight number void Departure (string key),//query starting point void Terminal (String key),//query arrival station void Start (string key),//query departure time void arrival ( string key);//query arrival time void FileOutput (int adr);//flight information deposited in file void Output (int adr);//flight information displays void Input (int adr) on the screen;// Enter flight information manually/** function name: work* function Description: Flight information Query and Retrieval System Selection function Menu * input parameter: none * output parameter: None * return value: None * Other instructions: no */int work () {while (1) {Cout<<e ndl;//form format cout<< "\ t" << "\ T" << "\ T" << "********* Welcome to Flight information Query and retrieval system *********" <<endl; cout<< "\ t" << "\ T" << "\ T" << "\ T" << "① read in data" << "\ T" << "② Delete data" << "\ n"; cout<< "\ t" << "\ T" << "\ T" << "\ T" << "③ flight number" << "\ T" << "④ starting point" << "\ n"; cout<< "\ t" << "\ T" << "\ T" << "\ T" << "⑤ Arrival Station" << "\ T" << "⑥ Departure time" << "\ n"; cout<< "\ t" << "\ T" << "\ T" << "\ T" << "⑦ Arrival Time" << " \ t "<<" ⑧ exit system "<<" \ n "; cout<< "\ t" << "\ T" << "\ T" << "************************************************" <<endl ; cout<<endl; cout<< "Please select the function (serial number) to be used:"; int t;//ordinal string temp;//flight number cin>>t; Switch (t) {case 1://① read in data {cout<< "Please select read mode (1: Auto read/2: Manual entry):"; int choose;//Select read Mode cin>>choose; if (choose==1) Create1 ();//1: Automatically read else if (choose==2) Create2 ();//2: Manual entry else cout<< "Ordinal error! Please verify and re-enter! "<<endl; cout<< "Information Entry success! "<<endl; Break } Case 2://② Delete data {cout<< "Please enter the flight number to be deleted and all information:"; String str; cin>>str; Delete (str); cout<< "deleted successfully! "<<endl; Break } Case 3://③ Flight number {cout<< "Please enter flight number:"; cin>>temp; Number (temp); Break Break } Case 4://④ starting point {cout<< "Please enter starting point:"; cin>>temp; Departure (temp); Break } Case 5://⑤ Arrival station {cout<< "Please enter terminal:"; cin>>temp; Terminal (temp); Break } Case 6://⑥ departure time {cout<< "Please enter departure time:"; cin>>temp; Start (temp); Break } Case 7://⑦ arrival time {cout<< "Please enter arrival time:"; cin>>temp; Arrival (temp); Break } Case 8://⑧ exit system {cout<< "program ends! After processing, please open the file to view the results! "<<endl; return 0; Break } default://The ordinal entered is not in the range between 1~8 {cout<< "ordinal error! Please verify and re-enter! "<<endl; Break }}} return 0;} /** function Name: getnum* function Description: Remove flight numberNumeric part * input parameter: K Flight number * OUTPUT parameter: no * return value: getnum-return flight number * Other instructions: no */int getnum (string k) {int getnum=0,temp=1000; getnum= (int (k[0]) *10000000+int (k[1]) *100000); for (int i=2; k[i]!= '); ++i)//Slightly off the first two capital letters {getnum+= (int (k[i]-' 0 ') *temp) that indicate the name of the airline; temp/=10; Return getnum;//Returns the flight number}/** function name: search* function Description: Find flight number in hash table * Input parameter: key-Flight number * OUTPUT parameter: no * return value: 0/-1-indicates yes/no find successful * Other instructions: no */int search (String key) {int I,adr; int K=getnum (key); adr=k%p; while (Ha[adr].number!=nullkey&&ha[adr].number!=key) {++i; Adr= (adr+1)%maxn;//uses linear probing method to find the next address} if (Ha[adr].number==key) return adr;//find success Else return-1;//find failed} /** function Name: delete* function Description: Delete the specified flight information in the hash table * Input parameters: key-Flight number * Output parameters: hashlen-Hash Table element number, ha[adr].number-flag is deleted * Return value: 0/-1-indicates yes/No lookup succeeded * Other instructions: no */int Delete (string key) {int ADR; Adr=search (key);//Find this keyword if (adr!=-1) {Ha[adr].number=delkey in the hash table; --hashlen;//hash Table element number minus one return 1; } else//The keyword was not found in the hash table return 0;} /** function Name: insert1* function Description:Automatically inserting flight information into a hash table * Input parameters: key-Flight number * Output parameter: hashlen-hash TABLE element number, ha[adr].number-keyword, ha[adr].cnt-probe count * return value: None * Other instructions: no */void Insert1 ( String key) {int i,adr; int K=getnum (key);//Take out the numerical part adr=k%p; if (ha[adr].number==nullkey| | Ha[adr].number==delkey) {ha[adr].number=key;//directly into the hash table ha[adr].cnt=1; infile>>ha[adr].data.departure>>ha[adr].data.terminal>>ha[adr].data.schedule; INFILE>>HA[ADR].DATA.START>>HA[ADR].DATA.ARRIVAL>>HA[ADR].DATA.TYPE>>HA[ADR]. Data.price; The else//conflict occurs when a linear probe is used to resolve the conflict {i=1;//element i is the number of times the conflict occurred while inserting (ha[adr].number!=nullkey&&ha[adr].number! =delkey) {adr= (adr+1)%p; ++i; } Ha[adr].number=key; ha[adr].cnt=i; infile>>ha[adr].data.departure>>ha[adr].data.terminal>>ha[adr].data.schedule; INFILE>>HA[ADR].DATA.START>>HA[ADR].DATA.ARRIVAL>>HA[ADR].DATA.TYPE>>HA[ADR]. Data.price; } ++hashlen;//the number of elements of a hash table plus a}/** function name: insert2* function Description: Manually inserting flight information into the hash tables by file * Input parameters: key-Flight number * Output parameters: hashlen-Hash table elements, ha[adr].number-keyword, ha[adr].cnt-probing times * return value: None * Other instructions: no */void Insert2 (string key) {int i,adr; int K=getnum (key); adr=k%p; if (ha[adr].number==nullkey| | Ha[adr].number==delkey) {Ha[adr].number=key; Ha[adr].cnt=1; Input (ADR);//Enter the remaining information manually} else {i=1; while (Ha[adr].number!=nullkey&&ha[adr].number!=delkey) {adr= (adr+1)%p; ++i; } Ha[adr].number=key; ha[adr].cnt=i; Input (ADR);//Enter the remaining information manually} ++hashlen;} /** function Name: create1* function Description: Automatically insert flight keyword information in hash table * Input parameters: key-Flight number * OUTPUT parameter: ha[adr].number-keyword is all initialized to Nullkey,ha[adr]. Cnt-probe count all initialized to 0* return value: None * Other instructions: no */void Create1 () {int i; for (i=0; i<maxn; ++i)//Initialize {Ha[i].number=nullkey; ha[i].cnt=0; } cout<< "Flight information read ... Please later ... "<<endl;//Prompt for (i=0;; ++i) {if (!infile)//test File Open operation succeeded {cerr<<" Open error! " <<endl; Exit (1); Open does not succeed, then prompt to exit} infile>>x[i];//read string s=x[i] from the file; if (s== "XX") break;//read in 00 o'clock end Read Else Insert1 (x[i]);//insert keyword into hash table} infile.close ();//read in to complete must Close file}/** function name: create2* function Description: Manually inserting flight keyword information into a hash table * Input parameters: key-Flight number * OUTPUT parameter: ha[adr].number-keyword is all initialized to Nullkey,ha[adr]. Cnt-probe count all initialized to 0* return value: None * Other instructions: no */void Create2 () {int i; for (i=0; i<maxn; ++i) {ha[i].number=nullkey; ha[i].cnt=0; } cout<< "Please enter flight information (input 00 o'clock entry end)" <<endl; for (i=0;; ++i) {cin>>x[i];//Enter the keyword manually from the keyboard string s=x[i]; if (s== "XX") break; else Insert2 (X[i]); }}/** function Name: number* function Description: Query the hash table for flight number * Input parameters: key-Flight number * Output parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, Ha[adr]. data.terminal-arrival station, ha[adr].data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, Ha[adr]. data.type-models, ha[adr].data.price-fares * return value: None * Other instructions: no */void number (string key) {int i,adr; int K=getnum (key); Adr=k%p; while (Ha[adr].number!=nullkey&&ha[adr].number!=key)//hash lookup {++i; Adr= (adr+1)%MAXN; } if (Ha[adr].number==key&&ha[adr].number!=delkey) {Output (ADR);//Display FileOutput (ADR) on the screen;//Deposit text The other cout<< "Sorry, no flight! "<<endl;} /** function Name: departure* function Description: Query the hash table for starting point * Input parameters: key-starting point * output parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, Ha[adr]. data.terminal-arrival station, ha[adr].data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, Ha[adr]. data.type-model, ha[adr].data.price-fare * return value: None * Other instructions: No */void departure (string key)//starting point {int i,flag=0; for (i=0; i<maxn; ++i)//order Find {if (flag) break; else if (Ha[i].data.departure==key) {Output (i); FileOutput (i); flag=1;//flag whether to query the relevant information}} if (!flag) cout<< "Sorry, no flight!" "<<endl;} /** function Name: terminal* function Description: Query terminal in hash table * Input parameters: key-Terminal * OUTPUT parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, Ha[adr]. data.terminal-Arrival Station, Ha[adr].data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, ha[adr].data.type-model, Ha[adr]. data.price-fare * return value: None * Other instructions: no */void Terminal (string key)//terminal {int i,flag=0; for (i=0; i<maxn; ++i) {if (flag) break; else if (Ha[i].data.terminal==key) {Output (i); FileOutput (i); flag=1; }} if (!flag) cout<< "Sorry, no flight!" "<<endl;} /** function Name: start* function Description: Check the departure time in the hash table * Input parameters: key-Departure time * OUTPUT parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, Ha[adr]. data.terminal-arrival station, ha[adr].data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, Ha[adr]. data.type-model, ha[adr].data.price-fare * return value: None * Other instructions: no */void Start (string key) {int i,flag=0; for (i=0; i<maxn; ++i) {if (flag) break; else if (Ha[i].data.start==key) {Output (i); FileOutput (i); flag=1; }} if (!flag) cout<< "Sorry, no flight!" "<<endl;} /** function Name: arrival* function Description: Query arrival in hash tableTime * Input parameters: key-arrival time * Output parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, ha[adr].data.terminal-arrival Station, Ha[adr]. data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, ha[adr].data.type-model, ha[adr].data.price-fare * return value: None * Other instructions: No */void arrival (string key) {int i,flag=0; for (i=0; i<maxn; ++i) {if (flag) break; else if (Ha[i].data.arrival==key) {Output (i); FileOutput (i); flag=1; }} if (!flag) cout<< "Sorry, no flight!" "<<endl;} /** function Name: fileoutput* function Description: Flight information Deposit file * Input parameters: location in hash table * OUTPUT parameters: ha[adr].number-Flight number, ha[adr].data.departure-starting point, Ha[adr]. data.terminal-arrival station, ha[adr].data.schedule-schedule, ha[adr].data.start-departure time, ha[adr].data.arrival-arrival time, Ha[adr]. data.type-models, ha[adr].data.price-fares * return value: None * Other instructions: no */void FileOutput (int adr) {ofstream outfile ("Search.dat", Ios::app) ///Append to File if (!outfile)//test File Open operation succeeded {cerr<< "open error!" <<endl; Exit (1);//Open unsuccessful, Prompt to exit} outfile<< "flight number" << "\ T" << "Starting point" << "T" << "Arrival Station" << "\ T" << "schedule" << "\ T" << "\ T" << "take-off time" << "\ T "; outfile<< "Arrival Time" << "T" << "model" << "\ T" << "fare" << "\ n"; outfile<
Operation Result:
Code Design Brief: (see code comment) use hash table storage according to flight number, the keyword is the flight number that is converted to numeric value; flight numbers resolve conflicts using linear probing; other queries use sequential lookups.
Flight information query and retrieval system-data Structure course design