/[hs]/AdviceTree/adtree.cpp
ViewVC logotype

Contents of /AdviceTree/adtree.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Thu Jan 23 17:01:39 2003 UTC (16 years, 7 months ago) by jeremyd
Branch: MAIN
CVS Tags: HEAD
works

1 //Jeremy Drake
2 //Period 2
3 //AdviceTree
4
5 #include <apstring.h>
6 #include "adtree.h"
7
8 void AdTree::BuildTheDamnTree()
9 {
10 myRoot=new AdviceNode("Do you want to see a romantic movie?");
11 myRoot->yes=new AdviceNode("A classic movie?");
12 myRoot->no=new AdviceNode("Science Fiction?");
13 myRoot->yes->yes=new AdviceNode("Casablanca");
14 myRoot->yes->no=new AdviceNode("Titanic");
15 myRoot->no->yes=new AdviceNode("Star Wars");
16 myRoot->no->no=new AdviceNode("Suspense?");
17 myRoot->no->no->yes=new AdviceNode("Jaws");
18 myRoot->no->no->no=new AdviceNode("Citizen Kane");
19 }
20 void AdTree::GiveAdvice(AdviceNode *T)
21 {
22 char Ans;
23 if(!T)
24 return GiveAdvice(myRoot);
25 if (IsQuestionNode(T))
26 {
27 cout << T->QorA << " ";
28 cin >> Ans;
29 if(Ans == 'Y' || Ans == 'y')
30 {
31 GiveAdvice(T->yes);
32 }else{
33 GiveAdvice(T->no);
34 }
35 }else{
36 cout << T->QorA << endl;
37 }
38 }
39
40 bool AdTree::IsQuestionNode(AdviceNode *T)
41 {
42 if(!T)
43 return false;
44 if(T->yes && T->no)
45 return true;
46 return false;
47 }
48
49 bool AdTree::TracePath(AdviceNode *T, const apstring &movie, apstack<apstring> & pathStack)
50 {
51 if(!T)
52 return TracePath(myRoot, movie, pathStack);
53 if(IsQuestionNode(T))
54 {
55 if(!TracePath(T->yes, movie, pathStack))
56 {
57 if(TracePath(T->no, movie, pathStack))
58 {
59 pathStack.push(T->QorA+" "+"No");
60 return true;
61 }else
62 {
63 return false;
64 }
65 }else{
66 pathStack.push(T->QorA+" "+"Yes");
67 return true;
68 }
69 }else{
70 if(T->QorA==movie)
71 {
72 pathStack.push(T->QorA);
73 return true;
74 }
75 else
76 return false;
77 }
78 }

cvs@jdrake.com
ViewVC Help
Powered by ViewVC 1.1.13