Page 658 - Computer science 868 Class 12
P. 658
39 {
40 char sm;
41 String pfx = "";
42 for(int i=0;i<inf.length();++i)
43 //while there is input to be read
44 {
45 sm = inf.charAt(i);
46 //if it's an operand, add it to the string
47 if (Character.isLetter(sm))
48 pfx = pfx + sm;
49 else if (sm=='(')
50 //push (
51 {
52 ob.push(sm);
53 }
54 else if (sm==')')
55 //push everything back to (
56 {
57 while (ob.peek() != '(')
58 {
59 pfx += ob.pop();
60 }
61 ob.pop(); //remove '('
62 }
63 else
64 //print operators occurring before it that have greater precedence
65 {
66 while (!ob.isEmpty() && !(ob.peek()=='(') && prec(sm) <= prec(ob.
peek()))
67 pfx+=ob.pop();
68 ob.push(sm);
69 }
70 }
71 while (!ob.isEmpty())
656656 Touchpad Computer Science-XII

