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
116 changes: 116 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
"$<$<CONFIG:Debug>:"
"_DEBUG"
">"
"$<$<CONFIG:Release>:"
"NDEBUG"
">"
"_CONSOLE;"
"UNICODE;"
"_UNICODE"
)
elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86")
target_compile_definitions(${PROJECT_NAME} PRIVATE
"$<$<CONFIG:Debug>:"
"_DEBUG"
">"
"$<$<CONFIG:Release>:"
"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
$<$<CONFIG:Release>:
/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
$<$<CONFIG:Release>:
/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
$<$<CONFIG:Release>:
/OPT:REF;
/OPT:ICF
>
/DEBUG;
/SUBSYSTEM:CONSOLE
)
elseif("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x86")
target_link_options(${PROJECT_NAME} PRIVATE
$<$<CONFIG:Release>:
/OPT:REF;
/OPT:ICF
>
/DEBUG;
/SUBSYSTEM:CONSOLE
)
endif()
endif()

28 changes: 28 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
38 changes: 38 additions & 0 deletions quadratic.cpp
Original file line number Diff line number Diff line change
@@ -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<std::streamsize>::max(), '\n');
}
}
}
12 changes: 12 additions & 0 deletions quadratic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef QUADRATIC_H
#define QUADRATIC_H

#include <iostream>
#include <cmath>
#include <stdexcept>

void solveQuadratic(double a, double b, double c);

double getValidCoefficient(const std::string& prompt);

#endif // QUADRATIC_H