Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need a program that will guess the animal. It needs to open the first ques

ID: 3702624 • Letter: H

Question

Hi, I need a program that will guess the animal. It needs to open the first question from a file and the subchilds from the question. It is C++ language and these are the steps:

Write a program that plays with the user the game commonly known as “Twenty Questions”. However, in this version of the game, the program will not be limited to asking twenty questions.  Instead, it may ask any number of questions. Also the program will learn and improve from playing each game.

Is it a non-living object?

Elephant

if not:

Stone

If the user chooses that it is not a living object, it will display that if the anime guessed is a stone. If not the program will save the question and the answer.

Explanation / Answer

Source Code

#ifndef BINARY_TREE_H
#define BINARY_TREE_H

#include <fstream>
#include <string>

class binaryTree {
public:
        class node;
        binaryTree();
        void addRoot(const std::string &data);
        void addLeft(node *nd, const std::string &data);
        void addRight(node *nd, const std::string &data);

        node *getRoot();

        std::string get(node *node);
        bool isEmpty();

        void print(std::ostream &os=std::cout);
        static binaryTree read(const char *fname);
        void write(const char *fname);
private:
        void print(std::ostream &os, node *nd, int indent);
        static node *read(std::ifstream &ifs);
        void write(std::ofstream &ofs, node *nd, int depthh);
        node *root;
};

struct binaryTree::node {
        node(const std::string &data);
        std::string data;
        node *l, *r;
};

#endif


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include "binaryTree.h"

using namespace std;

binaryTree::binaryTree() : root(0) {}

void binaryTree::addRoot(const string &data) {root = new node(data);}

void binaryTree::addLeft(node *nd, const string &data) {
    if (nd == 0) throw "Attempt addLeft on null";
    if (nd->l != 0) throw "Attempt addLeft on nd with existing l child";
    nd->l = new node(data);
}

void binaryTree::addRight(node *nd, const string &data) {
    if (nd == 0) throw "Attempt addRight on null";
    if (nd->r != 0) throw "Attempt addRight on nd with existing r child";
    nd->r = new node(data);
}

typename binaryTree::node *binaryTree::getRoot() {return root;}

bool binaryTree::isEmpty() {return root == 0;}

void binaryTree::print(std::ostream &os) {print(os, root, 0);}

void binaryTree::print(std::ostream &os, node *nd, int indent) {
    if (nd == 0) return;
    os << string(indent*3, ' ') << nd->data << endl;
    print(os, nd->l, indent+1);
    print(os, nd->r, indent+1);
}

binaryTree binaryTree::read(const char *fname) {
    binaryTree tree;
    std::ifstream ifs(fname);

    if (!ifs) {
        std::cerr << "*** Warning " << fname << " not found -- starting with new tree" << std::endl;
        return tree;
    }
    tree.root = read(ifs);
    return tree;
}

typename binaryTree::node *binaryTree::read(std::ifstream &ifs) {
    string line;
    if (getline(ifs, line))
        if (line == "")
            return 0;
        else {
            int pos = line.find_first_not_of(' ');
            if (pos != string::npos)
                line = line.substr(pos);
            node *nd = new node(line);
            nd->l = read(ifs);
            nd->r = read(ifs);
            return nd;
        }
}

void binaryTree::write(const char *fname) {
    ofstream ofs(fname);

    if (!ofs) {
                ostringstream oss;
                oss << fname << " could not be opened for output";
                throw oss.str();
        return;
    }

    write(ofs, root, 0);
    ofs.close();
}

void binaryTree::write(ofstream &ofs, node *nd, int depthh) {
    if (nd == 0) {
        ofs << "" << endl;
        return;
    }
    for (int i = 0; i < depthh; i++)
        ofs << ' ';
    ofs << nd->data << endl;
    write(ofs, nd->l, depthh+1);
    write(ofs, nd->r, depthh+1);
}

binaryTree::node::node(const string &data) : data(data), l(0), r(0) {}

Any help needed or did i missed anything or any requirement please do comment i will help you. Thank You.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote