Skip to content

Instantly share code, notes, and snippets.

@qycyfjy
Created October 24, 2021 09:47
Show Gist options
  • Save qycyfjy/70286b41e6951278c123b993acfe9a05 to your computer and use it in GitHub Desktop.
Save qycyfjy/70286b41e6951278c123b993acfe9a05 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"runtime/pprof"
"strconv"
"sync"
)
func main() {
profFile, err := os.Create("cpu.prof")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer profFile.Close()
pprof.StartCPUProfile(profFile)
defer pprof.StopCPUProfile()
outFile, err := os.Create("out.go")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outFile.Close()
w := bufio.NewWriter(outFile)
w.WriteString(`package main
import (
"fmt"
"bufio"
"os"
"strconv"
)
func main() {
fmt.Print("输入一个五位正整数: ")
input := bufio.NewScanner(os.Stdin)
input.Scan()
d, err := strconv.Atoi(input.Text())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if d >= 100000 || d <= 0{
fmt.Println("数字不符合要求")
os.Exit(1)
}
switch d {
`)
for i := 1; i <= 99999; i++ {
fmt.Fprintf(w, "\t\tcase %d: {\n", i)
w.WriteString(caseN(i))
w.WriteString("\t\t}\n")
}
w.WriteString("\t}\n}")
w.Flush()
}
var p = [5]string{"个", "十", "百", "千", "万"}
var pool = &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
func caseN(d int) string {
dd := d
buffer := pool.Get().(*bytes.Buffer)
defer func() {
buffer.Reset()
pool.Put(buffer)
}()
ds := strconv.Itoa(d)
size := len(ds)
fmt.Fprintf(buffer, "\t\t\tfmt.Println(\"是个%d位数\")\n", size)
pp := p[:size] // BCE
for i := 0; i < size; i++ {
fmt.Fprintf(buffer, "\t\t\tfmt.Println(\"%s位数是: %d\")\n", pp[i], d%10)
d /= 10
}
fmt.Fprintf(buffer, "\t\t\tfmt.Println(\"倒过来是: %d\")\n", reverse(dd))
return buffer.String()
}
func reverse(d int) int {
res := 0
for d != 0 {
res = res*10 + d%10
d /= 10
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment