Question

In: Computer Science

Please write code in c++ using iostream library. Also you can use any string library. Create...

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".

Solutions

Expert Solution

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:

.


Related Solutions

Please write code in c++. Use iostream (and any string library if you need it). Create...
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...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
(In C) Note: Can you create an example code of these tasks. use any variables you...
(In C) Note: Can you create an example code of these tasks. use any variables you wish to use. postfix expressions: (each individual text (T), (F), (NOT), etc is a token) F T NOT T F NOT T T T AND F T F NAND (1) Create stack s. (2) For each token, x, in the postfix expression: If x is T or F push it into the stack s. (T = true, F = false) Else if x is...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Please write this code in C++, also if you could please bold the names of the...
Please write this code in C++, also if you could please bold the names of the input files and provide comments for each choice. For this part, the program gives the user 4 choices for encrypting (or decrypting) the first character of a file. Non-lowercase characters are simply echoed. The encryption is only performed on lowercase characters. If c is char variable, then islower(c) will return true if c contains an lowercase character, false otherwise To read a single character...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT