3 семестр

This commit is contained in:
2022-05-14 15:22:55 +03:00
parent 64597ccc7e
commit b18d091370
35 changed files with 3547 additions and 0 deletions

View File

@@ -0,0 +1,255 @@
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <cstdio>
#include <time.h>
#include <iomanip>
using namespace std;
const char universum[] = "ABCDEFGHIJKMNLOPQRSTUVWXYZ";
const char filename[] = "Test_result.txt";
const int length = 26;
const int durationCoeff = 1000000;
int customTestAmount = 0;
char stringA[length], stringB[length], stringC[length], stringD[length];
unsigned int vectorA, vectorB, vectorC, vectorD, vectorF;
void setupConsole()
{
setlocale(LC_ALL, "Russian");
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
cout << fixed;
cout << setprecision(7);
}
void outputGreeting()
{
cout << "Программа рассчитывает значение множества F согласно следующим законам - " << endl;
cout << "F = D && (A || B) && !C, что равносильно F = D*(A + B) - C." << endl;
cout << "Универсум: " << universum << endl;
}
void input()
{
cout << endl;
cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
cout << "Введите множество A: ";
cin >> stringA;
cout << "Введите множество B: ";
cin >> stringB;
cout << "Введите множество C: ";
cin >> stringC;
cout << "Введите множество D: ";
cin >> stringD;
cout << endl;
}
bool isCorrectSymbol(char symbol)
{
return ((symbol >= universum[0] && symbol <= universum[length - 1]) || symbol == '\0') ? 1 : 0;
}
bool isStringCorrect(char* string)
{
for (int i = 0; i < length && string[i] != '\0'; i++)
if (!isCorrectSymbol(string[i]))
return 0;
return 1;
}
int memberToIndex(char member)
{
return member - universum[0];
}
char indexToMember(int number)
{
return number + universum[0];
}
unsigned int vectorFromString(const char string[])
{
int vector = 0;
for (int i = 0; string[i]; ++i)
vector |= (1 << memberToIndex(string[i]));
return vector;
};
void convertSets()
{
vectorA = vectorFromString(stringA);
vectorB = vectorFromString(stringB);
vectorC = vectorFromString(stringC);
vectorD = vectorFromString(stringD);
}
void calculateAnswer()
{
vectorF = vectorD & (vectorA | vectorB) & ~vectorC;
}
void outputSetToConsole(int vector)
{
for (int i = 0, k = 0; i < length; ++i)
if ((vector >> i) & 1)
cout << indexToMember(i);
}
void outputSetToFile(int vector, ofstream& output)
{
for (int i = 0, k = 0; i < length; ++i)
if ((vector >> i) & 1)
output << indexToMember(i);
}
bool isSetEmpty(int vector)
{
if (vector == 0)
return 1;
else return 0;
}
void outputAnswer()
{
cout << "Ответ: F = ";
if (isSetEmpty(vectorF))
cout << "пустое множество";
else
outputSetToConsole(vectorF);
cout << endl;
}
double calulateDuration()
{
double firstPoint, lastPoint, duration = 0;
for (int i = 0; i < durationCoeff; i++)
{
firstPoint = clock();
calculateAnswer();
lastPoint = clock();
duration += lastPoint - firstPoint;
}
return duration / durationCoeff;
}
void outputDuration()
{
cout << endl;
cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
cout << "Временная сложность алгоритма - O(1), постоянное время" << endl;
cout << "На выполнение алгоритма уходит " << calulateDuration() << " секунд." << endl;
cout << endl;
}
float factorial(int number)
{
float factorial = 1;
for (int i = 1; i <= number; i++)
factorial *= i;
return factorial;
}
int randomInt(int max)
{
return rand() % (max + 1);
}
unsigned int randomVector(long long max)
{
return (rand() * 2 + randomInt(1)) * (rand() * 2 + randomInt(3));
}
void fillVectors()
{
unsigned int maxVector = pow(2, length) - 1;
for (int i = 0; i < length; i++)
{
vectorA = randomVector(maxVector);
vectorB = randomVector(maxVector);
vectorC = randomVector(maxVector);
vectorD = randomVector(maxVector);
}
}
void badInputLoop()
{
while (cin.fail() || customTestAmount < 0)
{
cout << "Неправильный ввод, повторите еще раз:" << endl;
cin.clear();
cin.ignore(32767, '\n');
cin >> customTestAmount;
}
}
void testInfo()
{
cout << setprecision(0);
cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
cout << "Из 26 символов может быть составлено " << factorial(length) << " перестановок" << endl;
cout << "Сколько программа должна проделать тестов со случайными комбинациями?" << endl;
cin >> customTestAmount;
badInputLoop();
if (customTestAmount > 0)
cout << "Отчеты будут выведены в файл " << filename << endl;
else cout << "Тесты не будут проделаны" << endl;
}
void outputTestAnswer(ofstream& output)
{
if (isSetEmpty(vectorF))
output << "пустое множество";
else
outputSetToFile(vectorF, output);
}
void executeTest()
{
ofstream output;
output.open(filename);
for (int i = 0; i < customTestAmount; i++)
{
fillVectors();
vectorF = 0;
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;
calculateAnswer();
output << "Ответ: F = ";
outputTestAnswer(output);
output << endl;
output << '*' << endl;
}
output.close();
}
int main()
{
setupConsole();
outputGreeting();
input();
while (!isStringCorrect(stringA) || !isStringCorrect(stringB) || !isStringCorrect(stringC) || !isStringCorrect(stringD))
{
cout << "Некорректный ввод" << endl;
input();
}
convertSets();
calculateAnswer();
outputAnswer();
outputDuration();
testInfo();
executeTest();
}