From 19717a5396fd4e7e3bea5a83f3cae1ee210eb2ca Mon Sep 17 00:00:00 2001 From: dugulingping Date: Sun, 6 Nov 2022 22:22:16 +0800 Subject: [PATCH] =?UTF-8?q?day4=20=E5=8C=85=20=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E4=BD=9C=E7=94=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/MaxCommFactor/MaxCommFactor.go | 8 ++++++++ pkg/conv/conv.go | 7 +++++++ pkg/conv/tempconv.go | 16 +++++++++++++++ src/study/day4/package.go | 14 ++++++++++++++ src/study/day4/practice2.2.go | 31 ++++++++++++++++++++++++++++++ src/study/day4/scope.go | 11 +++++++++++ 6 files changed, 87 insertions(+) create mode 100644 pkg/MaxCommFactor/MaxCommFactor.go create mode 100644 pkg/conv/conv.go create mode 100644 pkg/conv/tempconv.go create mode 100644 src/study/day4/package.go create mode 100644 src/study/day4/practice2.2.go create mode 100644 src/study/day4/scope.go diff --git a/pkg/MaxCommFactor/MaxCommFactor.go b/pkg/MaxCommFactor/MaxCommFactor.go new file mode 100644 index 0000000..206082e --- /dev/null +++ b/pkg/MaxCommFactor/MaxCommFactor.go @@ -0,0 +1,8 @@ +package MaxCommFactor + +func MaxComm(x, y int) int { + for y != 0 { + x, y = y, x%y + } + return x +} diff --git a/pkg/conv/conv.go b/pkg/conv/conv.go new file mode 100644 index 0000000..31fbb1e --- /dev/null +++ b/pkg/conv/conv.go @@ -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) } diff --git a/pkg/conv/tempconv.go b/pkg/conv/tempconv.go new file mode 100644 index 0000000..2b8e2e4 --- /dev/null +++ b/pkg/conv/tempconv.go @@ -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) } diff --git a/src/study/day4/package.go b/src/study/day4/package.go new file mode 100644 index 0000000..4ab325c --- /dev/null +++ b/src/study/day4/package.go @@ -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)) +} diff --git a/src/study/day4/practice2.2.go b/src/study/day4/practice2.2.go new file mode 100644 index 0000000..5fde2e2 --- /dev/null +++ b/src/study/day4/practice2.2.go @@ -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)) +} diff --git a/src/study/day4/scope.go b/src/study/day4/scope.go new file mode 100644 index 0000000..259b372 --- /dev/null +++ b/src/study/day4/scope.go @@ -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) + } +}