/***************************************************************************** We run the program by calling something along the lines of: guessMushroom -train mushrooms.data -test moreMushrooms.data -out mushrooms.out ******************************************************************************/ int main(int argc, char* argv[]) { MushroomP trainingShroomList; trainingShroomList = NULL; MushroomP testingShroomList; testingShroomList = NULL; ValueP availableValues; DecisionP decisionTree; availableValues = NULL; decisionTree = NULL; char infilenameTraining[STRLEN]; /* input file name for training data */ char infilenameTesting[STRLEN]; /* input file name for testing data */ char outfilename[STRLEN]; /* output file name for printing the results */ printf( "This program processes data about the attributes mushrooms.\n" ); /* initialize filenames to dummy strings */ strcpy( infilenameTraining, "none" ); strcpy( infilenameTesting, "none" ); strcpy( outfilename, "none" ); /* TAKEN FROM p5.c EXAMLPE process command-line arguments */ while( *++argv ) { /* while there are still command-line args */ if ( !strcmp( *argv, "-train" ) ) /* set input file name */ strcpy( infilenameTraining, *++argv ); else if( !strcmp( *argv,"-test" ) ) /* set input file name */ strcpy( infilenameTesting, *++argv ); else if( !strcmp( *argv,"-out" ) ) /* set output file name */ strcpy( outfilename,*++argv ); else { /* error checking */ printf( "\nERROR: option %s not recognized\n", *argv ); printf( "...exiting program\n\n" ); exit( 0 ); } } /* end, while there are still command-line args */ /* this is the training data */ trainingShroomList = readMushrooms( infilenameTraining ); /* this is the testing data */ testingShroomList = readMushrooms( infilenameTesting ); availableValues = findAvailableValues( 'e', 0, trainingShroomList ); decisionTree = createDecisionTree( 'e', 0, trainingShroomList, availableValues ); printDecisionTree( decisionTree, outfilename ); printTestResults( 'e', 0, decisionTree, testingShroomList, outfilename ); /* free the list of test mushrooms */ freeMushrooms( trainingShroomList ); freeMushrooms( testingShroomList ); /* note that Decision structs are freed in printDecisionTree */ /* free value list */ freeValues( availableValues ); return (0); } /* end main */
/* * Recursively prints the decision tree * For debugging purposes only */ void printDecisionTree(node* nodePtr) { if (nodePtr == NULL) { return; } if (!nodePtr->children.empty()) { cout << " Value: " << nodePtr->label << endl; cout << "Split on: " << nodePtr->splitOn; int iii; for (iii = 0; iii < nodePtr->children.size(); iii++) { cout << "\t"; printDecisionTree(nodePtr->children[iii]); } return; } else { cout << "Predicted class = " << nodePtr->label; return; } }