Added main.cpp and Task_1..5.cpp

This commit is contained in:
Sarnavskiy Alexey 2021-11-27 23:54:28 +03:00
commit 5b03d9cea5
6 changed files with 380 additions and 0 deletions

40
Task_1.cpp Normal file
View File

@ -0,0 +1,40 @@
/*
Âûäåëèòü â ïàìÿòè äèíàìè÷åñêèé îäíîìåðíûé ìàññèâ òèïà int. Ðàçìåð ìàññèâà
çàïðîñèòü ó ïîëüçîâàòåëÿ. Èíèöèàëèçèðîâàòü åãî ÷èñëàìè ñòåïåíÿìè äâîéêè:
1, 2, 4, 8, 16, 32, 64, 128 Âûâåñòè ìàññèâ íà ýêðàí. Íå çàáûòü îñâîáîäèòü
ïàìÿòü. =) Ðàçáèòü ïðîãðàììó íà ôóíêöèè.
*/
#include <iostream>
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;
}

50
Task_2.cpp Normal file
View File

@ -0,0 +1,50 @@
/*
Äèíàìè÷åñêè âûäåëèòü ìàòðèöó 4õ4 òèïà int. Èíèöèàëèçèðîâàòü åå ïñåâäîñëó÷àéíûìè
÷èñëàìè ÷åðåç ôóíêöèþ rand. Âûâåñòè íà ýêðàí. Ðàçáåéòå âàøó ïðîãðàììó íà
ôóíêöèè êîòîðûå âûçûâàþòñÿ èç main.
*/
#include <iostream>
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;
}

39
Task_3.cpp Normal file
View File

@ -0,0 +1,39 @@
/*
Íàïèñàòü ïðîãðàììó, êîòîðàÿ ñîçäàñò äâà òåêñòîâûõ ôàéëà (*.txt), ïðèìåðíî
ïî 50-100 ñèìâîëîâ â êàæäîì (îñîáîãî çíà÷åíèÿ íå èìååò ñ êàêèì èìåííî ñîäåðæèìûì).
Èìåíà ôàéëîâ çàïðîñèòü ó ïîëüëçîâàòåëÿ.
*/
#include <iostream>
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;
}

41
Task_4.cpp Normal file
View File

@ -0,0 +1,41 @@
/*
* Íàïèñàòü ôóíêöèþ, «ñêëåèâàþùóþ» ýòè ôàéëû â òðåòèé òåêñòîâûé ôàéë (èìÿ ôàéëîâ
ñïðîñèòü ó ïîëüçîâàòåëÿ).
*/
#include <iostream>
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;
}

61
Task_5.cpp Normal file
View File

@ -0,0 +1,61 @@
/*
* Íàïèñàòü ïðîãðàììó, êîòîðàÿ ïðîâåðÿåò ïðèñóòñòâóåò ëè óêàçàííîå ïîëüçîâàòåëåì
ïðè çàïóñêå ïðîãðàììû ñëîâî â óêàçàííîì ïîëüçîâàòåëåì ôàéëå (äëÿ ïðîñòîòû ðàáîòàåì
òîëüêî ñ ëàòèíèöåé). Èñïîëüçóåì ôóíêöèþ find êîòîðàÿ åñòü â ñòðîêàõ std::string.
*/
#include <iostream>
#include <fstream>
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;
}

149
main.cpp Normal file
View File

@ -0,0 +1,149 @@
#include <iostream>
#include <fstream>
#include <string>
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;
}