作者BlackMatrix (BlackMatrix)
看板C_and_CPP
标题[问题] Boolean Tree
时间Fri Nov 13 12:18:11 2009
( *[1m *[m 为色码,可以按 Ctrl+V 预览会显示的颜色 )
( 未必需要依照此格式,文章条理清楚即可 )
遇到的问题: (题意请描述清楚)
我正在写一个Boolean Tree, 跟Binary Tree差不多, 只是我把Leaf都改成Bool, 比如说
&, |, ~ 代表的是我的Operation
T, F, 其他字母 代表着我的True, 跟False, 其他字母比如说X, Y到後面程序会说
"请输入X跟Y他们的Bool, 也就是T, Or F"
然後做出Tree的时候, 先把一串东西放进去
比如说
TX|F~&
我的树会变成
&
/ \
| ~
/ \ \
T X F
然後运用Recursion把X找出来, 这里我会, 可是重点是程序怪怪的
他会想要更改所有的字母, 就算我写了一个if statement
希望得到的正确结果:
只更改X
程式跑出来的错误结果:
全部一起更改
如果用Postfix看的话
全部选F就会变成
outputting postfix
FFFFFFFF
应该是
TF|F~&
而不是
FFFFFF
开发平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
C++
有问题的code: (请善用置底文标色功能)
template <class Object>
void operatorSymbolChange(Node<Object>* Node)
{
if( Node->left != NULL ) //Left
operatorSymbolChange(Node->left);
if( Node->right != NULL ) //Right
operatorSymbolChange(Node->right);
cout << "Currently in " << Node->element << endl;
Object temp = Node->element;
if ((temp != '&') || (temp != '|') || (temp != '~') || (temp != 'F') ||
(temp != 'T'))
{
Object userInput;
cout << "The element " << Node->element << endl;
cout << "Is a symbol, you would need to change it to << endl;
cout << "either T or F" << endl;
cin >> userInput;
int count = 1;
int tempCount = 0;
while (count!=tempCount)
{
if ((userInput == 'T') || (userInput == 'F'))
{
Node->element = userInput;
tempCount++;
}
else
{
cout << "You did not either enter T or F;
cin >> userInput;
}
}
}
else
{
cout << "The item " << Node->element << " is fine" << endl;
}
}
补充说明:
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 173.56.121.140
1F:推 snowlike:第三个if的逻辑是错的 11/13 13:14
2F:→ BlackMatrix:请问一下为什麽阿? 11/13 13:24
3F:→ BlackMatrix:放两个recursion是为了让他已Postfix跑 11/13 13:25
4F:→ BlackMatrix:if statement是让他测试我的字母到底是不是XYZ等等.. 11/13 13:26
5F:→ BlackMatrix:正确的if statement 应该是 11/13 15:35
6F:→ BlackMatrix:if (node->type == "operandSymbolType") 11/13 15:35
7F:推 snowlike:'&'在第二个条件式进入if,其它符号在第一个条件就进入if 11/13 17:54
8F:→ snowlike:没看到回文@_@其实更换成&&就可以 11/13 18:04
9F:→ BlackMatrix:喔~~~~~原来是这样, 害我想了老半天还得加个Type 11/13 23:19
10F:→ BlackMatrix:谢谢~~ 11/13 23:19