diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d6b116d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,116 @@ +set(PROJECT_NAME quadratic) + +################################################################################ +# Source groups +################################################################################ +set(Source_Files + "main.cpp" + "quadratic.cpp" + "quadratic.h" +) +source_group("Source Files" FILES ${Source_Files}) + +set(ALL_FILES + ${Source_Files} +) + +################################################################################ +# Target +################################################################################ +add_executable(${PROJECT_NAME} ${ALL_FILES}) + +use_props(${PROJECT_NAME} "${CMAKE_CONFIGURATION_TYPES}" "${DEFAULT_CXX_PROPS}") +set(ROOT_NAMESPACE quadratic) + +set_target_properties(${PROJECT_NAME} PROPERTIES + VS_GLOBAL_KEYWORD "Win32Proj" +) +if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64") + set_target_properties(${PROJECT_NAME} PROPERTIES + INTERPROCEDURAL_OPTIMIZATION_RELEASE "TRUE" + ) +elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86") + set_target_properties(${PROJECT_NAME} PROPERTIES + INTERPROCEDURAL_OPTIMIZATION_RELEASE "TRUE" + ) +endif() +################################################################################ +# Compile definitions +################################################################################ +if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64") + target_compile_definitions(${PROJECT_NAME} PRIVATE + "$<$:" + "_DEBUG" + ">" + "$<$:" + "NDEBUG" + ">" + "_CONSOLE;" + "UNICODE;" + "_UNICODE" + ) +elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86") + target_compile_definitions(${PROJECT_NAME} PRIVATE + "$<$:" + "_DEBUG" + ">" + "$<$:" + "NDEBUG" + ">" + "WIN32;" + "_CONSOLE;" + "UNICODE;" + "_UNICODE" + ) +endif() + +################################################################################ +# Compile and link options +################################################################################ +if(MSVC) + if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64") + target_compile_options(${PROJECT_NAME} PRIVATE + $<$: + /Oi; + /Gy + > + /permissive-; + /sdl; + /W3; + ${DEFAULT_CXX_DEBUG_INFORMATION_FORMAT}; + ${DEFAULT_CXX_EXCEPTION_HANDLING} + ) + elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86") + target_compile_options(${PROJECT_NAME} PRIVATE + $<$: + /Oi; + /Gy + > + /permissive-; + /sdl; + /W3; + ${DEFAULT_CXX_DEBUG_INFORMATION_FORMAT}; + ${DEFAULT_CXX_EXCEPTION_HANDLING} + ) + endif() + if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64") + target_link_options(${PROJECT_NAME} PRIVATE + $<$: + /OPT:REF; + /OPT:ICF + > + /DEBUG; + /SUBSYSTEM:CONSOLE + ) + elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86") + target_link_options(${PROJECT_NAME} PRIVATE + $<$: + /OPT:REF; + /OPT:ICF + > + /DEBUG; + /SUBSYSTEM:CONSOLE + ) + endif() +endif() + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ded908c --- /dev/null +++ b/main.cpp @@ -0,0 +1,28 @@ +#include "quadratic.h" + +int main() { + std::cout << "Programm for find value ax^2 + bx + c = 0\n"; + while (true) { + try { + double a = getValidCoefficient("a: "); + double b = getValidCoefficient("b: "); + double c = getValidCoefficient("c: "); + + solveQuadratic(a, b, c); + + std::cout << "\nWould you like more? (yes/no): "; + std::string answer; + std::cin >> answer; + + if (answer != "yes") { + std::cout << "Thanks for using programm.\n"; + break; + } + } + catch (const std::exception& e) { + std::cerr << "Error: " << e.what() << "\n"; + } + } + + return 0; +} \ No newline at end of file diff --git a/quadratic.cpp b/quadratic.cpp new file mode 100644 index 0000000..a809a3e --- /dev/null +++ b/quadratic.cpp @@ -0,0 +1,38 @@ +#include "quadratic.h" + +void solveQuadratic(double a, double b, double c) { + if (a == 0) { + throw std::invalid_argument("'a' != 0 !"); + } + + double discriminant = b * b - 4 * a * c; + + if (discriminant > 0) { + double root1 = (-b + std::sqrt(discriminant)) / (2 * a); + double root2 = (-b - std::sqrt(discriminant)) / (2 * a); + std::cout << "x1 = " << root1 << "\n"; + std::cout << "x2 = " << root2 << "\n"; + } + else if (discriminant == 0) { + double root = -b / (2 * a); + std::cout << "x = " << root << "\n"; + } + else { + std::cout << "complex value.\n"; + } +} + +double getValidCoefficient(const std::string& prompt) { + double coefficient; + while (true) { + std::cout << prompt; + if (std::cin >> coefficient) { + return coefficient; + } + else { + std::cout << "Error: not number.\n"; + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + } + } +} \ No newline at end of file diff --git a/quadratic.h b/quadratic.h new file mode 100644 index 0000000..5590f36 --- /dev/null +++ b/quadratic.h @@ -0,0 +1,12 @@ +#ifndef QUADRATIC_H +#define QUADRATIC_H + +#include +#include +#include + +void solveQuadratic(double a, double b, double c); + +double getValidCoefficient(const std::string& prompt); + +#endif // QUADRATIC_H \ No newline at end of file