A quadratic equation solver written in modern C++. It supports both real and complex coefficients and computes roots using the quadratic formula.
A quadratic equation is of the form:
The discriminant is given by:
The roots are then:
- If
$\Delta > 0$ : two distinct real roots. - If
$\Delta = 0$ : one real repeated root. - If
$\Delta < 0$ : two complex conjugate roots.
- Compute real roots and complex roots of any quadratic equation.
- Accept coefficients in both real and complex form.
- Support for square root notation in input (e.g.
sqrt(3)
,2sqrt(5)
,-sqrt(7)
). - Colored console output for clarity (on supported terminals).
- Cross-platform compatibility (Linux, macOS, Windows).
- A C++20 compiler (GCC β₯ 10, Clang β₯ 10, MSVC β₯ 2019)
- CMake β₯ 3.16
git clone https://github.com/yourname/QuadCalculator.git
cd QuadCalculator
mkdir build && cd build
cmake ..
cmake --build .
This will produce an executable named QuadCalc
(or QuadCalc.exe
on Windows).
./QuadCalc -h
Mode: -r
./QuadCalc -r 1 -3 2
Output:
The discriminant is: 1
Root 1 : (2.00)
Root 2 : (1.00)
Supports square root notation:
./QuadCalc -r 1 sqrt(2) 3
Mode: -cc
Arguments are written as real,imag
. Example:
./QuadCalc -cc 1,2 3,4 5,6
Output:
The discriminant is: (-28.00 + 44.00i)
Root 1 : (-0.23 + 0.69i)
Root 2 : (-2.77 - 1.69i)
You can also use square root syntax inside components:
./QuadCalc -cc "2,sqrt(3)" "3,4" "5,6"
"sqrt(3)"
.
- Numbers:
1
,-3
,2.5
- Square roots:
sqrt(3)
,2sqrt(5)
,-sqrt(7)
-
Format:
real,imag
-
Examples:
-
1,2
β$1 + 2i$ -
-3,0
β$-3$ -
0,-4
β$-4i$
-
- Both real and imaginary parts support
sqrt(...)
. - Shell-friendly form (alternative):
./QuadCalc -cc 1 2 3 4 5 6 # treated as (1+2i), (3+4i), (5+6i)
./QuadCalc -r 1 -5 6
Equation:
Discriminant:
Roots:
./QuadCalc -r 1 2 5
Equation:
Discriminant:
Roots:
./QuadCalc -cc 1,1 2,0 3,0
Equation:
Discriminant:
Roots:
./QuadCalc -r 1 sqrt(2) -3
Equation:
Discriminant:
Roots:
Real Discriminant (Ξ):
Ξ > 0: ββββββββββ (two distinct real roots)
Ξ = 0: β (one repeated real root)
Ξ < 0: β (complex conjugate pair)
β± β²
β β (on complex plane)
QuadCalculator/
βββ includes/
β βββ ColorMod.h
β βββ Complex.h
β βββ PPrintter.h
β βββ PPrintter.cpp # currently in includes (odd)
βββ src/
β βββ QuadCalculator.cpp
βββ CMakeLists.txt
|ββ .gitignore
βββ README.md
- Uses
std::complex<double>
for all calculations - Handles edge cases like
$a = 0$ (linear equation) - Robust square root parsing with error checking
- Parse input coefficients (real or complex, with sqrt support)
-
Validate that
$a \neq 0$ -
Calculate discriminant
$\Delta = b^2 - 4ac$ - Compute roots using quadratic formula
- Format output with appropriate precision
- Invalid coefficient formats
- Non-quadratic equations (
$a = 0$ ) - Malformed square root expressions
- Shell escaping issues
Create a script to solve multiple equations:
#!/bin/bash
echo "Solving multiple quadratics:"
./QuadCalc -r 1 -2 1 # (x-1)Β²
./QuadCalc -r 1 0 -4 # xΒ² - 4
./QuadCalc -r 1 1 1 # xΒ² + x + 1
Pipe results to analysis tools:
./QuadCalc -r 1 -3 2 | grep "Root" | awk '{print $3}'
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes and add tests
- Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
MIT Licenseβsee the LICENSE file for details.
- Thanks to the C++ community for modern language features
- Inspired by classic mathematical computation tools
- Built with β€οΈ for students and engineers