[ fromfile: ex-generics.xml id: ex-generics ]
This exercise requires you to come up with data structures that keep track of relationships between symbols.
Our relationship is a boolean operator frop on a set S of symbols that has two properties:
Reflexivity – For any symbol s
in S,
s frop s is always true.
Symmetry – For any symbols s
and t
in S,
ifs
fropt
is true, thent
frops
is also true.
frop is similar to the boolean operator is-friends-with between people on a social networking site.
is-friends-with is not transitive; you do not automatically become friends with all your friends' friends.[56]
Write a program that repeatedly asks the user to enter relationships or commands, and keeps track of relationships between all pairs of symbols that it sees. The interface should be pretty simple: The user enters a string that is parsed and processed by the dispatching function, processLine()
. In other words, processLine()
should expect strings of the following forms.
To add a frop between two strings: string1=string2
To list the friends of string1
: string1
(no = symbol in the line)
Add another function, takeback(int n)
, where n refers to the nth assertion. If the nth assertion added a frop, the function should "undo" that assertion, ensuring that relational integrity is kept. After this, show the updated friend lists of both symbols (string1 and string2) involved.
Have the processLine()
function scan for messages of the form "takeback n" and call the takeback()
function in response.
[56] Transitivity is a third property of boolean operators that we are not assuming – for any symbols s, t,
and u
in S, if s
op t
and t
op u
are both true, then s
op u
is also true.
If a boolean operator op is Reflexive, Symmetric, and Transitive, then it is an Equivalence Relation.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |