C/C++
Помогите пожалуйста написать код на с++
Определить тип данных структура, имеющий поля марка машины, год выпуска, цвет, дата продажи и цена. Определить массив из 10 структур. В программе ввести в массив данные и вывести на экран список машин, отсортированный по маркам машин.
#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
#include <array>
using namespace std;
struct Car {
int year;
double price;
string model;
string color;
string date;
private:
friend bool operator<(const Car& a, const Car& b) {
return a.model < b.model;
}
};
void clear() {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Car input() {
Car car;
cout << "Model: ";
getline(cin, car.model);
cout << "Year: ";
cin >> car.year;
clear();
cout << "Color: ";
getline(cin, car.color);
cout << "Date: ";
getline(cin, car.date);
cout << "Price: ";
cin >> car.price;
clear();
return car;
}
void output(const Car& car) {
cout
<< car.model << ' '
<< car.year << ' '
<< car.color << ' '
<< car.date << ' '
<< car.price << '\n';
}
int main() {
array<Car, 10> catalog;
for (auto& car : catalog) car = input();
sort(catalog.begin(), catalog.end());
for (const auto& car : catalog) output(car);
}
#include <iostream>
#include <limits>
#include <string>
#include <array>
using namespace std;
struct Car {
int year;
double price;
string model;
string color;
string date;
private:
friend bool operator<(const Car& a, const Car& b) {
return a.model < b.model;
}
};
void clear() {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
Car input() {
Car car;
cout << "Model: ";
getline(cin, car.model);
cout << "Year: ";
cin >> car.year;
clear();
cout << "Color: ";
getline(cin, car.color);
cout << "Date: ";
getline(cin, car.date);
cout << "Price: ";
cin >> car.price;
clear();
return car;
}
void output(const Car& car) {
cout
<< car.model << ' '
<< car.year << ' '
<< car.color << ' '
<< car.date << ' '
<< car.price << '\n';
}
int main() {
array<Car, 10> catalog;
for (auto& car : catalog) car = input();
sort(catalog.begin(), catalog.end());
for (const auto& car : catalog) output(car);
}
А у меня так:
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
struct date { int d, m, y; };
struct car { string brand, color;
int yom, price; date dos; };
// yom - year of manufacturing
// dos - date of sale
void print(car avto)
{ cout << left << setw(9) << avto.color <<
setw(11) << avto.brand << setw(7) <<
avto.yom << right << " (" << setw(2) <<
avto.dos.d << '.' << avto.dos.m << '.' <<
avto.dos.y << ')' << setw(12) << avto.price << endl; }
int main()
{ int i, j; car cars[10] =
{ { "Lada", "red", 2000, 234000, {1, 1, 2001 } },
{ "Chevrolet", "brown", 2001, 345000, { 3, 3, 2003 } },
{ "Mercedes", "white", 2002, 456000, { 2, 2, 2002 } },
{ "Renault", "black", 2003, 567000, { 4, 4, 2004 } },
{ "Honda", "yellow", 2004, 678000, { 5, 5, 2005 } },
{ "Daewoo", "orange", 2005, 789000, { 6, 6, 2006 } },
{ "Porsche", "blue", 2006, 890000, { 7, 7, 2007 } },
{ "Bentley", "rose", 2007, 910000, { 8, 8, 2008 } },
{ "Volvo", "green", 2008, 1000000, { 9, 9, 2009 } },
{"Audi", "magenta", 2009, 1234560, { 1, 1, 2009 } } };
for (i = 0; i < 10; i++) print(cars[i]); cout << endl;
for (i = 1; i < 10; i++) for (j = 9; j >= i; j--)
if (cars[j - 1].brand > cars[j].brand)
swap(cars[j - 1], cars[j]);
for (i = 0; i < 10; i++) print(cars[i]); }

#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
struct date { int d, m, y; };
struct car { string brand, color;
int yom, price; date dos; };
// yom - year of manufacturing
// dos - date of sale
void print(car avto)
{ cout << left << setw(9) << avto.color <<
setw(11) << avto.brand << setw(7) <<
avto.yom << right << " (" << setw(2) <<
avto.dos.d << '.' << avto.dos.m << '.' <<
avto.dos.y << ')' << setw(12) << avto.price << endl; }
int main()
{ int i, j; car cars[10] =
{ { "Lada", "red", 2000, 234000, {1, 1, 2001 } },
{ "Chevrolet", "brown", 2001, 345000, { 3, 3, 2003 } },
{ "Mercedes", "white", 2002, 456000, { 2, 2, 2002 } },
{ "Renault", "black", 2003, 567000, { 4, 4, 2004 } },
{ "Honda", "yellow", 2004, 678000, { 5, 5, 2005 } },
{ "Daewoo", "orange", 2005, 789000, { 6, 6, 2006 } },
{ "Porsche", "blue", 2006, 890000, { 7, 7, 2007 } },
{ "Bentley", "rose", 2007, 910000, { 8, 8, 2008 } },
{ "Volvo", "green", 2008, 1000000, { 9, 9, 2009 } },
{"Audi", "magenta", 2009, 1234560, { 1, 1, 2009 } } };
for (i = 0; i < 10; i++) print(cars[i]); cout << endl;
for (i = 1; i < 10; i++) for (j = 9; j >= i; j--)
if (cars[j - 1].brand > cars[j].brand)
swap(cars[j - 1], cars[j]);
for (i = 0; i < 10; i++) print(cars[i]); }

Похожие вопросы
- Помогите пожалуйста написать код.(C++)
- Помогите пожалуйста написать код на c++, выводящий имя, фамилию и дату рождения нескольких человек
- Помогите пожалуйста написать код на C++
- Срочно!!! Помогите пожалуйста написать код к задачке на с++! Заранее спасибо!
- Помогите, пожалуйста, написать код на C (Си)
- Программирование С++. Помогите, пожалуйста, написать код (по-проще как-нибудь)
- Помогите пожалуйста с кодом на с++
- Помогите пожалуйста написать программу на Си
- Помогите пожалуйста с кодом....
- Помогите пожалуйста с кодом на с++