Postfix to infix help with my functions

Mar 3, 2017 at 9:16pm
For my second function, preToIn() I first convert it from Prefix to Postfix, and then I want to call on my postToIn() Function to convert it to infix. How could I go about doing this? So far it converts to Postfix correctly, but I want to go back to infix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
string Expression::postToIn(string myExpression){

stack<string> Stack;
string infix = ""; // Initialize postfix as empty string.
string leftParenthesis = "(";
string rightParenthesis = ")";
string leftValue;
string rightValue;
string myOperator;
string currentinfix;
bool leftDone = true; // leftDone if false means left value needs to be initialised.
bool rightDone = false; // rightDone true means rightDone already has a value hence leftvalue should go to stack , right value to leftvalue and new value to right value in case of operand.
leftValue = myExpression[0];
        for(int i = 1;i< myExpression.length();i++){

            if (isOperand(myExpression[i])){
            if(leftDone){
            if(rightDone){
            Stack.push(leftValue);
            leftValue = rightValue;
            rightValue = myExpression[i];

            }else{
            rightValue = myExpression[i];
            rightDone = true;
            }
            }else{
            leftDone = myExpression[i];
            leftDone = true;
            }

            }else{
            if(rightDone){

            leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
            rightDone = false;
            }
            else{

            rightValue = leftValue;
            leftValue = Stack.top();
            Stack.pop();
            leftValue = leftParenthesis + leftValue + myExpression[i] + rightValue + rightParenthesis;
            }

            }

            }
            return leftValue;
            }




string Expression::preToIn(string myExpression){

             //   first convert prefix to postfix then postfix to infix

    stack<char> Stack;

	char prefix[30];
	int j=0,length;
	length = myExpression.length();
	for(int i = 0; i<length; i++)
	{
		if(isOperator(myExpression[i]))
		Stack.push(myExpression[i]);
		else
		{
			prefix[j++] = myExpression[i];
			while(!Stack.empty() && Stack.top()==flag)
			{
				Stack.pop();
				prefix[j++] = Stack.top();
				Stack.pop();
			}
			Stack.push(flag);

		}
	}
	prefix[j] = 0;
	//postfix = prefix;   

    // *************This is where I am Stuck it converts to POSTFIX but I want to go back to INFIX now*****************
   // for (int k=0; k<myExpression.length();k++)
     // {
     //  prefix = postToIn(myExpression);
    //  }

  return prefix;
}
Last edited on Mar 3, 2017 at 9:18pm
Topic archived. No new replies allowed.