Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OOP_3rdLaba.cpp: определяет точку входа для консольного приложения.
- //
- #include"stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <cstdlib> // для cls
- #include <fstream> // для файловых потоков
- #include <windows.h> // для setconsole
- #include <string> // для string
- #include <iomanip> // для setw
- using namespace std;
- const int N = 100;
- struct avto
- {
- string mark;
- int power;
- int capacity;
- int weight;
- };
- struct record
- {
- avto av;
- string fam;
- string name;
- string otch;
- string number;
- };
- struct marka
- {
- string mrk;
- int nm;
- };
- struct ves
- {
- int wght;
- int nw;
- };
- struct moshnost
- {
- int mst;
- int nmo;
- };
- class masrecord
- {
- private:
- record x[N];
- int n;
- public:
- masrecord() :n(0) {} // конструктор без параметров
- void inputMasRecordFile();
- void addRecord();
- void outputMasRecord();
- void outputMasRecordFile();
- void deleteRecord();
- void sortPower();
- void sortMark();
- void sortFamNameOtch();
- friend void perechMark(masrecord v1, masmark &v2);
- friend void perechWeight(masrecord v1, masweight &v3);
- friend void perechPower(masrecord v1, maspower &v4);
- };
- class masmark
- {
- private:
- marka z[N];
- int l;
- public:
- masmark() :l(0) {}
- friend void perechMark(masrecord v1, masmark &v2);
- void outputMasMark();
- void outputMasMarkFile();
- void sortMark();
- };
- class masweight
- {
- private:
- ves z[N];
- int l;
- public:
- masweight() :l(0) {}
- friend void perechWeight(masrecord v1, masweight &v3);
- void outputMasMark();
- void outputMasMarkFile();
- void sortChislo();
- };
- class maspower
- {
- private:
- moshnost z[N];
- int l;
- public:
- maspower() :l(0) {}
- friend void perechPower(masrecord v1, maspower &v4);
- void outputMasMark();
- void outputMasMarkFile();
- void sortChislo();
- };
- class Agregat
- {
- private:
- masrecord w1;
- masmark w2;
- masweight w3;
- maspower w4;
- public:
- void inputvFile();
- void outputv();
- void outputw();
- void outputwFile();
- void perechMark();
- Agregat(Agregat &z);
- Agregat operator= (Agregat &z);
- };
- void masrecord::addRecord()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- record t;
- if (n == N) { cout << "Нет места"; _getch(); return; }
- cout << "Марка автомобиля: "; cin >> t.av.mark;
- cout << "Мощность двигателя(в лошадиных силах): "; cin >> t.av.power;
- cout << "Объем бака(в литрах): "; cin >> t.av.capacity;
- cout << "Вес автомобиля(в килограммах): "; cin >> t.av.weight;
- cout << "Фамилия владельца: "; cin >> t.fam;
- cout << "Имя владельца: "; cin >> t.name;
- cout << "Отчество владельца: "; cin >> t.otch;
- cout << "Номер автомобиля: "; cin >> t.number;
- x[n] = t;
- n++;
- cout << "Запись добавлена." << endl;
- _getch();
- }
- void masrecord::outputMasRecord()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- int i;
- cout << "БД владельцев автомобилей:\n";
- cout << left;
- cout << " ------------------------------------------------------------------------------------------------\n";
- cout << " |Номер |Марка |Мощность |Объем|Вес | | | |Номер |\n";
- cout << " |записи|автомобиля|двигателя|бака |автомобиля|Фамилия |Имя |Отчество |автомобиля|\n";
- cout << " ------------------------------------------------------------------------------------------------\n";
- for (i = 0; i < n; i++)
- cout << " |" << setw(6) << i + 1 << "|" << setw(10) << x[i].av.mark << "|" << setw(9) << x[i].av.power << "|" << setw(5) << x[i].av.capacity << "|" << setw(10) << x[i].av.weight << "|" << setw(12) << x[i].fam << "|" << setw(10) << x[i].name << "|" << setw(14) << x[i].otch << "|" << setw(10) << x[i].number << "|" << endl;
- cout << " ------------------------------------------------------------------------------------------------\n";
- _getch(); return;
- }
- void masrecord::inputMasRecordFile()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string file;
- ifstream fin;
- cout << "Имя файла, который вы хотите открыть: ";
- cin >> file;
- fin.open(file);
- if (fin.fail()) { cout << file << " " << "Невозможно открыть файл...\n"; _getch(); return; }
- n = 0;
- while (true)
- {
- fin >> x[n].av.mark >> x[n].av.power >> x[n].av.capacity >> x[n].av.weight >> x[n].fam >> x[n].name >> x[n].otch >> x[n].number;
- if (fin.fail()) break;
- n++;
- }
- cout << "Файл введен\n";
- fin.close();
- _getch();
- }
- void masrecord::outputMasRecordFile()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- string file;
- ofstream fout;
- cout << "Имя файла, который вы хотите создать: ";
- cin >> file;
- fout.open(file);
- if (fout.fail()) { cout << file << " Файл не создан...\n"; _getch(); return; }
- fout << " ------------------------------------------------------------------------------------------------\n";
- fout << " |Номер |Марка |Мощность |Объем|Вес | | | |Номер |\n";
- fout << " |записи|автомобиля|двигателя|бака |автомобиля|Фамилия |Имя |Отчество |автомобиля|\n";
- fout << " ------------------------------------------------------------------------------------------------\n";
- for (int i = 0; i < n; ++i)
- {
- fout << " |" << setw(6) << i + 1 << "|" << setw(10) << x[i].av.mark << "|" << setw(9) << x[i].av.power << "|" << setw(5) << x[i].av.capacity << "|" << setw(10) << x[i].av.weight << "|" << setw(12) << x[i].fam << "|" << setw(10) << x[i].name << "|" << setw(14) << x[i].otch << "|" << setw(10) << x[i].number << "|" << endl;
- }
- fout << " ------------------------------------------------------------------------------------------------\n";
- fout.close();
- cout << "Результаты сохранены в файле " << file; _getch(); return;
- }
- void masrecord::deleteRecord()
- {
- int j, i;
- char ch;
- outputMasRecord();
- cout << "Удалить строку: ";
- cin >> j;
- if (j < 1 || j > n) { cout << "Такой строки нет.\n"; _getch(); return; }
- j--; // уменьшаем чтобы обратиться к индексу массива
- cout << x[j].av.mark << " " << x[j].av.power << " " << x[j].av.capacity << " " << x[j].av.weight << " " << x[j].fam << " " << x[j].name << " " << x[j].otch << " " << x[j].number << endl;
- cout << "Удалить?(y/n): ";
- cin >> ch;
- if (ch == 'n')return;
- if (ch == 'y') {
- for (i = j + 1; i < n; i++)
- x[i - 1] = x[i];
- n--;
- cout << "Запись удалена.\n";
- _getch();
- }
- else cout << "Вы ввели неверный вариант выбора.\n"; _getch(); return;
- }
- void masrecord::sortPower()
- {
- int i, fl;
- record t;
- do
- {
- fl = 0;
- for (i = 0; i < n - 1; i++)
- if (x[i].av.power > x[i + 1].av.power)
- {
- fl = 1;
- t = x[i];
- x[i] = x[i + 1];
- x[i + 1] = t;
- }
- } while (fl == 1);
- cout << "Массив упорядочен\n";
- _getch();
- }
- void masrecord::sortMark()
- {
- int i, fl;
- record t;
- do
- {
- fl = 0;
- for (i = 0; i < n - 1; i++)
- if (x[i].av.mark > x[i + 1].av.mark)
- {
- fl = 1;
- t = x[i];
- x[i] = x[i + 1];
- x[i + 1] = t;
- }
- } while (fl == 1);
- cout << "Массив упорядочен\n";
- _getch();
- }
- int sravnFamNameOtch(record v, record w)
- {
- if (v.fam > w.fam) return 1;
- if (v.fam < w.fam) return -1;
- if (v.name > w.name) return 1;
- if (v.name < w.name) return -1;
- if (v.otch > w.otch) return 1;
- if (v.otch < w.otch) return -1;
- return 0;
- }
- void masrecord::sortFamNameOtch()
- {
- int i, fl;
- record t;
- do
- {
- fl = 0;
- for (i = 0; i < n - 1; i++)
- if (sravnFamNameOtch(x[i], x[i + 1])>0)
- {
- fl = 1;
- t = x[i];
- x[i] = x[i + 1];
- x[i + 1] = t;
- }
- } while (fl == 1);
- cout << "Массив упорядочен\n";
- _getch();
- }
- Agregat::Agregat(Agregat &z)
- {
- w1 = z.w1;
- w2 = z.w2;
- w3 = z.w3;
- w4 = z.w4;
- }
- Agregat Agregat::operator= (Agregat &z)
- {
- if (this == &z) return *this;
- w1 = z.w1;
- w2 = z.w2;
- w3 = z.w3;
- w4 = z.w4;
- return *this;
- }
- void Agregat::inputvFile()
- {
- w1.masrecord::inputMasRecordFile(); // w1.input_from_file()
- }
- void Agregat::outputv()
- {
- w1.masrecord::outputMasRecord();
- }
- void Agregat::perechMark() {
- perechMark(w1, w2);
- }
- void perechMark(masrecord v1, masmark &v2)
- {
- int fl, n, l;
- n = v1.n;
- l = v2.l;
- for (int i = 0; i < n; ++i)
- {
- fl = 0;
- for (int j = 0; j < l; ++j)
- {
- if (v1.x[i].av.mark == v2.z[j].mrk) {
- fl = 1;
- v2.z[j].nm++;
- }
- }
- if (fl == 0) {
- v2.z[l].mrk = v1.x[i].av.mark;
- v2.z[l].nm = 1;
- l++;
- }
- }
- v2.l = l;
- cout << "Перечень создан.\n"; _getch();
- }
- /*void perechMrk(masrecord&c, masrecord&d)
- {
- int i, j, fl, n, l;
- l = 0;
- n = c.n;
- for (i = 0; i < n; i++)
- {
- for(j=0;j<l;j++)
- if (x[i].av.mark==d.y[j].mark)
- {
- fl = 1; d.y[j].nm++;
- }
- if (fl == 0)
- {
- d.y[l].mark == c.x[i].av.mark;
- d.y[l].nm = 1;
- l++;
- }
- }
- d.l = l;
- cout << ""; _getch();
- }
- */
- /*masrecord::masrecord(masrecord&y)
- {
- int i, j;
- n = y.n;
- if (n == 0) x == NULL;
- else {
- if (x == NULL) { cout << "Нет памяти.\n"; _getch(); exit(1); }
- for (i = 0; i < n; i++)
- x[i] = y.x[i];
- }
- l = y.l;
- if (l == 0) z == NULL;
- else {
- if (z == NULL) { cout << "Нет памяти.\n"; _getch(); exit(1); }
- for (j = 0; j <l; j++)
- z[j] = y.z[j];
- }
- cout << "Сработал конструктор копирования.\n";
- }
- masrecord& masrecord::operator=(masrecord& y)
- {
- if (this == &y) return *this;
- if (n == y.n)
- for (int i = 0; i < n; i++)
- x[i] = y.x[i];
- else {
- if (x != NULL)delete[]x;
- n = y.n;
- if (x == NULL) {}
- for (int i = 0; i < n; i++)
- x[i] = y.x[i];
- }
- if (l == y.l)
- for (int j = 0; j < l; j++)
- z[j] = y.z[j];
- else {
- if (z != NULL)delete[]x;
- l = y.l;
- if (z == NULL) {}
- for (int j = 0; j < l; j++)
- z[j] = y.z[j];
- }
- cout << "Присваивание выполнено.\n"; _getch();
- return *this;
- }
- */
- int main()
- {
- setlocale(0, "");
- masrecord a;
- Agregat aa;
- int j;
- while (true)
- {
- system("cls");
- cout << "1-Загрузка из файла\n";
- cout << "2-Сохранение результатов обработки в файл\n";
- cout << "3-Добавление записи\n";
- cout << "4-Удаление записи\n";
- cout << "5-Вывод на экран дисплея данных и результатов обработки\n";
- cout << "6-Алфавитная сортировка по Ф.И.О.\n";
- cout << "7-Алфавитная сортировка по названию марки автомобиля\n";
- cout << "8-Числовая сортировка по мощности двигателя\n";
- cout << "9-Перечень марок автомобиля с указанием их числа (результат отсортирован по названию марки автомобиля в алфавитном порядке)\n";
- cout << "10-Перечень весов автомобиля с указанием числа автомобилей (результат отсортирован по числу автомобилей)\n";
- cout << "11-Перечень мощностей двигателя с указанием числа автомобилей (результат отсортирован по мощности двигателя)\n";
- cout << "12-Сохранения перечня в файл\n";
- cout << "13-Вывод перечня\n";
- cout << "14-Проверка работы конструктора копирования\n";
- cout << "15-Проверка работы перегруженной операции присваивания\n";
- cout << "16-Выход из программы\n";
- cout << "\nВаш выбор (1-16):";
- cin >> j;
- switch (j)
- {
- case 1:aa.inputvFile(); break;
- case 2:a.outputMasRecordFile(); break;
- case 3:a.addRecord(); break;
- case 4:a.deleteRecord(); break;
- case 5:aa.outputv(); break;
- case 6:a.sortFamNameOtch(); break;
- case 7:a.sortMark(); break;
- case 8:a.sortPower(); break;
- case 9:
- {
- aa.perechMark();
- // aa.sortMarkPerech(); break;
- }
- case 10:
- {
- //aa.perechWeight();
- //aa.sortKolichestvoPerech(); break;
- }
- case 11:
- {
- // aa.perechPower();
- // aa.sortPowerPerech(); break;
- }
- case 12:aa.outputw(); break;
- case 13:aa.outputwFile(); break;
- /*case 14:
- {
- masrecord b(a);
- a.outputMasRecord();
- b.outputMasRecord();
- a.outputPerech();
- b.outputPerech(); break;
- }
- case 15:
- {
- c = d = a;
- a.outputMasRecord();
- a.outputoutputPerech();
- c.outputMasRecord();
- c.outputPerech();
- d.outputMasRecord();
- d.outputPerech(); break;
- }
- */
- case 15:cout << "Завершение работы..."; _getch(); return 0;
- default:cout << "Нет такого пункта в меню"; _getch(); break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement