#include <xtd/diagnostics/trace>
#include <xtd/diagnostics/default_trace_listener>
#include <xtd/console>
#include <xtd/startup>
class binomial {
public:
auto possibilities = .0l;
auto iter = .0l;
trace::listeners().erase(trace::listeners().begin());
auto default_listener = new_ptr<default_trace_listener>();
trace::listeners().push_back(default_listener);
default_listener->log_file_name(args[1]);
try {
const auto MAX_POSSIBILITIES = 99.0l;
possibilities = parse<decimal>(args[0]);
if (possibilities < 0 || possibilities > MAX_POSSIBILITIES)
throw new system_exception(string::format(
"The number of possibilities must be in the range 0..{}.", MAX_POSSIBILITIES));
auto fail_message = string::format("\"{}\" is not a valid number of possibilities.", args[0]);
default_listener->fail(fail_message, ex.
message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message +
"\n" + ex.
message());
return;
}
} else {
const auto ENTER_PARAM = "Enter the number of possibilities as a command line argument.";
default_listener->fail(ENTER_PARAM);
if (!default_listener->assert_ui_enabled())
console::write_line(ENTER_PARAM);
return;
}
for (iter = 0; iter <= possibilities; iter++) {
auto result = .0l;
auto binomial = string::empty_string;
try {
result = calc_binomial(possibilities, iter);
auto fail_message = string::format("An exception was raised when calculating Binomial( {}, {} ).", possibilities, iter);
default_listener->fail(fail_message, ex.
message());
if (!default_listener->assert_ui_enabled())
console::write_line(fail_message +
"\n" + ex.
message());
return;
}
binomial = string::format("Binomial( {0}, {1} ) = ", possibilities, iter);
default_listener->write(binomial);
default_listener->write_line(string::format("{}", result));
console::write_line("{0} {1}", binomial, result);
}
}
auto result = 1.0l;
auto iter = .0l;
for (iter = 1.0l; iter <= possibilities - outcomes; iter++) {
result *= outcomes + iter;
result /= iter;
}
return result;
}
};
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.h:71
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::collections::generic::list::...
Definition list.h:364
virtual const xtd::string & message() const noexcept
Gets message associate to the exception.
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.h:23
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.h:15
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition assert_dialog_result.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10