Mojiback »25 фев 2018, 12:50
Здравствуйте! У меня созрел следующий вопрос: как сравнить несколько динамических структур по определенным полям?
Имеется следующий код:
Код: Выделить всё
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct tour_agency
{
char name[15];
char tour[15];
char country[15];
int duration;
float price;
};
tour_agency* AddStruct(tour_agency* Obj, const int amount) // функция выделения памяти для структуры
{
if (amount == 0)
{
Obj = new tour_agency[amount + 1];
}
else
{
tour_agency* tempObj = new tour_agency[amount + 1];
for (int i = 0; i < amount; i++)
{
tempObj[i] = Obj[i];
}
delete[] Obj;
Obj = tempObj;
}
return Obj;
}
void setData(tour_agency* Obj, const int amount) // функция ввода данных в структуры
{
cout << "Название агенства: ";
cin.getline(Obj[amount].name, 15);
cout << "Тур: ";
cin.getline(Obj[amount].tour, 15);
cout << "Страна: ";
cin.getline(Obj[amount].country, 15);
cout << "Длительность тура: ";
cin >> Obj[amount].duration;
cout << "Стоимость: ";
cin >> Obj[amount].price;
cout << endl;
}
void showData(const tour_agency* Obj, const int amount) // функция вывода таблицы с введенными данными и записи в файл
{
cout << endl << "Введите название выходного файла (не более 20 символов!): ";
char file_name[20];
cin >> file_name;
strcat_s(file_name, ".txt");
ofstream out_file(file_name);
system("cls");
cout << "№ " << " Агенство\t" << setw(5) << "Тур\t" << setw(8) << "Страна\t" << setw(5) << "Длительность\t" << setw(5) << "Цена\t" << endl;
out_file << "№ " << " Агенство\t" << setw(5) << "Тур\t" << setw(8) << "Страна\t" << setw(5) << "Длительность\t" << setw(5) << "Цена\t" << endl;
cout << "----------------------------------------------------------------" << endl;
out_file << "----------------------------------------------------------------" << endl;
for (int i = 0; i < amount; i++)
{
cout << i + 1 << " " << Obj[i].name << '\t' << Obj[i].tour << '\t' << Obj[i].country << '\t' << Obj[i].duration << '\t' << setw(4) << Obj[i].price << endl;
out_file << i + 1 << " " << Obj[i].name << '\t' << Obj[i].tour << '\t' << Obj[i].country << '\t' << Obj[i].duration << '\t' << setw(4) << Obj[i].price << endl;
}
cout << endl;
system("pause");
}
int main()
{
setlocale(0, "");
tour_agency* our_tour_agency = 0;
int tour_agency_amount = 0;
int u_choice = 0; // продолжить или остановить ввод данных
do
{
our_tour_agency = AddStruct(our_tour_agency, tour_agency_amount);
setData(our_tour_agency, tour_agency_amount);
tour_agency_amount++;
cout << "Продолжить ввод данных? (1 - да, 0 - нет): ";
cin >> u_choice;
cin.get();
} while (u_choice != 0);
showData(our_tour_agency, tour_agency_amount);
delete[] our_tour_agency;
return 0;
}
Здравствуйте! У меня созрел следующий вопрос: как сравнить несколько динамических структур по определенным полям?
Имеется следующий код:
[code]#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct tour_agency
{
char name[15];
char tour[15];
char country[15];
int duration;
float price;
};
tour_agency* AddStruct(tour_agency* Obj, const int amount) // функция выделения памяти для структуры
{
if (amount == 0)
{
Obj = new tour_agency[amount + 1];
}
else
{
tour_agency* tempObj = new tour_agency[amount + 1];
for (int i = 0; i < amount; i++)
{
tempObj[i] = Obj[i];
}
delete[] Obj;
Obj = tempObj;
}
return Obj;
}
void setData(tour_agency* Obj, const int amount) // функция ввода данных в структуры
{
cout << "Название агенства: ";
cin.getline(Obj[amount].name, 15);
cout << "Тур: ";
cin.getline(Obj[amount].tour, 15);
cout << "Страна: ";
cin.getline(Obj[amount].country, 15);
cout << "Длительность тура: ";
cin >> Obj[amount].duration;
cout << "Стоимость: ";
cin >> Obj[amount].price;
cout << endl;
}
void showData(const tour_agency* Obj, const int amount) // функция вывода таблицы с введенными данными и записи в файл
{
cout << endl << "Введите название выходного файла (не более 20 символов!): ";
char file_name[20];
cin >> file_name;
strcat_s(file_name, ".txt");
ofstream out_file(file_name);
system("cls");
cout << "№ " << " Агенство\t" << setw(5) << "Тур\t" << setw(8) << "Страна\t" << setw(5) << "Длительность\t" << setw(5) << "Цена\t" << endl;
out_file << "№ " << " Агенство\t" << setw(5) << "Тур\t" << setw(8) << "Страна\t" << setw(5) << "Длительность\t" << setw(5) << "Цена\t" << endl;
cout << "----------------------------------------------------------------" << endl;
out_file << "----------------------------------------------------------------" << endl;
for (int i = 0; i < amount; i++)
{
cout << i + 1 << " " << Obj[i].name << '\t' << Obj[i].tour << '\t' << Obj[i].country << '\t' << Obj[i].duration << '\t' << setw(4) << Obj[i].price << endl;
out_file << i + 1 << " " << Obj[i].name << '\t' << Obj[i].tour << '\t' << Obj[i].country << '\t' << Obj[i].duration << '\t' << setw(4) << Obj[i].price << endl;
}
cout << endl;
system("pause");
}
int main()
{
setlocale(0, "");
tour_agency* our_tour_agency = 0;
int tour_agency_amount = 0;
int u_choice = 0; // продолжить или остановить ввод данных
do
{
our_tour_agency = AddStruct(our_tour_agency, tour_agency_amount);
setData(our_tour_agency, tour_agency_amount);
tour_agency_amount++;
cout << "Продолжить ввод данных? (1 - да, 0 - нет): ";
cin >> u_choice;
cin.get();
} while (u_choice != 0);
showData(our_tour_agency, tour_agency_amount);
delete[] our_tour_agency;
return 0;
}[/code]