infix to postfix output problem

i reviewed my code couple of times and still could not point out why is not showing any output at all. can anyone point me where im having it wrong.


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
#include<iostream>
using namespace std;

string stack; //initialize stack to contain operators
int top=-1;

void push(char a){	//add/push it to stack
	top++;
	stack[top] = a;
}

void pop(char a){ //delete/pop the stack
    top--;
    stack[top] = a;
}

int order_operation(char a){ //the precedence priority
	
	if(a=='+' || a=='-'){
		return 1;
	}
	else if(a=='*' || a=='/'){
		return 2;
	}
	else {
		return 3;
	}
}

int main(){
	string infix,postfix;
	
	cout<<"infix: ";
	getline(cin,infix);
	
	for(int x = 0; x<infix.length(); x++){   //scan the infix for operator
		if(infix[x]=='-' || infix[x]=='+' ||infix[x]=='*' || infix[x]=='/'){
			while(!stack.empty() && order_operation(stack[top])>=order_operation(infix[x])){ //if the stack is not empty and check the precedence
			
				postfix[x]+=stack[top];  //add it to postfix string
				pop(stack[top]);	//pop the stack operator
			}
			push(infix[x]); 
		}
		else{
			postfix[x]+=infix[x]; //add to postfix string if its operand
		}
	}
	
	while(!stack.empty()){ //if the stack is not empty put it to posfix string
		postfix+=stack[top];
		pop(stack[top]); 
	}
	cout<<postfix <<endl;
}
Last edited on
any help/feedback?
Your pop is totally screwed up.
Why would you pass stack[top] to it? Since stack is global, pop doesn't need any parameters.
Why doesn't pop return anything?
Since you pre-increment top for the push you need to post-decrement top for the pop.
And the assignment is basically backwards, to. You don't want to assign anything to the stack during a pop.
Last edited on
@tpb thanks for the reply and pointing out my error. i still cant get it to work. i change it to
1
2
3
4
 
char pop(){ 
	return stack[top--];
}


i tested it out in a small pop program and it was working just fine but when i tried to implement it to this program it does not show anything.

here is my updated program and somehow i mange to show some output. the only problem now is that i cant show the operators. thanks again

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
#include<iostream>
using namespace std;

string stack; //initialize stack to contain operators
int top=-1;

void push(char a){	//add/push it to stack
	top++;
	stack[top] = a;
}

char pop(){ //delete/pop the stack
    return stack[top--];
}

int order_operation(char a){ //the precedence priority
	
	if(a=='+' || a=='-'){
		return 1;
	}
	else if(a=='*' || a=='/'){
		return 2;
	}
	else {
		return 3;
	}
}

int main(){
	string infix,postfix;
	
	cout<<"infix: ";
	getline(cin,infix);
	
	for(int x = 0; x<infix.length(); x++){   //scan the infix for operator
		if(infix[x]=='-' || infix[x]=='+' ||infix[x]=='*' || infix[x]=='/'){
			while(!stack.empty() && order_operation(stack[top])>=order_operation(infix[x])){ //if the stack is not empty and check the precedence
			
				postfix+=stack[top];  //add it to postfix string
				pop(); 	//pop the stack operator
			}
			push(infix[x]); 
		}
		else{
			postfix+=infix[x]; //add to postfix string if its operand
		}
	}
	
	while(!stack.empty()){ //if the stack is not empty put it to posfix string
		postfix+=stack[top];
		pop();
	}
	cout<<postfix;
}
Many of the functions you’ve implemented already exist in the standard library for std::strings, so I’ve substituted those for yours.
std::string::pop_back() --> deletes last character
std::string::push_back() --> append one character (the same as ‘+=’)
std::string::back() --> returns (a reference to) the last character.

The range-for loop in line 20 helps avoiding using indexes.

Apart from what’s above, I’ve left your code nearly unchanged, but I added output statements which could help you to guess what could be the error.

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
#include <iostream>
#include <string>

int priority(char a);

int main()
{
    std::string infix;
    do {
        std::cout << "infix: ";
        std::getline(std::cin, infix);
        if(std::string::npos == infix.find_first_of("-+*/")) {
            std::cout << "Please insert a valid arithmetic expression.\n";
            infix.clear();
        }
    } while(infix.empty());

    std::string postfix;
    std::string stack;
    for(const char c : infix) {
        std::cout << "Elaborating '" << c << "':\n";
        if(c == '-' || c == '+' || c == '*' || c == '/') {
            while(!stack.empty() && priority(stack.back()) >= priority(c)) {
                postfix += stack.back();
                std::cout << "\tadded '" << stack.back() << "' to postfix ";
                stack.pop_back();
                std::cout << "and removed from stack\n";
            }
            stack.push_back(c);
            std::cout << "\tadded '" << c << "' to stack\n";
        }
        else {
            postfix += c; //add to postfix std::string if its operand
            std::cout << "\tadded '" << c << "' to postfix\n";
        }
        std::cout << "\tstack is now \"" << stack << "\" and postfix is \""
                  << postfix << "\".\n";
    }

    // if the stack is not empty put it to posfix std::string
    std::cout << "\nNow elaborating the stack:\n";
    while(!stack.empty()) {
        postfix += stack.back();
        std::cout << "\tadded '" << stack.back() << "' to postfix ";
        stack.pop_back();
        std::cout << "and removed from stack\n";
    }
    std::cout << "postfix: " << postfix << '\n';
}

// the precedence priority
int priority(char a)
{
    switch(a) {
        case '+': case '-': return 1;
        case '*': case '/': return 2;
        default           : return 3;
    }
}

@Enoizat wow, i thought those functions where different from #include<stack> functions, didn't know it could also be applied. thank you very much for the detailed explanation, i could now trace what went wrong.
Last edited on
@newguy17, I'd avoid doing cin stuff for now. Just have a bunch of hardcoded examples and incrementally improve the functionality until expected matches actual (keyword: bunch). You probably want a method to show the stack, showing it constantly to prove what should be stored is stored (there's always the debugger, too).
Topic archived. No new replies allowed.