#include #include #include "CPstring.h" #include "guess.h" GuessGame::GuessGame() : myRoot(0) { // all initialized } GuessGame::~GuessGame() { DoDelete(myRoot); } void GuessGame::BuildTree(istream & input,TreeNode * & root) // precondition: input open for reading, properly formed // postcondition: root points to a tree formed from contents of input { string s; if (getline(input,s)) // input succeeded? { if (s.substr(0,3) == "#Q:") // add a question { root = new TreeNode(s.substr(3,s.length())); BuildTree(input,root->left); BuildTree(input,root->right); } else { root = new TreeNode(s); // add an answer } } else { cerr << "error: no input" << endl; root = 0; } } void GuessGame::ReadFile(const string & filename) // postcondition: reads file, builds tree from contents { ifstream input(filename); if (input.fail()) { cerr << "could not read from " << filename << endl; exit(1); } BuildTree(input,myRoot); }