102using namespace Aleph;
111 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
112 cout <<
"| " << left << setw(68) << title <<
" |" <<
endl;
113 cout <<
"+" << string(70,
'-') <<
"+" <<
endl;
131 double add(
double a,
double b) {
return a + b; }
132 double sub(
double a,
double b) {
return a - b; }
133 double mul(
double a,
double b) {
return a * b; }
134 double div(
double a,
double b)
136 if (b == 0)
throw runtime_error(
"Division by zero");
139 double mod(
double a,
double b)
141 return static_cast<int>(a) %
static_cast<int>(b);
143 double pow(
double a,
double b) {
return std::pow(a, b); }
148 print_header(
"Example 1: Calculator with Function Pointers");
162 cout <<
"\n Registered operations: + - * / % ^\n" <<
endl;
165 struct TestCase {
double a;
char op;
double b; };
168 tests.append({10,
'-', 3});
169 tests.append({7,
'*', 8});
170 tests.append({100,
'/', 4});
171 tests.append({17,
'%', 5});
172 tests.append({2,
'^', 10});
174 cout <<
" Expression Result" <<
endl;
175 cout <<
" " << string(35,
'-') <<
endl;
177 for (
size_t i = 0; i <
tests.size(); ++i)
180 if (
calc.valid_key(t.op))
182 double result =
calc.run(t.op, t.a, t.b);
183 cout <<
" " << setw(6) << t.a <<
" " << t.op <<
" "
184 << setw(6) << t.b <<
" = " << setw(10) << result <<
endl;
189 cout <<
"\n Checking for unregistered operation '!':" <<
endl;
190 cout <<
" valid_key('!') = " << (
calc.valid_key(
'!') ?
"true" :
"false") <<
endl;
207 for (
char& c : result) c =
toupper(c);
211 processor.insert(
"lower", [](
const string& s) {
213 for (
char& c : result) c =
tolower(c);
217 processor.insert(
"reverse", [](
const string& s) {
218 return string(s.rbegin(), s.rend());
221 processor.insert(
"length", [](
const string& s) {
222 return to_string(s.length()) +
" characters";
225 processor.insert(
"words", [](
const string& s) {
235 processor.insert(
"vowels", [](
const string& s) {
247 string text =
"Colombia es un pais de gente trabajadora";
249 cout <<
"\n Original text: \"" <<
text <<
"\"\n" <<
endl;
251 cout <<
" Transformation Result" <<
endl;
252 cout <<
" " << string(55,
'-') <<
endl;
262 for (
size_t i = 0; i <
operations.size(); ++i)
266 cout <<
" " << left << setw(18) << op << result <<
endl;
270 cout <<
"\n Available operations: ";
272 for (
auto it =
keys.get_it(); it.has_curr(); it.next_ne())
273 cout << it.get_curr() <<
" ";
283 print_header(
"Example 3: Colombian Regions Information System");
289 cout <<
"\n REGION ANDINA" <<
endl;
290 cout <<
" Capital: Bogota" <<
endl;
291 cout <<
" Departments: Cundinamarca, Boyaca, Santander, Antioquia..." <<
endl;
292 cout <<
" Climate: Temperate to cold (varies with altitude)" <<
endl;
293 cout <<
" Products: Coffee, flowers, potatoes, emeralds" <<
endl;
296 regions.insert(
"caribe", []() {
297 cout <<
"\n REGION CARIBE" <<
endl;
298 cout <<
" Major cities: Barranquilla, Cartagena, Santa Marta" <<
endl;
299 cout <<
" Departments: Atlantico, Bolivar, Magdalena, La Guajira..." <<
endl;
300 cout <<
" Climate: Tropical hot" <<
endl;
301 cout <<
" Products: Bananas, coal, tourism, fishing" <<
endl;
304 regions.insert(
"pacifica", []() {
305 cout <<
"\n REGION PACIFICA" <<
endl;
306 cout <<
" Major cities: Cali, Buenaventura, Quibdo" <<
endl;
307 cout <<
" Departments: Valle del Cauca, Choco, Narino, Cauca" <<
endl;
308 cout <<
" Climate: Very humid tropical" <<
endl;
309 cout <<
" Products: Sugar cane, timber, gold, platinum" <<
endl;
312 regions.insert(
"orinoquia", []() {
313 cout <<
"\n REGION ORINOQUIA (Los Llanos)" <<
endl;
314 cout <<
" Major cities: Villavicencio, Yopal" <<
endl;
315 cout <<
" Departments: Meta, Casanare, Arauca, Vichada" <<
endl;
316 cout <<
" Climate: Tropical with dry season" <<
endl;
317 cout <<
" Products: Cattle, oil, rice, palm oil" <<
endl;
320 regions.insert(
"amazonia", []() {
321 cout <<
"\n REGION AMAZONIA" <<
endl;
322 cout <<
" Major cities: Leticia, Florencia" <<
endl;
323 cout <<
" Departments: Amazonas, Caqueta, Putumayo, Guaviare" <<
endl;
324 cout <<
" Climate: Humid equatorial" <<
endl;
325 cout <<
" Products: Timber, rubber, ecotourism, biodiversity" <<
endl;
328 regions.insert(
"insular", []() {
329 cout <<
"\n REGION INSULAR" <<
endl;
330 cout <<
" Islands: San Andres, Providencia, Santa Catalina" <<
endl;
331 cout <<
" Location: Caribbean Sea" <<
endl;
332 cout <<
" Climate: Tropical maritime" <<
endl;
333 cout <<
" Products: Tourism, coconut, fishing" <<
endl;
345 cout <<
"\n Colombia's Natural Regions:" <<
endl;
346 cout <<
" " << string(40,
'=') <<
endl;
358 print_header(
"Example 4: Order Processing State Machine");
369 history +=
" -> " +
action +
" (from " + state +
")\n";
373 Order order = {1001,
"created",
"Juan Perez", 250000.0,
""};
380 if (
o.state !=
"created") {
381 cout <<
" [ERROR] Cannot confirm - order not in 'created' state" << endl;
385 o.state =
"confirmed";
386 cout <<
" [OK] Order " <<
o.id <<
" confirmed" <<
endl;
390 if (
o.state !=
"confirmed") {
391 cout <<
" [ERROR] Cannot pay - order not confirmed" <<
endl;
396 cout <<
" [OK] Payment of $" << fixed << setprecision(0) <<
o.amount
397 <<
" COP received" <<
endl;
401 if (
o.state !=
"paid") {
402 cout <<
" [ERROR] Cannot ship - order not paid" <<
endl;
407 cout <<
" [OK] Order shipped to " <<
o.customer <<
endl;
411 if (
o.state !=
"shipped") {
412 cout <<
" [ERROR] Cannot deliver - order not shipped" <<
endl;
416 o.state =
"delivered";
417 cout <<
" [OK] Order delivered successfully!" <<
endl;
421 if (
o.state ==
"delivered") {
422 cout <<
" [ERROR] Cannot cancel delivered order" <<
endl;
426 o.state =
"cancelled";
427 cout <<
" [OK] Order cancelled" <<
endl;
431 cout <<
"\n Order #" << order.id <<
" - Customer: " << order.customer <<
endl;
432 cout <<
" Amount: $" << fixed << setprecision(0) << order.amount <<
" COP" <<
endl;
433 cout <<
"\n Processing order:" <<
endl;
434 cout <<
" " << string(40,
'-') <<
endl;
443 cout <<
"\n Attempting invalid transition:" <<
endl;
447 cout <<
"\n Order History:" <<
endl;
448 cout << order.history;
449 cout <<
" Final state: " << order.state <<
endl;
458 print_header(
"Example 5: Hash-based Dispatcher Performance");
473 metrics.insert(
"inflation_adjust", [](
double value) {
483 if (
income < 4500000)
return 0.0;
484 if (
income < 10000000)
return (
income - 4500000) * 0.19;
485 if (
income < 25000000)
return (
income - 10000000) * 0.28 + 1045000;
486 return (
income - 25000000) * 0.33 + 5245000;
489 cout <<
"\n Colombian Economic Calculations:" <<
endl;
490 cout <<
" " << string(50,
'-') <<
endl;
494 cout <<
"\n Currency Conversion:" <<
endl;
495 cout <<
" $" <<
usd_amount <<
" USD = $" << fixed << setprecision(0)
499 cout <<
" $" <<
cop_amount <<
" COP = $" << setprecision(2)
503 cout <<
"\n Price Calculations:" <<
endl;
505 cout <<
" Base price: $" << setprecision(0) <<
base_price <<
" COP" <<
endl;
511 cout <<
"\n Monthly Income Tax Examples:" <<
endl;
518 for (
size_t i = 0; i <
incomes.size(); ++i)
522 cout <<
" Income $" << setw(12) <<
income <<
" COP -> Tax: $"
523 << setw(10) <<
tax <<
" COP" <<
endl;
533 print_header(
"Example 6: Variadic Arguments Dispatcher");
541 for (
size_t i = 0; i <
args.size(); ++i) {
542 if (i > 0) result +=
" ";
550 for (
size_t i = 0; i <
args.size(); ++i)
556 return args.size() > 0 ?
args(0) :
"";
570 cities.append(
"Medellin");
572 cities.append(
"Barranquilla");
573 cities.append(
"Cartagena");
575 cout <<
"\n Arguments: Bogota, Medellin, Cali, Barranquilla, Cartagena\n" <<
endl;
577 cout <<
" Operation Result" <<
endl;
578 cout <<
" " << string(50,
'-') <<
endl;
594 cout <<
"========================================================================" <<
endl;
595 cout <<
" ALEPH-W DISPATCHER EXAMPLE" <<
endl;
596 cout <<
" Dynamic Command Dispatching" <<
endl;
597 cout <<
"========================================================================" <<
endl;
607 cout <<
"========================================================================" <<
endl;
608 cout <<
" Example completed successfully!" <<
endl;
609 cout <<
"========================================================================" <<
endl;
Command dispatcher pattern implementation.
Tree-based command dispatcher.
void insert(const Key &key, Operation op)
Register a key-operation pair.
Hash-based command dispatcher.
void insert(const Key &key, Operation op)
Register a key-operation pair.
T & append()
Allocate a new entry to the end of array.
void demo_state_machine()
void demo_hash_dispatcher()
void demo_variadic_dispatcher()
void demo_text_processor()
void print_subheader(const string &subtitle)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
std::string toupper(const char *str)
Convert a C std::string to upper-case.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Basic arithmetic operations for the calculator.
double sub(double a, double b)
double add(double a, double b)
double div(double a, double b)
double mul(double a, double b)
double pow(double a, double b)
double mod(double a, double b)
Lazy and scalable dynamic array implementation.