Browse Source

Update calculator.go

Instrye 9 năm trước cách đây
mục cha
commit
e8d45acd31
1 tập tin đã thay đổi với 14 bổ sung12 xóa
  1. 14 12
      eBook/exercises/chapter_12/calculator.go

+ 14 - 12
eBook/exercises/chapter_12/calculator.go

@@ -31,21 +31,23 @@ func main() {
 				i, _ := strconv.Atoi(token)
 				calc1.Push(i)  
 			case token == "+":
-				q := calc1.Pop()
-				p := calc1.Pop()
-				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p + q)
+				q, _ := calc1.Pop()
+				p, _ := calc1.Pop()
+				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p.(int) + q.(int))
 			case token == "-":
-				q := calc1.Pop()
-				p := calc1.Pop()
-				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p - q)
+				q, _ := calc1.Pop()
+				p, _ := calc1.Pop()
+				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p.(int) - q.(int))
+
 			case token == "*":
-				q := calc1.Pop()
-				p := calc1.Pop()
-				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p * q)
+				q, _ := calc1.Pop()
+				p, _ := calc1.Pop()
+				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p.(int) * q.(int))
+
 			case token == "/":
-				q := calc1.Pop()
-				p := calc1.Pop()
-				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p / q)
+				q, _ := calc1.Pop()
+				p, _ := calc1.Pop()
+				fmt.Printf("The result of %d %s %d = %d\n", p, token, q, p.(int) / q.(int))
 			default:
 				fmt.Println("No valid input")
 		}