PhotoShaman

Laba12

Feb 21st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <iostream> //---------Вариант 4---------
  2. #include <fstream>
  3. #include <windows.h>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. string const inputAddress = "C:\\Users\\User\\Desktop\\notebook.dat",
  11.              outputAddress = "C:\\Users\\User\\Desktop\\note_out.dat";
  12.  
  13. struct notebook {
  14.     char model[20];         // найменування моделі
  15.     int price;              // ціна
  16.     int disp_x;             // роздільна здатність дисплея по горизонталі
  17.     int disp_y;             // роздільна здатність дисплея по вертикалі
  18.     int f;                  // частота регенерації
  19.     float d;                // розмір діагоналі дисплея
  20.     float weight;           // вага
  21. };
  22.  
  23. void DataOutput() // Вывод данных из файла
  24. {
  25.     ifstream Fin(outputAddress, ios::binary);
  26.     int notebookCount;
  27.     notebook fileRead;
  28.     Fin >> notebookCount;
  29.     for (int i = 0; i < notebookCount; i++)
  30.     {
  31.         if (Fin.read((char*)&fileRead, sizeof(notebook)))
  32.         {
  33.             cout << endl << "-----notebook: " << i + 1;
  34.             cout << endl << "model: " << fileRead.model;
  35.             cout << endl << "price: " << fileRead.price;
  36.             cout << endl << "disp_x: " << fileRead.disp_x;
  37.             cout << endl << "disp_y: " << fileRead.disp_y;
  38.             cout << endl << "f: " << fileRead.f;
  39.             cout << endl << "d: " << fileRead.d;
  40.             cout << endl << "weight: " << fileRead.weight;
  41.         }
  42.     }
  43.     Fin.close();
  44. }
  45. void main()
  46. {
  47.     notebook *notebookArray;
  48.     int notebookCount = 0;
  49.  
  50.     ifstream Fin(inputAddress, ios::binary);
  51.     notebook fileRead;
  52.     while (!Fin.eof()) // Считаем сколько всего ноутбуков
  53.     {
  54.         if (Fin.read((char*)&fileRead, sizeof(notebook)))
  55.             notebookCount++;
  56.     }
  57.     Fin.close();
  58.     if (notebookCount > 0) // Если удалось считать
  59.     {
  60.         notebookArray = new notebook[notebookCount];
  61.  
  62.         Fin.open(inputAddress, ios::binary);
  63.         Fin.read((char*)notebookArray, notebookCount * sizeof(notebook)); // Считываем весь список в массив
  64.         Fin.close();
  65.         vector <notebook> fMoreThan80notebookArray; // Вектор структур ноутов с частотой больше чем 80
  66.         for (int i = 0; i < notebookCount; i++) // Заполняем вектор
  67.         {
  68.             if (notebookArray[i].f > 80)
  69.                 fMoreThan80notebookArray.push_back(notebookArray[i]);
  70.         }
  71.         int notebookArrayCount = fMoreThan80notebookArray.size();
  72.         // С радостью сортируем вектор структур по возрастанию цены
  73.         sort(fMoreThan80notebookArray.begin(), fMoreThan80notebookArray.end(), [](const notebook& a, const notebook& b) {
  74.             return a.price < b.price;
  75.         });
  76.         // Записуем вектор в файл
  77.         ofstream Fout(outputAddress, ios::binary);
  78.         Fout << notebookArrayCount;
  79.         Fout.write((char*)&fMoreThan80notebookArray[0], notebookArrayCount * sizeof(notebook));
  80.         Fout.close();
  81.  
  82.         delete[]notebookArray;
  83.  
  84.         DataOutput(); // Выводим данные из файла
  85.     }
  86.     else
  87.     {
  88.         cout << "No data in file.";
  89.     }
  90.     cout << endl << endl << "Press Esc to close.";
  91.     while (!GetAsyncKeyState(VK_ESCAPE));
  92. }
  93.  
  94. //---------Дополнительное задание---------
  95.  
  96. #include <fstream>
  97. #include <iostream>
  98. #include <clocale>
  99. #include <windows.h>
  100. #include <string>
  101.  
  102. using namespace std;
  103.  
  104. string const Address = "C:\\Users\\User\\Desktop\\OutMatrix.kd";
  105.  
  106. int IntInput() // Проверка на Int
  107. {
  108.     int imputNumber;
  109.     while (!(cin >> imputNumber) || (cin.peek() != '\n'))
  110.     {
  111.         cin.clear();
  112.         while (cin.get() != '\n');
  113.         cout << "Это не целое число. Попробуйте еще раз.\n";
  114.     }
  115.     return imputNumber;
  116. }
  117.  
  118. int Sum(int **Array, int line, int count)
  119. {
  120.     int sum = 0;
  121.     for (int i = 0; i < count; i++)
  122.         sum += Array[line][i];
  123.     return sum;
  124. }
  125.  
  126. void main()
  127. {
  128.     setlocale(LC_ALL, "Russian");
  129.     int heigth, weigth;
  130.     cout << "---Введите количество строчек матрицы: ";
  131.     heigth = IntInput();
  132.     cout << endl << "---Введите количество столбцов матрицы: ";
  133.     weigth = IntInput();
  134.  
  135.     typedef int *pInt; //Выделяем память под матрицу
  136.     pInt *Matrix;
  137.  
  138.     Matrix = new pInt[heigth];
  139.     for (int i = 0; i < heigth; i++)
  140.     {
  141.         Matrix[i] = new int[weigth];
  142.     }
  143.     for (int i = 0; i < heigth; i++) //Заполняем матрицу числами от 1 до 100
  144.     {
  145.         for (int j = 0; j < weigth; j++)
  146.         {
  147.             Matrix[i][j] = 1 + rand() % 100;
  148.         }
  149.     }
  150.  
  151.     for (int i = 0; i < heigth; i++) //Вывод матрицы на экран
  152.     {
  153.         cout << endl;
  154.         for (int j = 0; j < weigth; j++)
  155.         {
  156.             cout.width(4);
  157.             cout << Matrix[i][j];
  158.         }
  159.     }
  160.  
  161.     int minIndex = 0, sum = Sum(Matrix, 0, weigth); //Находим ряд с минимальной суммой элементов
  162.  
  163.     for (int i = 0; i < heigth; i++)
  164.     {
  165.         if (sum > Sum(Matrix, i, weigth))
  166.         {
  167.             minIndex = i;
  168.             sum = Sum(Matrix, i, weigth);
  169.         }
  170.     }
  171.     cout << endl << "---Минимальная сума: " << sum;
  172.     cout << endl;
  173.     for (int i = 0; i < weigth; i++) //Вывод этого ряда на экран
  174.     {
  175.         cout.width(4);
  176.         cout << Matrix[minIndex][i];
  177.     }
  178.  
  179.     ofstream Fout; //Запись в файл
  180.     Fout.open(Address, ios::binary);
  181.     Fout.write((char*)Matrix[minIndex], weigth * sizeof(int));
  182.     Fout.close();
  183.  
  184.     int Number;
  185.     ifstream Fin; //Чтение из файла
  186.     Fin.open(Address, ios::binary);
  187.     cout << endl << "---Числа из файла: " << endl;
  188.     while (!Fin.eof())
  189.     {
  190.         if (Fin.read((char*)&Number, sizeof(int)))
  191.         {
  192.             cout.width(4);
  193.             cout << Number;
  194.         }
  195.     }
  196.     cout << endl << "---Выход - Esc";
  197.     while (!GetAsyncKeyState(VK_ESCAPE));
  198. }
Advertisement
Add Comment
Please, Sign In to add comment