This program is a C++ implementation of a set. It consists of the
backbone, which is the set implementation itself, and the user interface,
which is a text-based expression parser for using the set operations.
The set implementation contains an Element class, which describes a
single element of a set. This can be either a number, and string, or
another set. Sets can contain any number or type of elements. For
example, a set can be {1, "hello", {4, 5}}, which is a set containing 3
elements.
The set is implemented using a vector of type Element. Elements are
inserted at the end of the set as long as they are not already in the set.
If the element is a set, it is inserted according to the element's
cardinality (for example, if you insert {1, 2} into the set {{1}, {1, 2,
3}}, the resulting set will be {{1}, {1, 2}, {1, 2, 3}}). This is done
merely to add organization to a set, especially in the case of a powerset.
All major set operations are implemented, including union,
intersection, difference, symmetric difference, and powerset. The powerset
algorithm is recursively handled through 3 cases: if the set has size N=0,
the result is the null set; if the set has size N=1, the result is the set
containing the null set and the set itself; if the set has size N>=2, all
combinations of subsets of size N-1 are assembled, and the powerset of each
subset is added to the result. The final result is the powerset.
Set tests are also included, such as subset, proper subset, equal, and
element exists in set. The subset test checks if set1 is a subset of set2.
The proper subset test uses the subset test to check if set1 is a subset of
set2, and then compares cardinality; if set1 and set2 have different sizes,
set1 is a proper subset. The equal test is done similarly, using the
subset test, then comparing cardinality; if set1 and set2 have the same
size, they are equal.
The interface allows the user access to all these operations through a
text based input system. The user runs the program, and can type set
operations, such as '{1, 2} + {3, 4}' to get the union of two sets.
Additional functionality is provided to make the interface simpler to
use. The user can create variables out of sets or elements, and use these
variables in whatever context requires a type of that kind. For example,
one creates a variable using the ":" operator, ie 'A : {1, 2}'. This
creates a variable of type set that is equal to {1, 2}. This variable
(case insensitive) can now be used wherever a set is expected, such as 'A +
{3, 4}' to take the union of the two sets. The same functionality applies
to elements ('X : 5' or 'Y: "hello"'). An assignment has high precedence
and can be used in any context where it's result can be used. For example,
you can do '{1, 2} + b:{3, 4} - c:{1, 6}' results in the union of {1,2} and
{3,4}, and the difference of that result and {1,6}, which is {2, 3, 4}. B
is also assigned with the set {3,4} and C with {1,6}.
Other operations accessible to the user include cardinality, using
'|set|', subset, using 'set1 <= set2', exists in set, using 'element [
set', and others. Set operations can result in other sets, or in numbers.
Operations such as union, intersection, difference, symmetric difference,
and powerset all result in new sets, and can be assigned to a variable or
used in any context requiring a set. Testing operations such as subset,
proper subset, equals, not equals, exists in set, not exists in set, all
result in numbers, 1 if true, 0 if false. These numbers are treated as
elements, and can be used as such (for example, you can assign the result
of a subset operation to a variable with 'a : ({1, 2} < {1, 2, 3})' - note
the need of parenthesis. The ':' operator has high precedence, and without
parenthesis the expression is '(a:{1,2}) < {1,2,3}').
The final operation is the cardinality operator, '|set|'. This
evaluates to a number, which again can be used in any context where a
number is expected (such as an element of a set). For example, the
statement 'A : |{1, 2} + {2, 3}|' will result in the variable A being
assigned the cardinality of the union of those two sets, which is 3.
Exact syntax for each of the set operations can be found in the USAGE
file, GRAMMAR file, or in the in-program help via the 'help' command.