MasC.Cpp:
#include "Err.h"
#include "masC.h"
#include "StructTypes.h"
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <cstring>
#include <exception>
masC::masC(masC& z)
try : masB(z) {
int i;
l = z.l;
if (l == 0) pz = NULL;
else {
try {
pz = new tur[l];
//throw bad_alloc();
}
catch (bad_alloc&)
{
throw ErrMem("нет памяти", "студентов группы(pz)", "masC(masC &z)");
}
for (i = 0; i < l; i++)
pz[i] = z.pz[i];
}
}
catch (ErrMem& ex) { throw ErrMem(ex.getMes(), ex.getMes1(), " masС(masС &z)"); }
masC& masC :: operator=(masC& z)
{
int i;
if (this == &z) return *this;
try { masB :: operator=(z); }
catch (ErrMem& ex) {
throw ErrMem(ex.getMes(), ex.getMes1(), "masC::operator==(masC & z)");
}
if (pz != NULL) delete[]pz;
l = z.l;
try {
pz = new tur[l];
//throw bad_alloc();
}
catch (bad_alloc)
{
throw ErrMem("нет памяти", "массива (pz)", "masC::operator==(masC &z)");
}
for (i = 0; i < l; i++)
pz[i] = z.pz[i];
return *this;
}
masC& masC :: operator=(masA& z)
{
int i;
masC r;
if (this == &z) return *this;
masB::operator=(z);
r = (masC&)z;
if (pz != NULL) delete[]pz;
l = r.l;
if (r.pz == NULL) pz = NULL;
else {
try {
pz = new tur[l];
throw bad_alloc();
}
catch (bad_alloc)
{
throw ErrMem("нет памяти", "массива (pz)", "masC::operator==(masA &z)");
}
for (i = 0; i < l; i++)
pz[i] = r.pz[i];
}
return *this;
}
void masC::findPrice()
{
int i, j, cnt;
tur* y;
try {
y = new tur[n];
//throw bad_alloc();
}
catch (bad_alloc) // Обработчик без передачи данных
{
throw ErrMem("нет памяти", "раб. массива (y)", "findGrup()");
}
cout << "Стоимость поездки: ";
cin >> cnt;
l = 0;
for (i = 0; i < n; i++)
if (px[i].price == cnt) {
y[l] = px[i];
l++;
}
if (pz != NULL) delete[] pz;
try {
pz = new tur[l];
//throw bad_alloc();
}
catch (bad_alloc) // Обработчик без передачи данных
{
pz = NULL;
l = 0;
delete[]y;
throw ErrMem("нет памяти", "массива студ. (pz)", "findGrup()");
}
for (j = 0; j < l; j++)
pz[j] = y[j];
delete[] y;
cout << "Массив групп сформирован.\n";
_getch();
}
void masC::sortName()
{
int i, fl, nn;
tur t;
nn = l;
do {
fl = 0; nn--;
for (i = 0; i < nn; i++)
if (pz[i].dr.name > pz[i + 1].dr.name)
{
fl = 1; t = pz[i];
pz[i] = pz[i + 1];
pz[i + 1] = t;
}
} while (fl == 1);
_getch();
}
void masC::sortCnt()
{
int fl, i, nn;
tur t;
nn = l;
int a;
do {
fl = 0; nn--;
for (i = 0; i < nn; i++)
if (pz[i].dr.cnt > pz[i + 1].dr.cnt)
{
fl = 1; t = pz[i];
pz[i] = pz[i + 1];
pz[i + 1] = t;
}
} while (fl == 1);
_getch();
}
void masC::outputFile()
{
ofstream fout;
string file;
int i;
cout << "Имя выходного файла:"; cin >> file;
fout.open(file.c_str());
if (fout.fail()) throw ErrFile("не создается файл", file, "masС::outputFile");
fout << "____________________________________________________________________________________\n";
fout << "| | | | | | | | \n";
fout << "| No | Фамилия и инициалы | Cтоимость | Название | Цена |Кол-во| Цена | \n";
fout << "| | | | тура | 1-го дня | дней | проезда | \n";
fout << "|____|______________________|____________|____________|____________|______|_________|\n";
for (i = 0; i < l; i++)
fout << "| " << setw(2) << i + 1 << " | " << setw(20) << pz[i].nameFIO << " | " << setw(10)
<< pz[i].price << " | " << setw(10) << pz[i].dr.name << " | " << setw(10) << pz[i].dr.priceDay
<< " | " << setw(4) << pz[i].dr.cnt << " | " << setw(6) << pz[i].dr.priceTransit << " | \n";
fout << "|____|______________________|____________|____________|____________|______|_________\n";
fout.close();
cout << "Массив структур сохранен в файл.\n";
_getch();
}
void masC::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 < l; i++)
cout << "| " << setw(2) << i + 1 << " | " << setw(20) << pz[i].nameFIO << " | " << setw(10)
<< pz[i].price << " | " << setw(10) << pz[i].dr.name << " | " << setw(10) << pz[i].dr.priceDay
<< " | " << setw(4) << pz[i].dr.cnt << " | " << setw(6) << pz[i].dr.priceTransit << " | \n";
cout << "|____|______________________|____________|____________|____________|______|_________\n";
_getch();
}
}
ostream& operator<<(ostream& out, masC& z)//перегруженная операция вывода на экран для объекта masA
{
int i;
out << "____________________________________________________________________________________\n";
out << "| | | | | | | \n";
out << "| No | Фамилия и инициалы | Cтоимость | Название | Цена |Кол-во| Цена \n";
out << "| | | | тура | 1-го дня | дней | проезда \n";
out << "|____|______________________|____________|____________|____________|______|_________\n";
for (i = 0; i < z.l; i++)
out << "| " << setw(2) << i + 1 << z.pz[i];
out << "|____|______________________|____________|____________|____________|______|_________\n";
_getch();
return out;
}
MASA.H
#ifndef MASA_H
#define MASA_H
#include "Err.h"
#include "StructTypes.h"
#include <iostream>
#include <string>
#include <stdexcept>
#include <conio.h> // для _getch()
using namespace std;
class masA {
protected:
tur* px;
int n;
public:
masA() :px(NULL), n(0) {}
masA(masA& z);
virtual ~masA() { if (px != NULL) delete[]px; cout << "~masA()\n"; }
virtual masA& operator=(masA& z) = 0;
friend ostream& operator<<(ostream& out, masA& z);
friend ostream& operator<<(ostream& in, tur& f);
void inputFile();
virtual void output() = 0;
void addTur();
void deleteTur();
virtual void outputFile()=0;
virtual void sortName();
virtual void sortNameTur();
virtual void sortCnt()=0;
};
#endif // MASA_H
#pragma once
