day4 包 文件 作用域

master
dugulingping 2022-11-06 22:22:16 +08:00
parent b69813a3e3
commit 19717a5396
6 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,8 @@
package MaxCommFactor
func MaxComm(x, y int) int {
for y != 0 {
x, y = y, x%y
}
return x
}

7
pkg/conv/conv.go Normal file
View File

@ -0,0 +1,7 @@
package conv
// CToF converts a Celsius temperature to Fahrenheit.
func CToF(c Celsius) Fahrenheit { return Fahrenheit(c*9/5 + 32) }
// FToC converts a Fahrenheit temperature to Celsius.
func FToC(f Fahrenheit) Celsius { return Celsius((f - 32) * 5 / 9) }

16
pkg/conv/tempconv.go Normal file
View File

@ -0,0 +1,16 @@
// Package conv performs Celsius and Fahrenheit conversions.
package conv
import "fmt"
type Celsius float64
type Fahrenheit float64
const (
AbsoluteZeroC Celsius = -273.15
FreezingC Celsius = 0
BoilingC Celsius = 100
)
func (c Celsius) String() string { return fmt.Sprintf("%g°C", c) }
func (f Fahrenheit) String() string { return fmt.Sprintf("%g°F", f) }

14
src/study/day4/package.go Normal file
View File

@ -0,0 +1,14 @@
package main
import (
"Study/pkg/MaxCommFactor"
"Study/pkg/conv"
"fmt"
)
func main() {
fmt.Println("AbsoluteZero:", conv.AbsoluteZeroC)
fmt.Println(conv.CToF(conv.BoilingC))
fmt.Println(MaxCommFactor.MaxComm(12, 24))
}

View File

@ -0,0 +1,31 @@
package main
import (
"Study/pkg/MaxCommFactor"
"fmt"
"os"
"strconv"
)
func main() {
var nums []int
var x, y int
for _, arg := range os.Args[1:] {
t, err := strconv.ParseInt(arg, 0, 64)
if err != nil {
fmt.Fprintln(os.Stderr, "cf:", err)
}
nums = append(nums, int(t))
}
if len(nums) <= 0 {
fmt.Println("Please input two numbers in console (one in a row)")
fmt.Scanln(&x, &y)
} else if len(nums) == 1 {
x = nums[0] // 将终端输入的数据赋值给x
fmt.Println("Please input one numbers in console (one in a row)")
fmt.Scanln(&y)
} else {
x, y = nums[0], nums[1]
}
fmt.Print(x, y, " Gcd is ", MaxCommFactor.MaxComm(x, y))
}

11
src/study/day4/scope.go Normal file
View File

@ -0,0 +1,11 @@
package main
import "fmt"
func main() {
x := "hello"
for _, x := range x {
x := x + 'A' - 'a' // 将 for外部的x代入运算后 赋值给新局部变量x
fmt.Printf("%c", x) // "HELLO" (one letter per iteration)
}
}