Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "iostream"
- #include "cstdlib"
- #include "ctime"
- #include "string"
- using namespace std;
- const int Years = 5;
- struct Gold
- {
- string CountryName;
- int GoldMining[Years];
- };
- class TGold
- {
- private:
- Gold * Array;
- int NumberOfCountries;
- public:
- TGold(int);
- ~TGold();
- void Input();
- void RandomInput();
- void TableOutput();
- int SearchMaxYear(int Gold);
- float AverageGold(int Gold);
- void FieldSort(int Field, bool q);
- void TableFileOutput(string path, string pathbin);
- };
- TGold::TGold(int N) //выдедление оперативной памяти
- {
- NumberOfCountries = N;
- Array = new Gold[NumberOfCountries];
- }
- TGold::~TGold() //деструктор
- {
- delete[]Array;
- }
- void TGold::RandomInput() //ввод количества золотодобывающих стран и случайных данных о них
- {
- char *randomname = new char;
- for (int i = 0; i < NumberOfCountries; i++)
- {
- int length = 3 + rand() % 10;
- for (int z = 0; z < length ; z++)
- {
- randomname[z] = (char)(65 + rand() % 26);
- randomname[z+1] = '\0';
- }
- Array[i].CountryName = randomname;
- for (int j = 0; j < Years; j++)
- {
- Array[i].GoldMining[j] = 1 + rand() % 2000;
- }
- }
- }
- void TGold::Input() //ввод с клавиатуры количества золотодобывающих стран и данных о них
- {
- for (int i = 0; i < NumberOfCountries; i++)
- {
- cout << "Введите название страны:" << endl;
- cin >> Array[i].CountryName;
- for (int j = 0; j < Years; j++)
- {
- cout << "Введите показатель золотодобычи за год " << j + 1 << endl;
- cin >> Array[i].GoldMining[j];
- }
- }
- }
- int TGold::SearchMaxYear(int Gold) //поиск года с максимальной добычей в стране
- {
- int MaxYear = 1;
- int MaxValue = Array[Gold].GoldMining[0];
- for (int j = 0; j < Years; j++)
- {
- if (Array[Gold].GoldMining[j] > MaxValue)
- {
- MaxValue = Array[Gold].GoldMining[j];
- MaxYear = j + 1;
- }
- }
- return MaxYear;
- }
- float TGold::AverageGold(int Gold) //поиск среднего арифметического показателя золотодобычи в стране за 1 год
- {
- int sum = 0;
- for (int j = 0; j < Years; j++)
- {
- sum += Array[Gold].GoldMining[j];
- }
- return float(sum) / Years;
- }
- void TGold::FieldSort(int Field, bool q)
- {
- Gold x;
- int i, j, tmp;
- for (i = 0; i < NumberOfCountries - 1; i++)
- {
- for (j = i + 1; j < NumberOfCountries; j++)
- {
- tmp = 0;
- switch (Field)
- {
- case 1:
- tmp = strcmp(Array[i].CountryName.c_str(), Array[j].CountryName.c_str()) > 0; break;
- case 2:case 3:case 4:case 5: case 6:
- tmp = Array[i].GoldMining[Field - 2] > Array[j].GoldMining[Field - 2]; break;
- case 7:
- tmp = SearchMaxYear(i) > SearchMaxYear(j); break;
- case 8:
- tmp = AverageGold(i) > AverageGold(j); break;
- }
- if (q) tmp = !tmp;
- if (tmp)
- {
- x = Array[i];
- Array[i] = Array[j];
- Array[j] = x;
- }
- }
- }
- }
- void TGold::TableOutput() //вывод данных в виде таблицы
- {
- printf("%-20s", "Страна");
- printf("%-12s", "1й год");
- printf("%-12s", "2й год");
- printf("%-12s", "3й год");
- printf("%-12s", "4й год");
- printf("%-12s", "5й год");
- printf("%-12s", "Макс. год");
- printf("%-12s", "Ср. арифм.");
- printf("\n");
- for (int i = 0; i < NumberOfCountries; i++)
- {
- printf("%-20s", Array[i].CountryName.c_str());
- printf("%-12d", Array[i].GoldMining[0]);
- printf("%-12d", Array[i].GoldMining[1]);
- printf("%-12d", Array[i].GoldMining[2]);
- printf("%-12d", Array[i].GoldMining[3]);
- printf("%-12d", Array[i].GoldMining[4]);
- printf("%-12d", SearchMaxYear(i));
- printf("%-12f", AverageGold(i));
- cout << endl;
- }
- }
- void TGold::TableFileOutput(string pathtxt, string pathbin)
- {
- FILE *kq, *q;
- fopen_s(&kq, pathtxt.c_str(), "wt");
- fprintf(kq, "%-20s", "Страна");
- fprintf(kq, "%-12s", "1й год");
- fprintf(kq, "%-12s", "2й год");
- fprintf(kq, "%-12s", "3й год");
- fprintf(kq, "%-12s", "4й год");
- fprintf(kq, "%-12s", "5й год");
- fprintf(kq, "%-12s", "Макс. год");
- fprintf(kq, "%-12s", "Ср. арифм.");
- fprintf(kq, "\n");
- for (int i = 0; i < NumberOfCountries; i++)
- {
- fprintf(kq, "%-20s", Array[i].CountryName.c_str());
- fprintf(kq, "%-12d", Array[i].GoldMining[0]);
- fprintf(kq, "%-12d", Array[i].GoldMining[1]);
- fprintf(kq, "%-12d", Array[i].GoldMining[2]);
- fprintf(kq, "%-12d", Array[i].GoldMining[3]);
- fprintf(kq, "%-12d", Array[i].GoldMining[4]);
- fprintf(kq, "%-12d", SearchMaxYear(i));
- fprintf(kq, "%-12f", AverageGold(i));
- fprintf(kq, "\n");
- }
- fclose(kq);
- cout << "Произведена запись таблицы в файл .txt" << endl;
- fopen_s(&q, pathbin.c_str(), "wb");
- for (int i = 0; i < 5; i++)
- {
- fwrite(Array + i, sizeof(Gold), 1, q);
- }
- fclose(q);
- cout << "Произведена запись таблицы в файл .bin" << endl;
- }
- int main()
- {
- srand(time(NULL));
- setlocale(LC_ALL, "Russian");
- int n;
- cout << "Введите количество золотодобывающих стран" << endl;
- cin >> n;
- TGold a(n);
- int vvod;
- cout << "Случайный ввод - 0, ввод с клавиатуры - 1" << endl;
- cin >> vvod;
- if (vvod == 0)
- a.RandomInput();
- else
- a.Input();
- a.TableOutput();
- int Field, Sort;
- int NextSort = 1;
- while (NextSort)
- {
- do {
- cout << "Введите поле для сортировки (1-8)" << endl;
- cin >> Field;
- } while (Field < 1 || Field > 8);
- do
- {
- cout << "Сортировка по возрастанию - 0, по убыванию - 1" << endl;
- cin >> Sort;
- } while (Sort < 0 || Sort > 1);
- a.FieldSort(Field, Sort);
- system("CLS");
- a.TableOutput();
- do {
- cout << "Продолжать сортировать таблицу(1 - да, 0 - нет)" << endl;
- cin >> NextSort;
- } while (NextSort < 0 || NextSort > 1);
- if (NextSort)
- system("CLS");
- }
- a.TableFileOutput("C:\\Users\\STUD\\Desktop\\test\\text.txt", "C:\\Users\\STUD\\Desktop\\test\\file.bin");
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement