Can anyone help me on how to display my infix notation with parentheses?
4+3 should be displayed as (4+3)
also, my code only works for simple expressions like 4+0, it can translate the infix,prefix,and postfix correctly, but when the expression grows, for some reason, its outputting the incorrect notations for the prefix and postfix. maybe i havent done enough checking. im so lost.
please help me):
template <class Item>
void infix(exp_tree_node<Item>* root_ptr){
if(root_ptr != NULL) {
infix(root_ptr->left());
if(root_ptr->getOp() == 'l'){
cout << root_ptr->data();//print the data field
} else {
cout << root_ptr->getOp(); // print the operator
}
infix(root_ptr->right());
}
}
}
template <class Item>
void prefix(exp_tree_node<Item>* root_ptr){
if(root_ptr != NULL) {
if(root_ptr->getOp() =='l') {
cout << root_ptr->data();
}else {
cout << root_ptr->getOp();
}
prefix(root_ptr->left());
prefix(root_ptr->right());
}
}
template <class Item>
void postfix(exp_tree_node<Item>* root_ptr){
if(root_ptr != NULL) {
postfix(root_ptr->left());
postfix(root_ptr->right());
if(root_ptr->getOp() == 'l') {
cout << root_ptr->data();
}else {
cout << root_ptr->getOp();
}
}
}
if the complete cpp template file is needed, i can upload it. thanks