diff --git a/Lab2/Source.cpp b/Lab2/Source.cpp new file mode 100644 index 0000000..117dee4 --- /dev/null +++ b/Lab2/Source.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/// +struct Equation { + double a, b, c; +}; + +/// +enum class StudentType { + Excellent, /// + Average, /// + Poor /// +}; + +/// +struct Student { + std::string name; + StudentType type; +}; + +/// +struct Letter { + Equation equation; /// + std::pair studentAnswer; /// + std::string studentName; /// +}; + +/// +/// , +std::vector readEquations(const std::string& filename) { + std::vector equations; + std::ifstream file(filename); + if (!file.is_open()) { + std::cerr << " : " << filename << std::endl; + return equations; + } + double a, b, c; + while (file >> a >> b >> c) { + equations.push_back({ a, b, c }); + } + file.close(); + return equations; +} + + /// +std::pair solveQuadratic(double a, double b, double c) { + if (a == 0) { /// + + if (b == 0) return { NAN, NAN }; /// + + else { + double root = -c / b; + return { root, root }; /// + } + } + double discriminant = b * b - 4 * a * c; + if (discriminant > 0) { /// 2 + double root1 = (-b + sqrt(discriminant)) / (2 * a); + double root2 = (-b - sqrt(discriminant)) / (2 * a); + return { root1, root2 }; + } + else if (discriminant == 0) { /// 1 + double root = -b / (2 * a); + return { root, root }; + } + else return { NAN, NAN }; + /// , - . +} + +/// +std::pair generateStudentAnswer(const Student& student, const Equation& eq) { + std::random_device rd; + std::mt19937 gen(rd()); /// + std::uniform_real_distribution<> dis(0.0, 1.0); /// + /// + + switch (student.type) { + case StudentType::Excellent: /// : + return solveQuadratic(eq.a, eq.b, eq.c); + case StudentType::Average: /// : 60% + if (dis(gen) < 0.6) { + return solveQuadratic(eq.a, eq.b, eq.c); + } + else { + return { dis(gen) * 10, dis(gen) * 10 }; + } + case StudentType::Poor: + return { 0.0, 0.0 }; + default: + return { NAN, NAN }; + } +} + +/// +bool checkAnswer(const Equation& eq, const std::pair& studentAnswer) { + auto correctAnswer = solveQuadratic(eq.a, eq.b, eq.c); + const double epsilon = 1e-6; + + if (std::isnan(correctAnswer.first) && std::isnan(studentAnswer.first)) return true; + + if (std::abs(correctAnswer.first - studentAnswer.first) < epsilon && + std::abs(correctAnswer.second - studentAnswer.second) < epsilon) { + return true; + } + + if (std::abs(correctAnswer.first - studentAnswer.second) < epsilon && + std::abs(correctAnswer.second - studentAnswer.first) < epsilon) { + return true; + } + + return false; +} + +/// +void printResults(const std::map>& results) { + /// + size_t maxNameLength = 0; + for (const auto& pair : results) { + if (pair.first.length() > maxNameLength) { + maxNameLength = pair.first.length(); + } + } + + std::cout << std::left << std::setw(maxNameLength + 2) << " " + << std::setw(20) << " " + << " " << "\n"; + std::cout << std::string(maxNameLength + 40, '-') << "\n"; + + for (const auto& pair : results) { + std::cout << std::left << std::setw(maxNameLength + 2) << pair.first + << std::setw(20) << pair.second.first + << pair.second.second << "\n"; + } +} + + +int main() { + setlocale(LC_CTYPE, "RU"); + + std::vector equations = readEquations("equations.txt"); + if (equations.empty()) { + std::cout << " .\n"; + return 1; + } + + /// + std::vector students = { + {" ", StudentType::Excellent}, + {" ", StudentType::Average}, + {" ", StudentType::Excellent}, + {" ", StudentType::Poor}, + {" ", StudentType::Average}, + {" ", StudentType::Average}, + {" ", StudentType::Excellent}, + {" ", StudentType::Average}, + {" ", StudentType::Average}, + {" ", StudentType::Poor}, + {" ", StudentType::Poor} + }; + + /// + std::queue letterQueue; + for (const auto& student : students) { + for (const auto& eq : equations) { + auto answer = generateStudentAnswer(student, eq); + letterQueue.push({ eq, answer, student.name }); + } + } + + /// : -> ( , ) + std::map> results; + while (!letterQueue.empty()) { + Letter letter = letterQueue.front(); + letterQueue.pop(); + bool isCorrect = checkAnswer(letter.equation, letter.studentAnswer); + auto& result = results[letter.studentName]; + result.second++; + if (isCorrect) { + result.first++; + } + } + + printResults(results); + + return 0; +} \ No newline at end of file diff --git a/Lab2/equations.txt b/Lab2/equations.txt new file mode 100644 index 0000000..2c4cf6b --- /dev/null +++ b/Lab2/equations.txt @@ -0,0 +1,13 @@ +1 5 6 +1 -2 1 +0 2 4 +1 6 5 +7 -49 84 +1 3 -10 +6 -42 72 +1 -7 12 +1 -9 20 +2 -8 6 +1 4 -21 +1 -11 30 +3 -15 18 \ No newline at end of file diff --git a/Lab2/lab2.sln b/Lab2/lab2.sln new file mode 100644 index 0000000..0847d91 --- /dev/null +++ b/Lab2/lab2.sln @@ -0,0 +1,36 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35828.75 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab2", "lab2.vcxproj", "{73EF44AC-7FDC-4963-AA27-76B486E7F42E}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Элементы решения", "Элементы решения", "{754FC069-D67B-A9D7-50A1-8D1CA196D8F1}" + ProjectSection(SolutionItems) = preProject + equations.txt = equations.txt + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Debug|x64.ActiveCfg = Debug|x64 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Debug|x64.Build.0 = Debug|x64 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Debug|x86.ActiveCfg = Debug|Win32 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Debug|x86.Build.0 = Debug|Win32 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Release|x64.ActiveCfg = Release|x64 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Release|x64.Build.0 = Release|x64 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Release|x86.ActiveCfg = Release|Win32 + {73EF44AC-7FDC-4963-AA27-76B486E7F42E}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B38A33E1-0923-4996-9BCB-C5E3A3D4E5CF} + EndGlobalSection +EndGlobal diff --git a/Lab2/lab2.vcxproj b/Lab2/lab2.vcxproj new file mode 100644 index 0000000..ccad58f --- /dev/null +++ b/Lab2/lab2.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {73ef44ac-7fdc-4963-aa27-76b486e7f42e} + lab2 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Lab2/lab2.vcxproj.filters b/Lab2/lab2.vcxproj.filters new file mode 100644 index 0000000..ef66db6 --- /dev/null +++ b/Lab2/lab2.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + \ No newline at end of file