Calculator C ++ implementation

Source: Internet
Author: User
1. infix expression to suffix expression

Infix: A/(B-c) + D * E
Suffix: ABC-/De * +
Conversion steps:
(1) scan the infix expression from left to right. If it encounters # conversion (6 );
(2) output directly when an operand is encountered; (not on the stack)
(3) When ")" is encountered, the stack is output continuously until "(") "is encountered (" out of the stack but not output); otherwise
(4) If it is another operator, it is compared with the operator on the top of the stack. If the priority is smaller than the priority on the top of the stack, such as a * B + C, note that the two numbers a and B required by "*" have been output, and "*" is popped up until the off-stack priority is higher than the intra-stack priority, and "+" is pushed to the stack. if the priority is higher than the top priority of the stack, for example, A + B * C, it indicates that there is no output for the two numbers B and C required for Number *, so the number * is pushed to the stack.
(5) Forwarding (1 );
(6) remaining operators in the output stack ().

2. Key to conversion: determining the operator priority
The priority determines whether the operator is in or out of the stack.
The operators inside and outside the stack should have different priorities to reflect the computing requirements of the infix expression with the same priority operator from left to right. "(" Has the highest priority outside the stack, but it should be lower than the # Operator after the stack is added, so that other operators in the brackets can be added to the stack.
ISP-intra-stack priority
ICP -- off-stack priority
Operator # (*/+ -)
ICP 0 7 4 2 1
ISP 0 1 5 3 7


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.