Advertisement
heroys6

Lab 2

Oct 22nd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <conio.h>
  5.  
  6. int func();
  7. float func1(float);
  8. float integrate_me(float, float, int, int);
  9.  
  10. int main(void)
  11. {
  12.     float x0, xn, E, Itgr1, Itgr2, Itgr_final;
  13.     int n, nc, ch, dowhl, clear=0;
  14.     char Y_n;
  15.  
  16.     func();
  17.     printf("\t\t%c%s%c\n", 179, "Hi there! Let's calculate the integral!", 179);
  18.     func();
  19.  
  20.     do
  21.     {
  22.         if (clear>0)
  23.         {
  24.             getch();
  25.             system("cls");
  26.             clear=0;
  27.         }
  28.         dowhl=0;
  29.  
  30.         printf("Enter X0: ");
  31.         scanf("%f", &x0);
  32.  
  33.         printf("Enter Xn: ");
  34.         scanf("%f", &xn);
  35.  
  36.         printf("Enter the number(n) of intervals: ");
  37.         scanf("%d", &n);
  38.  
  39.         printf("Enter riding error(E): ");
  40.         scanf("%f", &E);
  41.  
  42.         printf("\n");
  43.  
  44.         printf("Select method:\n1.Left rectangles\n2.Right rectangles\n3.Trapezoids\n4.Simpson's\nAnswer : ");
  45.         scanf("%d", &ch);
  46.  
  47.         Itgr1=integrate_me(x0, xn, n, ch);
  48.         Itgr_final=Itgr1;
  49.         if (Itgr1==999777)
  50.         {
  51.             clear=1;
  52.             dowhl=1;
  53.         }
  54.         else
  55.         {
  56.                 switch (ch)
  57.                 {
  58.                     case 1:
  59.                         printf("\nSelected method: Left rectangles\n\n");
  60.                         break;
  61.                     case 2:
  62.                         printf("\nSelected method: Right rectangles\n\n");
  63.                         break;
  64.                     case 3:
  65.                         printf("\nSelected method: Trapezoids\n\n");
  66.                         break;
  67.                     case 4:
  68.                         printf("\nSelected method: Simpson's\n\n");
  69.                         break;
  70.                     default:
  71.                         printf("Uncorrect answer\n");
  72.                         return 1;
  73.                         break;
  74.                 }
  75.  
  76.                 Itgr2=integrate_me(x0, xn, n+2, ch);
  77.  
  78.                 if (E>=fabsf(Itgr1-Itgr2))
  79.                 {
  80.                     printf("%s%d%s\n\n", "Well, you enter n = ", n, "\nE is normal now!");
  81.                 }
  82.                 else
  83.                 {
  84.                     printf("Bad news. E more than diving E\n");
  85.                     nc=n;
  86.  
  87.                     do
  88.                     {
  89.                             nc+=2;
  90.                             Itgr1=integrate_me(x0, xn, nc, ch);
  91.                             Itgr2=integrate_me(x0, xn, nc+2, ch);
  92.                     }
  93.                      while(E<fabsf(Itgr1-Itgr2));
  94.  
  95.                      printf("%s%d%s\n\n", "If you will enter n = ", nc, " next time E will be normal!");
  96.                 }
  97.  
  98.                     printf("\t\t\t X0 = %.2f\n", x0);
  99.                     printf("\t\t\t Xn = %.2f\n", xn);
  100.                     printf("\t\t\t n = %d\n", n);
  101.  
  102.                     func();
  103.                     printf("\t\t\t%s%.5f\n", " Integral = ", Itgr_final);
  104.                     func();
  105.         }
  106.  
  107.     printf("\nDo you want to start programm again?(Y/n)");
  108.     fflush(stdin);
  109.     scanf("%c", &Y_n);
  110.  
  111.     switch (Y_n)
  112.         {
  113.             case 'Y':
  114.                 dowhl=1; clear=1;
  115.                 printf("Ok, press something...");
  116.                 break;
  117.  
  118.             case 'y':
  119.                 dowhl=1; clear=1;
  120.                 printf("Ok, press something...");
  121.                 break;
  122.  
  123.             case 'N':
  124.                 return 0;
  125.  
  126.  
  127.             case 'n':
  128.                 return 0;
  129.  
  130.  
  131.             default:
  132.                 printf("Wrong answer. Programm will finish...\n");
  133.                 return 0;
  134.  
  135.             }
  136.  
  137.     }
  138.     while (dowhl>0);
  139.  
  140.     return 0;
  141. }
  142.  
  143. int func()
  144. {
  145.     int i;
  146.  
  147.     printf("\t\t%c ", 43);
  148.     for (i=1; i<=19; i++) printf("%c ", 196);
  149.     printf("%c\n", 43);
  150. }
  151.  
  152. float integrate_me(float x0, float xn, int n, int ch)
  153. {
  154.     float h, S=0, Sn=0, I, x;
  155.  
  156.     h=(xn-x0)/n;
  157.         switch(ch)
  158.     {
  159.     case 1:
  160.         for (x=x0; x<=xn-h; x+=h)
  161.             {
  162.                 S+=func1(x);
  163.             }
  164.         I=h*S;
  165.         break;
  166.  
  167.     case 2:
  168.         for (x=x0+h; x<=xn; x+=h)
  169.             {
  170.                 S+=func1(x);
  171.             }
  172.         I=h*S;
  173.         break;
  174.  
  175.     case 3:
  176.         for (x=x0+h; x<=xn-h; x+=h)
  177.             {
  178.                 S+=func1(x);
  179.             }
  180.         I=h*(func1(x0)/2+func1(xn)/2+S);
  181.         break;
  182.  
  183.     case 4:
  184.         if (n%2!=0)
  185.         {
  186.             printf("%s%c%d%c%s%c%d%c%s", "\nYour n(", 34, n, 34, ") is wrong!\nSimpson's method work for only paired numbers!(", 34, n+1, 34,  " for example)\nTry again!\n");
  187.             return 999777;
  188.         }
  189.         else
  190.         {
  191.             for (x=x0+h; x<=xn-h; x+=2*h)
  192.                 {
  193.                     S+=func1(x);
  194.                 }
  195.             for (x=x0+2*h; x<=xn-2*h; x+=2*h)
  196.                 {
  197.                     Sn+=func1(x);
  198.                 }
  199.             I=h/3*(func1(x0)+4*S+2*Sn+func1(xn));
  200.         }
  201.         break;
  202.  
  203.     default:
  204.         return 1;
  205.         break;
  206.     }
  207.     return I;
  208. }
  209.  
  210. float func1(float x)
  211. {
  212.     float F;
  213.  
  214.     F=x*pow(2.71828182,pow(x,2));
  215.     // F=7/x;
  216.     // + F=x/sqrt(pow(x,4)+16);
  217.     return F;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement