I don't understand why you're passing in a stack into the function. I've described an algorithm that starts with an postfix expression in a string, returns a single value and uses a stack within the process.
You've already done the hard part, the infix to postfix converter. Just take string output from that and pass it to the evaluator.
You write out the postfix expression, with .print, just make it write to a general stream instead of stdout.
1 2 3 4 5
void stack::print(std::ostream &os)
for (int i=0; i <= top; ++i)
os << data[i];
Then you can print to an in-memory stream and convert that to a string:
1 2 3 4 5 6
std::ostringstream os; // in-memory stream
post.print(os); // write the stack content to the stream
std::string postfix = os.str(); // save the stream into a string
int value = evaluate(postfix); // evaluate the postfix expression
std::cout << "postfix expression: " << postfix << std::endl;
std::cout << "value: " << value << std::endl;
Topic archived. No new replies allowed.
You are using a version without Ads of this website. Please, consider donating: