infix to postfix formatting

So it is correctly converting infix to postfix just in a bad format
for example when I put 55+8 it gives +558 instead of +55 8 how can I change this
where would I change it to fix this problem in my code
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
#include<iostream>
#include<stack>
#include<algorithm>
#include <string>
using namespace std;

int order(char o){
  if (o == '-' || o == '+')
		return 1;
	else if (o == '*' || o == '/')
		return 2;
	else if (o == '^')
		return 3;
    else{
	return -1;
}
}
bool isOperator(char C){
  return ((!isalnum(C) && !isdigit(C)));
}

string inToPost(string in){
  stack<char> s1;
  string postfix;
  in = '(' + in + ')';
  int size = in.size();
  for(int i = 0; i <size; i++)
  {
    if(isalnum(in[i])||isdigit(in[i]))
    {
      postfix= postfix + in[i];
    }

    else if(in[i]=='('){
      s1.push('(');
    }
    else if(in[i]=='^'){
      s1.push('^');
    }
    else if(in[i]==')'){
      while(s1.top() != '('){
        postfix+=s1.top();
        s1.pop();

      }
      s1.pop();
    }
      else{
        if (isOperator(s1.top())) {
  				while (order(in[i])
  				<= order(s1.top())) {
  					postfix += s1.top();
  					s1.pop();
  				}
  				s1.push(in[i]);
		}
  }
}
return postfix;
}

int main()
{
    string s;
    cout<< "Enter Infix: "<<endl;
    //cin>>s;
    s = "55+8";
    cout << "infixToPostfix: "<<inToPost(s) <<endl;
    return 0;
}
Last edited on
Change your output queue (postfix) from a string to a vector of strings.

Right now you are confusing boundaries between tokens and storage.

You'll have to use a loop to pring your output, complete with spaces.

Hope this helps.
@Duthomhas
is this the only possible way
a bit confused vector<string> postfix;?
Yes*

Each input token is a distinct object. If you use a string, each distinct object is a char.

Except "55" is composed of two characters. Hence token=char is broken.

Instead, make token=string. Then there is no crossing of token and input boundaries.

"55+8" → "55", "+", "8".


You will need to update your algorithm on lines 29–32 to get all the digits of a number and store them in a single string, then add that to your postfix vector.

A really easy way to deal with all this is to require your input to have spaces between tokens, and use >> to get each one. If your assignment has spaces between each input element, then you can do that. Otherwise you’ll just have to be careful to separate out the digits on lines 29–32.


BTW, postfix puts operators after the operands, so your output should be producing

    55 8 +

Hope this helps.
@Duthomas thanks for the help I understand just having a hard time implementing this
Yeah, it's a real pain to get past that hurdle.

I recently played with Shunting Yard for the first time myself, and it took me a little bit to get it perfect.

Simple algorithm, not-so-fun corner cases.
Topic archived. No new replies allowed.