
Masa.Cpp
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <cstring>
#include <exception>
#include "StructTypes.h"
#include "Err.h"
#include "masA.h"
masA::masA(masA& z)
{
int i;
n = z.n;
if (n == 0) px = NULL;
else {
try {
px = new tur[n];
//throw bad_alloc();
}
catch (bad_alloc)
{
throw ErrMem("нет памяти", "исх. массива (px)",
"masA(masA &z)");
}
for (i = 0; i < n; i++)
px[i] = z.px[i];
}
}
masA& masA :: operator = (masA& z)
{
int i;
if (this == &z) return *this;
if (px != NULL) delete[]px;
n = z.n;
if (z.px == NULL) px = NULL;
else {
try {
px = new tur[n];
//throw bad_alloc();
}
catch (bad_alloc)
{
throw ErrMem("Нет памяти", "исх. массива (px)",
"masA::operator=(masA &z)");
}
for (i = 0; i < n; i++)
px[i] = z.px[i];
}
return *this;
}
void masA::inputFile()
{
ifstream fin;
string file;
string iniz;
tur t;
cout << "Имя входного файла:";
cin >> file;
fin.open(file.c_str());
if (fin.fail()) throw ErrFile("не открывается", file, "inputFile()");
n = 0;
if (px != NULL) { delete[] px; px = NULL; }
while (1)
{
fin >> t.nameFIO >> iniz >> t.price >> t.dr.name >> t.dr.priceDay >> t.dr.cnt >> t.dr.priceTransit;
if (fin.fail()) break;
n++;
}
fin.clear();
fin.seekg(0, ios::beg);
try {
px = new tur[n];
//throw bad_alloc();
}
catch (bad_alloc) // Обработчик без передачи данных
{
px = NULL; n = 0;
throw ErrMem("Нет памяти", "исх. массива (px)", "inputFile()");
}
if (px == NULL) {
cout << "Нет памяти.\n"; fin.close();
cout << "Ввести фаил не удается.\n";
_getch(); n = 0; return;
}
for (int i = 0; i < n; i++)
{
fin >> px[i].nameFIO >> iniz >> px[i].price >> px[i].dr.name >> px[i].dr.priceDay >> px[i].dr.cnt >> px[i].dr.priceTransit;
px[i].nameFIO = px[i].nameFIO + " " + iniz;
}
fin.close();
cout << "Файл введен " << endl;
_getch();
}
void masA::output()
{
int i;
if (px == NULL) { cout << "\n Данных нет.\n Массив пуст.\n"; _getch(); }
else {
cout << "____________________________________________________________________________________\n";
cout << "| | | | | | | \n";
cout << "| No | Фамилия и инициалы | Cтоимость | Название | Цена |Кол-во| Цена \n";
cout << "| | | | тура | 1-го дня | дней | проезда \n";
cout << "|____|______________________|____________|____________|____________|______|_________\n";
int i;
for (i = 0; i < n; i++)
cout << "| " << setw(2) << i + 1 << px[i];
cout << "|____|______________________|____________|____________|____________|______|_________\n";
_getch();
}
}
ostream& operator<<(ostream& out, masA& z)
{
int i;
out << "____________________________________________________________________________________\n";
out << "| | | | | | | \n";
out << "| No | Фамилия и инициалы | Cтоимость | Название | Цена |Кол-во| Цена \n";
out << "| | | | тура | 1-го дня | дней | проезда \n";
out << "|____|______________________|____________|____________|____________|______|_________\n";
for (i = 0; i < z.n; i++)
out << "| " << setw(2) << i + 1 << z.px[i];
_getch();
return out;
}
void masA::outputFile()
{
ofstream fout;
string file;
int i;
cout << "Имя выходного файла:"; cin >> file;
fout.open(file.c_str());
if (fout.fail()) throw ErrFile("не создается файл", file, "masA::outputFile");
// Вывод заголовка
fout << "____________________________________________________________________________________\n";
fout << "| | | | | | | | \n";
fout << "| No | Фамилия и инициалы | Cтоимость | Название | Цена |Кол-во| Цена | \n";
fout << "| | | | тура | 1-го дня | дней | проезда | \n";
fout << "|____|______________________|____________|____________|____________|______|_________|\n";
for (i = 0; i < n; i++)
fout << "| " << setw(2) << i + 1 << " | " << setw(20) << px[i].nameFIO << " | " << setw(10)
<< px[i].price << " | " << setw(10) << px[i].dr.name << " | " << setw(10) << px[i].dr.priceDay
<< " | " << setw(4) << px[i].dr.cnt << " | " << setw(6) << px[i].dr.priceTransit << " | \n";
fout << "|____|______________________|____________|____________|____________|______|_________\n";
fout.close();
cout << "Массив структур сохранен в файл.\n";
_getch();
}
void masA::addTur() {
int i;
tur f, * p; string iniz;
try {
p = new tur[n + 1];
//throw bad_alloc();
cout << "Фамилия и инициалы: ";
cin >> f.nameFIO >> iniz;
f.nameFIO = f.nameFIO + " " + iniz;
cout << "Стоимость: ";
cin >> f.price;
cout << "Название тура: ";
cin >> f.dr.name;
cout << "Цена 1-го дня: ";
cin >> f.dr.priceDay;
cout << "Кол-во дней: ";
cin >> f.dr.cnt;
cout << "Цена проезда: ";
cin >> f.dr.priceTransit;
for (i = 0; i < n; i++) p[i] = px[i];
p[n] = f;
n++;
delete[] px;
px = p;
cout << "Запись добавлена.\n";
_getch();
}
catch (bad_alloc) // Обработчик без передачи данных
{
throw ErrMem("Нет памяти", "исх массива", "addTur()");
}
}
void masA::deleteTur()
{
int j, i;
tur* p;
char ch;
masA::output();
cout << "Номер строки, которую вы хотите удалить: ";
// Защита от неправильного ввода номера строк
try {
cin >> j;
if (cin.fail()) throw Err("Это не номер строки", "deletTur()");
}
catch (string ex)
{
cin.clear();
string s;
cin >> s;
throw; // повторная генерация исключения (3)
}
if (j<1 || j>n) throw Err("Нет такой строки", "deleteTur()");
j--;
cout << " " << j + 1 << "-я строка:\n";
cout << px[j].nameFIO << " " << px[j].price << " " << px[j].dr.name << " " << px[j].dr.priceDay << " " << px[j].dr.cnt << " " << px[j].dr.priceTransit << " \n";
cout << "Удалить?(y/n):"; cin >> ch;
if (ch == 'n') throw Err("Отказано в удалении", "deleteTur()");
if (ch != 'y') throw Err("Отказано в удалении", "deleteTur()");
if (n == 1) { delete[] px; px = NULL; n = 0; }
else {
try {
p = new tur[n - 1];
//throw bad_alloc();
}
catch (bad_alloc) // Обработчик без передачи данных
{
throw ErrMem("Нет памяти", "исх. массива (px)", "deleteTur()");
}
if (p == NULL) {
cout << " Нет памяти.\n ";
cout << " Удалить не удается.\n";
_getch(); return;
}
for (i = 0; i < j; i++)
p[i] = px[i];
for (i = j + 1; i < n; i++)
p[i - 1] = px[i];
delete[] px; // удаление старого массива
px = p; // ротация имени
n--;
}
cout << "Запись удалена.\n"; _getch();
}
void masA::sortName()
{
int i, fl, nn;
tur t;
nn = n;
do {
fl = 0; nn--;
for (i = 0; i < nn; i++)
if (px[i].nameFIO > px[i + 1].nameFIO)
{
fl = 1; t = px[i];
px[i] = px[i + 1];
px[i + 1] = t;
}
} while (fl == 1);
cout << "Массив структур упорядочен по ФИО в алфавитном порядке\n";
_getch();
}
void masA::sortNameTur()
{
int i, fl, nn;
tur t;
nn = n;
do {
fl = 0; nn--;
for (i = 0; i < nn; i++)
if (px[i].dr.name > px[i + 1].dr.name)
{
fl = 1; t = px[i];
px[i] = px[i + 1];
px[i + 1] = t;
}
} while (fl == 1);
cout << "Массив структур упорядочен по наименованию тура в алфавитном порядке\n";
_getch();
}
void masA::sortCnt()
{
int i, fl, nn;
tur t;
nn = n; // вводим копию размера массива
do {
fl = 0; nn--;
for (i = 0; i < nn; i++)
if (px[i].dr > px[i + 1].dr)
{
fl = 1; t = px[i];
px[i] = px[i + 1];
px[i + 1] = t;
}
} while (fl == 1);
cout << "Массив структур упорядочен по количеству дней\n";
_getch();
}
ERR.H
#ifndef ERR_H
#define ERR_H
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <fstream>
#include <string>
#include <conio.h>
# include <stdlib.h>
# include <new>
using namespace std;
class Err
{
protected:
string mes; // Что за ошибка
string mes2; // в каком методе произошла
public:
Err(string mes0, string mes20)
: mes(mes0), mes2(mes20) {}
virtual void ErrOutput();
};
class ErrFile :public Err
{
protected:
string mes1; // при работе с каким файлом
public:
ErrFile(string mes0, string mes10, string mes20)
: Err(mes0, mes20), mes1(mes10) {}
virtual void ErrOutput();
};
class ErrMem : public ErrFile
{
protected:
public:
ErrMem(string mes0, string mes10, string mes20)
: ErrFile(mes0, mes10, mes20) {}
virtual void ErrOutput();
string getMes() { return mes; }
string getMes1() { return mes1; }
};
#endif
#pragma once
Err.cpp
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <fstream>
#include <string>
#include <conio.h>
# include <stdlib.h>
# include <new>
#include "Err.h"
using namespace std;
void Err::ErrOutput()
{
cout << "\nОшибка: " << mes << endl;
cout << "Произошла в методе: " << mes2 << endl;
_getch();
}
void ErrFile::ErrOutput()
{
cout << "\nОшибка: " << mes << endl;
cout << "При работе с файлом: " << mes1 << endl;
cout << "Произошла в методе: " << mes2 << endl;
_getch();
}
void ErrMem::ErrOutput()
{
cout << "\nОшибка: " << mes << endl;
cout << "При работе с файлом: " << mes1 << endl;
cout << "Произошла в методе: " << mes2 << endl;
_getch();
}