Додана лабораторна робота № 10

This commit is contained in:
Stanislav Mykhailenko 2023-03-03 22:40:33 +02:00
parent 9189d6754f
commit 59b5a342e2
GPG key ID: 1E95E66A9C9D6A36
22 changed files with 576 additions and 0 deletions

14
lab10/README.md Normal file
View file

@ -0,0 +1,14 @@
# Лабораторна робота № 10
Тема Реалізація програмних модулів оброблення даних складових типів з файловим введенням/виведенням
Мета роботи полягає у набутті ґрунтовних вмінь і практичних навичок реалізації у Code::Blocks IDE мовою програмування С++ програмних модулів створення й оброблення даних типів масив, структура, об’єднання, множина, перелік, перетворення типів даних, використання файлових потоків та функцій стандартних бібліотек для оброблення символьної інформації.
Завдання
1. Реалізувати програмні модулі розв’язування задач 10.110.3 як складові статичної бібліотеки libModulesMykhailenko.а (проект ModulesMykhailenko лабораторних робіт No 89).
2. Реалізувати тестовий драйвер автоматизованої перевірки програмних модулів розв’язування задач 10.110.3.
Варіант № 1
Кропивницький | <a href="http://www.kntu.kr.ua/">ЦНТУ</a> | 2023

BIN
lab10/Report/Report.pdf Normal file

Binary file not shown.

BIN
lab10/Software/TestDriver.exe Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,34 @@
#ifndef MODULESMYKHAILENKO_H_INCLUDED
#define MODULESMYKHAILENKO_H_INCLUDED
#include <string>
using std::string;
float s_calculation(int x, int y, int z);
typedef struct Deposit {
float totalInterest;
float monthlyInterestPaid;
} Deposit;
typedef struct Size {
int french;
string international;
} Size;
Deposit getPayment(float value, int months);
Size getSize(int slovakSize);
int t9_3(int number);
bool validateCharacter(wchar_t character);
int t10_1(string inputFile, string outputFile);
int t10_2(string file);
int t10_3(string file, int x, int y, int z, int b);
#endif // MODULESMYKHAILENKO_H_INCLUDED

View file

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="ModulesMykhailenko" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/ModulesMykhailenko" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj/Debug/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-Wall" />
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/ModulesMykhailenko" prefix_auto="1" extension_auto="1" />
<Option working_dir="" />
<Option object_output="obj/Release/" />
<Option type="2" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-Wall" />
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Unit filename="ModulesMykhailenko.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View file

@ -0,0 +1,188 @@
#include <cmath>
#include <ctime>
#include <string>
#include <fstream>
#include <codecvt>
#include <bitset>
#include <limits>
#include "ModulesMykhailenko.h"
using namespace std;
wstring characters = L".,!?:;)\" (₴ʼ'№%*_-+=\\/0123456789абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
float s_calculation(int x, int y, int z) {
return fabs(sin(fabs(y-pow(z,2)))+sqrt(x)-sqrt(pow(y*z, x)+(y/(2*M_PI))));
}
Deposit getPayment(float value, int months) {
Deposit deposit;
if (months == 6 || months == 12) {
float interest;
deposit.totalInterest = months == 12 ? 13 : 11/(float)2;
interest = deposit.totalInterest / 100 / months;
deposit.monthlyInterestPaid = round(value*interest*100)/100;
} else {
deposit.totalInterest = -1;
deposit.monthlyInterestPaid = -1;
}
return deposit;
}
Size getSize(int slovakSize) {
Size size;
if (slovakSize >= 6 && slovakSize <= 10) {
size.french = slovakSize - 4;
switch (slovakSize)
{
case 6:
size.international = "S";
break;
case 7:
size.international = "M";
break;
case 8:
size.international = "L";
break;
case 9:
size.international = "XL";
break;
case 10:
size.international = "XXL";
break;
default:
size.international = "Error";
}
} else {
size.french = -1;
size.international = "Error";
}
return size;
}
int t9_3(int number) {
unsigned int count = 0;
if (number > 0 || number <= 7483650) {
bool set = number & 1;
while (number) {
count += (number & 1) == set;
number >>= 1;
}
} else {
return -1;
}
return count;
}
bool validateCharacter(wchar_t character) {
for (int i = 0; i < characters.length(); i++)
if (character == characters.at(i))
return true;
return false;
}
int t10_1(string inputFile, string outputFile) {
wstring line;
wstring words[4] = {L"програма", L"модуль", L"студент", L"програміст"};
bool found = false;
int number = 0;
wifstream indata;
indata.open(inputFile);
ofstream outdata;
outdata.open(outputFile);
if (!indata || !outdata)
return 1;
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
number += line.length();
for (int i = 0; i < line.length(); i++) {
line[i] = towlower(line[i]);
if (!validateCharacter(line[i]))
return 2;
}
if (!found) {
for (int i = 0; i < 4; i++) {
int index = line.find(words[i]);
if (index != wstring::npos && line.length()-words[i].length()-index == 0) { // слово у кінці
found = true;
goto out_found;
}
for (int j = 0; j < 9; j++) { // крапка, кома, знак оклику, знак питання, двокрапка, крапка з комою, закриваюча дужка, лапки, пробіл
wstring wordToFind = words[i] + characters.at(j);
if (line.find(wordToFind) != wstring::npos) {
found = true;
goto out_found;
}
}
}
}
out_found: continue;
}
outdata << "Розробник: Михайленко Станіслав" << endl << "Установа/організація: Центральноукраїнський національний технічний університет" << endl << "Місто: Кропивницький" << endl << "Країна: Україна" << endl << "Рік розробки: 2023" << endl << endl;
outdata << "Кількість символів у файлі: " << number << endl;
outdata << "У вхідному файлі " << ((found) ? "наявні" : "відсутні") << " слова \"програма\", \"модуль\", \"студент\", \"програміст\"." << endl;
return 0;
}
int t10_2(string file) {
wchar_t character;
time_t rawtime;
time(&rawtime);
int number = 0;
wifstream indata;
indata.open(file);
ofstream outdata;
outdata.open(file, ios_base::app);
if (!indata || !outdata)
return 1;
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (indata >> character) {
if (!validateCharacter(towlower(character)))
return 2;
for (int i = 0; i < 10; i++)
if (character == characters[i + 22]) // 0-9
number += 1;
}
outdata << endl << "Кількість цифр у файлі: " << number << endl;
outdata << "Дата й час дозапису: " << ctime(&rawtime);
return 0;
}
int t10_3(string file, int x, int y, int z, int b) {
wchar_t character;
time_t rawtime;
time(&rawtime);
int number = 0;
ofstream data;
data.open(file, ios_base::app);
if (!data)
return 1;
data << endl << "s = " << s_calculation(x, y, z) << endl;
if (b > 0) {
string binary = bitset<numeric_limits<int>::digits>(b).to_string();
binary.erase(0, binary.find_first_not_of('0'));
data << "b у двійковому коді: " << binary << endl;
}
else
data << "b — не натуральне число" << endl;
return 0;
}

View file

@ -0,0 +1,10 @@
# depslib dependency file v1.0
1677874156 source:z:\bmtp10\prj\modulesmykhailenko\modulesmykhailenko.cpp
<ctime>
<string>
<fstream>
<codecvt>
<bitset>
<limits>
"ModulesMykhailenko.h"

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="ModulesMykhailenko.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="1908" topLine="68" />
</Cursor>
</File>
</CodeBlocks_layout_file>

View file

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="TestDriver" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/TestDriver" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/TestDriver" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fexceptions" />
</Compiler>
<Unit filename="main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>

View file

@ -0,0 +1,13 @@
# depslib dependency file v1.0
1677244734 source:z:\bmtp9\prj\testdriver\main.cpp
"ModulesMykhailenko.h"
<iostream>
<clocale>
1677873860 source:z:\bmtp10\prj\testdriver\main.cpp
<iostream>
<locale>
<fstream>
<codecvt>
<string>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="520" topLine="9" />
</Cursor>
</File>
</CodeBlocks_layout_file>

Binary file not shown.

View file

@ -0,0 +1,4 @@
00000
Кількість цифр у файлі: 5
Дата й час дозапису: Fri Mar 03 22:30:28 2023

View file

@ -0,0 +1,192 @@
#include "ModulesMykhailenko.h"
#include <iostream>
#include <locale>
#include <fstream>
#include <codecvt>
#include <string>
using namespace std;
string input = "input.txt";
string output = "output.txt";
bool createInput(wstring content) {
wofstream data;
data.open(input);
data.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
if (!data) {
cout << "Помилка запису вхідних даних." << endl;
return false;
}
data << content << endl;
return true;
}
bool test1_1() {
wstring line;
const wstring lines[3] = {L"Розробник: Михайленко Станіслав", L"Установа/організація: Центральноукраїнський національний технічний університет", L"Рік розробки: 2023"};
bool linesFound[3] = {false, false, false};
int currentLine = 0;
if(!createInput(L"тест"))
return false;
t10_1(input, output);
wifstream indata;
indata.open(output);
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
if (line.find(lines[currentLine]) != wstring::npos) {
linesFound[currentLine] = true;
currentLine++;
}
if (linesFound[0] && linesFound[1] && linesFound[2])
return true;
}
return false;
}
bool test1(wstring data, bool wordPresent) {
wstring line;
if(!createInput(data))
return false;
t10_1(input, output);
wifstream indata;
indata.open(output);
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
if ((wordPresent && line.find(L"наявні") != wstring::npos) || (!wordPresent && line.find(L"відсутні") != wstring::npos))
return true;
}
return false;
}
bool test2_1() {
wstring line;
if(!createInput(L"тест"))
return false;
t10_2(input);
wifstream indata;
indata.open(input);
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
if (line.find(L"Дата й час дозапису: ") != wstring::npos)
return true;
}
return false;
}
bool test2(wstring data, int digits) {
wstring line;
if(!createInput(data))
return false;
t10_2(input);
wifstream indata;
indata.open(input);
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
if (line.find(L"Кількість цифр у файлі: " + digits) != wstring::npos)
return true;
}
return false;
}
bool test3(int x, int y, int z, int b, wstring s, wstring bin) {
wstring line;
bool firstValid = false;
t10_3(output, x, y, z, b);
wifstream indata;
indata.open(output);
indata.imbue(locale(locale(), new codecvt_utf8<wchar_t>));
while (getline(indata,line)) {
if (!firstValid && (line.find(L"s = " + s) != wstring::npos))
firstValid = true;
else if (firstValid && (line.find(L"b у двійковому коді: " + bin) != wstring::npos))
return true;
}
return false;
}
int main() {
const wstring data1_1[] = {L"Студент", L"Студентка", L"модуль!", L"модульчик"};
const bool wordPresent[] = {true, false, true, false};
const wstring data2_1[] = {L"Програміст", L"0123456789", L"12", L"00000"};
const int digits[] = {0, 10, 2, 5};
const int x[] = {7, 2, 9, 0, 7};
const int y[] = {2, 45, 0, 0, 5};
const int z[] = {1, 6, 1, 0, 4};
const int b[] = {1, 3, 8, 100, 127};
const wstring s[] = {L"7.84054", L"268.187", L"3.84147", L"1", L"35775.4"};
const wstring bin[] = {L"1", L"11", L"1000", L"1100100", L"1111111"};
setlocale(LC_ALL, "");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
bool value = false;
wstring description;
if (i < 2 && j == 0) {
description = L"виведення повідомлення";
if (i == 0)
value = test1_1();
else if (i == 1)
value = test2_1();
}
else if (i == 0) {
value = test1(data1_1[j-1], wordPresent[j-1]);
description = L"вхідні дані: " + data1_1[j-1] + L", очікуваний результат: слова " + (wordPresent[j-1] ? L"наявні" : L"відсутні");
}
else if (i == 1) {
value = test2(data2_1[j-1], digits[j-1]);
string tmpstr = to_string(digits[j-1]);
wstring wstr(tmpstr.begin(), tmpstr.end());
description = L"вхідні дані: " + data2_1[j-1] + L", очікуваний результат: кількість чисел: " + wstr;
}
else if (i == 2) {
value = test3(x[j], y[j], z[j], b[j], s[j], bin[j]);
string xtmpstr = to_string(digits[j-1]);
wstring xwstr(xtmpstr.begin(), xtmpstr.end());
string ytmpstr = to_string(digits[j-1]);
wstring ywstr(ytmpstr.begin(), ytmpstr.end());
string ztmpstr = to_string(digits[j-1]);
wstring zwstr(ztmpstr.begin(), ztmpstr.end());
string btmpstr = to_string(digits[j-1]);
wstring bwstr(btmpstr.begin(), btmpstr.end());
description = L"вхідні дані: x = " + xwstr + L", y = " + ywstr + L", z = " + zwstr + L", b = " + bwstr + L", очікуваний результат: s = " + s[j] + L", b у двійковому коді: " + bin[j];
}
wcout << "Test " << i + 1 << "." << j + 1 << " (" << description << ") " << (value ? "passed" : "failed") << endl;
}
}
return 0;
}

Binary file not shown.

View file

@ -0,0 +1,23 @@
Розробник: Михайленко Станіслав
Установа/організація: Центральноукраїнський національний технічний університет
Місто: Кропивницький
Країна: Україна
Рік розробки: 2023
Кількість символів у файлі: 9
У вхідному файлі відсутні слова "програма", "модуль", "студент", "програміст".
s = 7.84054
b у двійковому коді: 1
s = 268.187
b у двійковому коді: 11
s = 3.84147
b у двійковому коді: 1000
s = 1
b у двійковому коді: 1100100
s = 35775.4
b у двійковому коді: 1111111

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB