Advertisement
asmodeus94

calkaOznaczonaMonteCarloIProstokaty

May 9th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. // lab7.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <cstdlib>
  6. #include <cstdio>
  7. #include <conio.h>
  8. #include <ctime>
  9. #include <iostream>
  10. #define _USE_MATH_DEFINES
  11. #include <math.h>
  12.  
  13. using namespace std;
  14. const int liczbaElementow = 1000000;
  15. const double dx = 1 / (double)liczbaElementow;
  16.  
  17. void czekaj()
  18. {
  19.     getchar();
  20.     getchar();
  21. }
  22.  
  23. double funkcja(double wartosc, int numerFunkcji)
  24. {
  25.     switch(numerFunkcji)
  26.     {
  27.     case 0:
  28.         return wartosc;
  29.     case 1:
  30.         return wartosc * wartosc;
  31.     case 2:
  32.         return pow(M_E, wartosc)/(pow(M_E, wartosc) + 1);
  33.     default:
  34.         return 0;
  35.     }
  36. }
  37. double metodaMonteCarlo(double xK, double xP, int numerFunkcji)
  38. {
  39.     double zakres = (double)xK - (double)xP, suma = 0, l;
  40.     for(int i = 1; i <= liczbaElementow; i++)
  41.     {
  42.         l = ((double)rand()/(double)(RAND_MAX+1) * zakres);
  43.         suma += funkcja((double)xP + l, numerFunkcji);
  44.     }
  45.     suma = zakres * suma / liczbaElementow;
  46.     return suma;
  47. }
  48. double metodaProstokatow(double xK, double xP, int numerFunkcji) // przy malym przyroscie argumentow ekstremalnie dlugo liczy,
  49. {                               // ale bardzo dokladnie
  50.     double suma = 0;
  51.     for(double i = xP ; i <= xK ; i = i + dx )
  52.         suma += funkcja(i, numerFunkcji);
  53.     suma *= dx;
  54.     return suma;
  55. }
  56.  
  57. int _tmain(int argc, _TCHAR* argv[])
  58. {
  59.     srand(time(NULL));
  60.     int f = 0, xK, xP, nrCalki, nrMetody;
  61.     char decyzja = 't';
  62.  
  63.     while(decyzja == 't')
  64.     {
  65.         while(f == 0)
  66.         {
  67.             cout << "Jaka calke chcesz policzyc?" << endl;
  68.             cout << "0 - z x" << endl;
  69.             cout << "1 - z x^2" << endl;
  70.             cout << "2 - z e^x/(e^x + 1)" << endl;
  71.             cout << "Podaj: ";
  72.             scanf_s("%d", &nrCalki);
  73.             f = 1;
  74.             if((nrCalki < 0) || (2 < nrCalki))
  75.             {
  76.                 cout << "Nie podales prawidlowego numeru calki!";
  77.                 czekaj();
  78.                 system("cls");
  79.                 f = 0;
  80.             }
  81.         }
  82.         f = 0;
  83.         while(f == 0)
  84.         {
  85.             cout << "Podaj przedzial, w jakim chcesz policzyc calke" << endl;
  86.             cout << "Podaj koniec przedzialu: ";
  87.             scanf_s("%d", &xK);
  88.             cout << "Podaj poczatek przedzialu: ";
  89.             scanf_s("%d", &xP);
  90.             f = 1;
  91.             if(xK < xP)
  92.             {
  93.                 cout << "Podaj prawidlowy przedzial, po ktorym chcesz calkowac!";
  94.                 czekaj();
  95.                 system("cls");
  96.                 f = 0;
  97.             }
  98.         }
  99.         cout.precision(5);
  100.         f = 0;
  101.         while(f == 0)
  102.         {
  103.             cout << "Podaj metode, ktora ma zostac uzyta do obliczenia calki: " << endl;
  104.             cout << "1 - metoda Monte - Carlo" << endl;
  105.             cout << "2 - metoda prostokatow" << endl;
  106.             cout << "Podaj: ";
  107.             scanf_s("%d", &nrMetody);
  108.             f = 1;
  109.             if((nrMetody != 1) && (nrMetody != 2))
  110.             {
  111.                 cout << "Nie podales prawidlowego numeru metody!";
  112.                 czekaj();
  113.                 system("cls");
  114.                 f = 0;
  115.             }
  116.         }
  117.  
  118.         if(nrMetody == 1)
  119.             cout << metodaMonteCarlo(xK, xP, nrCalki);
  120.         else
  121.             cout << metodaProstokatow(xK, xP, nrCalki);
  122.         czekaj();
  123.         system("cls");
  124.         f = 0;
  125.         while(f == 0)
  126.         {
  127.             cout << "Czy chcesz policzyc inna? (t - tak, n - nie)";
  128.             decyzja = _getch();
  129.             f = 1;
  130.             if((decyzja != 't') && (decyzja != 'T') && (decyzja != 'n') && (decyzja != 'N'))
  131.                 f = 0;
  132.             system("cls");
  133.         }
  134.         f = 0;
  135.     }
  136.    
  137.     return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement