In: Computer Science
Please write code in c++.
Use iostream (and any string library if you need it).
Create s structure plane :
First line contains n(0 < n < 1001).
Then n lines inputed in given format:
First - ID[int type]
Second - FromLocation[char*]
Third - ToLocation[char*]
Fourth - DepartureTime[char*]
Output:
Sorted list of planes should be in UPPER CASE.
Example of input:(it's just one of an examples, you need to write code generally)
10
40 Shuch Satp 05:47
89 Kyzy Taldy 07:00
95 Taldy Arka 03:20
61 Baiko Tara 06:31
73 Asta Akto 05:42
77 Akta Turkis 09:42
38 Oske Asta 08:35
94 Rid Sem 08:58
94 Taldy Baiko 03:00
55 Shah Temi 05:43
Output:
73 ASTA AKTO 05:42
95 TALDY ARKA 03:20
38 OSKE ASTA 08:35
94 TALDY BAIKO 03:00
40 SHUCH SATP 05:47
84 RID SEM 08:58
89 KYZY TALDY 07:00
61 BAIKO TARA 06:31
55 SHAH TEMI 05:43
77 AKTA TURKIS 09:42
As you see, you need to sort ToLocation by alphabet.
Program Code Screenshot
Program Sample Input/Output Screenshot
Program Code to Copy
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// create structure for plane
struct plane
{
int ID;
string FromLocation;
string ToLocation;
string DepartureTime;
};
// custom comparator to do sorting
bool sort_by_to_location(plane a, plane b)
{
return a.ToLocation < b.ToLocation;
}
string toUpperCase(string word)
{
// convert to uppercase
for (int i = 0; i < word.size(); i++)
{
if (word[i] >= 'a' && word[i] <= 'z')
{
word[i] = word[i] - 'a' + 'A';
}
}
return word;
}
int main()
{
// create a vector of planes
vector<plane> arr;
// read input from command line
int n;
cin >> n;
// read n values
while (n--)
{
plane p;
// read id
cin >> p.ID;
// read from
cin >> p.FromLocation;
//read to
cin >> p.ToLocation;
// read time
cin >> p.DepartureTime;
// convert from and to to uppercase
p.FromLocation = toUpperCase(p.FromLocation);
p.ToLocation = toUpperCase(p.ToLocation);
// add in vector
arr.push_back(p);
}
// sort using custom comparator
sort(arr.begin(), arr.end(), sort_by_to_location);
cout << endl
<< endl;
// show output
for (int i = 0; i < arr.size(); i++)
{
plane x = arr[i];
cout << x.ID << " " << x.FromLocation << " " << x.ToLocation << " " << x.DepartureTime << endl;
}
}