Last active
August 29, 2019 08:12
-
-
Save triztian/5bfe9e0621eb4a545e338f9c7cc8063a to your computer and use it in GitHub Desktop.
A very simple C program that uses the General Formula for quadratic polynomials (Ax^2 + Bx + C)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
/** | |
* Declare a helper boolean data type and then define | |
* some handy macros (true, false). | |
* | |
* Learn more about `typedef` here: | |
* | |
* https://overiq.com/c-programming-101/typedef-statement-in-c/ | |
*/ | |
typedef unsigned char bool; | |
// some handy boolean constants. | |
#define true 1 | |
#define false 0 | |
/** | |
* Define an enum that represents a Positive (+) and a Negative (-) sign. | |
* Learn more about enums here: | |
* | |
* https://www.geeksforgeeks.org/enumeration-enum-c | |
*/ | |
enum Sign { | |
Positive = 1, | |
Negative = -1 | |
}; | |
/** | |
* Function declaration. | |
*/ | |
float general_formula(float, float, float, enum Sign, bool*); | |
int main() { | |
bool error; | |
int a, b, c; | |
printf("Introducir el valor Ax^2: "); | |
scanf("%d",&a); | |
printf("\nIntroducir el valor Bx: "); | |
scanf("%d",&b); | |
printf("\nIntroducir el valor C: "); | |
scanf("%d",&c); | |
float x1 = general_formula(a, b, c, Positive, &error); | |
if (error) { | |
printf("\nNo se puede hacer la formula general\nYa que la raiz es negativa. "); | |
return 1; | |
} | |
float x2 = general_formula(a, b, c, Negative, &error); | |
if (error) { | |
printf("\nNo se puede hacer la formula general\nYa que la raiz es negativa. "); | |
return 1; | |
} | |
printf("El primer x es: %.2f y el segundo x es: %.2f",x1,x2); | |
getchar(); | |
return 0; | |
} | |
/** | |
* The general formula of a quadratic polynomial, i.e.: of the form: | |
* | |
* Ax^2 + Bx + C | |
* | |
* Formula: | |
* | |
* x = - (b +/- sqrt(b^2 - 4ac) ) / 2a | |
* | |
* @param a The A coeficient. | |
* @param b The B coeficient. | |
* @param c The C constant. | |
* @param sign That determines whether to _add_ (+) or subtract the expression sqrt(b^2 - 4ac) from b. | |
* @param error A non-null pointer that will be set to `true` if the expression b^2 - 4ac is a negative | |
* number. | |
* | |
* @returns x1 or x2 depending on the `sign` parameter. If `error` is `true` then the return value should be ignored. | |
*/ | |
float general_formula(float a, float b, float c, enum Sign sign, bool *error) { | |
float sq = b * b - 4 * a * c; // b^2 - 4ac | |
*error = sq < 0; // set the error, if sq is less than 0, then we have an error. | |
float numerator = b + (float)(sign) * sqrt(sq); // b +/- sqrt(b^2 - 4ac) | |
float denominator = 2 * a; // 2a | |
return -(numerator / denominator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment