Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <string.h>
- #define TOLERANCE 1.0e-6
- #define TRUE 1
- #define FALSE 0
- //COLOR
- #define KNRM "\x1B[0m"
- #define KRED "\x1B[31m"
- #define KGRN "\x1B[32m"
- #define KYEL "\x1B[33m"
- #define KBLU "\x1B[34m"
- #define KMAG "\x1B[35m"
- #define KCYN "\x1B[36m"
- #define KWHT "\x1B[37m"
- //---------------------------------------------------------------------
- // UTILS
- //--------------------------------------------------------------------
- char *suitableReverse(char *str){
- for(int i = 0, j = strlen(str) -1; i<j; i++, j--){
- char tmp = str[i];
- str[i] = str[j];
- str[j] = tmp;
- }
- return str;
- }
- int isDigit(char c){
- return (c >= '0' && c <= '9');
- }
- int isAlpha(char c){
- return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
- }
- double ft_atof(char *nb){
- char party[2][10] = {"\0", "\0"};
- int i = 0, j = 0, k = 0, exp = 1,negatif = 0, sign, decimal=FALSE;
- char c;
- int exp_ent = 1, exp_dec = 1;
- sign = (nb[i] == '-' || nb[i] == '+');
- if(sign){
- negatif = (nb[i] == '-');
- i += 1;
- }
- printf("\nnegatif= %d\n", negatif);
- while(nb[i]){
- c = nb[i];
- if(isDigit(c) && !decimal){
- party[j][k++] = c;
- exp_ent *= 10;
- }
- else if (c == '.'){
- decimal = TRUE;
- j++;
- k = 0;
- } else if(isDigit(c) && decimal){
- party[j][k++] = c;
- exp_dec *= 10;
- }
- i++;
- }
- printf("[%s, [%s, len = %ld, %ld\n", party[0], party[1], strlen(party[0]), strlen(party[1]));
- j = 0;
- double result = 0.0f;
- exp_dec = 10;
- printf("exp: %d\n", exp_ent);
- while(j < 2){
- k = 0;
- while(k < strlen(party[j])){
- c = party[j][k];
- if(j == 0){
- if(j == 0)
- exp_ent /= 10;
- result += (c - '0')*exp_ent;
- exp_ent /= 10;
- }
- else if (j == 1){
- exp_ent = exp_ent < 10 ? 10: exp_ent;
- result += (c - '0')/((float)exp_ent);
- exp_ent *= 10;
- }
- k++;
- }
- j++;
- }
- return -negatif * result ;
- }
- //---------------------------------------------------------------------
- // EQUATIONS DEGRE 1, 2, 3, 4 SOLVER
- //--------------------------------------------------------------------
- int degre_one(double a, double b, double *roots){
- if(fabs(a) < TOLERANCE)
- return 0;
- roots[0] = -b/((float)a);
- return 1;
- }
- int quadratic(double a, double b, double c, double *roots)
- {
- double delta;
- if (fabs(a) < (double)TOLERANCE)
- {
- if (fabs(b) < (double)TOLERANCE)
- return (0);
- else
- {
- roots[0] = -c / b;
- return (1);
- }
- return (0);
- }
- delta = (b * b) - (4 * a * c);
- if (delta < (double)(-TOLERANCE))
- return (0);
- if(fabs(delta) < (double)TOLERANCE)
- {
- roots[0] = -b / (2.0 * a);
- roots[1] = roots[0];
- return (2);
- }
- roots[0] = (-b + sqrtf(delta)) / (2.0 * a);
- roots[1] = (-b - sqrtf(delta)) / (2.0 * a);
- return (2);
- }
- int cubic(double a, double b, double c, double d, double roots[])
- {
- double p;
- double q;
- double w1;
- double w2;
- double u;
- double v;
- double t;
- double delta;
- double sign;
- if (fabs(a) <= (double)TOLERANCE)
- return (quadratic(b, c, d, &roots[0]));
- p = -b*b / (3.0 * a*a) + c/a;
- q = ((2.0 * b * b * b) / (27.0 * a*a*a)) - ((b*c)/(3.0 * a * a)) + d/a;
- if(fabs(p) <= (double)TOLERANCE)
- {
- roots[0] = powf(-q, 1.0/3.0) - b /(3.0 * a);
- return (1);
- }
- if (fabs(q) <= (double)TOLERANCE && fabs(p) > (double)TOLERANCE)
- {
- if(p < (double)(-TOLERANCE))
- {
- roots[0] = 0.0;
- roots[1] = sqrtf(p);
- roots[2] = -1.0 * sqrtf(p);
- return(3);
- }
- else
- {
- roots[0] = 0.0;
- return (1);
- }
- }
- delta = (q * q / 4.0) + (p * p * p / 27.0);
- if (delta > (double)TOLERANCE)
- {
- sign = ((-q/2.0 + sqrtf(delta)) < 0.0) ? -1.0 : 1.0;
- w1 = pow(fabs(-q/2.0 + sqrtf(delta)), 1.0/3.0);
- w1 *= sign;
- sign = ((-q/2.0 - sqrtf(delta)) < 0.0) ? -1.0 : 1.0;
- w2 = pow(fabs(-q/2.0 - sqrtf(delta)), 1.0/3.0);
- w2 *= sign;
- roots[0] = w1 + w2 - b/(3.0 * a);
- return (1);
- }
- else if (fabs(delta) <= (double)TOLERANCE)
- {
- roots[0] = (3.0 * q)/p - b/(3.0 * a);
- roots[1] = ((-3.0 * q) / (2.0 * p)) - b/(3.0 * a);
- roots[2] = roots[1];
- return(3);
- }
- else
- {
- u = 2.0 * sqrtf(-p/3.0);
- //v = -q/(2.0 * copysign(pow(fabs(-p/3.0), 3/2), -p/3.0));
- v = -q/(2.0 * pow(-p/3.0, 3.0/2.0));
- t = acos(v)/3.0;
- roots[0] = u * cos(t) - b/(3*a);
- roots[1] = u * cos(t + (2.0 * M_PI/3.0)) - b/(3*a);
- roots[2] = u * cos(t + (4.0 * M_PI/3.0)) - b/(3*a);
- return (3);
- }
- }
- int quartic(double a, double b, double c, double d, double e, double *roots)
- {
- double p;
- double q;
- double r;
- double y[4];
- int num_sol[2];
- double a0;
- double b0;
- int i;
- int j;
- if(fabs(a) < (double)TOLERANCE)
- return(cubic(b, c, d, e, &roots[0]));
- p = -3.0 * b*b/(8.0 * a*a) + c/a;
- q = b*b*b/(8.0*a*a*a) - b*c/(2.0*a*a) + d/a;
- r = -3*b*b*b*b/(256.0*a*a*a*a) + c*b*b/(16.0*a*a*a) - b*d/(4.0*a*a) + e/a;
- if(fabs(p) <= (double)TOLERANCE)
- {
- if (fabs(q) <= (double)TOLERANCE)
- {
- if (r > 0.0)
- return (0);
- else
- {
- roots[0] = -sqrtf(-r);
- roots[1] = sqrtf(-r);
- return (2);
- }
- }
- }
- if (fabs(q) <= (double)TOLERANCE)
- {
- num_sol[0] = quadratic(1.0, p, r, &y[0]);
- i = -1;
- j = 0;
- while(++i < num_sol[0])
- {
- if (y[i] > 0.0)
- {
- roots[j++] = sqrtf(y[i]);
- roots[j++] = -sqrtf(y[i]);
- }
- if (i == num_sol[0])
- return(j);
- }
- }
- num_sol[0] = cubic(8.0, -4.0*p, -8.0*r, 4.0*r*p - q*q, &y[0]);
- if (0 == num_sol[0])
- return (0);
- if (-p + 2.0 * y[0] < 0.0)
- return (0);
- a0 = sqrtf(-p + 2.0 * y[0]);
- //b0 = (fabs(a0) <= (double)TOLERANCE) ? sqrtf(y[0]*y[0] - r) : sqrtf(-q/(2.0*a0));
- //num_sol[0] = quadratic(1.0, a0, y[0]+b0, &roots[0]);
- num_sol[0] = quadratic(1.0, a0, y[0] - q/(2.0*a0), &roots[0]);
- i = -1;
- while(++i < num_sol[0])
- roots[i] -= b/(4.0 * a);
- //num_sol[1] = quadratic(1.0, a0, y[0]+b0, &roots[num_sol[0]]);
- num_sol[1] = quadratic(1.0, -a0, y[0] + q/(2.0 * a0), &roots[num_sol[0]]);
- i = -1;
- while(++i < num_sol[1])
- roots[num_sol[0] + i] -= b/(4.0 * a);
- return(num_sol[0] + num_sol[1]);
- }
- //--------------------------------------------------------------
- // SECURITY SCANF
- //--------------------------------------------------------------
- void clear_stdin(void)
- {
- int c;
- while((c = getchar()) != '\n' && c != EOF)
- ;
- }
- int myCharCleanScanf(char *character){
- int secure = scanf("%c", character);
- clear_stdin();
- return (secure != 0 && secure != EOF ) ? TRUE : FALSE;
- }
- int myEquationCleanScanf(char *str){
- int secure = scanf("%s", str);
- clear_stdin();
- return (secure != 0 && secure != EOF ) ? TRUE : FALSE;
- }
- //--------------------------------------------------------------------
- // PARSING INPUT
- //--------------------------------------------------------------------
- int get_coef(char *input, double *coef ){
- char c;
- char *start = input, *end;
- int i = 0;
- char dest[10];
- coef[0]=0.0f;
- coef[1]=0.0f;
- coef[2]=0.0f;
- coef[3]=0.0f;
- coef[4]=0.0f;
- while((end = strchr(start, 'X')) && end){
- //coef[i]= atof(strncpy(dest, start, end - start));
- //printf("coef[%lf] = ", coef[i]);
- i++;
- if(*(end + 1) == '^'){
- if(*(end + 2) == '4'){
- coef[0]= atof(strncpy(dest, start, end - start));
- }
- if(*(end + 2) == '3'){
- coef[1]= atof(strncpy(dest, start, end - start));
- }
- if(*(end + 2) == '2'){
- coef[2]= atof(strncpy(dest, start, end - start));
- }
- end += 3;
- }
- else if(*end == 'X'){
- if(*(end + 1) == '+')
- end += 2;
- else if (*(end + 1) == '-')
- end += 1;
- coef[3]= atof(strncpy(dest, start, end - start));
- }
- start = end;
- i++;
- }
- //printf("start: %s\n", start);
- coef[4] = atof(strcpy(dest, start));
- //printf("Coef[%lf %lf %lf %lf %lf] ", coef[0], coef[1], coef[2], coef[3], coef[4]);
- //printf("coef[%lf] = ", coef[i + 1]);
- if(fabs(coef[0]) > TOLERANCE) return 4;
- if(fabs(coef[1]) > TOLERANCE) return 3;
- if(fabs(coef[2]) > TOLERANCE) return 2;
- if(fabs(coef[3]) > TOLERANCE) return 1;
- return 0;
- }
- int main(int argc, char **argv)
- {
- int continu = TRUE, numsol;
- char input[50] = {'\0'};
- double coef[8] = {0.0f, 0.0f, 0.0f, 0.0f};
- char answer;
- double roots[5];
- do {
- printf("\e[1;1H\e[2J");
- printf("\nPlease, Enter your equation from degre 1 to degre 4\n\n\n");
- int secure = myEquationCleanScanf(&input[0]);
- if(!secure){
- printf("Error Error\n");
- exit(1);
- }
- int degree = get_coef(&input[0], &coef[0]);
- if(4 == degree){
- numsol = quartic(coef[0], coef[1], coef[2], coef[3], coef[4], &roots[0]);
- if(numsol == 4){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("4 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf, X4 = %.3lf }", roots[0], roots[1], roots[2], roots[3]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- } else if(numsol == 3){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("3 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf }", roots[0], roots[1], roots[2]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else if(numsol == 2){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("2 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else if(numsol == 1){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("1 SOLUTION: {X1 = %.3lf }", roots[0]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else {
- printf("%s\n", KRED);
- printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }
- }else if(3 == degree){
- numsol = cubic(coef[1], coef[2], coef[3], coef[4], roots);
- if(numsol == 3){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("3 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf }", roots[0], roots[1], roots[2]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else if(numsol == 2){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("2 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
- }else if(numsol == 1){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("1 SOLUTION: { X1 = %.3lf }", roots[0]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else {
- printf("%s\n", KRED);
- printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
- }
- }else if(2 == degree){
- numsol=quadratic(coef[2], coef[3], coef[4], &roots[0]);
- if(numsol == 2){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("2 SOLUTIONS: {X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else if(numsol == 1){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("1 SOLUTION: { X1 = %.3lf }", roots[0]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else {
- printf("%s\n", KRED);
- printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
- }
- }else if(1 == degree){
- numsol = degre_one(coef[3], coef[4], &roots[0]);
- if(numsol == 1){
- printf("%s\n", KGRN);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- printf("SOLUTION: { X = %.3lf }", roots[0]);
- printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
- }else {
- printf("%s\n", KRED);
- printf("\n\n\n\n SORRY: No SOLUTION FOUND");
- }
- }
- printf("%s\n", KWHT);
- }while(printf("\n\n\n\nContinu? :yes = 'y', no: 'n' ->\n") && myCharCleanScanf(&answer) && (answer != 'n'));
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement