上代码
|
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
package main import ( "fmt" "os" "strconv" ) type stack struct { maxCount int arr [20]interface{} top_point int //栈顶指针 } func (this *stack) push(val interface{}) { if this.maxCount-1 == this.top_point { fmt.Println("栈已经满了,无法加入") return } this.top_point++ this.arr[this.top_point] = val } func (this *stack) pop() interface{} { if this.top_point == -1 { fmt.Println("空栈,没有任何元素弹出") return nil } val := this.arr[this.top_point] this.top_point-- return val } func (this *stack) showStack() { if this.top_point == -1 { fmt.Println("栈中空无一物") return } fmt.Println("显示栈") for i := this.top_point; i >= 0; i-- { fmt.Printf("arr[%d]=%v\n", i, this.arr[i]) } } func isMark(str string) bool { if str == "+" || str == "-" || str == "/" || str == "*" { return true } return false } /** 计算函数 */ func calculate(num1, num2 interface{}, mark string) (float64) { var res, v1, v2 float64 var err1, err2 error switch num1.(type) { case string: v1, err1 = strconv.ParseFloat(num1.(string), 64) handleError(err1, "计算函数出错") case float64: v1 = num1.(float64) } switch num2.(type) { case string: v2, err2 = strconv.ParseFloat(num2.(string), 64) handleError(err2, "计算函数出错") case float64: v2 = num2.(float64) } switch mark { case "+": res = v1 + v2 case "-": res = v2 - v1 case "*": res = v1 * v2 case "/": res = v2 / v1 } return res } /** +与-优先权为0,*与/优先权为1 */ func checkPriority(mark string) int { priority := 3 //默认为3,如果为3代表用户输入错误了 switch { case mark == "+" || mark == "-": priority = 0 case mark == "*" || mark == "/": priority = 1 } return priority } func handleError(err error, why string) { if err != nil { fmt.Println(why) os.Exit(1) } } func main() { exp := "10+3/2.5+10*3-56" //四则运算表达式 //数栈 numStack := &stack{ maxCount: 20, arr: [20]interface{}{}, top_point: -1, } //符号栈 markStack := &stack{ maxCount: 20, arr: [20]interface{}{}, top_point: -1, } var num1, num2, mark interface{} //临时计算变量 var complete_str string for i := 0; i < len(exp); i++ { byte2str := string(exp[i]) is_mark := isMark(byte2str) //exp[i]必须要用string转一下,否则输出的是uint8类型的字符数字ascii码 if is_mark { if markStack.top_point == -1 { //如果是符号栈为空栈则直接入栈 markStack.push(byte2str) } else { //判断符号的优先权 cur_priority := checkPriority(byte2str) mark_stack_top := markStack.arr[markStack.top_point] if res := checkPriority(mark_stack_top.(string)); res >= cur_priority { //如果符号栈的栈顶的优先权>=当前的准备入栈的符号,我们就准备从数栈中弹出两个数 // 并且弹出符号栈的栈顶符号,进行计算,并将计算的结果压入数栈 num1 = numStack.pop() num2 = numStack.pop() mark = markStack.pop() res := calculate(num1, num2, mark.(string)) numStack.push(res) markStack.push(byte2str) } else { markStack.push(byte2str) } } } else { switch { case i == len(exp)-1: is_long_num := len(complete_str) > 0 if is_long_num { complete_str += byte2str numStack.push(complete_str) complete_str = "" } else { numStack.push(byte2str) } //numStack.push(byte2str) case i < len(exp)-1: //探测下一位是否是符号,如果是符号,那我们就正常压入数栈 next := string(exp[i+1]) is_mark := isMark(next) if !is_mark { complete_str += byte2str } else { //先判断complete_str的长度,来证明上一次的循环是否是为了一个多位数或者小数在循环 is_long_num := len(complete_str) > 0 if is_long_num { complete_str += byte2str numStack.push(complete_str) complete_str = "" } else { numStack.push(byte2str) } } } } //如果是最后一次执行 if i == len(exp)-1 { times := markStack.top_point + 1 for j := 0; j < times; j++ { num1 = numStack.pop() num2 = numStack.pop() mark = markStack.pop() res := calculate(num1, num2, mark.(string)) numStack.push(res) } } } res := numStack.pop() fmt.Printf("%s=%.2f", exp, res) //10+3/2/2.5+10*3-56=-15.400000 } |
© 著作权归作者所有
文章评论(0)