Advertisement
dimon2242

Untitled

Nov 9th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <conio.h>
  6. int ccout(double xi, int i, clock_t stop, clock_t start);
  7. void reserveData(void);
  8.  
  9. double reserveXi, UnlinearFuncData, reserveTime;
  10. int reserveI;
  11.  
  12. double unlinear_func(double x)
  13. {
  14.     double f;
  15.     f = pow(x,3)-3;
  16.     //f = pow(x,3)+2*x;
  17.     //f = pow(x,3)-x+1;
  18.     return f;
  19. }
  20.  
  21. double chords(double x1, double x2)
  22. {
  23.     return (unlinear_func(x2)*x1-unlinear_func(x1)*x2)/(unlinear_func(x2)-unlinear_func(x1));
  24. }
  25.  
  26. double bisection(double x1, double x2)
  27. {
  28.     return (x1+x2)/2;
  29. }
  30.  
  31. int iter(int imax, int i, double xi, double x1, double x2, int ch, int deb, double e, clock_t start)
  32. {
  33.     int ich;
  34.     clock_t stop;
  35.  
  36.     if (i>imax)
  37.     {
  38.         printf("\nExceeded max number of i(iterations)!\n\nWhat we should do?\n");
  39.         printf("1.Continue with the same number of i\n2.Run to find a solution\n3.Exit and print current result\nYour choose: ");
  40.         scanf("%d", &ich);
  41.         switch(ich)
  42.         {
  43.         case 1:
  44.             return i-imax;
  45.             break;
  46.  
  47.         case 2:
  48.             if (ch==1)
  49.             {
  50.  
  51.                 do
  52.                 {
  53.                     xi=chords(x1, x2);
  54.  
  55.                     if (deb==1)
  56.                     {
  57.                         stop=clock();
  58.                         ccout(xi, i, stop, start);
  59.                         printf("x1=%lf    x2=%lf\n", x1, x2);
  60.                     }
  61.  
  62.                     i++;
  63.  
  64.                     if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
  65.                     else
  66.                     {
  67.                         if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
  68.                     }
  69.  
  70.                 }
  71.                 while (e<fabs(unlinear_func(xi)));
  72.                 stop=clock();
  73.  
  74.                 printf("\n(!)Final: \n");
  75.                 stop=clock();
  76.                 ccout(xi, i=0, stop, start);
  77.                 exit(0);
  78.  
  79.             }
  80.             else
  81.             {
  82.                 if (ch==2)
  83.                 {
  84.                     do
  85.                     {
  86.                         xi=bisection(x1, x2);
  87.  
  88.                         if (deb==1)
  89.                         {
  90.                             stop=clock();
  91.                             ccout(xi, i, stop, start);
  92.                             printf("x1=%lf    x2=%lf\n", x1, x2);
  93.                         }
  94.  
  95.                         i++;
  96.  
  97.                         if (unlinear_func(xi)<0) x1=xi;
  98.                         else
  99.                         {
  100.                             if (unlinear_func(xi)>0) x2=xi;
  101.                         }
  102.  
  103.                     }
  104.                     while(e<fabs(x1-x2));
  105.  
  106.                     printf("\n(!)Final: \n");
  107.                     stop=clock();
  108.                     ccout(xi, i=0, stop, start);
  109.                     exit(0);
  110.  
  111.                 }
  112.             }
  113.  
  114.             break;
  115.  
  116.         case 3:
  117.             reserveData();
  118.             //stop=clock(); Old
  119.             //ccout(xi, i=0, stop, start);
  120.             exit(0);
  121.             break;
  122.  
  123.         default:
  124.             printf("\nBad answer!");
  125.             exit(0);
  126.             break;
  127.         }
  128.     }
  129. }
  130.  
  131. int ccout(double xi, int i, clock_t stop, clock_t start)
  132. {
  133.     reserveXi = xi;
  134.     UnlinearFuncData = unlinear_func(xi);
  135.     reserveTime = (stop-start)/CLK_TCK;
  136.     printf("\n\t\t\t\t.::-Result-::.");
  137.     printf("\nRoot value(Xi) = %.8lf", xi);
  138.     printf("\nFunction value = %.8lf", UnlinearFuncData);
  139.     if (i==0) ;
  140.     else
  141.     {
  142.         reserveI = i;
  143.         printf("\nIterations value = %d", reserveI);
  144.     }
  145.     printf("\nElapsed time: %.10f seconds\n", reserveTime);
  146.     return 101;
  147. }
  148.  
  149. void reserveData(void) {
  150.     printf("\n\t\t\t\t.::-Result-::.");
  151.     printf("\nRoot value(Xi) = %.8lf", reserveXi);
  152.     printf("\nFunction value = %.8lf", UnlinearFuncData);
  153.     if (reserveI==0);
  154.         else
  155.         {
  156.             printf("\nIterations value = %d", reserveI);
  157.         }
  158.         printf("\nElapsed time: %.10f seconds\n", reserveTime);
  159. }
  160.  
  161. int repeat(int povt)
  162. {
  163.     char Y_n;
  164.  
  165.     printf("\nDo you want to start program again?(Y/n)");
  166.     fflush(stdin);
  167.     scanf("%c", &Y_n);
  168.  
  169.     switch (Y_n)
  170.         {
  171.             case 'Y':
  172.                 povt=1;
  173.                 printf("Ok, press something...");
  174.                 break;
  175.  
  176.             case 'y':
  177.                 povt=1;
  178.                 printf("Ok, press something...");
  179.                 break;
  180.  
  181.             case 'N':
  182.                 return 0;
  183.  
  184.  
  185.             case 'n':
  186.                 return 0;
  187.  
  188.  
  189.             default:
  190.                 printf("Wrong answer. Program will finish...\n");
  191.                 return 0;
  192.  
  193.             }
  194. return povt;
  195. }
  196.  
  197. int main()
  198. {
  199.     double x1, x2, e, xi;
  200.     int i=0, imax, ch, povt=0, prvr, deb;
  201.     clock_t start, stop;
  202.  
  203.     printf("\t\t+++_Calculating_of_nonlinear_equation_+++\n\n");
  204.  
  205.     do
  206.     {
  207.         if (povt==1)
  208.         {
  209.             getch();
  210.             system("cls");
  211.         }
  212.  
  213.         povt=0;
  214.  
  215.         do
  216.         {
  217.             prvr=0;
  218.             printf("Enter x1: ");
  219.             fflush(stdin);
  220.             scanf("%lf", &x1);
  221.  
  222.             printf("Enter x2: ");
  223.             fflush(stdin);
  224.             scanf("%lf", &x2);
  225.  
  226.             if (unlinear_func(x1)*unlinear_func(x2)>0)
  227.             {
  228.                 printf("Wrong x1 or x2. Try again!\n");
  229.                 prvr=1;
  230.             }
  231.         }
  232.         while(prvr>0);
  233.  
  234.         do
  235.         {
  236.             prvr=0;
  237.             printf("Enter allowable error(E): ");
  238.             fflush(stdin);
  239.             scanf("%lf", &e);
  240.             if (e<pow(10,-5) || e>pow(10,-3))
  241.             {
  242.                 printf("E must be from 10^-5 to 10^-3 Try again!\n");
  243.                 prvr=1;
  244.             }
  245.         }
  246.         while(prvr>0);
  247.  
  248.         do
  249.         {
  250.             prvr=0;
  251.             printf("Enter max number of iterations(i): ");
  252.             fflush(stdin);
  253.             scanf("%d", &imax);
  254.             if (imax<=0)
  255.             {
  256.                 printf("Max 'i' must be more then 0! Try again\n");
  257.                 prvr=1;
  258.             }
  259.         }
  260.         while(prvr>0);
  261.  
  262.         printf("\nSelect method:\n\n1.Chords\n2.Bisection\nYour choose will be: ");
  263.         fflush(stdin);
  264.         scanf("%d", &ch);
  265.  
  266.         switch(ch)
  267.         {
  268.         case 1:
  269.             printf("\n\t\t\tSelected method of chords\n\n");
  270.  
  271.             printf("Activate debugging mod?(1-yes/2-no): ");
  272.             scanf("%d", &deb);
  273.  
  274.             if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
  275.             else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
  276.  
  277.             start=clock();
  278.             do
  279.             {
  280.             xi=chords(x1, x2);
  281.             i++;
  282.             i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
  283.  
  284.             if (deb==1)
  285.             {
  286.                 ccout(xi, i, stop, start);
  287.                 printf("x1=%lf    x2=%lf\n", x1, x2);
  288.             }
  289.  
  290.             if (unlinear_func(xi)*unlinear_func(x1)<0) x2=xi;
  291.             else
  292.             {
  293.                 if (unlinear_func(xi)*unlinear_func(x2)<0) x1=xi;
  294.             }
  295.  
  296.             }
  297.             while (e<fabs(unlinear_func(xi))); // f(xi)<=E --> End of cycle
  298.             stop=clock();
  299.  
  300.             ccout(xi, i, stop, start);
  301.  
  302.             break;
  303.  
  304.         case 2:
  305.             printf("\n\t\t\tSelected method of bisection\n\n");
  306.  
  307.             printf("Activate debugging mod?(1-yes/2-no): ");
  308.             scanf("%d", &deb);
  309.  
  310.             if (deb==1) printf("\n\t\t\t\tDebugging mod status: [ON]\n\n");
  311.             else printf("\n\t\t\t\tDebugging mod status: [OFF]\n\n");
  312.  
  313.             start=clock();
  314.             do
  315.             {
  316.             xi=bisection(x1, x2);
  317.             i++;
  318.             i=iter(imax, i, xi, x1, x2, ch, deb, e, start);
  319.  
  320.             if (deb==1)
  321.             {
  322.                 ccout(xi, i, stop, start);
  323.                 printf("x1=%lf    x2=%lf\n", x1, x2);
  324.             }
  325.  
  326.             if (unlinear_func(xi)<0) x1=xi;
  327.             else
  328.             {
  329.                 if (unlinear_func(xi)>0) x2=xi;
  330.             }
  331.  
  332.             }
  333.             while(e<fabs(x1-x2));
  334.             stop=clock();
  335.  
  336.             ccout(xi, i, stop, start);
  337.  
  338.             break;
  339.  
  340.         default:
  341.             printf("\nBad answer! Try again!");
  342.             break;
  343.         }
  344.  
  345. povt=repeat(povt);
  346.     }
  347.     while(povt==1);
  348.  
  349.     return 0;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement