Who knows what will happen with this? I have some code that compiles
and seems to construct the tree. I did not mention this in the comments
of the code, but a good intermediate step that I intend to pursue next
is to traverse the tree and demonstrate that it is indeed constructed
correctly by outputting the node values. The code is attached. If it
gets any bigger I will start posting it to my web site. I don't feel
too bad about doing this here at least to get something started. I hope
it will improve the programming skills of the list participants and thus
make them more valuable linux community members.
Good Night!
Steve
/* Program : Weylix (In honor of Hermann Weyl)
Keeper of the Code: Steven T. Hatton
Version : 0.0
Project initiation: April 17, 1999
Last update : April 21, 1999
License : GPL
Language : C++ (currently uses minimal "++" functionality)
Purpose(s) : This project was begun in as a learning exercise. It is
hoped the following will be gained form its development:
The program is currently capable of parsing a basic symbolic logic statement
and storing the statement in a binary tree formed of instances of a recursive
data structure.
Short term programming goals:
1) Write a function capable of traversing the tree and evaluating each node
according to the rules of symbolic logic. The set of variables should be
evaluated for each possible combination. For example if a statement were
input with variables 'A', 'B', 'C' then the program should traverse the tree
for each of the following:
A | B | C
------
1 | 1 | 1
1 | 1 | 0
1 | 0 | 1
1 | 0 | 0
0 | 1 | 1
0 | 1 | 0
0 | 0 | 1
0 | 0 | 0
The program should output the results in a truth table form.
2) Allow for the use of parentheses. This should be done by modifying the
MakeTree function so that it calls itself and returns a pointer to the root
of the tree resulting from evaluating the contents of the parentheses.
3) Verify that all possible statements ( currently limited to 26 variables
['A'..'Z'], and the operators "and" and "or") can be correctly evaluated.
4) Add a negation operator.
5) Better document the source.
Medium range goals:
1) Using either gtk or qt, create a GUI that will accept user input, and
display both the truth table and a graphical representation of the binary
tree.
2) Extend this GUI so that is will also display a Ven diagram of the symbolic
logic statement.
Long range goals:
1) Extend the program so that it can handle more sophisticated concepts such
as predicate algebra.
2) Further extend the program so that It can be used as a heuristic tool to
represent basic concepts axiomatic set theory.
3) Continue to extend the program to explore deeper aspect of abstract algebra.
It is hoped the participants will:
1) improve our programming skills,
2) learn how to manage a collective project of this nature,
3) gain a better understanding of foundational mathematics,
4) collectively produce a heuristic program that will enable others to learn
these lessons from reading the code and using the program.
5) remove ourselves from the ranks of the "low-lifes who can't write code."
*/
#include <stdio.h>
#include <stream.h>
#include <strings.h>
struct node{
char* token;
node* p;
node* lc;
node* rc;
};
node* MakeNode(char*);
node* MakeTree(int argc, char* argv[]);
main(int argc, char* argv[])
{
int i=1;
node* root, current;
root = MakeTree(argc, argv);
}
node* MakeTree(int argc, char* argv[]){
int i=1;
node* curr;
node* root;
node* sr;
curr=root=sr=MakeNode(argv[i++]);
for (i; i<argc; i++){
curr=MakeNode(argv[i]);
if (isupper(curr->token[0]) && curr->token[1]=='\0'){// token is a variable
sr->rc=curr;
curr->p=sr;
if (!strcmp(sr->token,"or")){
sr=sr->rc;
}
cout << "variable: ";
}
else if (!strcmp(curr->token,"and")){
curr->lc=sr;
sr->p=curr;
sr=curr;
if(sr->lc == root){
root = sr;
}
else{
root->rc = sr;
sr->lc->p=sr;
}
cout << "operator is: ";
}
else if(!strcmp(curr->token,"or")){
curr->lc = root;
root->p=curr;
sr = curr;
if (sr->lc == root){
root = sr;
}
cout << "operator is or: ";
}
else{
cout << "invalid input: ";
}
cout << argv[i] << '\n';
}
}
node* MakeNode(char* token)
{
node* pointer;
pointer = new (node);
pointer->p=NULL;
pointer->lc=NULL;
pointer->rc=NULL;
pointer->token=token;
return pointer;
}