From dee39a3ff149a5b6eb95f29e0012b83cddaa3e54 Mon Sep 17 00:00:00 2001 From: Fromant Date: Thu, 20 Feb 2025 14:53:49 +0300 Subject: [PATCH 1/2] 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/2] 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<