Advertisement
tresonance

equation1_2_3_4_solver

Feb 18th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define TOLERANCE 1.0e-6
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10. //COLOR
  11. #define KNRM  "\x1B[0m"
  12. #define KRED  "\x1B[31m"
  13. #define KGRN  "\x1B[32m"
  14. #define KYEL  "\x1B[33m"
  15. #define KBLU  "\x1B[34m"
  16. #define KMAG  "\x1B[35m"
  17. #define KCYN  "\x1B[36m"
  18. #define KWHT  "\x1B[37m"
  19.  
  20.  
  21. //---------------------------------------------------------------------
  22. //              UTILS
  23. //--------------------------------------------------------------------
  24.  
  25. char    *suitableReverse(char *str){
  26.     for(int i = 0, j = strlen(str) -1; i<j;  i++, j--){
  27.         char tmp = str[i];
  28.         str[i] = str[j];
  29.         str[j] = tmp;
  30.     }
  31.     return str;
  32. }
  33.  
  34. int        isDigit(char c){
  35.  
  36.     return (c >= '0' && c <= '9');
  37. }
  38.  
  39. int         isAlpha(char c){
  40.  
  41.     return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  42. }
  43.  
  44.  
  45. double  ft_atof(char *nb){
  46.  
  47.     char party[2][10] = {"\0", "\0"};
  48.     int i = 0, j = 0, k = 0, exp = 1,negatif = 0, sign, decimal=FALSE;
  49.     char c;
  50.     int exp_ent = 1, exp_dec = 1;
  51.  
  52.      
  53.      sign = (nb[i] == '-' ||  nb[i] == '+');
  54.      if(sign){
  55.         negatif = (nb[i] == '-');
  56.         i += 1;
  57.      }
  58.      printf("\nnegatif= %d\n", negatif);
  59.     while(nb[i]){
  60.         c = nb[i];
  61.  
  62.         if(isDigit(c) && !decimal){
  63.             party[j][k++] = c;
  64.             exp_ent *= 10;
  65.         }
  66.         else if (c == '.'){
  67.             decimal = TRUE;
  68.             j++;
  69.             k = 0;
  70.  
  71.         } else if(isDigit(c) && decimal){
  72.             party[j][k++] = c;
  73.             exp_dec *= 10;
  74.         }
  75.  
  76.         i++;
  77.  
  78.     }
  79.  
  80.     printf("[%s,  [%s, len = %ld, %ld\n", party[0], party[1], strlen(party[0]), strlen(party[1]));
  81.     j = 0;
  82.     double result = 0.0f;
  83.     exp_dec = 10;
  84.  
  85.     printf("exp: %d\n", exp_ent);
  86.     while(j < 2){
  87.  
  88.         k = 0;
  89.         while(k < strlen(party[j])){
  90.  
  91.             c = party[j][k];
  92.  
  93.            
  94.             if(j == 0){
  95.                 if(j == 0)
  96.                     exp_ent /= 10;
  97.                 result += (c - '0')*exp_ent;
  98.                 exp_ent /= 10;
  99.  
  100.             }
  101.             else if (j == 1){
  102.                 exp_ent = exp_ent < 10 ? 10: exp_ent;
  103.                 result += (c - '0')/((float)exp_ent);
  104.                 exp_ent *= 10;
  105.             }
  106.             k++;
  107.         }
  108.         j++;
  109.        
  110.     }
  111.  
  112.     return -negatif * result ;
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119. //---------------------------------------------------------------------
  120. //                   EQUATIONS DEGRE 1, 2, 3, 4 SOLVER
  121. //--------------------------------------------------------------------
  122.  
  123. int     degre_one(double a, double b, double *roots){
  124.     if(fabs(a) < TOLERANCE)
  125.         return 0;
  126.     roots[0] = -b/((float)a);
  127.     return 1;
  128. }
  129.  
  130. int     quadratic(double a, double b, double c, double *roots)
  131. {
  132.     double delta;
  133.  
  134.  
  135.     if (fabs(a) < (double)TOLERANCE)
  136.     {
  137.         if (fabs(b) < (double)TOLERANCE)
  138.             return (0);
  139.         else
  140.         {
  141.             roots[0] = -c / b;
  142.             return (1);
  143.         }
  144.         return (0);
  145.     }
  146.     delta = (b * b) - (4 * a * c);
  147.     if (delta < (double)(-TOLERANCE))
  148.         return (0);
  149.     if(fabs(delta) < (double)TOLERANCE)
  150.     {
  151.         roots[0] = -b / (2.0 * a);
  152.         roots[1] = roots[0];
  153.         return (2);
  154.     }
  155.     roots[0] = (-b + sqrtf(delta)) / (2.0 * a);
  156.     roots[1] = (-b - sqrtf(delta)) / (2.0 * a);
  157.     return (2);
  158. }
  159.  
  160. int             cubic(double a, double b, double c, double d, double  roots[])
  161. {
  162.     double p;
  163.     double q;
  164.     double w1;
  165.     double w2;
  166.     double u;
  167.     double v;
  168.     double t;
  169.     double delta;
  170.     double      sign;
  171.  
  172.     if (fabs(a) <= (double)TOLERANCE)
  173.         return (quadratic(b, c, d, &roots[0]));
  174.     p = -b*b / (3.0 * a*a) + c/a;
  175.     q = ((2.0 * b * b * b) / (27.0 * a*a*a)) - ((b*c)/(3.0 * a * a)) + d/a;
  176.     if(fabs(p) <= (double)TOLERANCE)
  177.     {
  178.         roots[0] = powf(-q, 1.0/3.0) - b /(3.0 * a);
  179.         return (1);
  180.     }
  181.     if (fabs(q) <= (double)TOLERANCE && fabs(p) > (double)TOLERANCE)
  182.     {
  183.         if(p < (double)(-TOLERANCE))
  184.         {
  185.             roots[0] = 0.0;
  186.             roots[1] = sqrtf(p);
  187.             roots[2] = -1.0 * sqrtf(p);
  188.             return(3);
  189.         }
  190.         else
  191.         {
  192.             roots[0] = 0.0;
  193.             return (1);
  194.         }
  195.     }
  196.     delta = (q * q / 4.0)  + (p * p * p / 27.0);
  197.     if (delta > (double)TOLERANCE)
  198.     {
  199.         sign = ((-q/2.0  + sqrtf(delta)) < 0.0) ? -1.0 : 1.0;
  200.         w1 = pow(fabs(-q/2.0 + sqrtf(delta)), 1.0/3.0);
  201.         w1 *= sign;
  202.         sign = ((-q/2.0  - sqrtf(delta)) < 0.0) ? -1.0 : 1.0;
  203.         w2 = pow(fabs(-q/2.0 - sqrtf(delta)), 1.0/3.0);
  204.         w2 *= sign;
  205.  
  206.         roots[0] = w1 + w2 - b/(3.0 * a);
  207.         return (1);
  208.     }
  209.     else if (fabs(delta) <= (double)TOLERANCE)
  210.     {
  211.         roots[0] = (3.0 * q)/p - b/(3.0 * a);
  212.         roots[1] = ((-3.0 * q) / (2.0 * p)) - b/(3.0 * a);
  213.         roots[2] = roots[1];
  214.         return(3);
  215.     }
  216.     else
  217.     {
  218.         u = 2.0 * sqrtf(-p/3.0);
  219.         //v = -q/(2.0 * copysign(pow(fabs(-p/3.0), 3/2), -p/3.0));
  220.         v = -q/(2.0 * pow(-p/3.0, 3.0/2.0));
  221.         t = acos(v)/3.0;
  222.         roots[0] = u * cos(t) - b/(3*a);
  223.         roots[1] = u * cos(t + (2.0 * M_PI/3.0)) - b/(3*a);
  224.         roots[2] = u * cos(t + (4.0 * M_PI/3.0)) - b/(3*a);
  225.         return (3);
  226.     }
  227. }
  228.  
  229. int         quartic(double a, double b, double c, double d, double e, double *roots)
  230. {
  231.     double p;
  232.     double q;
  233.     double r;
  234.     double y[4];
  235.     int num_sol[2];
  236.     double a0;
  237.     double b0;
  238.     int i;
  239.     int j;
  240.  
  241.     if(fabs(a) < (double)TOLERANCE)
  242.         return(cubic(b, c, d, e, &roots[0]));
  243.     p = -3.0 * b*b/(8.0 * a*a) + c/a;
  244.     q = b*b*b/(8.0*a*a*a) - b*c/(2.0*a*a) + d/a;
  245.     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;
  246.     if(fabs(p) <= (double)TOLERANCE)
  247.     {
  248.         if (fabs(q) <= (double)TOLERANCE)
  249.         {
  250.             if (r > 0.0)
  251.                 return (0);
  252.             else
  253.             {
  254.                 roots[0] = -sqrtf(-r);
  255.                 roots[1] = sqrtf(-r);
  256.                 return (2);
  257.             }
  258.         }
  259.     }
  260.     if (fabs(q) <= (double)TOLERANCE)
  261.     {
  262.         num_sol[0] = quadratic(1.0, p, r, &y[0]);
  263.         i = -1;
  264.         j = 0;
  265.         while(++i < num_sol[0])
  266.         {
  267.             if (y[i] > 0.0)
  268.             {
  269.                 roots[j++] = sqrtf(y[i]);
  270.                 roots[j++] = -sqrtf(y[i]);
  271.             }
  272.             if (i == num_sol[0])
  273.                 return(j);
  274.         }
  275.     }
  276.     num_sol[0] = cubic(8.0, -4.0*p, -8.0*r, 4.0*r*p - q*q, &y[0]);
  277.     if (0 == num_sol[0])
  278.         return (0);
  279.     if (-p + 2.0 * y[0] < 0.0)
  280.         return (0);
  281.     a0 = sqrtf(-p + 2.0 * y[0]);
  282.     //b0 = (fabs(a0) <= (double)TOLERANCE) ? sqrtf(y[0]*y[0] - r) : sqrtf(-q/(2.0*a0));
  283.     //num_sol[0] = quadratic(1.0, a0, y[0]+b0, &roots[0]);
  284.     num_sol[0] = quadratic(1.0, a0, y[0] - q/(2.0*a0), &roots[0]);
  285.     i = -1;
  286.     while(++i < num_sol[0])
  287.         roots[i] -= b/(4.0 * a);
  288.     //num_sol[1] = quadratic(1.0, a0, y[0]+b0, &roots[num_sol[0]]);
  289.     num_sol[1] = quadratic(1.0, -a0, y[0] + q/(2.0 * a0), &roots[num_sol[0]]);
  290.     i = -1;
  291.     while(++i < num_sol[1])
  292.         roots[num_sol[0] + i] -= b/(4.0 * a);
  293.     return(num_sol[0] + num_sol[1]);
  294. }
  295.  
  296. //--------------------------------------------------------------
  297. //              SECURITY SCANF
  298. //--------------------------------------------------------------
  299.  
  300. void clear_stdin(void)
  301. {
  302.     int c;
  303.     while((c = getchar()) != '\n' && c != EOF)
  304.         ;
  305. }
  306.  
  307. int myCharCleanScanf(char *character){
  308.     int secure = scanf("%c", character);
  309.     clear_stdin();
  310.  
  311.     return (secure != 0 && secure != EOF ) ? TRUE : FALSE;
  312. }
  313.  
  314. int myEquationCleanScanf(char *str){
  315.     int secure = scanf("%s", str);
  316.     clear_stdin();
  317.     return (secure != 0 && secure != EOF ) ? TRUE : FALSE;
  318. }
  319.  
  320. //--------------------------------------------------------------------
  321. //                          PARSING INPUT
  322. //--------------------------------------------------------------------
  323.  
  324.  
  325. int get_coef(char *input, double *coef ){
  326.        
  327.        
  328.         char c;
  329.         char *start = input, *end;
  330.         int i = 0;
  331.         char dest[10];
  332.  
  333.         coef[0]=0.0f;
  334.         coef[1]=0.0f;
  335.         coef[2]=0.0f;
  336.         coef[3]=0.0f;
  337.         coef[4]=0.0f;
  338.         while((end = strchr(start, 'X')) && end){
  339.                 //coef[i]= atof(strncpy(dest, start, end - start));
  340.                 //printf("coef[%lf] = ", coef[i]);
  341.             i++;
  342.          
  343.             if(*(end + 1) == '^'){
  344.                 if(*(end + 2) == '4'){
  345.                     coef[0]= atof(strncpy(dest, start, end - start));
  346.                 }
  347.                 if(*(end + 2) == '3'){
  348.                     coef[1]= atof(strncpy(dest, start, end - start));
  349.                 }
  350.                 if(*(end + 2) == '2'){
  351.  
  352.                     coef[2]= atof(strncpy(dest, start, end - start));
  353.                 }
  354.                 end += 3;
  355.             }
  356.             else if(*end == 'X'){
  357.                 if(*(end + 1) == '+')
  358.                     end += 2;
  359.                 else if (*(end + 1) == '-')
  360.                     end += 1;
  361.                 coef[3]= atof(strncpy(dest, start, end - start));
  362.             }
  363.  
  364.             start = end;
  365.             i++;
  366.  
  367.        }
  368.        //printf("start: %s\n", start);
  369.        coef[4] = atof(strcpy(dest, start));
  370.        //printf("Coef[%lf %lf %lf %lf %lf] ", coef[0], coef[1], coef[2], coef[3], coef[4]);
  371.        //printf("coef[%lf] = ", coef[i + 1]);
  372.        if(fabs(coef[0]) > TOLERANCE) return 4;
  373.        if(fabs(coef[1]) > TOLERANCE) return 3;
  374.        if(fabs(coef[2]) > TOLERANCE) return 2;
  375.        if(fabs(coef[3]) > TOLERANCE) return 1;
  376.        return 0;
  377. }
  378.  
  379.  
  380.  
  381. int         main(int argc, char **argv)
  382. {
  383.     int continu = TRUE, numsol;
  384.     char input[50] = {'\0'};
  385.     double coef[8] = {0.0f, 0.0f, 0.0f, 0.0f};
  386.     char answer;
  387.     double roots[5];
  388.  
  389.     do {
  390.         printf("\e[1;1H\e[2J");
  391.         printf("\nPlease, Enter your equation from degre 1 to degre 4\n\n\n");
  392.         int secure = myEquationCleanScanf(&input[0]);
  393.         if(!secure){
  394.             printf("Error Error\n");
  395.             exit(1);
  396.         }
  397.         int degree = get_coef(&input[0], &coef[0]);
  398.        
  399.         if(4 == degree){
  400.                   numsol = quartic(coef[0], coef[1], coef[2], coef[3], coef[4], &roots[0]);
  401.                   if(numsol == 4){
  402.                      printf("%s\n", KGRN);
  403.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  404.                     printf("4 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf, X4 = %.3lf }", roots[0], roots[1], roots[2], roots[3]);
  405.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  406.                   } else if(numsol == 3){
  407.                      printf("%s\n", KGRN);
  408.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  409.                     printf("3 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf }", roots[0], roots[1], roots[2]);
  410.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  411.                   }else if(numsol == 2){
  412.                      printf("%s\n", KGRN);
  413.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  414.                     printf("2 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
  415.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  416.                   }else if(numsol == 1){
  417.                      printf("%s\n", KGRN);
  418.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  419.                     printf("1 SOLUTION: {X1 = %.3lf }", roots[0]);
  420.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  421.                   }else {
  422.                      printf("%s\n", KRED);
  423.                     printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
  424.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  425.                   }
  426.         }else if(3 == degree){
  427.                     numsol = cubic(coef[1], coef[2], coef[3], coef[4],  roots);
  428.                    
  429.                   if(numsol == 3){
  430.                      printf("%s\n", KGRN);
  431.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  432.                     printf("3 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf, X3 = %.3lf }", roots[0], roots[1], roots[2]);
  433.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  434.                   }else if(numsol == 2){
  435.                      printf("%s\n", KGRN);
  436.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  437.                     printf("2 SOLUTIONS: { X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
  438.                   }else if(numsol == 1){
  439.                      printf("%s\n", KGRN);
  440.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  441.                     printf("1 SOLUTION: { X1 = %.3lf }", roots[0]);
  442.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  443.                   }else {
  444.                      printf("%s\n", KRED);
  445.                     printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
  446.                   }
  447.  
  448.  
  449.         }else if(2 == degree){
  450.  
  451.                     numsol=quadratic(coef[2], coef[3], coef[4], &roots[0]);
  452.  
  453.                     if(numsol == 2){
  454.                         printf("%s\n", KGRN);
  455.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  456.                     printf("2 SOLUTIONS: {X1 = %.3lf, X2 = %.3lf }", roots[0], roots[1]);
  457.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  458.                   }else if(numsol == 1){
  459.                     printf("%s\n", KGRN);
  460.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  461.                     printf("1 SOLUTION: { X1 = %.3lf }", roots[0]);
  462.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  463.                   }else {
  464.                      printf("%s\n", KRED);
  465.                     printf("\n\n\n\n SORRY: No REAL SOLUTIONS FOUND, THERE ARE COMPLEX NUMBERS AS SOLUTIONS");
  466.                   }
  467.         }else if(1 == degree){
  468.                     numsol =  degre_one(coef[3], coef[4], &roots[0]);
  469.                     if(numsol == 1){
  470.                     printf("%s\n", KGRN);
  471.                     printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  472.                     printf("SOLUTION: { X = %.3lf }", roots[0]);
  473.                      printf("\n\n\n\n[-----------------------------------------------------------------\n\n\n\n");
  474.                   }else {
  475.                      printf("%s\n", KRED);
  476.                     printf("\n\n\n\n SORRY: No SOLUTION FOUND");
  477.                   }
  478.         }
  479.         printf("%s\n", KWHT);
  480.     }while(printf("\n\n\n\nContinu? :yes = 'y', no: 'n'       ->\n") && myCharCleanScanf(&answer) && (answer != 'n'));
  481.  
  482.  
  483.     return (0);
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement