Другие языки программирования и технологии

Задание по структурам c++

Помогите пожалуйста выполнить задание по структурам, буду очень благодарен
struct Train
{
string dest;
int no;
int hour;
int min;
};
Максим Сартаков
Максим Сартаков
6 159
Лучший ответ
Олег Зенько я не до конца пойму как сделать ввод данных с клавиатуры в массив, состоящий из 10 структур типа TRAIN, а не то, как создать структуру))
#include <iostream>
#include <array>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;
struct Train {
string destination;
string number;
string time;
friend bool operator<(const Train& a, const Train& b) {
return a.destination < b.destination;
}
friend bool operator>(const Train& a, const Train& b) {
return a.time > b.time;
}
};
using schedule_t = array<Train, 10U>;
enum class Format { All, Destination, Time };
void fill(schedule_t& schedule);
void show(const schedule_t& schedule, Format format = Format::All);
void output(const Train& train);
int main() {
schedule_t schedule;
fill(schedule);
show(schedule);
cout.put('\n');
show(schedule, Format::Destination);
cout.put('\n');
show(schedule, Format::Time);
cout.put('\n');
system("pause");
}
void fill(schedule_t& schedule) {
string destination, number, time;
for (auto &item : schedule) {
cout << "destination >>> ";
getline(cin, destination);
cout << "number >>> ";
cin >> number;
cout << "time format 00:00 >>> ";
cin >> time;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
item = { destination, number, time };
cout.put('\n');
}
}
void show(const schedule_t& schedule, Format format) {
if (format == Format::All) {
for (const auto &item : schedule) output(item);
return;
}
schedule_t tmp = schedule;
if (format == Format::Destination) {
auto lambda = [](const Train& a, const Train& b) {
return a < b;
};
sort(tmp.begin(), tmp.end(), lambda);
for (const auto &item : tmp) output(item);
} else if (format == Format::Time) {
auto lambda = [](const Train& a, const Train& b) {
return a > b;
};
sort(tmp.begin(), tmp.end(), lambda);
stack<Train> box;
cout << "Control time >>> ";
string time;
cin >> time;
for (const auto &item : tmp) {
if (item.time > time) box.push(item);
else break;
}
if (box.empty()) cout << "All trains sent!\n";
while (!box.empty()) {
output(box.top());
box.pop();
}
}
}
void output(const Train& train) {
cout << train.number << " " << train.destination << " " << train.time << '\n';
}
Sergii Ischenko
Sergii Ischenko
50 713
struct train { char *destination; usigned number; time_t departure; } tickets[10];

ctime - для работы с временем (http://www.cplusplus.com/reference/ctime/)
new/malloc и delete/free - для строки (сначала в буфер)
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Вводим структуру для времени
struct Time
{
int hours;
int minutes;

void set() {
cout << "hours = ";
cin >> hours;
cout << "minutes = ";
cin >> minutes;
}
};

// Вводим структуру для поезда
struct Train
{
string destinatioName;
int number;
Time departureTime;
};

// Ввод данных поезда
Train input()
{
Train tr;

cout << "Enter a destination: ";
cin >> tr.destinatioName;
cout << "Enter a train number: ";
cin >> tr.number;
cout << "Enter a departure time:\n";
tr.departureTime.set();

return tr;
}

// Вывод данных поезда
void display(const Train& tr)
{
cout << "Train:\n";
cout << "\tDestination: ";
cout << tr.destinatioName << endl;
cout << "\tTrain number: ";
cout << tr.number << endl;
cout << "\tDeparture time: ";
cout << tr.departureTime.hours << " : ";
cout << tr.departureTime.minutes << endl;
}

// Компаратор поездов по пункту назначения
bool compTrain(const Train& a, const Train& b)
{
return a.destinatioName < b.destinatioName;
}

// Компаратор времени
bool compTime(const Time& a, const Time& b)
{
if (a.hours != b.hours) return a.hours < b.hours;
return a.minutes < b.minutes;
}

int main()
{
const int all = 10;
vector<Train> trains; // массив поездов

// Вводим данные поездов
for ( int i = 0; i < all; ++i ) {
Train tr = input();
trains.emplace_back(tr);
}

// Сортируем по пункту назначения
sort(trains.begin(), trains.end(), compTrain);

// Выводим отсортированный список поездов
for ( auto train: trains ) display(train);

Time t; // задаем с клавиатуры время
cout << "Enter time:\n";
t.set();

int count = 0;
// выводим поезда, отправляющихся после введенного времени
for ( auto train: trains )
if (compTime(t, train.departureTime)) {
display(train);
count++;
}

// если таких поездов нет, выводим сообщение
if (!count) cout << "No such trains.\n";

system("pause");
return 0;
}