From dee39a3ff149a5b6eb95f29e0012b83cddaa3e54 Mon Sep 17 00:00:00 2001 From: Fromant Date: Thu, 20 Feb 2025 14:53:49 +0300 Subject: [PATCH 1/6] First lab completed --- lab1/CMakeLists.txt | 5 +++++ lab1/lab1.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lab1/CMakeLists.txt create mode 100644 lab1/lab1.cpp diff --git a/lab1/CMakeLists.txt b/lab1/CMakeLists.txt new file mode 100644 index 0000000..e61174e --- /dev/null +++ b/lab1/CMakeLists.txt @@ -0,0 +1,5 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.20) + +project(LAB1 CXX) + +add_executable(LAB1 lab1.cpp) \ No newline at end of file diff --git a/lab1/lab1.cpp b/lab1/lab1.cpp new file mode 100644 index 0000000..ae50019 --- /dev/null +++ b/lab1/lab1.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +std::array read_polynom_from_stdin() { + std::array to_return{}; + std::cout << "Quadratic equation ax^2+bx+c=0" << std::endl; + std::cout << "Write \"a\" coefficient numeric value: "; + std::cin >> to_return[0]; + std::cout << "Write \"b\" coefficient numeric value: "; + std::cin >> to_return[1]; + std::cout << "Write \"c\" coefficient numeric value: "; + std::cin >> to_return[2]; + return to_return; +} + +void solve_and_print_equation(const std::array& equation) { + //count discriminant + double discr = equation[1]*equation[1]-4*equation[0]*equation[2]; + if(discr<0) { + std::cout << "There is no roots in real numbers" << std::endl; + return; + } + if(discr==0) { + double root = (-equation[1]+std::sqrt(discr))/2.0; + std::cout << "There is only one root in real numbers: " << root<< std::endl; + return; + } + //2 roots + double sqrt_discr_halfed = std::sqrt(discr)/2; + double minus_b_halfed = -equation[1]/2; + double root1 = minus_b_halfed + sqrt_discr_halfed; + double root2 = minus_b_halfed - sqrt_discr_halfed; + std::cout << "There are two real roots: " << root1<<"; " << root2< Date: Fri, 28 Mar 2025 18:42:28 +0300 Subject: [PATCH 2/6] Changed reading from console to reading from file --- lab1/lab1.cpp | 60 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/lab1/lab1.cpp b/lab1/lab1.cpp index ae50019..b14ea67 100644 --- a/lab1/lab1.cpp +++ b/lab1/lab1.cpp @@ -1,42 +1,54 @@ #include #include #include +#include -std::array read_polynom_from_stdin() { +std::array read_polynom_from_file() { std::array to_return{}; - std::cout << "Quadratic equation ax^2+bx+c=0" << std::endl; - std::cout << "Write \"a\" coefficient numeric value: "; - std::cin >> to_return[0]; - std::cout << "Write \"b\" coefficient numeric value: "; - std::cin >> to_return[1]; - std::cout << "Write \"c\" coefficient numeric value: "; - std::cin >> to_return[2]; + std::ifstream input("input.txt"); + + if (!input.is_open()) { + std::cerr << "Error: Could not open input file" << std::endl; + exit(1); + } + + input >> to_return[0] >> to_return[1] >> to_return[2]; return to_return; } -void solve_and_print_equation(const std::array& equation) { - //count discriminant +void solve_and_write_equation(const std::array& equation) { + std::ofstream output("output.txt"); + + if (!output.is_open()) { + std::cerr << "Error: Could not open output file" << std::endl; + exit(1); + } + + // Count discriminant double discr = equation[1]*equation[1]-4*equation[0]*equation[2]; - if(discr<0) { - std::cout << "There is no roots in real numbers" << std::endl; + + if(discr < 0) { + output << "There is no roots in real numbers" << std::endl; return; } - if(discr==0) { - double root = (-equation[1]+std::sqrt(discr))/2.0; - std::cout << "There is only one root in real numbers: " << root<< std::endl; + + if(discr == 0) { + double root = (-equation[1]+std::sqrt(discr))/(2*equation[0]); + output << "There is only one root in real numbers: " << root << std::endl; return; } - //2 roots - double sqrt_discr_halfed = std::sqrt(discr)/2; - double minus_b_halfed = -equation[1]/2; - double root1 = minus_b_halfed + sqrt_discr_halfed; - double root2 = minus_b_halfed - sqrt_discr_halfed; - std::cout << "There are two real roots: " << root1<<"; " << root2< Date: Fri, 28 Mar 2025 19:01:08 +0300 Subject: [PATCH 3/6] lab2 init --- lab1/CMakeLists.txt | 4 ++-- lab1/lab1.cpp | 54 --------------------------------------------- 2 files changed, 2 insertions(+), 56 deletions(-) delete mode 100644 lab1/lab1.cpp diff --git a/lab1/CMakeLists.txt b/lab1/CMakeLists.txt index e61174e..34fda5d 100644 --- a/lab1/CMakeLists.txt +++ b/lab1/CMakeLists.txt @@ -1,5 +1,5 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.20) -project(LAB1 CXX) +project(LAB2 CXX) -add_executable(LAB1 lab1.cpp) \ No newline at end of file +add_executable(LAB2 lab2.cpp) \ No newline at end of file diff --git a/lab1/lab1.cpp b/lab1/lab1.cpp deleted file mode 100644 index b14ea67..0000000 --- a/lab1/lab1.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include - -std::array read_polynom_from_file() { - std::array to_return{}; - std::ifstream input("input.txt"); - - if (!input.is_open()) { - std::cerr << "Error: Could not open input file" << std::endl; - exit(1); - } - - input >> to_return[0] >> to_return[1] >> to_return[2]; - return to_return; -} - -void solve_and_write_equation(const std::array& equation) { - std::ofstream output("output.txt"); - - if (!output.is_open()) { - std::cerr << "Error: Could not open output file" << std::endl; - exit(1); - } - - // Count discriminant - double discr = equation[1]*equation[1]-4*equation[0]*equation[2]; - - if(discr < 0) { - output << "There is no roots in real numbers" << std::endl; - return; - } - - if(discr == 0) { - double root = (-equation[1]+std::sqrt(discr))/(2*equation[0]); - output << "There is only one root in real numbers: " << root << std::endl; - return; - } - - // 2 roots - double sqrt_discr = std::sqrt(discr); - double denominator = 2*equation[0]; - double root1 = (-equation[1] + sqrt_discr) / denominator; - double root2 = (-equation[1] - sqrt_discr) / denominator; - output << "There are two real roots: " << root1 << "; " << root2 << std::endl; -} - -int main() { - std::cout << "Lab 1 by Krutyanscky RI" << std::endl; - const auto eq = read_polynom_from_file(); - solve_and_write_equation(eq); - return 0; -} \ No newline at end of file From ef348f001db7af71fd957183b7d781580885c119 Mon Sep 17 00:00:00 2001 From: Fromant Date: Mon, 31 Mar 2025 20:03:45 +0300 Subject: [PATCH 4/6] lab2 complete --- lab1/CMakeLists.txt | 7 +++++- lab1/lab1.h | 25 +++++++++++++++++++++ lab1/lab2.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++ lab1/letter.h | 13 +++++++++++ lab1/student.cpp | 28 +++++++++++++++++++++++ lab1/student.h | 19 ++++++++++++++++ lab1/teacher.cpp | 27 +++++++++++++++++++++++ lab1/teacher.h | 20 +++++++++++++++++ 8 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 lab1/lab1.h create mode 100644 lab1/lab2.cpp create mode 100644 lab1/letter.h create mode 100644 lab1/student.cpp create mode 100644 lab1/student.h create mode 100644 lab1/teacher.cpp create mode 100644 lab1/teacher.h diff --git a/lab1/CMakeLists.txt b/lab1/CMakeLists.txt index 34fda5d..cc25774 100644 --- a/lab1/CMakeLists.txt +++ b/lab1/CMakeLists.txt @@ -2,4 +2,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.20) project(LAB2 CXX) -add_executable(LAB2 lab2.cpp) \ No newline at end of file +add_executable(LAB2 lab2.cpp + student.h + student.cpp + teacher.cpp + letter.h + teacher.h) \ No newline at end of file diff --git a/lab1/lab1.h b/lab1/lab1.h new file mode 100644 index 0000000..13d617f --- /dev/null +++ b/lab1/lab1.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +inline std::pair solve_quadratic_equation(const std::array& equation) { + // Count discriminant + double discr = equation[1]*equation[1]-4*equation[0]*equation[2]; + + if(discr < 0) { + return {NAN, NAN}; + } + + if(discr == 0) { + double root = (-equation[1]+std::sqrt(discr))/(2*equation[0]); + return {root, NAN}; + } + + // 2 roots + double sqrt_discr = std::sqrt(discr); + double denominator = 2*equation[0]; + double root1 = (-equation[1] + sqrt_discr) / denominator; + double root2 = (-equation[1] - sqrt_discr) / denominator; + return {root1, root2}; +} \ No newline at end of file diff --git a/lab1/lab2.cpp b/lab1/lab2.cpp new file mode 100644 index 0000000..9dbb73c --- /dev/null +++ b/lab1/lab2.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#include "student.h" +#include "teacher.h" + +std::vector > read_polynoms_from_file() { + std::vector > to_return{}; + std::ifstream input("input.txt"); + + if (!input.is_open()) { + std::cerr << "Error: Could not open input file" << std::endl; + exit(1); + } + + while (!input.eof()) { + double a, b, c; + input >> a >> b >> c; + to_return.emplace_back(std::array{a, b, c}); + } + + return to_return; +} + +int main() { + std::cout << "Lab 2 by Krutyanscky RI" << std::endl; + + std::vector students{ + Student{0.7, "Peter"}, + Student{0.3, "Siemens"}, + Student{1.0, "Steve Jops"}, + Student{0.0, "Scibidi Dobidi"} + }; + + Teacher teacher; + + auto tasks = read_polynoms_from_file(); + + // solve equations and send them to teacher + for (const auto &student: students) { + for (const auto& task : tasks) { + auto answer = student.solve(task); + teacher.add_letter({task, answer, student.name}); + } + } + + teacher.check_all_letters(); + teacher.print_table(); + + return 0; +} diff --git a/lab1/letter.h b/lab1/letter.h new file mode 100644 index 0000000..30f2c1c --- /dev/null +++ b/lab1/letter.h @@ -0,0 +1,13 @@ +#ifndef LETTER_H +#define LETTER_H +#include +#include + +struct Letter { + std::array equation; + std::pair solution; + std::string student_name; +}; + + +#endif //LETTER_H diff --git a/lab1/student.cpp b/lab1/student.cpp new file mode 100644 index 0000000..b0e7323 --- /dev/null +++ b/lab1/student.cpp @@ -0,0 +1,28 @@ +#include "student.h" + +#include "lab1.h" + +std::pair Student::solve(const std::array &coefs) const { + if (rightness_chance <= 0) { + //Bad student + return std::make_pair(0, NAN); + } + const auto solution = solve_quadratic_equation(coefs); + if (rightness_chance >= 1) { + //Good student + return solution; + } + //else, avg student + float rand_val = float(rand()) / float(RAND_MAX); // random float between 0 and 1 + + if (rand_val < rightness_chance) { + // if less, return right solution + return solution; + } + if (rand_val < rightness_chance * 2) { + // semi right solution, like student too lazy to count second root + return {solution.first,NAN}; + } + // else, return horrible solution + return {solution.first - 1, -solution.second}; +} diff --git a/lab1/student.h b/lab1/student.h new file mode 100644 index 0000000..5d1272a --- /dev/null +++ b/lab1/student.h @@ -0,0 +1,19 @@ +#ifndef STUDENTS_H +#define STUDENTS_H +#include +#include +#include + +class Student { + double rightness_chance; + +public: + std::string name; + + explicit Student(double rightness_chance, const std::string& name): rightness_chance(rightness_chance), name(name) { + } + + std::pair solve(const std::array &coefs) const; +}; + +#endif //STUDENTS_H diff --git a/lab1/teacher.cpp b/lab1/teacher.cpp new file mode 100644 index 0000000..4c1cfff --- /dev/null +++ b/lab1/teacher.cpp @@ -0,0 +1,27 @@ +#include "teacher.h" + +#include +#include + +#include "lab1.h" + + +void Teacher::check_all_letters() { + while (!queue.empty()) { + const auto &letter = queue.front(); + if (solve_quadratic_equation(letter.equation) == letter.solution) { + log[letter.student_name]++; + } else { + // ensure student is added to log + log.try_emplace(letter.student_name, 0); + } + queue.pop(); + } +} + +void Teacher::print_table() { + std::cout << "name: right answer count" << std::endl; + for (const auto &entry: log) { + std::cout << entry.first << ": " << entry.second << std::endl; + } +} diff --git a/lab1/teacher.h b/lab1/teacher.h new file mode 100644 index 0000000..2daf724 --- /dev/null +++ b/lab1/teacher.h @@ -0,0 +1,20 @@ +#ifndef TEACHER_H +#define TEACHER_H +#include +#include + +#include "letter.h" + +class Teacher { + std::queue queue; + std::map log; + +public: + void add_letter(const Letter &l) { queue.push(l); } + + void check_all_letters(); + + void print_table(); +}; + +#endif //TEACHER_H From a5f5bd48b2ea91dc8211d71a207190d6d6f19287 Mon Sep 17 00:00:00 2001 From: Fromant Date: Mon, 31 Mar 2025 20:04:05 +0300 Subject: [PATCH 5/6] add example file --- lab1/input.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 lab1/input.txt diff --git a/lab1/input.txt b/lab1/input.txt new file mode 100644 index 0000000..587d364 --- /dev/null +++ b/lab1/input.txt @@ -0,0 +1,4 @@ +1 3 2 +-1 0 12 +-1 1 0 +1 12 2 \ No newline at end of file From a0a6947e88878d6e8660a0ae98cb087a2f7911fe Mon Sep 17 00:00:00 2001 From: Fromant Date: Mon, 31 Mar 2025 20:05:00 +0300 Subject: [PATCH 6/6] add input file name --- lab1/lab2.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lab1/lab2.cpp b/lab1/lab2.cpp index 9dbb73c..e03cb33 100644 --- a/lab1/lab2.cpp +++ b/lab1/lab2.cpp @@ -7,9 +7,9 @@ #include "student.h" #include "teacher.h" -std::vector > read_polynoms_from_file() { +std::vector > read_polynoms_from_file(const std::string& filename) { std::vector > to_return{}; - std::ifstream input("input.txt"); + std::ifstream input(filename); if (!input.is_open()) { std::cerr << "Error: Could not open input file" << std::endl; @@ -37,7 +37,7 @@ int main() { Teacher teacher; - auto tasks = read_polynoms_from_file(); + auto tasks = read_polynoms_from_file("input.txt"); // solve equations and send them to teacher for (const auto &student: students) {