In: Computer Science
Please write code in c++ using iostream library. Also you can use any string library.
Create structure plane with the following:
1)plane's ID[int type]
2) location[char*]
3) departure time[char*]
Your task is to find the plane with the smallest departure time for the selected city.
Pointer, dynamic array and structures have to be used in this code.
Input:
NOTE: It's just an example of input, you should write code generally for any inputs.
First line contains n(0 < n < 1001)
Then n lines inputed in given format:
First - plane's ID[int type]
Second - location address[char*]
Third - departure time[char*]
Departure city.
example of input:
3
21 LoNdon 23:00
02 Bath 10:00
03 LondoN 22:00
LonDon
Output: example of output: 1_LONDON_22:00 output should be in uppercase
Ouput the information in the following format ID_LOCATION_TIME If there is more that one plane - output with smallest time. If there is no case to go by plane output "Impossible".
Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct plane //create structure named plane with 3 elements
{
int id;
char location[100];
char departure[100];
}planes[1001];
int removeColon(string s) //it remove colon from string and return integer
{
if (s.size() == 4) //if string has length 4 then remove colon from 1st position
s.replace(1, 1, "");
if (s.size() == 5)
s.replace(2, 1, ""); //if string has length 5 then remove colon from 2ndt position
return stoi(s);
}
int main()
{
int n,ind;
cout << "Enter total planes: "; //get total planes
cin>>n;
if(n>0 && n<1001){
for(int i=0;i<n;i++)
{
cin>>planes[i].id; //get id
cin >> planes[i].location; //get location
cin >> planes[i].departure; //get departure time
}
string loc; //get string to compare
cin>>loc;
transform(loc.begin(), loc.end(), loc.begin(), ::tolower); //convert location into lowercase
string t="23:59"; //define maximum time
for(int i=0;i<n;i++)
{
string l=planes[i].location;
transform(l.begin(), l.end(), l.begin(), ::tolower); //convert location into lowercase
if(l.compare(loc)==0){ //compare both location if return 0 then its same
string t1 = planes[i].departure;
if(removeColon(t1)<=removeColon(t)){ //remove colon from departure time and compare time
t=t1; //if minimun then store new time into t
ind=i; //and store index
}
}
}
cout<<ind<<endl;
string l1=planes[ind].location;
transform(l1.begin(), l1.end(), l1.begin(), ::toupper); //convert location into uppercase
cout<<endl<<planes[ind].id<<"_"<<l1<<"_"<<planes[ind].departure;
}
else
cout<<"Imposibble";
return 0;
}
Output:
.