Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <conio.h>
- int func();
- float func1(float);
- float integrate_me(float, float, int, int);
- int main(void)
- {
- float x0, xn, E, Itgr1, Itgr2, Itgr_final;
- int n, nc, ch, dowhl, clear=0;
- char Y_n;
- func();
- printf("\t\t%c%s%c\n", 179, "Hi there! Let's calculate the integral!", 179);
- func();
- do
- {
- if (clear>0)
- {
- getch();
- system("cls");
- clear=0;
- }
- dowhl=0;
- printf("Enter X0: ");
- scanf("%f", &x0);
- printf("Enter Xn: ");
- scanf("%f", &xn);
- printf("Enter the number(n) of intervals: ");
- scanf("%d", &n);
- printf("Enter riding error(E): ");
- scanf("%f", &E);
- printf("\n");
- printf("Select method:\n1.Left rectangles\n2.Right rectangles\n3.Trapezoids\n4.Simpson's\nAnswer : ");
- scanf("%d", &ch);
- Itgr1=integrate_me(x0, xn, n, ch);
- Itgr_final=Itgr1;
- if (Itgr1==999777)
- {
- clear=1;
- dowhl=1;
- }
- else
- {
- switch (ch)
- {
- case 1:
- printf("\nSelected method: Left rectangles\n\n");
- break;
- case 2:
- printf("\nSelected method: Right rectangles\n\n");
- break;
- case 3:
- printf("\nSelected method: Trapezoids\n\n");
- break;
- case 4:
- printf("\nSelected method: Simpson's\n\n");
- break;
- default:
- printf("Uncorrect answer\n");
- return 1;
- break;
- }
- Itgr2=integrate_me(x0, xn, n+2, ch);
- if (E>=fabsf(Itgr1-Itgr2))
- {
- printf("%s%d%s\n\n", "Well, you enter n = ", n, "\nE is normal now!");
- }
- else
- {
- printf("Bad news. E more than diving E\n");
- nc=n;
- do
- {
- nc+=2;
- Itgr1=integrate_me(x0, xn, nc, ch);
- Itgr2=integrate_me(x0, xn, nc+2, ch);
- }
- while(E<fabsf(Itgr1-Itgr2));
- printf("%s%d%s\n\n", "If you will enter n = ", nc, " next time E will be normal!");
- }
- printf("\t\t\t X0 = %.2f\n", x0);
- printf("\t\t\t Xn = %.2f\n", xn);
- printf("\t\t\t n = %d\n", n);
- func();
- printf("\t\t\t%s%.5f\n", " Integral = ", Itgr_final);
- func();
- }
- printf("\nDo you want to start programm again?(Y/n)");
- fflush(stdin);
- scanf("%c", &Y_n);
- switch (Y_n)
- {
- case 'Y':
- dowhl=1; clear=1;
- printf("Ok, press something...");
- break;
- case 'y':
- dowhl=1; clear=1;
- printf("Ok, press something...");
- break;
- case 'N':
- return 0;
- case 'n':
- return 0;
- default:
- printf("Wrong answer. Programm will finish...\n");
- return 0;
- }
- }
- while (dowhl>0);
- return 0;
- }
- int func()
- {
- int i;
- printf("\t\t%c ", 43);
- for (i=1; i<=19; i++) printf("%c ", 196);
- printf("%c\n", 43);
- }
- float integrate_me(float x0, float xn, int n, int ch)
- {
- float h, S=0, Sn=0, I, x;
- h=(xn-x0)/n;
- switch(ch)
- {
- case 1:
- for (x=x0; x<=xn-h; x+=h)
- {
- S+=func1(x);
- }
- I=h*S;
- break;
- case 2:
- for (x=x0+h; x<=xn; x+=h)
- {
- S+=func1(x);
- }
- I=h*S;
- break;
- case 3:
- for (x=x0+h; x<=xn-h; x+=h)
- {
- S+=func1(x);
- }
- I=h*(func1(x0)/2+func1(xn)/2+S);
- break;
- case 4:
- if (n%2!=0)
- {
- 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");
- return 999777;
- }
- else
- {
- for (x=x0+h; x<=xn-h; x+=2*h)
- {
- S+=func1(x);
- }
- for (x=x0+2*h; x<=xn-2*h; x+=2*h)
- {
- Sn+=func1(x);
- }
- I=h/3*(func1(x0)+4*S+2*Sn+func1(xn));
- }
- break;
- default:
- return 1;
- break;
- }
- return I;
- }
- float func1(float x)
- {
- float F;
- F=x*pow(2.71828182,pow(x,2));
- // F=7/x;
- // + F=x/sqrt(pow(x,4)+16);
- return F;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement