Nyoj128 prefix Calculation

Source: Internet
Author: User
Prefix computing time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description

First, describe what is an infix:

Such as 2 + (3 + 4) * 5. The most common formula is the infix.

Adding brackets to the infix type in the order of operation is: (2 + (3 + 4) * 5 ))

Then, the operator is written before the brackets (+ (2 * (+ (3 4) 5 ))

Remove the brackets: + 2 * + 3 4 5

The formula is the prefix of the expression.

Here is a prefix expression. Calculate the value of this prefix.

For example:

+ 2 * + 3 4 5 is 37

 
Input
There are multiple groups of test data. Each group of test data occupies one row. There is a space between any two operators and between any two operands. The two input operands may be decimal places. The data ensures that all input operands are positive and smaller than 10, and the number of operands cannot exceed 500.
End sign with EOF as input.
Output
Output the value of this prefix expression for each group of data. The output result is retained with two decimal places.
Sample Input
+ 2 * + 3 4 5+ 5.1 / 3 7
Sample output
37.00
5.53

This topic is also made of stacks, but the sequence is a little different from the suffix type. The difference between it and the infix is that you do not need to compare the priority,
Because there are no parentheses in the prefix and suffix types, you can simply calculate them in order. The following is the specific implementation code, with comments in the code,
If you do not understand, you can leave a message...

1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 # include <math. h> 5 typedef struct stack {6 double data; 7 struct stack * Next; 8} stack; 9 stack * Init (); 10 int isoperand (char ch ); 11 stack * push_stack (stack * Top, double data); 12 stack * pop_stack (stack * Top); 13 void calc (stack ** top, char ch ); 14 int main () 15 {16 char ch [5001], temp [1000]; 17 stack * Top; 18 Top = Init (); 19 d Ouble num; int I, Count, flag, J; 20 while (gets (CH )! = NULL) 21 {22 I = strlen (CH)-1; 23 while (I> = 0) 24 {25 if (CH [I] = '') // if it is a space, continue with the next 26 {27 I --; 28 continue; 29} 30 if (isoperand (CH [I]) // if it is an operator, this is the 31 {32 calc (& Top, CH [I]); 33 I --; 34 continue; 35} 36 COUNT = 0; flag = 1; j = 0; 37 while (! Isoperand (CH [I]) & Ch [I]! = ''& I> = 0) // if it is a number, it is saved to a new array. 38 {39 temp [J ++] = CH [I]; 40 I --; 41} 42 J = J-1; 43 num = 0; 44 While (j> = 0) // parses the string into a double type Number 45 {46 47 If (temp [J] = '. ') 48 {49 flag = 0; 50 J --; 51 continue; 52} 53 If (FLAG) 54 num = num * 10 + (temp [J]-'0'); // if no decimal point exists, all of them are * 10 + 55 else 56 {57 count ++; 58 num + = POW (0.1, count) * (temp [J]-'0 '); // after the decimal point is displayed, add 0.1 count to this side 59} 60 J --; 61} 62 Top = push_stack (top, num); 63} 64 printf ("%. 2f \ n ", top-> data); 65} 66 return 0; 67} 68 stack * Init () // initialize stack 69 {70 stack * node; 71 node = (stack *) malloc (sizeof (stack); 72 node-> next = NULL; 73 return node; 74} 75 int isoperand (char ch) // determine whether the operator is 76 {77 If (CH = '+' | CH = '-' | CH = '*' | CH = '/') 78 return 1; 79 return 0; 80} 81 stack * push_stack (stack * Top, double data) // inbound stack 82 {83 stack * node; 84 node = Init (); 85 node-> DATA = data; 86 node-> next = top; 87 Top = node; 88 return top; 89} 90 stack * pop_stack (stack * top) // output stack 91 {92 stack * node; 93 node = top; 94 Top = Top-> next; 95 free (node); 96 return top; 97} 98 void calc (stack ** top, char ch) // compute the function. Of course, the second-level pointer is not required, change void to the stack return type. 99 {100 double num1, num2; 101 num1 = (* Top)-> data; 102 (* Top) = pop_stack (* Top ); 103 num2 = (* Top)-> data; 104 (* Top) = pop_stack (* Top); 105 switch (CH) 106 {107 case '+ ': 108 num1 = num1 + num2; 109 break; 110 case '-': 111 num1 = num1-num2; 112 break; 113 case '*': 114 num1 = num1 * num2; 115 break; 116 case '/': 117 num1 = num1/num2; 118 break; 119} 120 (* Top) = push_stack (* Top, num1 ); // continue the result to stack 121 after calculation}

 

Nyoj128 prefix Calculation

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.