/*********************************************************************** * prog2.cpp * * A simple program to implement user commands for manipulating * a basic binary tree * * Written by Paul Bonamy - 24 January 2010 * Rewritten in pure C - 30 March 2010 ************************************************************************/ #include // replaces iostream for I/O #include // we get isspace() from here #include "binarytree.h" /*********************************************************************** * int main() * * main function for the program. deals with accepting/parsing * user input and generally keeping things running * * no parameters / always returns 0 ***********************************************************************/ int main () { /* * note that because of the way we structured the tree functions * we can have as many trees as we want, rooted at different pointers */ BINTREE * tree = 0; char cmd; // this will hold our user command int value; // user-supplied value to insert /* * scanf takes a format string and pointers to variables * which will receive the values read in from the terminal */ scanf("%c", &cmd); // read in an intial command /* * scanf will return whatever character it finds, so we need some way * to weed out the whitespaces while looking for characters */ while(isspace(cmd)) { scanf("%c", &cmd); } while(cmd != 'q') { // dump out if we get a q switch (cmd) { // figure out what we're doing case 'i': // insert into tree // scanf is smart enough to know spaces aren't integers scanf("%d", &value); // note that the insert needs the address of the tree pointer bintree_insert(&tree, value); // do the insert break; case 'e': // empty the tree bintree_empty(&tree); // again we need the tree pointer break; case 'p': // print the tree bintree_print(tree); // and once more with the three pointer break; default: // here for giggles printf("Oi! That's not a legal command"); } scanf("%c", &cmd); // get the next command while(isspace(cmd)) { scanf("%c", &cmd); } } return 0; }