commit 5b03d9cea51aa874414cff76995d44079b172abe Author: Sarnavskiy Alexey Date: Sat Nov 27 23:54:28 2021 +0300 Added main.cpp and Task_1..5.cpp diff --git a/Task_1.cpp b/Task_1.cpp new file mode 100644 index 0000000..ad321ad --- /dev/null +++ b/Task_1.cpp @@ -0,0 +1,40 @@ +/* +Выделить в памяти динамический одномерный массив типа int. Размер массива +запросить у пользователя. Инициализировать его числами степенями двойки: +1, 2, 4, 8, 16, 32, 64, 128 … Вывести массив на экран. Не забыть освободить +память. =) Разбить программу на функции. +*/ + +#include + +void PrintArray(int*, const size_t, std::string s = ""); +void FillArrayByTwoPowers(int*, const size_t); + +int Task_1() +{ + std::cout << "\t\tTask #1" << std::endl; + size_t n = 0; + std::cout << "Enter array size: "; + std::cin >> n; + int* aInts = new (std::nothrow) int[n]; + + if (aInts != nullptr && n > 0) + { + std::cout << "Memory allocation is successful." << std::endl; + FillArrayByTwoPowers(aInts, n); + PrintArray(aInts, n); + } + else + { + std::cerr << "Something wrong with memory allocation in Task #1.\n"; + return -1; + } + + std::cout << "Allocated memory deletion ...\n"; + delete[] aInts; + + std::cout << "________________________________________________\n\n"; + system("pause"); + system("cls"); + return 0; +} diff --git a/Task_2.cpp b/Task_2.cpp new file mode 100644 index 0000000..d518114 --- /dev/null +++ b/Task_2.cpp @@ -0,0 +1,50 @@ +/* +Динамически выделить матрицу 4х4 типа int. Инициализировать ее псевдослучайными +числами через функцию rand. Вывести на экран. Разбейте вашу программу на +функции которые вызываются из main. +*/ + +#include + +void PrintArray(int**, const size_t); +void FillRandArray(int**, const size_t); + +int Task_2() +{ + std::cout << "\t\tTask #2" << std::endl; + + const size_t SIZE = 4U; + int** aaInts = new (std::nothrow) int* [SIZE]; + if (aaInts != nullptr && SIZE > 0) + { + for (size_t i = 0; i < SIZE; i++) + { + aaInts[i] = new int[SIZE]; + if (aaInts[i] == nullptr) + { + std::cerr << "Something wrong with memory allocation in Task #2.\n"; + return -1; + } + } + std::cout << "Memory allocation is successful." << std::endl; + FillRandArray(aaInts, SIZE); + PrintArray(aaInts, SIZE); + } + else + { + std::cerr << "Something wrong with memory allocation in Task #2.\n"; + return -1; + } + + std::cout << "Allocated memory deletion ...\n"; + for (size_t i = 0; i < SIZE; i++) + { + delete[] aaInts[i]; + } + delete[] aaInts; + + std::cout << "________________________________________________\n\n"; + system("pause"); + system("cls"); + return 0; +} diff --git a/Task_3.cpp b/Task_3.cpp new file mode 100644 index 0000000..826e934 --- /dev/null +++ b/Task_3.cpp @@ -0,0 +1,39 @@ +/* +Написать программу, которая создаст два текстовых файла (*.txt), примерно +по 50-100 символов в каждом (особого значения не имеет с каким именно содержимым). +Имена файлов запросить у польлзователя. +*/ + +#include + +int FillFileBySmth(std::string); + +int Task_3() +{ + std::cout << "\t\tTask #3" << std::endl; + + std::string filename_1; + std::string filename_2; + int result = 0; + + std::cout << "Enter name of the 1st text file without extension:\n"; + std::cin >> filename_1; + std::cout << "Enter name of the 2nd text file without extension:\n"; + std::cin >> filename_2; + + filename_1 += ".txt"; + filename_2 += ".txt"; + + result = FillFileBySmth(filename_1); + if (result != 0) + { + std::cerr << "Something wrong with file creation in Task #3." << std::endl; + return result; + } + result = FillFileBySmth(filename_2); + + std::cout << "________________________________________________\n\n"; + system("pause"); + system("cls"); + return result; +} diff --git a/Task_4.cpp b/Task_4.cpp new file mode 100644 index 0000000..b62dc87 --- /dev/null +++ b/Task_4.cpp @@ -0,0 +1,41 @@ +/* +* Написать функцию, «склеивающую» эти файлы в третий текстовый файл (имя файлов +спросить у пользователя). +*/ + +#include + +int GlueTwoFilesInThird(std::string, std::string, std::string); + +int Task_4() +{ + std::cout << "\t\tTask #4" << std::endl; + + int result = 0; + std::string filename_1; + std::string filename_2; + std::string filename_3; + + std::cout << "Enter name of the 1st file without extension:\n"; + std::cin >> filename_1; + std::cout << "Enter name of the 2nd file without extension:\n"; + std::cin >> filename_2; + std::cout << "Enter name of the 3rd glued file without extension:\n"; + std::cin >> filename_3; + + filename_1 += ".txt"; + filename_2 += ".txt"; + filename_3 += ".txt"; + + result = GlueTwoFilesInThird(filename_1, filename_2, filename_3); + if (result != 0) + { + std::cerr << "Files not found or not created.\n"; + return result; + } + + std::cout << "________________________________________________\n\n"; + system("pause"); + system("cls"); + return result; +} diff --git a/Task_5.cpp b/Task_5.cpp new file mode 100644 index 0000000..cc902cd --- /dev/null +++ b/Task_5.cpp @@ -0,0 +1,61 @@ +/* +* Написать программу, которая проверяет присутствует ли указанное пользователем +при запуске программы слово в указанном пользователем файле (для простоты работаем +только с латиницей). Используем функцию find которая есть в строках std::string. +*/ + +#include +#include + +const unsigned int MAX_SENTENTECE_SIZE = 100U; + +int Task_5() +{ + std::cout << "\t\tTask #5" << std::endl; + + int result = 0; + std::string filename = ""; + std::string word_to_find = ""; + std::string s1 = ""; + + std::cout << "Enter file name where you want to find your word (without extension):\n"; + std::cin >> filename; + filename += ".txt"; + + std::ifstream in_file(filename); + if (!in_file) + { + std::cerr << "File is not open! Check file existance!\n"; + return -3; + } + + std::cout << "Enter the word to find in this file: "; + std::cin >> word_to_find; + bool word_is_found = false; + + while (!in_file.eof() || word_is_found != false) + { + char tmp[MAX_SENTENTECE_SIZE + 1] = {}; + in_file.getline(tmp, MAX_SENTENTECE_SIZE, ' '); + s1 += tmp; + s1 += ' '; + } + if (s1.find(word_to_find) != std::string::npos) + { + word_is_found = true; + } + + if (word_is_found) + { + std::cout << "Word was found!\n"; + } + else + { + std::cout << "Word was not found!\n"; + } + + std::cout << "________________________________________________\n\n"; + system("pause"); + system("cls"); + return result; +} \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d0199ab --- /dev/null +++ b/main.cpp @@ -0,0 +1,149 @@ +п»ї#include +#include +#include + +const unsigned int MAX_SENTENTECE_SIZE = 100U; + +int Task_1(); +int Task_2(); +int Task_3(); +int Task_4(); +int Task_5(); + +int main() +{ + int result = 0; + + result = Task_1(); + if (result != 0) + { + return result; + } + result = Task_2(); + if (result != 0) + { + return result; + } + result = Task_3(); + if (result != 0) + { + return result; + } + result = Task_4(); + if (result != 0) + { + return result; + } + result = Task_5(); + if (result != 0) + { + return result; + } + + return result; +} + +void PrintArray(int* pArr, const size_t count, std::string s) +{ + std::cout << s; + for (size_t i = 0U; i < count; i++) + { + std::cout << "Array[" << i << "] = " << pArr[i] << std::endl; + } +} + +void PrintArray(int** pArr, const size_t count) +{ + for (size_t i = 0U; i < count; i++) + { + for (size_t j = 0U; j < count; j++) + { + std::cout << "\t" << pArr[i][j]; + } + std::cout << std::endl; + } +} + +void FillArrayByTwoPowers(int* pArr, const size_t count) +{ + pArr[0U] = 1; + for (size_t i = 1U; i < count; i++) + { + pArr[i] = pArr[i - 1U] << 1; + } +} + +void FillRandArray(int** ppArr, const size_t count) +{ + for (size_t i = 0U; i < count; i++) + { + for (size_t j = 0U; j < count; j++) + { + ppArr[i][j] = rand(); + } + } +} + +int FillFileBySmth(std::string filename) +{ + std::string s = "This is a small text with little quantity of symbols created in Microsoft Visual Studio.\n"; + std::ofstream outfile(filename); + + if (!outfile) + { + return -2; + } + + std::cout << "File \"" << filename << "\" is open successfully.\n"; + + outfile << s; + + outfile.close(); + return 0; +} + +int GlueTwoFilesInThird(std::string filename_1, std::string filename_2, std::string outfilename) +{ + std::string s1 = ""; + std::string s2 = ""; + std::ifstream in_file_1(filename_1); + std::ifstream in_file_2(filename_2); + + if (!in_file_1 || !in_file_2) + { + return -2; + } + std::cout << "Files \"" << filename_1 << "\" and \"" << filename_2 << "\" are succesfully open.\n"; + + while (!in_file_1.eof()) + { + char tmp[MAX_SENTENTECE_SIZE + 1] = {}; + in_file_1.getline(tmp, MAX_SENTENTECE_SIZE, '\n'); + s1 += tmp; + s1 += "\n"; + } + while (!in_file_2.eof()) + { + char tmp[MAX_SENTENTECE_SIZE + 1] = {}; + in_file_2.getline(tmp, MAX_SENTENTECE_SIZE, '\n'); + s2 += tmp; + s2 += "\n"; + } + + in_file_1.close(); + in_file_2.close(); + std::cout << "Files \"" << filename_1 << "\" and \"" << filename_2 << "\" are closed.\n"; + + std::ofstream out_file(outfilename); + if (!out_file) + { + return -2; + } + std::cout << "File \"" << outfilename << "\" is successfully open.\n"; + + out_file << s1 + s2; + out_file.close(); + std::cout << "File \"" << outfilename << "\" is closed.\n"; + + return 0; +}