Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <climits>
- #include <limits>
- using namespace std;
- // 1.3.1. Создание текстового файла.
- void createFile(const string& filename, const vector<vector<int>>& data) {
- ofstream outfile(filename);
- if (!outfile.is_open()) {
- cerr << "Не удалось создать файл." << endl;
- return;
- }
- for (const auto& row : data) {
- for (const int num : row) {
- outfile << num << " ";
- }
- outfile << "\n";
- }
- outfile.close();
- }
- // 1.3.2. Вывод содержимого текстового файла.
- void displayFileContent(const string& filename) {
- ifstream infile(filename);
- if (!infile.is_open()) {
- cerr << "Не удалось открыть файл." << endl;
- return;
- }
- string line;
- while (getline(infile, line)) {
- cout << line << "\n";
- }
- infile.close();
- }
- // 1.3.3. Добавление новой строки в конец файла.
- void appendToFile(const string& filename, const vector<int>& data) {
- ofstream outfile(filename, ios::app);
- if (!outfile.is_open()) {
- cerr << "Не удалось открыть файл." << endl;
- return;
- }
- for (const int num : data) {
- outfile << num << " ";
- }
- outfile << "\n";
- outfile.close();
- }
- // 1.3.4. Прочитать значение числа, указав его порядковый номер в файле.
- int readNumberByIndex(const string& filename, size_t index) {
- ifstream infile(filename);
- if (!infile.is_open()) {
- cerr << "Не удалось открыть файл." << endl;
- return -1;
- }
- size_t count = 0;
- string line;
- while (getline(infile, line)) {
- istringstream ss(line);
- int num;
- while (ss >> num) {
- if (count == index) {
- infile.close();
- return num;
- }
- count++;
- }
- }
- infile.close();
- cerr << "Индекс вне диапазона." << endl;
- return -1;
- }
- // 1.3.5. Определить количество чисел в файле.
- size_t countNumbersInFile(const string& filename) {
- ifstream infile(filename);
- if (!infile.is_open()) {
- cerr << "Не удалось открыть файл." << endl;
- return 0;
- }
- size_t count = 0;
- string line;
- while (getline(infile, line)) {
- istringstream ss(line);
- int num;
- while (ss >> num) {
- count++;
- }
- }
- infile.close();
- return count;
- }
- // 10. Создать новый файл из значений исходного, умножив каждое четное число на максимальное число в файле.
- void createModifiedFile(const string& inputFilename, const string& outputFilename) {
- ifstream infile(inputFilename);
- if (!infile.is_open()) {
- cerr << "Не удалось открыть файл." << endl;
- return;
- }
- int maxNumber = INT_MIN;
- string line;
- while (getline(infile, line)) {
- istringstream ss(line);
- int num;
- while (ss >> num) {
- if (num > maxNumber) {
- maxNumber = num;
- }
- }
- }
- infile.close();
- infile.open(inputFilename);
- ofstream outfile(outputFilename);
- while (getline(infile, line)) {
- istringstream ss(line);
- int num;
- while (ss >> num) {
- if (num % 2 == 0) {
- outfile << num * maxNumber << " ";
- }
- else {
- outfile << num << " ";
- }
- }
- outfile << "\n";
- }
- infile.close();
- outfile.close();
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- string filename;
- cout << "Введите имя файла: ";
- cin >> filename;
- while (true) {
- cout << "\n--- Меню ---\n";
- cout << "1. Создать файл\n";
- cout << "2. Показать содержимое файла\n";
- cout << "3. Добавить строку в файл\n";
- cout << "4. Прочитать число по индексу\n";
- cout << "5. Узнать количество чисел в файле\n";
- cout << "6. Создать модифицированный файл\n";
- cout << "7. Выход\n";
- cout << "Выберите действие: ";
- int choice;
- cin >> choice;
- switch (choice) {
- case 1: {
- vector<vector<int>> data = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
- createFile(filename, data);
- break;
- }
- case 2: {
- displayFileContent(filename);
- break;
- }
- case 3: {
- cout << "Введите числа для добавления (через пробел): ";
- vector<int> newData;
- int value;
- while (cin >> value) {
- newData.push_back(value);
- }
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- appendToFile(filename, newData);
- break;
- }
- case 4: {
- size_t index;
- cout << "Введите индекс числа: ";
- cin >> index;
- int num = readNumberByIndex(filename, index);
- if (num != -1) {
- cout << "Число: " << num << "\n";
- }
- break;
- }
- case 5: {
- size_t count = countNumbersInFile(filename);
- cout << "Количество чисел: " << count << "\n";
- break;
- }
- case 6: {
- string outputFilename;
- cout << "Введите имя выходного файла: ";
- cin >> outputFilename;
- createModifiedFile(filename, outputFilename);
- break;
- }
- case 7:
- return 0;
- default:
- cout << "Неверный выбор.\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement