Update
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
# 04-operators - 运算符
|
||||
|
||||
## 知识点讲解
|
||||
|
||||
### 算术运算符
|
||||
|
||||
| 运算符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `+` | 加法 | `a + b` |
|
||||
| `-` | 减法 | `a - b` |
|
||||
| `*` | 乘法 | `a * b` |
|
||||
| `/` | 除法 | `a / b` |
|
||||
| `%` | 取余 | `a % b` |
|
||||
|
||||
**注意**:整数相除结果仍为整数(丢失小数)
|
||||
|
||||
### 关系运算符
|
||||
|
||||
| 运算符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `==` | 等于 | `a == b` |
|
||||
| `!=` | 不等于 | `a != b` |
|
||||
| `>` | 大于 | `a > b` |
|
||||
| `<` | 小于 | `a < b` |
|
||||
| `>=` | 大于等于 | `a >= b` |
|
||||
| `<=` | 小于等于 | `a <= b` |
|
||||
|
||||
### 逻辑运算符
|
||||
|
||||
| 运算符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `&&` | 逻辑与(AND) | `a && b` |
|
||||
| `\|\|` | 逻辑或(OR) | `a \|\| b` |
|
||||
| `!` | 逻辑非(NOT) | `!a` |
|
||||
|
||||
**注意**:
|
||||
- `&&`:两个都为true时结果为true
|
||||
- `||`:任一为true时结果为true
|
||||
- `!`:取反
|
||||
|
||||
### 位运算符
|
||||
|
||||
| 运算符 | 描述 | 示例 |
|
||||
|--------|------|------|
|
||||
| `&` | 按位与 | `a & b` |
|
||||
| `\|` | 按位或 | `a \| b` |
|
||||
| `^` | 按位异或 | `a ^ b` |
|
||||
| `<<` | 左移 | `a << n` |
|
||||
| `>>` | 右移 | `a >> n` |
|
||||
|
||||
### 赋值运算符
|
||||
|
||||
| 运算符 | 等价于 | 示例 |
|
||||
|--------|--------|------|
|
||||
| `=` | | `a = 10` |
|
||||
| `+=` | `a = a + b` | `a += 5` |
|
||||
| `-=` | `a = a - b` | `a -= 5` |
|
||||
| `*=` | `a = a * b` | `a *= 5` |
|
||||
| `/=` | `a = a / b` | `a /= 5` |
|
||||
| `%=` | `a = a % b` | `a %= 5` |
|
||||
|
||||
### 自增自减
|
||||
|
||||
- `a++` 相当于 `a = a + 1`
|
||||
- `a--` 相当于 `a = a - 1`
|
||||
|
||||
**注意**:Go语言中,`++`和`--`是语句而非表达式,不能用于赋值或直接参与运算。
|
||||
|
||||
**错误用法**:
|
||||
```go
|
||||
b := a++ // 错误
|
||||
if c := a++ > 5 { // 错误
|
||||
```
|
||||
|
||||
## 学习目标
|
||||
|
||||
完成本练习后,你将:
|
||||
- ✅ 掌握各类运算符的使用
|
||||
- ✅ 理解运算符优先级
|
||||
- ✅ 了解Go中自增自减的特点
|
||||
- ✅ 能够正确使用位运算符
|
||||
|
||||
## 练习说明
|
||||
|
||||
打开 `main.go`,根据注释提示填充代码,完成后运行 `go test -v` 验证答案。
|
||||
@@ -0,0 +1,125 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func exercise1() {
|
||||
fmt.Println("=== 练习1:算术运算符 ===")
|
||||
|
||||
a, b := 10, 3
|
||||
|
||||
// 使用算术运算符计算以下值:
|
||||
sum := a + b // 加法
|
||||
diff := ____ // 减法
|
||||
product := __ // 乘法
|
||||
quotient := __ // 除法
|
||||
remainder := __ // 取余
|
||||
|
||||
fmt.Printf("%d + %d = %d\n", a, b, sum)
|
||||
fmt.Printf("%d - %d = %d\n", a, b, diff)
|
||||
fmt.Printf("%d * %d = %d\n", a, b, product)
|
||||
fmt.Printf("%d / %d = %d\n", a, b, quotient)
|
||||
fmt.Printf("%d %% %d = %d\n", a, b, remainder)
|
||||
}
|
||||
|
||||
func exercise2() {
|
||||
fmt.Println("\n=== 练习2:关系运算符 ===")
|
||||
|
||||
x, y := 5, 10
|
||||
|
||||
// 使用关系运算符,判断x是否小于y
|
||||
isLess := ____
|
||||
|
||||
// 使用关系运算符,判断x是否等于y
|
||||
isEqual := ____
|
||||
|
||||
// 使用关系运算符,判断x是否不等于y
|
||||
isNotEqual := ____
|
||||
|
||||
fmt.Printf("%d < %d: %t\n", x, y, isLess)
|
||||
fmt.Printf("%d == %d: %t\n", x, y, isEqual)
|
||||
fmt.Printf("%d != %d: %t\n", x, y, isNotEqual)
|
||||
}
|
||||
|
||||
func exercise3() {
|
||||
fmt.Println("\n=== 练习3:逻辑运算符 ===")
|
||||
|
||||
hasPermission := true
|
||||
isAdmin := false
|
||||
|
||||
// 使用逻辑运算符 &&,判断"有权限且是管理员"的条件
|
||||
canDelete := ____ && ____
|
||||
|
||||
// 使用逻辑运算符 ||,判断"有权限或是管理员"的条件
|
||||
canView := ____ || ____
|
||||
|
||||
// 使用逻辑运算符 !,对isAdmin取反
|
||||
isNotAdmin := ____
|
||||
|
||||
fmt.Printf("有权限: %t, 是管理员: %t\n", hasPermission, isAdmin)
|
||||
fmt.Printf("可以删除: %t, 可以查看: %t\n", canDelete, canView)
|
||||
fmt.Printf("不是管理员: %t\n", isNotAdmin)
|
||||
}
|
||||
|
||||
func exercise4() {
|
||||
fmt.Println("\n=== 练习4:自增自减 ===")
|
||||
|
||||
count := 5
|
||||
|
||||
// 使用自增运算符,count加1
|
||||
____
|
||||
fmt.Printf("自增后: %d\n", count)
|
||||
|
||||
// 使用自减运算符,count减1
|
||||
____
|
||||
fmt.Printf("自减后: %d\n", count)
|
||||
}
|
||||
|
||||
func exercise5() {
|
||||
fmt.Println("\n=== 练习5:赋值运算符 ===")
|
||||
|
||||
num := 10
|
||||
|
||||
// 使用 += 运算符,给num加5
|
||||
num ____
|
||||
|
||||
// 使用 *= 运算符,将num乘2
|
||||
num ____
|
||||
|
||||
// 使用 /= 运算符,将num除4
|
||||
num ____
|
||||
|
||||
fmt.Printf("最终值: %d\n", num)
|
||||
}
|
||||
|
||||
func exercise6() {
|
||||
fmt.Println("\n=== 练习6:位运算符 ===")
|
||||
|
||||
// 二进制: a=5(101), b=3(011)
|
||||
a, b := 5, 3
|
||||
|
||||
// 使用位运算符 & 进行按位与
|
||||
andResult := ____
|
||||
|
||||
// 使用位运算符 | 进行按位或
|
||||
orResult := ____
|
||||
|
||||
// 使用位运算符 ^ 进行按位异或
|
||||
xorResult := ____
|
||||
|
||||
// 使用位运算符 << 将a左移1位(相当于乘以2)
|
||||
leftShift := ____
|
||||
|
||||
fmt.Printf("%d & %d = %d\n", a, b, andResult)
|
||||
fmt.Printf("%d | %d = %d\n", a, b, orResult)
|
||||
fmt.Printf("%d ^ %d = %d\n", a, b, xorResult)
|
||||
fmt.Printf("%d << 1 = %d\n", a, leftShift)
|
||||
}
|
||||
|
||||
func main() {
|
||||
exercise1()
|
||||
exercise2()
|
||||
exercise3()
|
||||
exercise4()
|
||||
exercise5()
|
||||
exercise6()
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func captureOutput(f func()) string {
|
||||
old := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
f()
|
||||
|
||||
w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestExercise1(t *testing.T) {
|
||||
output := captureOutput(exercise1)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"加法检查", "10 + 3 = 13"},
|
||||
{"减法检查", "10 - 3 = 7"},
|
||||
{"乘法检查", "10 * 3 = 30"},
|
||||
{"除法检查", "10 / 3 = 3"},
|
||||
{"取余检查", "10 % 3 = 1"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise2(t *testing.T) {
|
||||
output := captureOutput(exercise2)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"小于检查", "5 < 10: true"},
|
||||
{"等于检查", "5 == 10: false"},
|
||||
{"不等于检查", "5 != 10: true"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise3(t *testing.T) {
|
||||
output := captureOutput(exercise3)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"可以删除检查", "可以删除: false"},
|
||||
{"可以查看检查", "可以查看: true"},
|
||||
{"不是管理员检查", "不是管理员: true"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise4(t *testing.T) {
|
||||
output := captureOutput(exercise4)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"自增检查", "自增后: 6"},
|
||||
{"自减检查", "自减后: 5"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise5(t *testing.T) {
|
||||
output := captureOutput(exercise5)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"最终值检查", "最终值: 7"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise6(t *testing.T) {
|
||||
output := captureOutput(exercise6)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
expected string
|
||||
}{
|
||||
{"按位与检查", "5 & 3 = 1"},
|
||||
{"按位或检查", "5 | 3 = 7"},
|
||||
{"按位异或检查", "5 ^ 3 = 6"},
|
||||
{"左移检查", "5 << 1 = 10"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if !strings.Contains(output, tt.expected) {
|
||||
t.Errorf("期望输出包含 '%s', 但实际输出: %s", tt.expected, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainFunction(t *testing.T) {
|
||||
output := captureOutput(main)
|
||||
|
||||
requiredSections := []string{
|
||||
"练习1:算术运算符",
|
||||
"练习2:关系运算符",
|
||||
"练习3:逻辑运算符",
|
||||
"练习4:自增自减",
|
||||
"练习5:赋值运算符",
|
||||
"练习6:位运算符",
|
||||
}
|
||||
|
||||
for _, section := range requiredSections {
|
||||
if !strings.Contains(output, section) {
|
||||
t.Errorf("期望输出包含 '%s'", section)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user