diff --git a/lab1/CMakeLists.txt b/lab1/CMakeLists.txt new file mode 100644 index 0000000..cc25774 --- /dev/null +++ b/lab1/CMakeLists.txt @@ -0,0 +1,10 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.20) + +project(LAB2 CXX) + +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/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 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..e03cb33 --- /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(const std::string& filename) { + std::vector > to_return{}; + std::ifstream input(filename); + + 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("input.txt"); + + // 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