Модуль 2 / M2L4
.docx#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
setlocale(LC_ALL, "russsian");
ofstream fout("file.txt", ios_base::out | ios_base::trunc);
const int cnt = 5;
struct goods
{
int number;
char *name;
char *min_descript;
double price;
char *date;
} gds[cnt];
gds[0] = { 1, "Хлебушек", "Вкусный, домашний", 7.90, "годен до 24.02.16" };
gds[1] = { 2, "Гелевая ручка", "Обычная, бесмысленная", 1.20, "~" };
gds[2] = { 3, "Сыр Бельгийский", "Замечательный", 17.88, "годен до 11.02.16" };
gds[3] = { 4, "Молоко Домашнее", "Свежее", 18.55, "годно до 12.01.16" };
gds[4] = { 5, "Колбаса Колбико", "Докторская", 22.30, "годна до 22.03.16" };
fout << cnt << endl;
for (int i = 0; i < cnt; i++) {
fout << gds[i].number << endl
<< gds[i].name << endl
<< gds[i].min_descript << endl
<< gds[i].price << endl
<< gds[i].date << endl;
}
fout.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <locale.h>
#include <iomanip>
using namespace std;
int main() {
setlocale(LC_ALL, "russsian");
struct goods {
int number;
string name;
string min_descript;
double price;
string date;
};
ifstream fin;
fin.open("file.txt");
int cnt;
fin >> cnt;
goods *gds = new goods[cnt];
for (int i = 0; i < cnt; i++) {
fin >> gds[i].number >> gds[i].name >>
gds[i].min_descript >> gds[i].price >> gds[i].date;
}
cout << "\t----------------------------------------------------------------------------" << endl;
cout << "\t| Продовольчий магазин \"Антониус\" |" << endl;
cout << "\t----------------------------------------------------------------------------" << endl;
cout << "\t----------------------------------------------------------------------------" << endl;
cout << "\t|№ Товара | Наименование | Описание | Цена | Срок годности |" << endl;
for (int i = 0; i < 5; i++) {
cout << "\t----------------------------------------------------------------------------" << endl;
cout << "\t|" << setw(9) << gds[i].number << "|"
<< setw(16) << gds[i].name << "|"
<< setw(22) << gds[i].min_descript << "|"
<< setw(6) << gds[i].price << "|"
<< setw(17) << gds[i].date << "|" << endl;
}
cout << "\t----------------------------------------------------------------------------" << endl;
double comp;
int p;
if (1 == 1) {
double comp = 0;
int p = 0;
comp = gds[0].price;
for (int i = 0;i < 5;i++) {
if (gds[i].price >= comp) {
comp = gds[i].price;
p = i;
}
}
cout << "\t Самый дорогой продукт = " << gds[p].name << endl << endl;
fin.close();
cin.sync();
cin.get();
delete[] gds;
system("pause");
return 0;
}
}