[Post-order operation]: Operation

Source: Internet
Author: User

[Post-order operation]: Operation

/* Description of post-order operations: the advantage of converting a middle-order to a post-order operation is that you do not have to deal with the suborder Problems of the operation, as long as the pre-order operation is followed by the Post-order operation. Solution: When an operation is performed, it is read from the front of the back-order type. When an operator is encountered, it is first saved to the stack. If an operator is encountered, two operators are taken out from the stack for calculation, then store the result back to the stack. If the operation is completed, the value at the top of the stack is the answer, for example, we calculate the formula 12 + 34 + * (that is, (1 + 2) * (3 + 4 )): read stack 1 12 12 + 33 33 (saved back after 1 + 2) 4 334 + 37 (stored back after 3 + 4) * 21 (stored back after 3*7) */# include <stdio. h> # include <stdlib. h> void evalPf (char *); double cal (double, char, double); int main (void) {char input [80]; printf ("input the postfix: "); scanf (" % s ", input); evalPf (input); return 0 ;}void evalPf (char * postfix) {double stack [80] ={ 0.0 }; char temp [2]; char token; int top = 0, I = 0; temp [1] = '\ 0'; while (1) {token = postfix [I]; switch (token) {case '\ 0': printf ("ans = % f \ n", stack [top]); return; case' + ': case '-': case '*': case '/': stack [top-1] = cal (stack [top], token, stack [top-1]); top --; break; default: if (top <sizeof (stack)/sizeof (float) {temp [0] = postfix [I]; top ++; stack [top] = atof (temp);} break;} I ++;} double cal (double p1, char op, double p2) {switch (op) {case '+': return p1 + p2; case '-': return p1-p2; case '*': return p1 * p2; case '/': return p1/p2 ;}}

 

Related Article

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.