Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //---------Вариант 4---------
- #include <fstream>
- #include <windows.h>
- #include <vector>
- #include <algorithm>
- #include <string>
- using namespace std;
- string const inputAddress = "C:\\Users\\User\\Desktop\\notebook.dat",
- outputAddress = "C:\\Users\\User\\Desktop\\note_out.dat";
- struct notebook {
- char model[20]; // найменування моделі
- int price; // ціна
- int disp_x; // роздільна здатність дисплея по горизонталі
- int disp_y; // роздільна здатність дисплея по вертикалі
- int f; // частота регенерації
- float d; // розмір діагоналі дисплея
- float weight; // вага
- };
- void DataOutput() // Вывод данных из файла
- {
- ifstream Fin(outputAddress, ios::binary);
- int notebookCount;
- notebook fileRead;
- Fin >> notebookCount;
- for (int i = 0; i < notebookCount; i++)
- {
- if (Fin.read((char*)&fileRead, sizeof(notebook)))
- {
- cout << endl << "-----notebook: " << i + 1;
- cout << endl << "model: " << fileRead.model;
- cout << endl << "price: " << fileRead.price;
- cout << endl << "disp_x: " << fileRead.disp_x;
- cout << endl << "disp_y: " << fileRead.disp_y;
- cout << endl << "f: " << fileRead.f;
- cout << endl << "d: " << fileRead.d;
- cout << endl << "weight: " << fileRead.weight;
- }
- }
- Fin.close();
- }
- void main()
- {
- notebook *notebookArray;
- int notebookCount = 0;
- ifstream Fin(inputAddress, ios::binary);
- notebook fileRead;
- while (!Fin.eof()) // Считаем сколько всего ноутбуков
- {
- if (Fin.read((char*)&fileRead, sizeof(notebook)))
- notebookCount++;
- }
- Fin.close();
- if (notebookCount > 0) // Если удалось считать
- {
- notebookArray = new notebook[notebookCount];
- Fin.open(inputAddress, ios::binary);
- Fin.read((char*)notebookArray, notebookCount * sizeof(notebook)); // Считываем весь список в массив
- Fin.close();
- vector <notebook> fMoreThan80notebookArray; // Вектор структур ноутов с частотой больше чем 80
- for (int i = 0; i < notebookCount; i++) // Заполняем вектор
- {
- if (notebookArray[i].f > 80)
- fMoreThan80notebookArray.push_back(notebookArray[i]);
- }
- int notebookArrayCount = fMoreThan80notebookArray.size();
- // С радостью сортируем вектор структур по возрастанию цены
- sort(fMoreThan80notebookArray.begin(), fMoreThan80notebookArray.end(), [](const notebook& a, const notebook& b) {
- return a.price < b.price;
- });
- // Записуем вектор в файл
- ofstream Fout(outputAddress, ios::binary);
- Fout << notebookArrayCount;
- Fout.write((char*)&fMoreThan80notebookArray[0], notebookArrayCount * sizeof(notebook));
- Fout.close();
- delete[]notebookArray;
- DataOutput(); // Выводим данные из файла
- }
- else
- {
- cout << "No data in file.";
- }
- cout << endl << endl << "Press Esc to close.";
- while (!GetAsyncKeyState(VK_ESCAPE));
- }
- //---------Дополнительное задание---------
- #include <fstream>
- #include <iostream>
- #include <clocale>
- #include <windows.h>
- #include <string>
- using namespace std;
- string const Address = "C:\\Users\\User\\Desktop\\OutMatrix.kd";
- int IntInput() // Проверка на Int
- {
- int imputNumber;
- while (!(cin >> imputNumber) || (cin.peek() != '\n'))
- {
- cin.clear();
- while (cin.get() != '\n');
- cout << "Это не целое число. Попробуйте еще раз.\n";
- }
- return imputNumber;
- }
- int Sum(int **Array, int line, int count)
- {
- int sum = 0;
- for (int i = 0; i < count; i++)
- sum += Array[line][i];
- return sum;
- }
- void main()
- {
- setlocale(LC_ALL, "Russian");
- int heigth, weigth;
- cout << "---Введите количество строчек матрицы: ";
- heigth = IntInput();
- cout << endl << "---Введите количество столбцов матрицы: ";
- weigth = IntInput();
- typedef int *pInt; //Выделяем память под матрицу
- pInt *Matrix;
- Matrix = new pInt[heigth];
- for (int i = 0; i < heigth; i++)
- {
- Matrix[i] = new int[weigth];
- }
- for (int i = 0; i < heigth; i++) //Заполняем матрицу числами от 1 до 100
- {
- for (int j = 0; j < weigth; j++)
- {
- Matrix[i][j] = 1 + rand() % 100;
- }
- }
- for (int i = 0; i < heigth; i++) //Вывод матрицы на экран
- {
- cout << endl;
- for (int j = 0; j < weigth; j++)
- {
- cout.width(4);
- cout << Matrix[i][j];
- }
- }
- int minIndex = 0, sum = Sum(Matrix, 0, weigth); //Находим ряд с минимальной суммой элементов
- for (int i = 0; i < heigth; i++)
- {
- if (sum > Sum(Matrix, i, weigth))
- {
- minIndex = i;
- sum = Sum(Matrix, i, weigth);
- }
- }
- cout << endl << "---Минимальная сума: " << sum;
- cout << endl;
- for (int i = 0; i < weigth; i++) //Вывод этого ряда на экран
- {
- cout.width(4);
- cout << Matrix[minIndex][i];
- }
- ofstream Fout; //Запись в файл
- Fout.open(Address, ios::binary);
- Fout.write((char*)Matrix[minIndex], weigth * sizeof(int));
- Fout.close();
- int Number;
- ifstream Fin; //Чтение из файла
- Fin.open(Address, ios::binary);
- cout << endl << "---Числа из файла: " << endl;
- while (!Fin.eof())
- {
- if (Fin.read((char*)&Number, sizeof(int)))
- {
- cout.width(4);
- cout << Number;
- }
- }
- cout << endl << "---Выход - Esc";
- while (!GetAsyncKeyState(VK_ESCAPE));
- }
Advertisement
Add Comment
Please, Sign In to add comment