943,964 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1281
  • C RSS
May 3rd, 2006
0

need help in animal game guessing

Expand Post »
i having a problem on how 2 save the animal name each time i create a new animal...can any1 please help me solve this problem on how 2 do the save function to save the animal name that i had created...thank you very much


  1. #include <cstddef>
  2. #include <cassert>
  3. typedef char* TreeType;
  4. typedef void(*FunctionType)(TreeType& anItem);
  5. class TreeNode
  6. {
  7. private:
  8. TreeNode(){};
  9. TreeNode(const TreeType& node, TreeNode *left=NULL, TreeNode *right=NULL):
  10. item(node), lftPtr(left), rtPtr(right){}
  11. TreeType item;
  12. TreeNode *ancestor;//pointer to ancestor
  13. TreeNode *lftPtr;//pointer to left child
  14. TreeNode *rtPtr;//pointer to right child
  15. friend class BinaryTree;//allow BinaryTree access to these pointers
  16. };
  17. class BinaryTree
  18. {
  19. public:
  20. //basic tree functions
  21. BinaryTree();
  22. BinaryTree(const TreeType& rootItem);
  23. BinaryTree(const BinaryTree& tree);
  24. virtual ~BinaryTree();
  25. virtual bool isEmpty()const;
  26. virtual TreeType getRootData()const;
  27. virtual void setRootData(const TreeType& newItem);
  28. virtual void attachLeft(const TreeType& newItem);
  29. virtual void attachRight(const TreeType& newItem);
  30. virtual void preTrav(FunctionType visit);
  31. virtual void inTrav(FunctionType visit);
  32. virtual void postTrav(FunctionType visit);
  33. virtual BinaryTree& operator=(const BinaryTree& rhs);
  34. //iterator functions
  35. virtual void moveLeft();
  36. virtual void moveRight();
  37. virtual void moveBack();
  38. virtual void getTravData(TreeType& item);
  39. virtual void setTravData(TreeType& item);
  40. virtual void moveRoot();
  41. virtual bool lftNull();
  42. virtual bool rtNull();
  43. protected:
  44. //constructors and internal functions
  45. BinaryTree(TreeNode *ptr);
  46. void CopyTree(TreeNode *treePtr, TreeNode *& newTreePtr)const;
  47. void DestroyTree(TreeNode *& treePtr);
  48. TreeNode *rootPtr()const;
  49. void setRootPtr(TreeNode *newRoot);
  50. void getChildPtrs(TreeNode *nodePtr, TreeNode *& lftPtr, TreeNode *& rtPtr)const;
  51. void setChildPtrs(TreeNode *nodePtr, TreeNode *& lftPtr, TreeNode *& rtPtr);
  52. void preorder(TreeNode *treePtr, FunctionType visit);
  53. void inorder(TreeNode *treePtr, FunctionType visit);
  54. void postorder(TreeNode *treePtr, FunctionType visit);
  55. private:
  56. TreeNode *root;//root pointer
  57. TreeNode *trav;//iterator
  58.  
  59. };
  60. /***********************************************
  61. begin class implementation of BinaryTree class
  62. ***********************************************/
  63. /***********************************************
  64. default constructor
  65. ***********************************************/
  66. BinaryTree::BinaryTree(): root(NULL),trav(NULL){}
  67. /***********************************************
  68. constructor for setting the root item
  69. ***********************************************/
  70. BinaryTree::BinaryTree(const TreeType& rootItem)
  71. {
  72. root = new TreeNode(rootItem, NULL, NULL);
  73. assert(root != NULL);
  74. trav = root;
  75. }
  76. /***********************************************
  77. copy constructor
  78. ***********************************************/
  79. BinaryTree::BinaryTree(const BinaryTree& tree)
  80. {
  81. CopyTree(tree.root, root);
  82. }
  83. /***********************************************
  84. constructor for basing this tree on another
  85. ***********************************************/
  86. BinaryTree::BinaryTree(TreeNode *ptr):root(ptr)
  87. {
  88. trav = root;
  89. }
  90. /***********************************************
  91. destructor
  92. ***********************************************/
  93. BinaryTree::~BinaryTree()
  94. {
  95. DestroyTree(root);
  96. }
  97. /***********************************************
  98. checks for empty tree state
  99. ***********************************************/
  100. bool BinaryTree::isEmpty()const
  101. {
  102. return (root==NULL);
  103. }
  104. /***********************************************
  105. returns root data item
  106. ***********************************************/
  107. TreeType BinaryTree::getRootData()const
  108. {
  109. if(!isEmpty())
  110. {
  111. return root->item;
  112. }
  113. else
  114. {
  115. return 0;
  116. }
  117. }
  118. /***********************************************
  119. sets root data
  120. ***********************************************/
  121. void BinaryTree::setRootData(const TreeType& newItem)
  122. {
  123. if(!isEmpty())
  124. {
  125. root->item = newItem;
  126. }
  127. else
  128. {
  129. root = new TreeNode(newItem, NULL, NULL);
  130. trav = root;
  131. }
  132. }
  133. /***********************************************
  134. attaches item to left pointer
  135. ***********************************************/
  136. void BinaryTree::attachLeft(const TreeType& newItem)
  137. {
  138. TreeNode *temp;
  139. if(!isEmpty() && trav->lftPtr == NULL)
  140. {
  141. temp = trav;//save trav's position
  142. trav->lftPtr = new TreeNode(newItem, NULL, NULL);//create new node
  143. trav = trav->lftPtr;//move to new node
  144. trav->ancestor = temp;//set new nodes ancestor pointer
  145. trav = temp;//reset position
  146. }
  147. temp = NULL;
  148. }
  149. /***********************************************
  150. attaches item to right pointer
  151. ***********************************************/
  152. void BinaryTree::attachRight(const TreeType& newItem)
  153. {
  154. TreeNode *temp;
  155. if(!isEmpty() && trav->rtPtr == NULL)
  156. {
  157. temp = trav;//save trav's position
  158. trav->rtPtr = new TreeNode(newItem, NULL, NULL);//create new node
  159. trav=trav->rtPtr;//move to new node
  160. trav->ancestor = temp;//set new nodes ancestor pointer
  161. trav = temp;//reset position
  162. }
  163. temp = NULL;
  164. }
  165. /***********************************************
  166. preorder traversal
  167. ***********************************************/
  168. void BinaryTree::preTrav(FunctionType visit)
  169. {
  170. preorder(root, visit);
  171. }
  172. /***********************************************
  173. inorder traversal
  174. ***********************************************/
  175. void BinaryTree::inTrav(FunctionType visit)
  176. {
  177. inorder(root,visit);
  178. }
  179. /***********************************************
  180. postorder traversal
  181. ***********************************************/
  182. void BinaryTree::postTrav(FunctionType visit)
  183. {
  184. postorder(root, visit);
  185. }
  186. /***********************************************
  187. overloaded = operator for assigments
  188. ***********************************************/
  189. BinaryTree& BinaryTree::operator = (const BinaryTree& rhs)
  190. {
  191. if(this != &rhs)
  192. {
  193. DestroyTree(root);
  194. CopyTree(rhs.root, root);
  195. }
  196. return *this;
  197. }
  198. /***********************************************
  199. actual copytree function that does a deep copy
  200. ***********************************************/
  201. void BinaryTree::CopyTree(TreeNode *treePtr,TreeNode *&newTreePtr)const
  202. {
  203. if(treePtr != NULL)
  204. {
  205. newTreePtr = new TreeNode(treePtr->item, NULL, NULL);
  206. CopyTree(treePtr->lftPtr, newTreePtr->lftPtr);
  207. CopyTree(treePtr->rtPtr, newTreePtr->rtPtr);
  208. }
  209. else
  210. {
  211. newTreePtr = NULL;
  212. }
  213. }
  214. /***********************************************
  215. destructor function that destroys a tree
  216. ***********************************************/
  217. void BinaryTree::DestroyTree(TreeNode *&treePtr)
  218. {
  219. if(treePtr != NULL)
  220. {
  221. DestroyTree(treePtr->lftPtr);
  222. DestroyTree(treePtr->rtPtr );
  223. delete treePtr;
  224. treePtr = NULL;
  225. }
  226. }
  227. /***********************************************
  228. returns root pointer
  229. ***********************************************/
  230. TreeNode *BinaryTree::rootPtr() const
  231. {
  232. return root;
  233. }
  234. /***********************************************
  235. sets root pointer
  236. ***********************************************/
  237. void BinaryTree::setRootPtr(TreeNode *newRoot)
  238. {
  239. root = newRoot;
  240. }
  241. /***********************************************
  242. returns child pointers of a node
  243. ***********************************************/
  244. void BinaryTree::getChildPtrs(TreeNode *nodePtr,TreeNode *& lftPtr,TreeNode *& rtPtr)const
  245. {
  246. lftPtr = nodePtr->lftPtr;
  247. rtPtr = nodePtr->rtPtr;
  248. }
  249. /***********************************************
  250. sets child pointers of a node
  251. ***********************************************/
  252. void BinaryTree::setChildPtrs(TreeNode *nodePtr,TreeNode *& lftPtr,TreeNode *& rtPtr)
  253. {
  254. nodePtr->lftPtr = lftPtr;
  255. nodePtr->rtPtr = rtPtr;
  256. }
  257. /***********************************************
  258. does the actual preorder traversal
  259. ***********************************************/
  260. void BinaryTree::preorder(TreeNode *treePtr, FunctionType visit)
  261. {
  262. if(treePtr != NULL)
  263. {
  264. visit(treePtr->item);
  265. preorder(treePtr->lftPtr, visit);
  266. preorder(treePtr->rtPtr, visit);
  267. }
  268. }
  269. /***********************************************
  270. does the actual inorder traversal
  271. ***********************************************/
  272. void BinaryTree::inorder(TreeNode *treePtr, FunctionType visit)
  273. {
  274. if(treePtr != NULL)
  275. {
  276. inorder(treePtr->lftPtr, visit);
  277. visit(treePtr->item);
  278. inorder(treePtr->rtPtr, visit);
  279. }
  280. }
  281. /***********************************************
  282. does the actual postorder traversal
  283. ***********************************************/
  284. void BinaryTree::postorder(TreeNode *treePtr, FunctionType visit)
  285. {
  286. if(treePtr != NULL)
  287. {
  288. postorder(treePtr->lftPtr, visit);
  289. postorder(treePtr->rtPtr, visit);
  290. visit(treePtr->item);
  291. }
  292. }
  293. /***********************************************
  294. moves iterator to the left pointer
  295. ***********************************************/
  296. void BinaryTree::moveLeft()
  297. {
  298. if(trav->lftPtr != NULL)
  299. {
  300. trav = trav->lftPtr;
  301. }
  302. }
  303. /***********************************************
  304. moves iterator to the right pointer
  305. ***********************************************/
  306. void BinaryTree::moveRight()
  307. {
  308. if(trav->rtPtr != NULL)
  309. {
  310. trav = trav->rtPtr;
  311. }
  312. }
  313. /***********************************************
  314. moves iterator to the ancestor pointer
  315. ***********************************************/
  316. void BinaryTree::moveBack()
  317. {
  318. if(trav->ancestor != NULL)
  319. {
  320. trav = trav->ancestor;
  321. }
  322. }
  323. /***********************************************
  324. resets iterator to the root pointer
  325. ***********************************************/
  326. void BinaryTree::moveRoot()
  327. {
  328. trav = root;
  329. }
  330. /***********************************************
  331. returns the data from the current position
  332. ***********************************************/
  333. void BinaryTree::getTravData(TreeType& item)
  334. {
  335. if (trav != NULL)
  336. {
  337. item = trav->item;
  338. }
  339. }
  340. /***********************************************
  341. sets the current positions data
  342. ***********************************************/
  343. void BinaryTree::setTravData(TreeType& item)
  344. {
  345. if (trav!=NULL)
  346. {
  347. trav->item = item;
  348. }
  349. }
  350. /***********************************************
  351. checks to see if left child is null
  352. ***********************************************/
  353. bool BinaryTree::lftNull()
  354. {
  355. return (trav->lftPtr == NULL);
  356. }
  357. /***********************************************
  358. checks to see if right child is null
  359. ***********************************************/
  360. bool BinaryTree::rtNull()
  361. {
  362. return (trav->rtPtr == NULL);
  363. }
  364. /***********************************************
  365. library includes
  366. ***********************************************/
  367. #include <iostream>
  368. #include <ctype.h>
  369. #include <stdlib.h>
  370. #include <string>
  371. #include <stdio.h>
  372. using namespace std;
  373. /***********************************************
  374. converts a string to uppercase
  375. ***********************************************/
  376. void convUp(char *s)
  377. {
  378. int i;//converts a string to all uppercase
  379. i = strlen(s);
  380. for(int y=0; y < i; y++)
  381. {
  382. s[y] = toupper(s[y]);
  383. }
  384. }
  385. /***********************************************
  386. gets a string from the user....I dont like
  387. cin.getline.....or the string getline function
  388. ***********************************************/
  389. void getstr( char *s, int len )
  390. {
  391. char buffer[81];
  392. int i, ch;
  393. fflush( stdin );//clear the input stream.......
  394. /* Read in single line from "stdin": got this from msdn....*/
  395. for( i = 0; (i < len) && ((ch = getchar()) != EOF)
  396. && (ch != '\n'); i++ )
  397. buffer[i] = (char)ch;
  398. /* Terminate string with null character: */
  399. buffer[i] = '\0';
  400.  
  401. strcpy(s,buffer);
  402. }
  403. /*************************************************
  404. common input function for the whole program...takes
  405. as a const a phrase to display....then calls getstr
  406. to get the string from the user....does validation
  407. and wont release the user till they type a valid
  408. string............................................
  409. *************************************************/
  410. void getInput(const char phrase[], char ret[], int len)
  411. {
  412. int l=0;
  413. if(len > 0)//if user wants a string greater then 0
  414. {
  415. printf(phrase);//print thew phrase
  416. getstr(ret, len);//get the user's input
  417. if(strlen(ret) <=0)//check length
  418. {
  419. while(strlen(ret)<= 0)//while length is invalid
  420. {
  421. printf("Invalid input: ");//get user input
  422. getstr(ret,len);
  423. }//end while
  424. }//end if
  425. }//end if
  426. }
  427. /*************************************************
  428. if user fails to add a question mark to a question
  429. this will do it for them............................
  430. *************************************************/
  431. void fixPunc(char str[])//str must have extra spaces
  432. {
  433. int i = strlen(str);
  434. if(i>0)
  435. {
  436. char c = str[i-1];
  437. if(c != '?')
  438. {
  439. str[i] = '?';
  440. str[i+1]='\0';
  441. }//end if
  442. }//end if
  443. }
  444. /*************************************************
  445. passed to BinaryTree during traversals..........
  446. used it during testing of the classes............
  447. *************************************************/
  448. void display(TreeType& anItem)
  449. {
  450. cout << anItem << endl;
  451. }
  452. /***************************************************
  453. adds the nodes to the tree during the program
  454. takes a binaryb tree as a parameter
  455. ***************************************************/
  456. void addaNode(BinaryTree& t)
  457. {
  458. char *temp = new char[80];//declaring them this way because the class requires a pointer to a reference parameter
  459. char *ques = new char[80];
  460. char *ans = new char[80];
  461. char *hld = new char[80];
  462. char input[80];
  463. t.getTravData(temp);//get the value of the current node
  464. strcpy(input,"I give up. Type the animals name: ");
  465. getInput(input,ans,80);//get user input
  466. strcpy(hld,ans);
  467. strcpy(ans,"Is it a ");
  468. strcat(ans,hld);
  469. strcat(ans, "?");//create the new node value
  470. strcpy(input,"Please type a question about it: ");
  471. getInput(input,ques,80);//get user input
  472. fixPunc(ques);
  473. t.setTravData(ques);//create the new node
  474. t.attachLeft(ans);
  475. t.attachRight(temp);
  476. }
  477. /**************************************************
  478. does the bulk of the work such as get responses
  479. from the user.....................................
  480. **************************************************/
  481. void game(BinaryTree& t)
  482. {
  483. char *k;
  484. char input[80];
  485. char res[2];
  486. bool null;
  487. for(;;)
  488. {
  489. null = false;//reset for loop
  490. t.moveRoot();//reset for loop
  491. while(null == false)//if we have not reached a leaf
  492. {
  493. t.getTravData(k);
  494. strcpy(input, k);//display node value
  495. strcat(input," yes or no(y or n):");
  496. getInput(input,res,2);//get user response
  497. convUp(res);
  498. if(res[0] == 'Y')//if ans is yes
  499. {
  500. if (t.lftNull() == false)//if the left node is not null
  501. {
  502. t.moveLeft();//move left on tree
  503. }
  504. else//else if we are at a leaf then game over
  505. {
  506. printf("I win!!!!!!\n");
  507. strcpy(input, "Quit? yes or no(y or n):");
  508. getInput(input,res,2);
  509. convUp(res);
  510. if(res[0] != 'N')//if ans is yes
  511. {
  512. exit(1);//exit program
  513. }
  514. null = true;
  515. }
  516. }
  517. else if(res[0] == 'N')//if ans is no
  518. {
  519. if (t.rtNull() == false)//if we are not at a leaf
  520. {
  521. t.moveRight();//move right
  522. }
  523. else
  524. {//check to see if user wants to quit
  525. addaNode(t);//add a new node
  526. strcpy(input, "Quit? yes or no(y or n):");
  527. getInput(input,res,2);
  528. convUp(res);
  529. if(res[0] != 'N')//if ans is yes
  530. {
  531. exit(1);//exit program
  532. }
  533. null = true;
  534. }
  535. }
  536. else//else indicate an error state...
  537. {
  538. printf("Error in entry...\n");
  539. }
  540. }
  541. // break
  542. }//end for loop
  543. }
  544. /***********************************************
  545. creates and sets up the tree and calls game,
  546. which is the main loop for the program.........
  547. ***********************************************/
  548. int main()//entry point
  549. {
  550. BinaryTree tree1("Does the Animal Have Legs?");
  551. tree1.attachLeft("Is it a cat?");
  552. tree1.attachRight("Is it a Snake?");
  553. game(tree1);//start main loop
  554. return 0;
  555. }
Last edited by cscgal; May 3rd, 2006 at 9:43 am. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpd_gnv is offline Offline
1 posts
since May 2006
May 3rd, 2006
0

Re: need help in animal game guessing

Use code tags to preserve indentation please!

And don't ask silly requests like, here's my code, this is the problem, solve it.

In fact a succint description of what your program does would help!
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Dynamic Programming
Next Thread in C Forum Timeline: struct linked list problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC