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
10 changes: 10 additions & 0 deletions lab1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions lab1/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 3 2
-1 0 12
-1 1 0
1 12 2
25 changes: 25 additions & 0 deletions lab1/lab1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <array>
#include <cmath>

inline std::pair<double, double> solve_quadratic_equation(const std::array<double, 3>& 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};
}
54 changes: 54 additions & 0 deletions lab1/lab2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <array>
#include <cmath>
#include <fstream>
#include <vector>

#include "student.h"
#include "teacher.h"

std::vector<std::array<double, 3> > read_polynoms_from_file(const std::string& filename) {
std::vector<std::array<double, 3> > 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<Student> 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;
}
13 changes: 13 additions & 0 deletions lab1/letter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef LETTER_H
#define LETTER_H
#include <array>
#include <string>

struct Letter {
std::array<double, 3> equation;
std::pair<double, double> solution;
std::string student_name;
};


#endif //LETTER_H
28 changes: 28 additions & 0 deletions lab1/student.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "student.h"

#include "lab1.h"

std::pair<double, double> Student::solve(const std::array<double, 3> &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};
}
19 changes: 19 additions & 0 deletions lab1/student.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef STUDENTS_H
#define STUDENTS_H
#include <array>
#include <string>
#include <utility>

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<double, double> solve(const std::array<double, 3> &coefs) const;
};

#endif //STUDENTS_H
27 changes: 27 additions & 0 deletions lab1/teacher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "teacher.h"

#include <iostream>
#include <ostream>

#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;
}
}
20 changes: 20 additions & 0 deletions lab1/teacher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef TEACHER_H
#define TEACHER_H
#include <map>
#include <queue>

#include "letter.h"

class Teacher {
std::queue<Letter> queue;
std::map<std::string, int> log;

public:
void add_letter(const Letter &l) { queue.push(l); }

void check_all_letters();

void print_table();
};

#endif //TEACHER_H