The function print_postfix() only outputs the string as required if it is not empty.
If the message "Error: invalid number of operands" is to be output if, for example, only "+" is entered, the to_postfix() function should recognize this and output it.
Currently the function to_postfix() does not recognize such errors and print_postfix() outputs the "+". In such cases, both the error message should be output and the result string should also be empty.
Improvements for to_postfix()
- Remove spaces in advance instead of interpreting them as errors
- With valid infix notation, a number or an open bracket should be at the beginning.
->Currently this is not checked and a single "+" ends up on the stack.
- If an invalid character occurs, there is always an immediate error
- The sequence and priority of operands and numbers must be checked
The function to_postfix() seems to have many shortcomings. I therefore suggest improving the function.
You can look at this to convert infix notation:
https://en.wikipedia.org/wiki/Shunting_yard_algorithm