C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_riddle.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Logical Deduction Riddle *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
/*
21
22
The Broken Window Riddle
23
24
A window has been broken, there are 5 suspects (Annie, Betsy, Chloe,
25
Tori and Zoey). Each suspect is asked who they think broke the window.
26
27
The following is each suspect's statement:
28
29
Tori : It wasn't Zoey, It was Annie
30
Annie: It wasn't Betsy, It wasn't Zoey
31
Betsy: It was Zoey, It wasn't Tori
32
Chloe: It was Betsy, It was Annie
33
Zoey : It was Chloe, It wasn't Tori
34
35
Given each of the suspects spoke only one lie and one truth, and only
36
one person is responsible for the broken window:
37
38
Who then was it that broke the window?
39
40
*/
41
42
#include <string>
43
44
#include "
exprtk.hpp
"
45
46
47
template
<
typename
T>
48
void
logical_deducation_riddle
()
49
{
50
typedef
exprtk::symbol_table<T>
symbol_table_t;
51
typedef
exprtk::expression<T>
expression_t;
52
typedef
exprtk::parser<T>
parser_t;
53
typedef
exprtk::function_compositor<T>
compositor_t;
54
typedef
typename
compositor_t::function function_t;
55
56
symbol_table_t symbol_table;
57
58
exprtk::rtl::io::println<T>
println;
59
60
symbol_table.add_function(
"println"
, println);
61
62
compositor_t compositor(symbol_table);
63
64
compositor.add(function_t()
65
.name(
"constraint"
)
66
.var(
"person_x"
).var(
"person_y"
)
67
.expression
68
(
69
" person_x xor person_y; "
70
));
71
72
const
std::string logical_deducation_riddle_program =
73
" const var number_of_culprits := 1; "
74
" "
75
" for (var Tori := false; Tori <= true; Tori += true) "
76
" { "
77
" for (var Annie := false; Annie <= true; Annie += true) "
78
" { "
79
" for (var Betsy := false; Betsy <= true; Betsy += true) "
80
" { "
81
" for (var Chloe := false; Chloe <= true; Chloe += true) "
82
" { "
83
" for (var Zoey := false; Zoey <= true; Zoey += true) "
84
" { "
85
" if (sum(Annie, Betsy, Chloe, Tori, Zoey) != number_of_culprits) "
86
" continue; "
87
" "
88
" var solution := "
89
" constraint( not(Zoey) , Annie ) and /* Tori */ "
90
" constraint( not(Betsy) , not(Zoey) ) and /* Annie */ "
91
" constraint( Zoey , not(Tori) ) and /* Betsy */ "
92
" constraint( Betsy , Annie ) and /* Chloe */ "
93
" constraint( Chloe , not(Tori) ) ; /* Zoey */ "
94
" "
95
" if (solution == true) "
96
" { "
97
" var culprit := ''; "
98
" [*] "
99
" { "
100
" case Annie : culprit := 'Annie'; "
101
" case Betsy : culprit := 'Betsy'; "
102
" case Chloe : culprit := 'Chloe'; "
103
" case Tori : culprit := 'Tori' ; "
104
" case Zoey : culprit := 'Zoey' ; "
105
" }; "
106
" "
107
" println(culprit,' broke the window!'); "
108
" } "
109
" } "
110
" } "
111
" } "
112
" } "
113
" } "
114
" "
;
115
116
expression_t expression;
117
expression.register_symbol_table(symbol_table);
118
119
parser_t parser;
120
parser.compile(logical_deducation_riddle_program,expression);
121
122
expression.value();
123
}
124
125
int
main
()
126
{
127
logical_deducation_riddle<double>
();
128
return
0;
129
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
logical_deducation_riddle
void logical_deducation_riddle()
Definition
exprtk_riddle.cpp:48
main
int main()
Definition
exprtk_riddle.cpp:125
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
Generated by
1.17.0