Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab7.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <cstdlib>
- #include <cstdio>
- #include <conio.h>
- #include <ctime>
- #include <iostream>
- #define _USE_MATH_DEFINES
- #include <math.h>
- using namespace std;
- const int liczbaElementow = 1000000;
- const double dx = 1 / (double)liczbaElementow;
- void czekaj()
- {
- getchar();
- getchar();
- }
- double funkcja(double wartosc, int numerFunkcji)
- {
- switch(numerFunkcji)
- {
- case 0:
- return wartosc;
- case 1:
- return wartosc * wartosc;
- case 2:
- return pow(M_E, wartosc)/(pow(M_E, wartosc) + 1);
- default:
- return 0;
- }
- }
- double metodaMonteCarlo(double xK, double xP, int numerFunkcji)
- {
- double zakres = (double)xK - (double)xP, suma = 0, l;
- for(int i = 1; i <= liczbaElementow; i++)
- {
- l = ((double)rand()/(double)(RAND_MAX+1) * zakres);
- suma += funkcja((double)xP + l, numerFunkcji);
- }
- suma = zakres * suma / liczbaElementow;
- return suma;
- }
- double metodaProstokatow(double xK, double xP, int numerFunkcji) // przy malym przyroscie argumentow ekstremalnie dlugo liczy,
- { // ale bardzo dokladnie
- double suma = 0;
- for(double i = xP ; i <= xK ; i = i + dx )
- suma += funkcja(i, numerFunkcji);
- suma *= dx;
- return suma;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- srand(time(NULL));
- int f = 0, xK, xP, nrCalki, nrMetody;
- char decyzja = 't';
- while(decyzja == 't')
- {
- while(f == 0)
- {
- cout << "Jaka calke chcesz policzyc?" << endl;
- cout << "0 - z x" << endl;
- cout << "1 - z x^2" << endl;
- cout << "2 - z e^x/(e^x + 1)" << endl;
- cout << "Podaj: ";
- scanf_s("%d", &nrCalki);
- f = 1;
- if((nrCalki < 0) || (2 < nrCalki))
- {
- cout << "Nie podales prawidlowego numeru calki!";
- czekaj();
- system("cls");
- f = 0;
- }
- }
- f = 0;
- while(f == 0)
- {
- cout << "Podaj przedzial, w jakim chcesz policzyc calke" << endl;
- cout << "Podaj koniec przedzialu: ";
- scanf_s("%d", &xK);
- cout << "Podaj poczatek przedzialu: ";
- scanf_s("%d", &xP);
- f = 1;
- if(xK < xP)
- {
- cout << "Podaj prawidlowy przedzial, po ktorym chcesz calkowac!";
- czekaj();
- system("cls");
- f = 0;
- }
- }
- cout.precision(5);
- f = 0;
- while(f == 0)
- {
- cout << "Podaj metode, ktora ma zostac uzyta do obliczenia calki: " << endl;
- cout << "1 - metoda Monte - Carlo" << endl;
- cout << "2 - metoda prostokatow" << endl;
- cout << "Podaj: ";
- scanf_s("%d", &nrMetody);
- f = 1;
- if((nrMetody != 1) && (nrMetody != 2))
- {
- cout << "Nie podales prawidlowego numeru metody!";
- czekaj();
- system("cls");
- f = 0;
- }
- }
- if(nrMetody == 1)
- cout << metodaMonteCarlo(xK, xP, nrCalki);
- else
- cout << metodaProstokatow(xK, xP, nrCalki);
- czekaj();
- system("cls");
- f = 0;
- while(f == 0)
- {
- cout << "Czy chcesz policzyc inna? (t - tak, n - nie)";
- decyzja = _getch();
- f = 1;
- if((decyzja != 't') && (decyzja != 'T') && (decyzja != 'n') && (decyzja != 'N'))
- f = 0;
- system("cls");
- }
- f = 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement