The arithmetic expression has the form of prefix notation, infix notation and suffix notation. The prefix expression means that the two-tuple operator precedes two operands, such as the prefix expression for 2+3* (7-4) +8/4: + + 2 * 3-7 4/8 4. Please design the program to calculate the result value of the prefix expression.
Input Format Description:
Enter a prefix expression of no more than 30 characters in a row, containing only the + 、-、 *, \, and operands, separated by a space between the different objects (operands, operation symbols).
Output Format Description:
The output prefix expression evaluates to 1 digits after the decimal point, or error message "error".
Sample input and output:
Serial number |
Input |
Output |
1 |
+ + 2 * 3-7 4/8 4 |
13.0 |
2 |
/-25 + *-2 3 4/8 4 |
12.5 |
3 |
/5 + *-2 3 4/8 2 |
ERROR |
4 |
+10.23 |
10.2 |
"Solution"
The evaluation of the prefix expression, the algorithm is not difficult, I think the more difficult place is the processing of strings and some of the details of the place, compared to spend time.
The algorithm is as follows:
"For the evaluation of a prefix expression, the first is to scan the expression from right to left, starting with the first character on the right, if the current character is a number, until the end of the number string is recorded, if the operator, the right is the nearest two" number string "as the corresponding operation, as a new" number string " and recorded. The value of the last operation is the value of the expression, which is always scanned to the leftmost end of the expression. For example, the prefix expression "-1 + 2 3" Evaluation, scan to 3 o'clock, record the number string, scan to 2 o'clock, record the number string, when the scan to +, will + right to do two adjacent numbers of binary operators, recorded as 2+3, the result is 5, record the new number string, and continue to scan to the left, scan to 1 o'clock, Record this number string, scan to-when, move-right to the next two-digit string operator, recorded as 1-5, the result is-4, so the value of the expression is-4. "--from Baidu encyclopedia entry" prefix expression "
According to this algorithm to write out on the stack, time complexity O (n).
The AC code is as follows:
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <math.h>5 6 #defineException ();7printf"ERROR");8Exit0);9 Ten intContain (Charch) One { A if(ch=='+'|| ch=='-'|| ch=='*'|| ch=='/')return 1; - return 0; - } the - floatCalcfloatAfloatBCharch) - { - floatans; + Switch(CH) - { + Case '+': AAns = a +b; at Break; - Case '-': -Ans = ab; - Break; - Case '*': -Ans = A *b; in Break; - Case '/': to if(b = =0) {exception ();} +Ans = A/b; - Break; the } * returnans; $ }Panax Notoginseng - floatAddbit (floatAnsintBooCharChintPower) the { + if(!boo)return(ans*Ten+ch-'0'); A Else return(Ans+ (float) (ch-'0')/pow (Ten, Power)); the } + - floatStr2flo (Char*ch) $ { $ floatans; - intPower =0; - intBoo =0, positive =1; the - if(*ch = ='-') Wuyi { thech++; -Positive =0; Wu } - Else if(*ch = ='+') ch++; About $Ans = *ch-'0'; -ch++; - - while(*ch! =' '&& *ch! =' /'&& *ch! ='\ n') A { + if(*ch>='0'&& *ch<='9') the { - if(Boo) power++; $Ans = addbit (ans, Boo, *ch, power); the } the Else if(*ch=='.') the { theBoo =1; - } inch++; the } the About if(positive)returnAnsElse return-ans; the } the the intMain () + { - Charstr[ *]; the intn, I, tail =0;Bayi floatans[ *]; the intBoo =0; the - gets (str); - then = strlen (str)-1; the the while(N >=0) the { - if(contain (Str[n])) the { the if(Tail <2) {exception ();} the Else 94 { theans[tail-2] = Calc (ans[tail-1], ans[tail-2], str[n]); thetail--; then = n-2;98 } About } - Else if(Str[n]! =' ')101 {102 while(str[n]!=' '&& n>=0) n--;103ans[tail++] = Str2flo (str+n+1);104n--; the }106 Elsen--;107 }108 109 if(Tail = =1) the {111printf"%.1f", ans[0]); the } 113 Else{exception ();} the the return 0; the}
"Zhejiang University patest" 02-3. To find the value of a prefix expression