3 семестр
This commit is contained in:
189
Part1/Lab1/Mashinnoe/zavtrak.cpp
Normal file
189
Part1/Lab1/Mashinnoe/zavtrak.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <Windows.h>
|
||||
#include <cstdio>
|
||||
#include <time.h>
|
||||
using namespace std;
|
||||
|
||||
const int length = 32;
|
||||
const char universum[] = "АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
|
||||
char stringA[length];
|
||||
char stringB[length];
|
||||
char stringC[length];
|
||||
char stringD[length];
|
||||
bool vectorA[length], vectorB[length], vectorC[length], vectorD[length], vectorF[length];
|
||||
double firstTimePoint, lastTimePoint, totalTime = 0;
|
||||
const char filename[] = "output.txt";
|
||||
const int testAmount = 10;
|
||||
|
||||
|
||||
|
||||
void setupConsole()
|
||||
{
|
||||
setlocale(LC_ALL, "Russian");
|
||||
SetConsoleOutputCP(1251);
|
||||
SetConsoleCP(1251);
|
||||
}
|
||||
|
||||
void outputGreeting()
|
||||
{
|
||||
cout << "Программа рассчитывает значение множества F согласно следующим законам - " << endl;
|
||||
cout << "F = A - (B v C v D), что равносильно F = A - B - C - D." << endl;
|
||||
cout << "Универсум: " << universum << endl;
|
||||
}
|
||||
|
||||
void input()
|
||||
{
|
||||
cout << endl;
|
||||
cout << "Введите множество A: ";
|
||||
cin >> stringA;
|
||||
cout << "Введите множество B: ";
|
||||
cin >> stringB;
|
||||
cout << "Введите множество C: ";
|
||||
cin >> stringC;
|
||||
cout << "Введите множество D: ";
|
||||
cin >> stringD;
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int memberToIndex(char member)
|
||||
{
|
||||
return member - 'А';
|
||||
}
|
||||
|
||||
char indexToMember(int number)
|
||||
{
|
||||
return number + 'А';
|
||||
}
|
||||
|
||||
void vectorFromString(bool*vector, const char string[])
|
||||
{
|
||||
for (int i = 0; string[i]; i++)
|
||||
vector[memberToIndex(string[i])] = 1;
|
||||
};
|
||||
|
||||
void convertSets()
|
||||
{
|
||||
vectorFromString(vectorA, stringA);
|
||||
vectorFromString(vectorB, stringB);
|
||||
vectorFromString(vectorC, stringC);
|
||||
vectorFromString(vectorD, stringD);
|
||||
}
|
||||
|
||||
void calculateAnswer()
|
||||
{
|
||||
for (int i = 0; i < length; i++)
|
||||
vectorF[i] = (vectorA[i] && !vectorB[i] && !vectorC[i] && !vectorD[i]);
|
||||
}
|
||||
|
||||
void outputSetToConsole(bool(&vector)[length])
|
||||
{
|
||||
for (int i = 0; i < length; ++i)
|
||||
if (vector[i])
|
||||
cout << indexToMember(i);
|
||||
}
|
||||
|
||||
void outputSetToFile(bool(&vector)[length], ofstream& output)
|
||||
{
|
||||
for (int i = 0; i < length; ++i)
|
||||
if (vector[i])
|
||||
output << indexToMember(i);
|
||||
}
|
||||
|
||||
void executeTask()
|
||||
{
|
||||
convertSets();
|
||||
calculateAnswer();
|
||||
}
|
||||
|
||||
void outputAnswer()
|
||||
{
|
||||
cout << "Ответ: F = ";
|
||||
outputSetToConsole(vectorF);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int factorial(int number)
|
||||
{
|
||||
int factorial = 1;
|
||||
for (int i = 1; i <= number; i++)
|
||||
factorial *= number;
|
||||
return factorial;
|
||||
}
|
||||
|
||||
bool randomBool()
|
||||
{
|
||||
return rand() % 2;
|
||||
}
|
||||
|
||||
void fillVectors()
|
||||
{
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
vectorA[i] = randomBool();
|
||||
vectorB[i] = randomBool();
|
||||
vectorC[i] = randomBool();
|
||||
vectorD[i] = randomBool();
|
||||
}
|
||||
}
|
||||
|
||||
void clearAnswerVector()
|
||||
{
|
||||
for (int i = 0; i < length; i++)
|
||||
vectorF[i] = 0;
|
||||
}
|
||||
|
||||
void executeTest()
|
||||
{
|
||||
ofstream output;
|
||||
output.open(filename);
|
||||
for (int i = 0; i < testAmount; i++)
|
||||
{
|
||||
fillVectors();
|
||||
clearAnswerVector();
|
||||
output << "Множество A: ";
|
||||
outputSetToFile(vectorA, output);
|
||||
output << endl;
|
||||
output << "Множество B: ";
|
||||
outputSetToFile(vectorB, output);
|
||||
output << endl;
|
||||
output << "Множество C: ";
|
||||
outputSetToFile(vectorC, output);
|
||||
output << endl;
|
||||
output << "Множество D: ";
|
||||
outputSetToFile(vectorD, output);
|
||||
output << endl;
|
||||
firstTimePoint = clock();
|
||||
calculateAnswer();
|
||||
lastTimePoint = clock();
|
||||
totalTime += lastTimePoint - firstTimePoint;
|
||||
output << "Ответ: F = ";
|
||||
outputSetToFile(vectorF, output);
|
||||
output << endl;
|
||||
output << '*' << endl;
|
||||
}
|
||||
output.close();
|
||||
}
|
||||
|
||||
void outputTestInfo()
|
||||
{
|
||||
cout << endl;
|
||||
cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
|
||||
cout << "Из 33 символов может быть составлено " << factorial(length) << " перестановок" << endl;
|
||||
cout << "Программой будут рассмотрены лишь " << testAmount << " случайных комбинаций" << endl;
|
||||
cout << "Отчеты будут выведены в файл output.txt" << endl;
|
||||
cout << "На выполнение алгоритма уходит " << totalTime / 1000 << " секунд." << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
setupConsole();
|
||||
outputGreeting();
|
||||
input();
|
||||
executeTask();
|
||||
outputAnswer();
|
||||
executeTest();
|
||||
outputTestInfo();
|
||||
}
|
||||
Reference in New Issue
Block a user