My current program takes user input for an expression tree, example *2x and outputs
* 0 0
2 0 0
x 0 0
*2x is stored in a string.
the 2nd and 3rd columns represent a left-child right-child array. When one of the following operands are input "+ - * / " the zeros need to be changed to the locations of their child's. Like in this example 2 would be the left child of * and x would be the right child so the output should look like
* 1 2
2 0 0
x 0 0
The only way I have thought to do this is to use a switch statement to look for the operands but I don't know how to call on the index that it's child is in.
This is my code:
Code:#include <iostream> #include <iomanip> using namespace std; int main() { char s[50]; cout << "Input the prefix expression.\n"; cin >> s; cout << "\nThe expression tree is:"; int row = strlen(s); int col = 2; char a[10][10]; int i, j, c; int pos; for (j = 0; j < row; j++) { a[j][0] = s[j]; for(i = 1; i <= col; i++) { a[j][i] = '0'; switch (s[j]) { case '+': case '-': case '*': case '/': a[j][i] =; break; } } } for (j = 0; j < row; j++) { cout << "\n"; for(i = 0; i <= col; i++) { cout << a[j][i] << setw(2); } }



LinkBack URL
About LinkBacks



