Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.

Solution For C Program - 

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.


#include <iostream>
#include <math.h>
#include <windows.h>

using namespace std;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);



double findUnknown(int, int);
double findUnknown(double, double);

double findUnknown(int, int, int);
double findUnknown(double, double, double);

void testFindUnknow2IntType(int test, int a, int b , double expected)
{
double result = findUnknown(a,b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow2DoubleType(int test, double a, double b, double expected)
{
double result = findUnknown(a, b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3IntType(int test, int a, int b, int c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3DoubleType(int test, double a, double b, double c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}


void main()
{
int menuButton = 0;
int show = 0;

int a = 0;
int b = 0;
int c = 0;

cout << "1.Line\n2.Square\n";
cin >> menuButton;

if (menuButton == 1)
{
/*cout << "1. a*x + b = 0" << endl;
cin >> a >> b;

cout << findUnknown(a, b) << endl;*/

testFindUnknow2IntType(1, 2, 4, -2);
testFindUnknow2IntType(2, 1, 5, -5);
testFindUnknow2DoubleType(3, 4, 2, 0.5);
testFindUnknow2DoubleType(4, 5, 2.5 , 0.5);

}

if (menuButton == 2)
{
/*cout << "2. a*x^2 + b*x + c = 0" << endl;
cin >> a >> b >> c;

show = findUnknown(a, b, c);
cout << "X2 = " << show << endl;*/

testFindUnknow3IntType(1, 1, -2, -3, -1);
testFindUnknow3IntType(2, 1, 5, 6, -3);
testFindUnknow3DoubleType(3, 0.5, 6, 0, -12);
testFindUnknow3DoubleType(4, 12, 5, -0.5, -0.5);

}
}


double findUnknown(int a, int b)
{
double x = b * (-1) / a;
return x;
}

double findUnknown(double a, double b)
{
double x = b / a;
return x;
}

double findUnknown(int a, int b, int c)
{
int discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / 2 * a;
//double x2 = (b*(-1) - sqrt(discriminant)) / 2 * a;

return x1;
}

double findUnknown(double a, double b, double c)
{
double discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
//double x2 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );

return x1;
}


Learn More :