Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions Lab2/Source.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <random>
#include <iomanip>

/// ��������� ��� �������� ������������� ����������� ���������
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<double, double> studentAnswer; /// ����� ��������
std::string studentName; /// ��� ��������
};

/// ������ ��������� �� �����
/// � ����� ������ ������������ ���������� ���������, � ���� ������� �������� ��
std::vector<Equation> readEquations(const std::string& filename) {
std::vector<Equation> 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<double, double> 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<double, double> 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<double, double>& 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<std::string, std::pair<int, int>>& 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<Equation> equations = readEquations("equations.txt");
if (equations.empty()) {
std::cout << "�� ������� ��������� ���������.\n";
return 1;
}

/// ������ ���������
std::vector<Student> 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<Letter> letterQueue;
for (const auto& student : students) {
for (const auto& eq : equations) {
auto answer = generateStudentAnswer(student, eq);
letterQueue.push({ eq, answer, student.name });
}
}

/// ������� �����������: ��� -> (���������� ������, ����� ����������)
std::map<std::string, std::pair<int, int>> 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;
}
13 changes: 13 additions & 0 deletions Lab2/equations.txt
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions Lab2/lab2.sln
Original file line number Diff line number Diff line change
@@ -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
135 changes: 135 additions & 0 deletions Lab2/lab2.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{73ef44ac-7fdc-4963-aa27-76b486e7f42e}</ProjectGuid>
<RootNamespace>lab2</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Source.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Loading