Infix to Postfix conversion. Am I on the right track?

//Am I on the right track?
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <queue>

using namespace std;

int GetOperatorWeight(char op)//detects precedence of operator.
{
int weight = -1;
switch(op)
{
case '(':
case '=':
weight = 0;
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
}
return weight;
}


int main ()

{
stack <int> num; //used to sort through infix expression.
queue <int> nums2; //going to store all final values in queue to be displayed.
string number;
int x = 0;
int y = 0;
ifstream myfile;
myfile.open ("input.txt");


getline (myfile,number);


for(unsigned int i = 0; i<number.length(); i++) {//detects if item is digit.

if (isdigit(number[i]) )//if item is digit, push onto queue.
{
nums2.push(number[i]);
}

else if (isalnum (number[i]) == 0)//detects if item is operand.

{
x = GetOperatorWeight(number[i]);//gets weight of operand being input.
y = GetOperatorWeight(num.top());//gets weight of operand on stack.
// if/else statements put here to organize operands.

num.push(number[i]);//pushes operand onto stack.
}


}
myfile.close();


}
it's easier to read your code with code-tags
You need break; statements after each case in GetOperatorWeight. By default, control just flows from case down to the next otherwise.

You can read a number or an operator from a stream by reading one character (after skipping whitespace). If it's a digit then put it back in the stream and scan the number. Otherwise the character is the operator:
1
2
3
4
5
6
7
8
9
        char op, ch;
        int num;
        istr >> wc >> ch;
        if (isdigit(ch)) {
            istr.unget(ch);
            istr >> num;
        } else {
            op = ch;
        }


You will also want to set a flag of some sort to say whether you read an operator or a number. Work on this code, called parsing, until your program can read an infix expression. Then you can concentrate on what to do with the input.


The only tricky part is cin >> ws. This extracts and discards white space. It's necessary because otherwise cin >> op would read space characters, newlines, tabs etc.
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <queue>

using namespace std;

int GetOperatorWeight(char op)//detects precedence of operator.
{
	int weight = -1;
	switch(op)
	{
    case '(':
        weight = 3;
        break;
	case '+':
	case '-':
		weight = 1;
		break;
	case '*':
	case '/':
		weight = 2;
		break;
    case ')':
        weight = 0;
        break;
	}
	return weight;
}


int main ()

{
int temp1 = 0;
int temp2 = 0;
int counter = 0;
stack <int> num; //used to sort through infix expression.
queue <int> display; //going to store all final values in queue to be displayed.
string number;
int x = 0;
int y = 0;
ifstream myfile;
  myfile.open ("input.txt");


getline (myfile,number);


  for(unsigned int i = 0; i<number.length(); i++) {//detects if  item is digit.

    if (isdigit(number[i])  )//if item is digit, push onto queue.
    {
        display.push(number[i]);
    }

    else if (isalnum (number[i]) == 0)//detects if item is operand.

    {
        x = GetOperatorWeight(number[i]);//gets weight of operand being input.
         y = GetOperatorWeight(num.top());//gets weight of operand on stack.

        if (x > y)   // if operand being input has greater precedence.
        {
            num.push(number[i]);//pushes operand onto stack.
            counter++;
        }

        else if ((x < y) && (x != 0)) //if operand being input has lower precedence and is not a parenthesis.
        {
            while (counter != 0) //while loop pushes values into display queue, and pops values off of stack.
            {
                display.push(num.top());
                num.pop();
                counter--;

            }
        }

        else if ((x = 3))//if operand being input is a open parenthesis.
        {
            num.push(number[i]);//push operand onto stack.
            temp1 = i; //store place of open parenthesis.
        }

        else if (x = 0)//if operand being input is a close parenthesis.
        {
            temp2 = i; //store place of closed parenthesis.
            while(temp1 != temp2)//while loop pops everything between parenthesis off stack and into display queue.
            {
              display.push(num.top());
                num.pop();
                temp1++;
            }
        }

    }


}
    myfile.close();

return 0;

}


Last edited on
This is what I have so far. No matter what I do, it keeps crashing. Any help?
1. On lines 84 and 90 you're doing assignment (=), not comparison (==).
2. What is the 'counter' variable used for? It seems to be loosely correlated to num.size().
3. In several places you call num.top() and num.pop(), but these functions can only be called if num.size() > 0, yet I see nothing checking for this.
4. Please try to choose more meaningful names that 'temp1' and 'temp2'.
Topic archived. No new replies allowed.