Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <set>
- #include <iomanip>
- using namespace std;
- #define INPUT_DATA_SIZE 1024
- #define SEPARATOR ","
- #define INPUT_DATA_EXCEPTION_CODE 111
- #define EXPECTATION_PROBABILITY_PRECISION 20
- #define ACCURACY 0.05
- #define MAX_PROBABILITY_NAME_LENGTH 3
- #define DEPDENDENT_EVENTS_PROBABILITY_LENGTH 2
- double median(const double sorted[], int start, int end) {
- int size = end - start + 1;
- int middle = start + (size - 1) / 2;
- if (size % 2 == 0) {
- return (sorted[middle] + sorted[middle + 1]) / 2.0;
- } else {
- return sorted[middle];
- }
- }
- double median_value(const double population[], const int population_size) {
- double sorted[population_size];
- for(int i = 0; i < population_size; i++) sorted[i] = population[i];
- int n = sizeof(sorted) / sizeof(sorted[0]);
- sort(sorted, sorted + n);
- return median(sorted, 0, population_size - 1);
- }
- void calculate_quartiles(const double population[], const int population_size, double *q1, double *q3) {
- double sorted[population_size];
- for(int i = 0; i < population_size; i++) sorted[i] = population[i];
- int n = sizeof(sorted) / sizeof(sorted[0]);
- sort(sorted, sorted + n);
- double median_value = median(sorted, 0, population_size - 1);
- int middle_index = population_size / 2;
- (*q1) = median(sorted, 0, middle_index - 1);
- if(population_size % 2 != 0) (*q3) = median(sorted, middle_index + 1, population_size - 1);
- else (*q3) = median(sorted, middle_index, population_size - 1);
- }
- double mode(const double population[], const int population_size) {
- if (population_size == 1) return population[0];
- double mode_value;
- unordered_map<double,int>map;
- int max_seen_count_of_num = INT32_MIN;
- for(int i = 0; i < population_size; i++) {
- map[population[i]]++;
- if(map[population[i]] > max_seen_count_of_num) {
- max_seen_count_of_num = map[population[i]];
- mode_value = population[i];
- }
- }
- bool mode_exists = false;
- for (int i = 0; i < population_size; i++) {
- if(map[population[i]] < max_seen_count_of_num) {
- mode_exists = true;
- break;
- }
- }
- if(!mode_exists) return -0.0; else return mode_value;
- }
- double variance(const double population[], const int population_size, const double mean) {
- double sum_of_squares = 0.0;
- for(int i = 0; i < population_size; i++) {
- sum_of_squares += (population[i] - mean) * (population[i] - mean);
- }
- return sum_of_squares / population_size;
- }
- void calculate_dispersion(const double population[], const int length) {
- double min_value = population[0];
- double max_value = population[0];
- double sum = 0.0;
- for (int i = 0; i < length; i++) {
- double curr = population[i];
- sum += curr;
- if(curr < min_value) min_value = curr;
- if(curr > max_value) max_value = curr;
- }
- double mean_value = sum / length;
- double median = median_value(population, length);
- double mode_value = mode(population, length);
- double variance_value = variance(population, length, mean_value);
- double first_quartile_value;
- double third_quartile_value;
- calculate_quartiles(population, length, &first_quartile_value, &third_quartile_value);
- cout << endl << "-------Dispersion Result-------" << endl;
- cout << "Population size: " << length << endl;
- cout << "Lowest value: " << min_value << endl;
- cout << "Highest value: " << max_value << endl;
- cout << "Mean(average): " << mean_value << endl;
- cout << "Median: " << median << endl;
- cout << "Range: " << max_value - min_value << endl;
- cout << "Variance: " << variance_value << endl;
- cout << "Standard deviation: " << sqrt(variance_value) << endl;
- cout << "First quartile: " << first_quartile_value << endl;
- cout << "Third quartile: " << third_quartile_value << endl;
- cout << "Interquartile range: " << third_quartile_value - first_quartile_value << endl;
- cout << "Quartile deviation: " << (third_quartile_value - first_quartile_value) / 2 << endl;
- if(mode_value == -0.0) cout << "Mode: does not exist" << endl << endl;
- else cout << "Mode: " << mode_value << endl << endl;
- }
- bool dispersion_input_is_valid(char *num) {
- for(int i = 0; i < strlen(num); i++) {
- if (isalpha(num[i])) return false;
- }
- return true;
- }
- void dispersion() {
- char data_string[INPUT_DATA_SIZE];
- cout << "Enter comma separeted data(-100.3, 23.5, 230 - for instance): ";
- cin >> data_string;
- double buffer_population[INPUT_DATA_SIZE];
- int population_size = 0;
- char *character;
- character = strtok (data_string, SEPARATOR);
- while (character != NULL) {
- if(dispersion_input_is_valid(character)) {
- buffer_population[population_size++] = atof(character);
- character = strtok(NULL, SEPARATOR);
- } else {
- cout << endl << "InputError: Input data should contain only comma(,) and nums" << endl;
- throw INPUT_DATA_EXCEPTION_CODE;
- }
- }
- double population[population_size];
- for(int i = 0; i < population_size; i++) population[i] = buffer_population[i];
- calculate_dispersion(population, population_size);
- }
- struct Expectation {
- double x;
- double p_of_x;
- };
- void calculate_expectation(Expectation *expectations, int expectations_size, int precision) {
- double result_expectation = 0.0;
- for(int i = 0; i < expectations_size; i++) {
- Expectation expecation = expectations[i];
- result_expectation += expecation.x * expecation.p_of_x;
- }
- cout << endl << "Result expectations is " << setprecision(precision) << result_expectation;
- }
- bool expectation_input_is_valid(char *num) {
- for(int i = 0; i < strlen(num); i++) {
- if (isalpha(num[i])) return false;
- }
- return true;
- }
- void expectation() {
- int huge_way_of_precision = -1;
- int precision = EXPECTATION_PROBABILITY_PRECISION;
- do {
- cout << "Choose precision(count of symbols after comma) \n 0 - small \n1 - huge: ";
- cin >> huge_way_of_precision;
- } while (huge_way_of_precision < 0 || huge_way_of_precision > 1);
- if(huge_way_of_precision) precision = EXPECTATION_PROBABILITY_PRECISION; else precision = 3;
- int expectations_size = 0;
- double used_probability = 0.0;
- Expectation *expectations = NULL;
- cout << endl << "Formula of probability is E(x) = x1 * P(x1) + x2 * P(x2) + ... xn * P(xn)" << endl;
- cout << "Sum of P(xn) should be equalled to 1" << endl << endl;
- do
- {
- char x_string[INPUT_DATA_SIZE];
- char p_of_x_string[INPUT_DATA_SIZE];
- double x;
- double p_of_x;
- double probability_left = 1.0 - used_probability;
- cout << "Current sum of P(xn): " << setprecision(precision) << used_probability << endl << endl;
- do {
- cout << "Enter x" << expectations_size + 1 << "(should be a number): ";
- cin >> x_string;
- } while(!expectation_input_is_valid(x_string));
- x = atof(x_string);
- do
- {
- if(used_probability + 0.001 >= 1.0) break;
- cout << endl << "Enter P(x" << expectations_size + 1 << ") (from 0.0 until " << setprecision(precision) << probability_left << ", should be a number): ";
- cin >> p_of_x_string;
- } while (!expectation_input_is_valid(p_of_x_string) || atof(p_of_x_string) < 0.0 || atof(p_of_x_string) > probability_left + ACCURACY);
- p_of_x = atof(p_of_x_string);
- expectations = (Expectation*)realloc(expectations, (expectations_size + 1) * sizeof(Expectation));
- used_probability += p_of_x;
- Expectation curr_expectation;
- curr_expectation.x = x;
- curr_expectation.p_of_x = p_of_x;
- expectations[expectations_size++] = curr_expectation;
- } while (used_probability + 0.001 < 1.0);
- calculate_expectation(expectations, expectations_size, precision);
- }
- struct Probability {
- char name[MAX_PROBABILITY_NAME_LENGTH];
- double value;
- };
- void calculate_probability(double p_a, double p_b) {
- double p_a_and_p_b = p_a * p_b;
- cout << endl << "P(A) = " << p_a << endl;
- cout << "P(B) = " << p_b << endl;
- cout << "not(P(A)) = " << 1 - p_a << endl;
- cout << "not(P(B)) = " << 1 - p_b << endl;
- cout << "P(A∩B) = P(A) × P(B) = " << p_a_and_p_b << endl;
- cout << "not(P(A∩B)) = " << 1 - p_a_and_p_b << endl;
- cout << "P(A∪B) = P(A) + P(B) - P(A∩B) = " << p_a + p_b - p_a_and_p_b << endl;
- cout << "not(P(A∪B)) = 1 - (P(A) + P(B) - P(A∩B)) = " << 1 - (p_a + p_b - p_a_and_p_b) << endl;
- cout << "P(AΔB) = P(A) + P(B) - 2P(A∩B) = " << p_a + p_b - (2 * p_a_and_p_b) << endl;
- }
- void calculate_probability_depedent_events_when_pa_and_pb() {
- bool probability_a_is_entered = false;
- char p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char p_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "B" : "A" ;
- cout << "Enter P(" << probability_name << ")'value (from 0 until 1): ";
- if(probability_a_is_entered) cin >> p_b_string; else cin >> p_a_string;
- } 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);
- calculate_probability(atof(p_a_string), atof(p_b_string));
- }
- void calculate_probability_depedent_events_when_not_pa_and_not_pb() {
- bool probability_a_is_entered = false;
- char not_p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char not_p_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- 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;
- string probability_name = probability_a_is_entered ? "not(B)" : "not(A)" ;
- cout << "Enter P(" << probability_name << ")'value (from 0 until 1): ";
- if(probability_a_is_entered) cin >> not_p_b_string; else cin >> not_p_a_string;
- } 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);
- calculate_probability(1 - atof(not_p_a_string), 1 - atof(not_p_b_string));
- }
- void calculate_probability_depedent_events_when_pa_and_not_pb() {
- bool probability_a_is_entered = false;
- char p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char not_p_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "not(P(B))" : "P(A)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1): ";
- if(probability_a_is_entered) cin >> not_p_b_string; else cin >> p_a_string;
- } 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);
- calculate_probability(atof(p_a_string), 1 - atof(not_p_b_string));
- }
- void calculate_probability_depedent_events_when_not_pa_and_pb() {
- bool probability_a_is_entered = false;
- char not_p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char p_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- 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;
- string probability_name = probability_a_is_entered ? "P(B)" : "not(P(A))" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1): ";
- if(probability_a_is_entered) cin >> p_b_string; else cin >> not_p_a_string;
- } 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);
- calculate_probability(1 - atof(not_p_a_string), atof(p_b_string));
- }
- void calculate_probability_depedent_events_when_pa_pa_and_p_a_and_b() {
- bool probability_a_is_entered = false;
- char p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_and_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(A∩B)" : "P(A)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∩B) cannot be large than P(A)) ";
- if(probability_a_is_entered) cin >> p_a_and_b_string; else cin >> p_a_string;
- } 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));
- double p_b = atof(p_a_and_b_string) / atof(p_a_string);
- calculate_probability(atof(p_a_string), p_b);
- }
- void calculate_probability_depedent_events_when_pb_and_p_a_and_b() {
- bool probability_a_is_entered = false;
- char p_b_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_and_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(A∩B)" : "P(B)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∩B) cannot be large than P(B)): ";
- if(probability_a_is_entered) cin >> p_a_and_b_string; else cin >> p_b_string;
- } 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));
- double p_a = atof(p_a_and_b_string) / atof(p_b_string);
- calculate_probability(p_a, atof(p_b_string));
- }
- void calculate_probability_depedent_events_when_pa_and_p_a_or_b() {
- bool probability_a_is_entered = false;
- char p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_or_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(A∪B)" : "P(A)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∪B) cannot be less than P(A)): ";
- if(probability_a_is_entered) cin >> p_a_or_b_string; else cin >> p_a_string;
- } 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));
- double p_b = (atof(p_a_or_b_string) - atof(p_a_string)) / (1 - atof(p_a_string));
- calculate_probability(atof(p_a_string), p_b);
- }
- void calculate_probability_depedent_events_when_pb_and_p_a_or_b() {
- bool probability_a_is_entered = false;
- char p_b_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_or_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(A∪B)" : "P(B)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A∪B) cannot be less than P(B)): ";
- if(probability_a_is_entered) cin >> p_a_or_b_string; else cin >> p_b_string;
- } 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));
- double p_a = (atof(p_a_or_b_string) - atof(p_b_string)) / (1 - atof(p_b_string));
- calculate_probability(p_a, atof(p_b_string));
- }
- void calculate_probability_depedent_events_when_pa_and_p_a_xor_b() {
- bool probability_a_is_entered = false;
- char p_a_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_xor_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_a_string) && atof(p_a_string) >= 0 && atof(p_a_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(AΔB)" : "P(A)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(B) = (P(AΔB) - P(A)) / (1 - 2 * P(A)) should be positive: ";
- if(probability_a_is_entered) cin >> p_a_xor_b_string; else cin >> p_a_string;
- } 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);
- double p_b = (atof(p_a_xor_b_string) - atof(p_a_string)) / (1 - 2*atof(p_a_string));
- calculate_probability(atof(p_a_string), p_b);
- }
- void calculate_probability_depedent_events_when_pb_and_p_a_xor_b() {
- bool probability_a_is_entered = false;
- char p_b_string[INPUT_DATA_SIZE] = "-1.0";
- char p_a_xor_b_string[INPUT_DATA_SIZE] = "-1.0";
- do
- {
- if(expectation_input_is_valid(p_b_string) && atof(p_b_string) >= 0 && atof(p_b_string) <= 1) probability_a_is_entered = true;
- string probability_name = probability_a_is_entered ? "P(AΔB)" : "P(B)" ;
- cout << "Enter " << probability_name << "'value (from 0 until 1, also P(A) = (P(AΔB) - P(B)) / (1 - 2 * P(B)) should be positive: ";
- if(probability_a_is_entered) cin >> p_a_xor_b_string; else cin >> p_b_string;
- } 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);
- double p_a = (atof(p_a_xor_b_string) - atof(p_b_string)) / (1 - 2 * atof(p_b_string));
- calculate_probability(p_a, atof(p_b_string));
- }
- void probability_depedent_events() {
- int action;
- while(true) {
- cout << "Actions"
- << "\n1 - Given P(A) and P(B)"
- << "\n2 - Given not(P(A)) and not(P(B))"
- << "\n3 - Given P(A) and not(P(B))"
- << "\n4 - Given not(P(A)) and P(B)"
- << "\n5 - Given P(A) and P(A∩B)"
- << "\n6 - Given P(B) and P(A∩B)"
- << "\n7 - Given P(A) and P(A∪B)"
- << "\n8 - Given P(B) and P(A∪B)"
- << "\n9 - Given P(A) and P(AΔB)"
- << "\n10 - Given P(B) and P(AΔB)"
- << "\n0 - Exit: ";
- cin >> action;
- if(action == 0) break;
- if(action < 1 || action > 10) continue; else break;
- }
- switch(action) {
- case 1: calculate_probability_depedent_events_when_pa_and_pb(); break;
- case 2: calculate_probability_depedent_events_when_not_pa_and_not_pb(); break;
- case 3: calculate_probability_depedent_events_when_pa_and_not_pb(); break;
- case 4: calculate_probability_depedent_events_when_not_pa_and_pb(); break;
- case 5: calculate_probability_depedent_events_when_pa_pa_and_p_a_and_b(); break;
- case 6: calculate_probability_depedent_events_when_pb_and_p_a_and_b(); break;
- case 7: calculate_probability_depedent_events_when_pa_and_p_a_or_b(); break;
- case 8: calculate_probability_depedent_events_when_pb_and_p_a_or_b(); break;
- case 9: calculate_probability_depedent_events_when_pa_and_p_a_xor_b(); break;
- case 10: calculate_probability_depedent_events_when_pb_and_p_a_xor_b(); break;
- }
- }
- bool independent_pobability_input_is_valid(char *num) {
- for(int i = 0; i < strlen(num); i++) {
- if (isalpha(num[i])) return false;
- }
- return true;
- }
- void calculate_independent_probability_events(Probability *probabilities, int size) {
- double ocurrence_all = 1.0;
- double no_occurences = 1.0;
- double or_occurences = 0.0;
- double not_or_occurences = 0.0;
- cout << endl << "--------Calculating probabilities for independent events--------" << endl;
- for(int i = 0; i < size; i++) {
- Probability probability = probabilities[i];
- cout << "P(" << probability.name << ") = " << probability.value << endl;
- }
- for(int i = 0; i < size; i++) {
- Probability probability = probabilities[i];
- string postfix = i + 1 == size ? "" : " * ";
- cout << "P(" << probability.name << ")" << postfix;
- ocurrence_all *= probability.value;
- }
- cout << " = " << ocurrence_all << endl << endl;
- for(int i = 0; i < size; i++) {
- Probability probability = probabilities[i];
- string postfix = i + 1 == size ? "" : " * ";
- cout << "not(P(" << probability.name << "))" << postfix;
- no_occurences *= (1 - probability.value);
- }
- cout << " = " << no_occurences << endl << endl;
- for(int i = 0; i < size; i++) {
- Probability probability = probabilities[i];
- string postfix = i + 1 == size ? "" : " + ";
- cout << "P(" << probability.name << ")" << postfix;
- or_occurences += probability.value;
- }
- cout << " = " << or_occurences << endl << endl;
- for(int i = 0; i < size; i++) {
- Probability probability = probabilities[i];
- string postfix = i + 1 == size ? "" : " + ";
- cout << "not(P(" << probability.name << "))" << postfix;
- not_or_occurences += (1 - probability.value);
- }
- cout << " = " << not_or_occurences << endl << endl;
- }
- void probability_independent_events() {
- set<string> taken_name;
- Probability *probabilities = NULL;
- int probabilities_size = 0;
- cout << endl << "--------Entering probabilities--------" << endl;
- while(true) {
- char name[MAX_PROBABILITY_NAME_LENGTH];
- char value[INPUT_DATA_SIZE];
- int iteration = 0;
- while(true) {
- string additional = (probabilities_size > 0) ? "next" : "";
- cout << "Enter name for the " << additional << " probability(name's length is in range(1,3)) or enter -1 to exit: ";
- cin >> name;
- int name_len = strlen(name);
- if(taken_name.count(name)) {
- cout << "Name " << name << " is already taken, choose another one" << endl;
- continue;
- }
- if(name_len < 1 || name_len > 3) continue; else break;
- }
- if(strcmp(name, "-1") == 0) break;
- do
- {
- cout << endl << "Enter a value for P(" << name << ") probability(should be in range(0,1)): " << endl;
- cin >> value;
- } while (!independent_pobability_input_is_valid(value) || atof(value) < 0 || atof(value) > 1);
- Probability curr_probability;
- taken_name.insert(name);
- strcpy(curr_probability.name, name);
- curr_probability.value = atof(value);
- probabilities = (Probability*)realloc(probabilities, (probabilities_size + 1) * sizeof(Probability));
- probabilities[probabilities_size++] = curr_probability;
- }
- calculate_independent_probability_events(probabilities, probabilities_size);
- }
- void probability() {
- int way;
- do
- {
- cout << endl << "Actions \n1 - Probability of a Series of Independent Events \n2 - Probability for dependent Events \n0 - Exit: " << endl;
- cin >> way;
- switch(way) {
- case 0: break;
- case 1: probability_independent_events(); break;
- case 2: probability_depedent_events(); break;
- }
- if(way == 0) break;
- } while (way != 1 || way != 2 || way != 0);
- }
- int main() {
- int way;
- do
- {
- cout << endl << "1 - Dispersion" << endl;
- cout << "2 - Expectation" << endl;
- cout << "3 - Probability" << endl;
- cout << "0 - Exit" << endl;
- cout << endl << "Choose what to do: " << endl;
- cin >> way;
- if(way == 0) break;
- switch(way) {
- case 1: {
- try
- {
- dispersion();
- }
- catch(const int exception)
- {}
- break;
- }
- case 2: expectation(); break;
- case 3: probability(); break;
- default: cout << "Variant by " << way << " has not been found" << endl;
- }
- } while (way != 0 || way < 1 || way > 3);
- return 0;
- }
Advertisement
Advertisement