
contents
Problem
給一個浮點數的五則運算表達式,計算其值。
1 2 3 4
|
3 ( 3.00 + 4.50 ) ( 5.00 - ( 2.50 * 3.00 ) ) ( ( 7.00 / 3.00 ) + ( 4.00 - ( 3.00 * 7.00 ) ) )
|
Sample Output
Solution
中序表達轉換成後序表達,隨後再使用 stack 完成計算。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <stdio.h> #include <stack> #include <string.h> #include <algorithm> #include <iostream> #include <sstream> using namespace std; int priority_op(char c) { switch(c) { case '(':return 0; case '+': case '-':return 1; case '*': case '/':return 2; } return -1; } void trans(char infix[], char buffer[]) { int len = strlen(infix); char *ptr = buffer; stack<char> op; *ptr = '
|
近期评论