Advertisement
Kostiggig

Untitled

Mar 30th, 2023
488
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <set>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. #define INPUT_DATA_SIZE 1024
  10. #define SEPARATOR ","
  11.  
  12. #define INPUT_DATA_EXCEPTION_CODE 111
  13. #define EXPECTATION_PROBABILITY_PRECISION 20
  14. #define ACCURACY 0.05
  15.  
  16. #define MAX_PROBABILITY_NAME_LENGTH 3
  17. #define DEPDENDENT_EVENTS_PROBABILITY_LENGTH 2
  18.  
  19. double median(const double sorted[], int start, int end) {
  20.    
  21.  
  22.     int size = end - start + 1;
  23.     int middle = start + (size - 1) / 2;
  24.    
  25.     if (size % 2 == 0) {
  26.         return (sorted[middle] + sorted[middle + 1]) / 2.0;
  27.     } else {
  28.         return sorted[middle];
  29.     }
  30. }
  31.  
  32. double median_value(const double population[], const int population_size) {
  33.     double sorted[population_size];
  34.     for(int i = 0; i < population_size; i++) sorted[i] = population[i];
  35.  
  36.     int n = sizeof(sorted) / sizeof(sorted[0]);
  37.     sort(sorted, sorted + n);
  38.  
  39.     return median(sorted, 0, population_size - 1);
  40. }
  41.  
  42. void calculate_quartiles(const double population[], const int population_size, double *q1, double *q3) {
  43.     double sorted[population_size];
  44.     for(int i = 0; i < population_size; i++) sorted[i] = population[i];
  45.  
  46.     int n = sizeof(sorted) / sizeof(sorted[0]);
  47.     sort(sorted, sorted + n);
  48.  
  49.     double median_value = median(sorted, 0, population_size - 1);
  50.     int middle_index = population_size / 2;
  51.  
  52.     (*q1) = median(sorted, 0, middle_index - 1);
  53.  
  54.     if(population_size % 2 != 0) (*q3) = median(sorted, middle_index + 1, population_size - 1);
  55.     else (*q3) = median(sorted, middle_index, population_size - 1);
  56.  
  57. }
  58.  
  59. double mode(const double population[], const int population_size) {
  60.     if (population_size == 1) return population[0];
  61.    
  62.     double mode_value;
  63.     unordered_map<double,int>map;
  64.     int max_seen_count_of_num = INT32_MIN;
  65.  
  66.     for(int i = 0; i < population_size; i++) {
  67.         map[population[i]]++;
  68.         if(map[population[i]] > max_seen_count_of_num) {
  69.             max_seen_count_of_num = map[population[i]];
  70.             mode_value = population[i];
  71.         }
  72.     }
  73.  
  74.     bool mode_exists = false;
  75.     for (int i = 0; i < population_size; i++) {
  76.         if(map[population[i]] < max_seen_count_of_num) {
  77.             mode_exists = true;
  78.             break;
  79.         }
  80.     }
  81.  
  82.     if(!mode_exists) return -0.0; else return mode_value;
  83. }
  84.  
  85. double variance(const double population[], const int population_size, const double mean) {
  86.     double sum_of_squares = 0.0;
  87.  
  88.     for(int i = 0; i < population_size; i++) {
  89.         sum_of_squares += (population[i] - mean) * (population[i] - mean);
  90.     }
  91.  
  92.     return sum_of_squares / population_size;
  93. }
  94.  
  95. void calculate_dispersion(const double population[], const int length) {
  96.    
  97.     double min_value = population[0];
  98.     double max_value = population[0];
  99.     double sum = 0.0;
  100.  
  101.     for (int i = 0; i < length; i++) {
  102.         double curr = population[i];
  103.         sum += curr;
  104.  
  105.         if(curr < min_value) min_value = curr;
  106.         if(curr > max_value) max_value = curr;
  107.     }
  108.  
  109.     double mean_value = sum / length;
  110.     double median = median_value(population, length);
  111.     double mode_value = mode(population, length);
  112.     double variance_value = variance(population, length, mean_value);
  113.     double first_quartile_value;
  114.     double third_quartile_value;
  115.    
  116.     calculate_quartiles(population, length, &first_quartile_value, &third_quartile_value);
  117.  
  118.     cout << endl << "-------Dispersion Result-------" << endl;
  119.     cout << "Population size: " << length << endl;
  120.     cout << "Lowest value: " << min_value << endl;
  121.     cout << "Highest value: " << max_value << endl;
  122.     cout << "Mean(average): " << mean_value << endl;
  123.     cout << "Median: " << median << endl;
  124.     cout << "Range: " << max_value - min_value << endl;
  125.     cout << "Variance: " << variance_value << endl;
  126.     cout << "Standard deviation: " << sqrt(variance_value) << endl;
  127.     cout << "First quartile: " << first_quartile_value << endl;
  128.     cout << "Third quartile: " << third_quartile_value << endl;
  129.     cout << "Interquartile range: " << third_quartile_value - first_quartile_value << endl;
  130.     cout << "Quartile deviation: " << (third_quartile_value - first_quartile_value) / 2 << endl;
  131.    
  132.  
  133.     if(mode_value == -0.0) cout << "Mode: does not exist" << endl << endl;
  134.     else cout << "Mode: " << mode_value << endl << endl;
  135. }
  136.  
  137. bool dispersion_input_is_valid(char *num) {
  138.     for(int i = 0; i < strlen(num); i++) {
  139.         if (isalpha(num[i])) return false;
  140.     }
  141.     return true;
  142. }
  143.  
  144. void dispersion() {
  145.     char data_string[INPUT_DATA_SIZE];
  146.     cout << "Enter comma separeted data(-100.3, 23.5, 230 - for instance): ";
  147.     cin >> data_string;
  148.  
  149.     double buffer_population[INPUT_DATA_SIZE];
  150.     int population_size = 0;
  151.  
  152.     char *character;
  153.     character = strtok (data_string, SEPARATOR);
  154.     while (character != NULL) {
  155.         if(dispersion_input_is_valid(character)) {
  156.             buffer_population[population_size++] = atof(character);
  157.             character = strtok(NULL, SEPARATOR);
  158.         } else {
  159.             cout << endl << "InputError: Input data should contain only comma(,) and nums" << endl;
  160.             throw INPUT_DATA_EXCEPTION_CODE;
  161.         }
  162.     }
  163.  
  164.     double population[population_size];
  165.     for(int i = 0; i < population_size; i++) population[i] = buffer_population[i];
  166.    
  167.     calculate_dispersion(population, population_size);
  168. }
  169.  
  170. struct Expectation {
  171.     double x;
  172.     double p_of_x;
  173. };
  174.  
  175. void calculate_expectation(Expectation *expectations, int expectations_size, int precision) {
  176.     double result_expectation = 0.0;
  177.    
  178.     for(int i = 0; i < expectations_size; i++) {
  179.         Expectation expecation = expectations[i];
  180.         result_expectation += expecation.x * expecation.p_of_x;
  181.     }
  182.  
  183.     cout << endl << "Result expectations is " << setprecision(precision) << result_expectation;
  184. }
  185.  
  186. bool expectation_input_is_valid(char *num) {
  187.     for(int i = 0; i < strlen(num); i++) {
  188.         if (isalpha(num[i])) return false;
  189.     }
  190.     return true;
  191. }
  192.  
  193. void expectation() {
  194.  
  195.     int huge_way_of_precision = -1;
  196.     int precision = EXPECTATION_PROBABILITY_PRECISION;
  197.     do {
  198.         cout << "Choose precision(count of symbols after comma) \n 0 - small \n1 - huge: ";
  199.         cin >> huge_way_of_precision;
  200.     } while (huge_way_of_precision < 0 || huge_way_of_precision > 1);
  201.  
  202.     if(huge_way_of_precision) precision = EXPECTATION_PROBABILITY_PRECISION; else precision = 3;
  203.  
  204.     int expectations_size = 0;
  205.     double used_probability = 0.0;
  206.  
  207.     Expectation *expectations = NULL;
  208.  
  209.     cout << endl << "Formula of probability is E(x) = x1 * P(x1) + x2 * P(x2) + ... xn * P(xn)" << endl;
  210.     cout << "Sum of P(xn) should be equalled to 1" << endl << endl;
  211.    
  212.     do
  213.     {
  214.         char x_string[INPUT_DATA_SIZE];
  215.         char p_of_x_string[INPUT_DATA_SIZE];
  216.  
  217.         double x;
  218.         double p_of_x;
  219.  
  220.         double probability_left = 1.0 - used_probability;
  221.  
  222.         cout << "Current sum of P(xn): " << setprecision(precision) << used_probability << endl << endl;
  223.  
  224.         do {
  225.             cout << "Enter x" << expectations_size + 1 << "(should be a number): ";
  226.             cin >> x_string;
  227.         } while(!expectation_input_is_valid(x_string));
  228.  
  229.         x = atof(x_string);
  230.  
  231.         do
  232.         {
  233.             if(used_probability + 0.001 >= 1.0) break;
  234.             cout << endl << "Enter P(x" << expectations_size + 1 << ") (from 0.0 until " << setprecision(precision) << probability_left << ", should be a number): ";
  235.             cin >> p_of_x_string;
  236.         } while (!expectation_input_is_valid(p_of_x_string) || atof(p_of_x_string) < 0.0 || atof(p_of_x_string) > probability_left + ACCURACY);
  237.  
  238.         p_of_x = atof(p_of_x_string);
  239.         expectations = (Expectation*)realloc(expectations, (expectations_size + 1) * sizeof(Expectation));
  240.         used_probability += p_of_x;
  241.  
  242.  
  243.         Expectation curr_expectation;
  244.         curr_expectation.x = x;
  245.         curr_expectation.p_of_x = p_of_x;
  246.         expectations[expectations_size++] = curr_expectation;
  247.  
  248.     } while (used_probability + 0.001 < 1.0);
  249.    
  250.     calculate_expectation(expectations, expectations_size, precision);
  251. }
  252.  
  253. struct Probability {
  254.     char name[MAX_PROBABILITY_NAME_LENGTH];
  255.     double value;
  256. };
  257.  
  258. void calculate_probability(double p_a, double p_b) {
  259.     double p_a_and_p_b = p_a * p_b;
  260.  
  261.     cout << endl << "P(A) = " << p_a << endl;
  262.     cout << "P(B) = " << p_b << endl;
  263.     cout << "not(P(A)) = " << 1 - p_a << endl;
  264.     cout << "not(P(B)) = " << 1 - p_b << endl;
  265.     cout << "P(A∩B) = P(A) × P(B) = " << p_a_and_p_b << endl;
  266.     cout << "not(P(A∩B)) = " << 1 - p_a_and_p_b << endl;
  267.     cout << "P(A∪B) = P(A) + P(B) - P(A∩B) = " << p_a + p_b - p_a_and_p_b << endl;
  268.     cout << "not(P(A∪B)) = 1 - (P(A) + P(B) - P(A∩B)) = " << 1 - (p_a + p_b - p_a_and_p_b) << endl;
  269.     cout << "P(AΔB) = P(A) + P(B) - 2P(A∩B) = " << p_a + p_b - (2 * p_a_and_p_b) << endl;
  270. }
  271.  
  272. void calculate_probability_depedent_events_when_pa_and_pb() {
  273.     bool probability_a_is_entered = false;
  274.     char p_a_string[INPUT_DATA_SIZE] = "-1.0";
  275.     char p_b_string[INPUT_DATA_SIZE] = "-1.0";
  276.     do
  277.     {
  278.         if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
  279.  
  280.         string probability_name = probability_a_is_entered ? "B" : "A" ;
  281.         cout << "Enter P(" << probability_name << ")'value (from 0 until 1): ";
  282.         if(probability_a_is_entered) cin >> p_b_string; else cin >> p_a_string;
  283.     } while (!expectation_input_is_valid(p_a_string) || !expectation_input_is_valid(p_b_string) || atof(p_a_string) < 0 || atof(p_b_string) < 0 || atof(p_a_string) > 1 || atof(p_b_string) > 1);
  284.    
  285.     calculate_probability(atof(p_a_string), atof(p_b_string));
  286. }
  287.  
  288. void calculate_probability_depedent_events_when_not_pa_and_not_pb() {
  289.     bool probability_a_is_entered = false;
  290.     char not_p_a_string[INPUT_DATA_SIZE] = "-1.0";
  291.     char not_p_b_string[INPUT_DATA_SIZE] = "-1.0";
  292.     do
  293.     {
  294.         if(expectation_input_is_valid(not_p_a_string) && atof(not_p_a_string) >= 0 && atof(not_p_a_string) <= 1) probability_a_is_entered = true;
  295.  
  296.         string probability_name = probability_a_is_entered ? "not(B)" : "not(A)" ;
  297.         cout << "Enter P(" << probability_name << ")'value (from 0 until 1): ";
  298.         if(probability_a_is_entered) cin >> not_p_b_string; else cin >> not_p_a_string;
  299.     } while (!expectation_input_is_valid(not_p_b_string) || !expectation_input_is_valid(not_p_b_string) || atof(not_p_a_string) < 0 || atof(not_p_b_string) < 0 || atof(not_p_a_string) > 1 || atof(not_p_b_string) > 1);
  300.    
  301.     calculate_probability(1 - atof(not_p_a_string), 1 - atof(not_p_b_string));
  302. }
  303.  
  304. void calculate_probability_depedent_events_when_pa_and_not_pb() {
  305.     bool probability_a_is_entered = false;
  306.     char p_a_string[INPUT_DATA_SIZE] = "-1.0";
  307.     char not_p_b_string[INPUT_DATA_SIZE] = "-1.0";
  308.     do
  309.     {
  310.         if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
  311.  
  312.         string probability_name = probability_a_is_entered ? "not(P(B))" : "P(A)" ;
  313.         cout << "Enter " << probability_name << "'value (from 0 until 1): ";
  314.         if(probability_a_is_entered) cin >> not_p_b_string; else cin >> p_a_string;
  315.     } while (!expectation_input_is_valid(not_p_b_string) || !expectation_input_is_valid(not_p_b_string) || atof(p_a_string) < 0 || atof(not_p_b_string) < 0 || atof(p_a_string) > 1 || atof(not_p_b_string) > 1);
  316.    
  317.     calculate_probability(atof(p_a_string), 1 - atof(not_p_b_string));
  318. }
  319.  
  320. void calculate_probability_depedent_events_when_not_pa_and_pb() {
  321.     bool probability_a_is_entered = false;
  322.     char not_p_a_string[INPUT_DATA_SIZE] = "-1.0";
  323.     char p_b_string[INPUT_DATA_SIZE] = "-1.0";
  324.     do
  325.     {
  326.         if(expectation_input_is_valid(not_p_a_string) && atof(not_p_a_string) >= 0 && atof(not_p_a_string) <= 1) probability_a_is_entered = true;
  327.  
  328.         string probability_name = probability_a_is_entered ? "P(B)" : "not(P(A))" ;
  329.         cout << "Enter " << probability_name << "'value (from 0 until 1): ";
  330.         if(probability_a_is_entered) cin >> p_b_string; else cin >> not_p_a_string;
  331.     } while (!expectation_input_is_valid(p_b_string) || !expectation_input_is_valid(p_b_string) || atof(not_p_a_string) < 0 || atof(p_b_string) < 0 || atof(not_p_a_string) > 1 || atof(p_b_string) > 1);
  332.    
  333.     calculate_probability(1 - atof(not_p_a_string), atof(p_b_string));
  334. }
  335.  
  336. void calculate_probability_depedent_events_when_pa_pa_and_p_a_and_b() {
  337.     bool probability_a_is_entered = false;
  338.     char p_a_string[INPUT_DATA_SIZE] = "-1.0";
  339.     char p_a_and_b_string[INPUT_DATA_SIZE] = "-1.0";
  340.     do
  341.     {
  342.         if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
  343.  
  344.         string probability_name = probability_a_is_entered ? "P(A∩B)" : "P(A)" ;
  345.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∩B) cannot be large than P(A)) ";
  346.         if(probability_a_is_entered) cin >> p_a_and_b_string; else cin >> p_a_string;
  347.     } while (!expectation_input_is_valid(p_a_string) || !expectation_input_is_valid(p_a_and_b_string) || atof(p_a_string) < 0 || atof(p_a_and_b_string) < 0 || atof(p_a_string) > 1 || atof(p_a_and_b_string) > 1 || atof(p_a_and_b_string) > atof(p_a_string));
  348.    
  349.    
  350.     double p_b = atof(p_a_and_b_string) / atof(p_a_string);
  351.     calculate_probability(atof(p_a_string), p_b);
  352. }
  353.  
  354. void calculate_probability_depedent_events_when_pb_and_p_a_and_b() {
  355.     bool probability_a_is_entered = false;
  356.     char p_b_string[INPUT_DATA_SIZE] = "-1.0";
  357.     char p_a_and_b_string[INPUT_DATA_SIZE] = "-1.0";
  358.     do
  359.     {
  360.         if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
  361.  
  362.         string probability_name = probability_a_is_entered ? "P(A∩B)" : "P(B)" ;
  363.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∩B) cannot be large than P(B)): ";
  364.         if(probability_a_is_entered) cin >> p_a_and_b_string; else cin >> p_b_string;
  365.     } while (!expectation_input_is_valid(p_b_string) || !expectation_input_is_valid(p_a_and_b_string) || atof(p_b_string) < 0 || atof(p_a_and_b_string) < 0 || atof(p_b_string) > 1 || atof(p_a_and_b_string) > 1 || atof(p_a_and_b_string) > atof(p_b_string));
  366.    
  367.    
  368.     double p_a = atof(p_a_and_b_string) / atof(p_b_string);
  369.     calculate_probability(p_a, atof(p_b_string));
  370. }
  371.  
  372. void calculate_probability_depedent_events_when_pa_and_p_a_or_b() {
  373.     bool probability_a_is_entered = false;
  374.     char p_a_string[INPUT_DATA_SIZE] = "-1.0";
  375.     char p_a_or_b_string[INPUT_DATA_SIZE] = "-1.0";
  376.     do
  377.     {
  378.         if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
  379.  
  380.         string probability_name = probability_a_is_entered ? "P(A∪B)" : "P(A)" ;
  381.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∪B) cannot be less than P(A)): ";
  382.         if(probability_a_is_entered) cin >> p_a_or_b_string; else cin >> p_a_string;
  383.     } while (!expectation_input_is_valid(p_a_string) || !expectation_input_is_valid(p_a_or_b_string) || atof(p_a_string) < 0 || atof(p_a_or_b_string) < 0 || atof(p_a_string) > 1 || atof(p_a_or_b_string) > 1 || atof(p_a_or_b_string) < atof(p_a_string));
  384.    
  385.    
  386.     double p_b = (atof(p_a_or_b_string) - atof(p_a_string)) / (1 - atof(p_a_string));
  387.     calculate_probability(atof(p_a_string), p_b);
  388. }
  389.  
  390. void calculate_probability_depedent_events_when_pb_and_p_a_or_b() {
  391.     bool probability_a_is_entered = false;
  392.     char p_b_string[INPUT_DATA_SIZE] = "-1.0";
  393.     char p_a_or_b_string[INPUT_DATA_SIZE] = "-1.0";
  394.     do
  395.     {
  396.         if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
  397.  
  398.         string probability_name = probability_a_is_entered ? "P(A∪B)" : "P(B)" ;
  399.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∪B) cannot be less than P(B)): ";
  400.         if(probability_a_is_entered) cin >> p_a_or_b_string; else cin >> p_b_string;
  401.     } while (!expectation_input_is_valid(p_b_string) || !expectation_input_is_valid(p_a_or_b_string) || atof(p_b_string) < 0 || atof(p_a_or_b_string) < 0 || atof(p_b_string) > 1 || atof(p_a_or_b_string) > 1 || atof(p_a_or_b_string) < atof(p_b_string));
  402.    
  403.  
  404.     double p_a = (atof(p_a_or_b_string) - atof(p_b_string)) / (1 - atof(p_b_string));
  405.     calculate_probability(p_a, atof(p_b_string));
  406. }
  407.  
  408. void calculate_probability_depedent_events_when_pa_and_p_a_xor_b() {
  409.     bool probability_a_is_entered = false;
  410.     char p_a_string[INPUT_DATA_SIZE] = "-1.0";
  411.     char p_a_xor_b_string[INPUT_DATA_SIZE] = "-1.0";
  412.     do
  413.     {
  414.         if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
  415.  
  416.         string probability_name = probability_a_is_entered ? "P(AΔB)" : "P(A)" ;
  417.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(B) = (P(AΔB) - P(A)) / (1 - 2 * P(A)) should be positive: ";
  418.         if(probability_a_is_entered) cin >> p_a_xor_b_string; else cin >> p_a_string;
  419.     } while (!expectation_input_is_valid(p_a_string) || !expectation_input_is_valid(p_a_xor_b_string) || atof(p_a_string) < 0 || atof(p_a_xor_b_string) < 0 || atof(p_a_string) > 1 || atof(p_a_xor_b_string) > 1 || ((atof(p_a_xor_b_string) - atof(p_a_string)) / (1 - 2*atof(p_a_string))) < 0);
  420.    
  421.    
  422.     double p_b = (atof(p_a_xor_b_string) - atof(p_a_string)) / (1 - 2*atof(p_a_string));
  423.     calculate_probability(atof(p_a_string), p_b);
  424. }
  425.  
  426. void calculate_probability_depedent_events_when_pb_and_p_a_xor_b() {
  427.     bool probability_a_is_entered = false;
  428.     char p_b_string[INPUT_DATA_SIZE] = "-1.0";
  429.     char p_a_xor_b_string[INPUT_DATA_SIZE] = "-1.0";
  430.     do
  431.     {
  432.         if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
  433.  
  434.         string probability_name = probability_a_is_entered ? "P(AΔB)" : "P(B)" ;
  435.         cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A) = (P(AΔB) - P(B)) / (1 - 2 * P(B)) should be positive: ";
  436.         if(probability_a_is_entered) cin >> p_a_xor_b_string; else cin >> p_b_string;
  437.     } while (!expectation_input_is_valid(p_b_string) || !expectation_input_is_valid(p_a_xor_b_string) || atof(p_b_string) < 0 || atof(p_a_xor_b_string) < 0 || atof(p_b_string) > 1 || atof(p_a_xor_b_string) > 1 || ((atof(p_a_xor_b_string) - atof(p_b_string)) / (1 - 2 * atof(p_b_string))) < 0);
  438.    
  439.  
  440.     double p_a = (atof(p_a_xor_b_string) - atof(p_b_string)) / (1 - 2 * atof(p_b_string));
  441.     calculate_probability(p_a, atof(p_b_string));
  442. }
  443.  
  444. void probability_depedent_events() {
  445.     int action;
  446.    
  447.     while(true) {
  448.         cout << "Actions"
  449.         << "\n1 - Given P(A) and P(B)"
  450.         << "\n2 - Given not(P(A)) and not(P(B))"
  451.         << "\n3 - Given P(A) and not(P(B))"
  452.         << "\n4 - Given not(P(A)) and P(B)"
  453.         << "\n5 - Given P(A) and P(A∩B)"
  454.         << "\n6 - Given P(B) and P(A∩B)"
  455.         << "\n7 - Given P(A) and P(A∪B)"
  456.         << "\n8 - Given P(B) and P(A∪B)"
  457.         << "\n9 - Given P(A) and P(AΔB)"
  458.         << "\n10 - Given P(B) and P(AΔB)"
  459.         << "\n0 - Exit: ";
  460.         cin >> action;
  461.  
  462.         if(action == 0) break;
  463.         if(action < 1 || action > 10) continue; else break;
  464.     }
  465.  
  466.     switch(action) {
  467.         case 1: calculate_probability_depedent_events_when_pa_and_pb(); break;
  468.         case 2: calculate_probability_depedent_events_when_not_pa_and_not_pb(); break;
  469.         case 3: calculate_probability_depedent_events_when_pa_and_not_pb(); break;
  470.         case 4: calculate_probability_depedent_events_when_not_pa_and_pb(); break;
  471.         case 5: calculate_probability_depedent_events_when_pa_pa_and_p_a_and_b(); break;
  472.         case 6: calculate_probability_depedent_events_when_pb_and_p_a_and_b(); break;
  473.         case 7: calculate_probability_depedent_events_when_pa_and_p_a_or_b(); break;
  474.         case 8: calculate_probability_depedent_events_when_pb_and_p_a_or_b(); break;
  475.         case 9: calculate_probability_depedent_events_when_pa_and_p_a_xor_b(); break;
  476.         case 10: calculate_probability_depedent_events_when_pb_and_p_a_xor_b(); break;
  477.     }
  478. }
  479.  
  480. bool independent_pobability_input_is_valid(char *num) {
  481.     for(int i = 0; i < strlen(num); i++) {
  482.         if (isalpha(num[i])) return false;
  483.     }
  484.     return true;
  485. }
  486.  
  487. void calculate_independent_probability_events(Probability *probabilities, int size) {
  488.     double ocurrence_all = 1.0;
  489.     double no_occurences = 1.0;
  490.     double or_occurences = 0.0;
  491.     double not_or_occurences = 0.0;
  492.  
  493.     cout << endl << "--------Calculating probabilities for independent events--------" << endl;
  494.     for(int i = 0; i < size; i++) {
  495.         Probability probability = probabilities[i];
  496.         cout << "P(" << probability.name << ") = " << probability.value << endl;
  497.     }
  498.  
  499.     for(int i = 0; i < size; i++) {
  500.         Probability probability = probabilities[i];
  501.         string postfix = i + 1 == size ? "" : " * ";
  502.         cout << "P(" << probability.name << ")" << postfix;
  503.         ocurrence_all *= probability.value;
  504.     }
  505.  
  506.     cout << " = " << ocurrence_all << endl << endl;
  507.  
  508.     for(int i = 0; i < size; i++) {
  509.         Probability probability = probabilities[i];
  510.         string postfix = i + 1 == size ? "" : " * ";
  511.         cout << "not(P(" << probability.name << "))" << postfix;
  512.         no_occurences *= (1 - probability.value);
  513.     }
  514.  
  515.     cout << " = " << no_occurences << endl << endl;
  516.  
  517.     for(int i = 0; i < size; i++) {
  518.         Probability probability = probabilities[i];
  519.         string postfix = i + 1 == size ? "" : " + ";
  520.         cout << "P(" << probability.name << ")" << postfix;
  521.         or_occurences += probability.value;
  522.     }
  523.  
  524.     cout << " = " << or_occurences << endl << endl;
  525.  
  526.     for(int i = 0; i < size; i++) {
  527.         Probability probability = probabilities[i];
  528.         string postfix = i + 1 == size ? "" : " + ";
  529.         cout << "not(P(" << probability.name << "))" << postfix;
  530.         not_or_occurences += (1 - probability.value);
  531.     }
  532.  
  533.     cout << " = " << not_or_occurences << endl << endl;
  534.  
  535. }
  536.  
  537. void probability_independent_events() {
  538.     set<string> taken_name;
  539.     Probability *probabilities = NULL;
  540.     int probabilities_size = 0;
  541.  
  542.     cout << endl << "--------Entering probabilities--------" << endl;
  543.     while(true) {
  544.         char name[MAX_PROBABILITY_NAME_LENGTH];
  545.         char value[INPUT_DATA_SIZE];
  546.  
  547.         int iteration = 0;
  548.         while(true) {
  549.             string additional = (probabilities_size > 0) ? "next" : "";
  550.             cout << "Enter name for the " << additional << " probability(name's length is in range(1,3)) or enter -1 to exit: ";
  551.             cin >> name;
  552.             int name_len = strlen(name);
  553.             if(taken_name.count(name)) {
  554.                 cout << "Name " << name << " is already taken, choose another one" << endl;
  555.                 continue;
  556.             }
  557.             if(name_len < 1 || name_len > 3) continue; else break;
  558.         }
  559.  
  560.         if(strcmp(name, "-1") == 0) break;
  561.  
  562.         do
  563.         {
  564.             cout << endl << "Enter a value for P(" << name << ") probability(should be in range(0,1)): " << endl;
  565.             cin >> value;
  566.         } while (!independent_pobability_input_is_valid(value) || atof(value) < 0 || atof(value) > 1);
  567.        
  568.         Probability curr_probability;
  569.  
  570.         taken_name.insert(name);
  571.  
  572.         strcpy(curr_probability.name, name);
  573.         curr_probability.value = atof(value);
  574.  
  575.         probabilities = (Probability*)realloc(probabilities, (probabilities_size + 1) * sizeof(Probability));
  576.         probabilities[probabilities_size++] = curr_probability;
  577.     }
  578.    
  579.     calculate_independent_probability_events(probabilities, probabilities_size);
  580. }
  581.  
  582. void probability() {
  583.     int way;
  584.  
  585.     do
  586.     {
  587.         cout << endl << "Actions \n1 - Probability of a Series of Independent Events \n2 - Probability for dependent Events \n0 - Exit: " << endl;
  588.         cin >> way;
  589.         switch(way) {
  590.             case 0: break;
  591.             case 1: probability_independent_events(); break;
  592.             case 2: probability_depedent_events(); break;
  593.         }
  594.         if(way == 0) break;
  595.     } while (way != 1 || way != 2 || way != 0);
  596.    
  597. }
  598.  
  599. int main() {
  600.     int way;
  601.    
  602.     do
  603.     {
  604.         cout << endl << "1 - Dispersion" << endl;
  605.         cout << "2 - Expectation" << endl;
  606.         cout << "3 - Probability" << endl;
  607.         cout << "0 - Exit" << endl;
  608.  
  609.         cout << endl << "Choose what to do: " << endl;
  610.         cin >> way;
  611.         if(way == 0) break;
  612.  
  613.         switch(way) {
  614.             case 1: {
  615.                 try
  616.                 {
  617.                     dispersion();
  618.                 }
  619.                 catch(const int exception)
  620.                 {}
  621.                 break;
  622.             }
  623.             case 2: expectation(); break;
  624.             case 3: probability(); break;
  625.             default: cout << "Variant by " << way << " has not been found" << endl;
  626.         }
  627.  
  628.     } while (way != 0 || way < 1 || way > 3);
  629.    
  630.  
  631.     return 0;
  632. }
  633.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement