testsAndMisc/CPP/miscelanious/solveQuadraticEquation.cpp
Krzysztof kuhy Rudnicki f6b6995b0e Add tests and fix pre-commit issues across all projects
- C/lichess_random_engine, vocabulary_curve, misc/split,
  1dvelocitysimulator, opening_learner: test suites added
- CPP/miscelanious: tests added
- TS/battery-status, champions_leauge_scores, two-inputs: tests added
- python_pkg/fm24_searcher, wake_alarm: new packages added
- Fix ruff/cppcheck/eslint/clang-format failures
- Update .gitignore for C/C++ build artifacts
2026-04-12 20:45:24 +02:00

43 lines
1.0 KiB
C++

#include "solveQuadraticEquation.h"
#include <iostream>
#include <math.h>
#include <string>
const std::string ENTER = "Enter quadratic equation constants: ";
const std::string WHAT_TO_INPUT = "a, b, c as in: ax^2 + bx + c = 0";
const std::string START = ENTER + WHAT_TO_INPUT;
void print(const std::string s) { std::cout << s << std::endl; }
float getDelta(float a, float b, float c) { return b * b - 4 * a * c; }
float calculateFirstTerm(float a, float b, float delta) {
return (-b - sqrt(delta)) / (2 * a);
}
float calculateSecondTerm(float a, float b, float delta) {
return (-b + sqrt(delta)) / (2 * a);
}
#ifndef TESTING
int main() {
print(START);
float a, b, c;
std::cin >> a;
std::cin >> b;
std::cin >> c;
float delta = getDelta(a, b, c);
if (delta < 0) {
print("delta smaller than 0");
return -1;
}
float x_1 = calculateFirstTerm(a, b, delta);
float x_2 = calculateSecondTerm(a, b, delta);
print("Solutions:");
std::cout << "x_1 = " << x_1 << std::endl;
std::cout << "x_2 = " << x_2 << std::endl;
return 0;
}
#endif