02-Linear Structure 3. To find the value of the prefix expression (25) time limit--ms
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions Standard
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 |
Enter |
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 |
#include <stdio.h> #include <string.h> #include <ctype.h>double parsenum (char *s, int head, int rear) {/ /parse string, return the corresponding numeric value double ret = 0;double flag = 1;//Mark numeric sign if (s[head] = = ' + ' | | s[head] = = '-') {//Judgment sign if (s[head] = = '-') flag = -1;++head;} while (head <= rear && s[head]! = '. ') {//process before the decimal point, that is, the integer part ret = ten * ret + (S[head]-' 0 '); ++head;} if (s[head] = = '. ') {//If there is a decimal part, calculate the decimal part ++head;double weight = 0.1;while (Head <= rear) {ret + = (double) (S[head]-' 0 ') * weight;weight *= 0.1 ; ++head;}} return flag * RET;} int main () {char s[30];gets (s);d ouble numstack[15];//is used to store operands of the stack int numsize = 0;//stack elements int rear = strlen (s)-1;//starts at the end of the input string Traversal, the operand is pressed into the stack, encountered operator, popup stack top two elements operation, the result of the operation into the stack for (int head = rear; rear >= 0;--head) {if (head = =-1 | | s[head] = =) {//Two There is a processing unit between the spaces, and the first processing unit has no space, head = 1 judgment//If it is an operator, two numbers are popped from the operand stack, and the result of the operation is in the stack if (rear-head = = 1 && (s[rear] = = ' + ' | | s[r Ear] = = '-' | | S[rear] = = ' * ' | | S[rear] = = '/') {double num1 = numstack[--numsize];d ouble num2 = Numstack[--numsize];switch (S[rear]) {case ' + ': numstack[numsize++] = num1 + num2;break;case '-': numstack[numsize++] = Nu M1-num2;break;case ' * ': numstack[numsize++] = num1 * num2;break;case '/': numstack[numsize++] = num1/num2;if (Num2 < 1e-5 && num2 > -1e-5) {//dividend is 0 o'clock output error message, terminating program printf ("error\n"); return 0;} Break;}} else {//If the operand is parsed, the value is entered in the stack numstack[numsize++] = Parsenum (s, head + 1, rear);} Rear = head-1;}} printf ("%.1f\n", Numstack[0]); return 0;}
Title Link: http://www.patest.cn/contests/mooc-ds/02-%E7%BA%BF%E6%80%A7%E7%BB%93%E6%9E%843
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
02-Linear Structure 3. Find the value of the prefix expression (25)