初识Go

意外关注到一位牛人的微信,提到了2014年他推荐的编程语言是Go。于是乎饶有兴趣的淘了一本书《Go语言程序设计》,学习起来。

第一章的练习题我的答案如下:

 1 // Copyright © 2010-12 Qtrac Ltd.
 2 //
 3 // This program or package and any associated files are licensed under the
 4 // Apache License, Version 2.0 (the "License"); you may not use these files
 5 // except in compliance with the License. You can get a copy of the License
 6 // at: http://www.apache.org/licenses/LICENSE-2.0.
 7 //
 8 // Unless required by applicable law or agreed to in writing, software
 9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 package main
15 
16 import (
17     "fmt"
18     "log"
19     "os"
20     "path/filepath"
21     "strings"
22 )
23 
24 func main() {
25     if len(os.Args) == 1 {
26         fmt.Printf("usage: %s <whole-number>\n", filepath.Base(os.Args[0]))
27         os.Exit(1)
28     }
29 
30     drawBar := false
31     stringOfDigits := ""
32 
33     if len(os.Args) == 2 {
34         if os.Args[1] == "-h" || os.Args[1] == "--help" {
35             fmt.Printf("usage: %s [-b|--bar] <whole-number>\n-b --bar draw an underbar and an overbar\n", filepath.Base(os.Args[0]))
36             os.Exit(1)
37         } else {
38             drawBar = false
39             stringOfDigits = os.Args[1]
40         }
41     }
42 
43     if len(os.Args) == 3 {
44         if os.Args[1] == "-b" || os.Args[1] == "--bar" {
45             drawBar = true
46             stringOfDigits = os.Args[2]
47         }
48     }
49 
50     for row := range bigDigits[0] {
51 
52         line := ""
53         for column := range stringOfDigits {
54             digit := stringOfDigits[column] - '0'
55             if 0 <= digit && digit <= 9 {
56                 line += bigDigits[digit][row] + "  "
57             } else {
58                 log.Fatal("invalid whole number")
59             }
60         }
61 
62         if drawBar && row == 0 {
63             fmt.Println(strings.Repeat("*", len(line)))
64         }
65         fmt.Println(line)
66         if drawBar && row == len(bigDigits[0])-1 {
67             fmt.Println(strings.Repeat("*", len(line)))
68         }
69     }
70 
71     if drawBar {
72     }
73 }
74 
75 var bigDigits = [][]string{
76     {"  000  ",
77         " 0   0 ",
78         "0     0",
79         "0     0",
80         "0     0",
81         " 0   0 ",
82         "  000  "},
83     {" 1 ", "11 ", " 1 ", " 1 ", " 1 ", " 1 ", "111"},
84     {" 222 ", "2   2", "   2 ", "  2  ", " 2   ", "2    ", "22222"},
85     {" 333 ", "3   3", "    3", "  33 ", "    3", "3   3", " 333 "},
86     {"   4  ", "  44  ", " 4 4  ", "4  4  ", "444444", "   4  ",
87         "   4  "},
88     {"55555", "5    ", "5    ", " 555 ", "    5", "5   5", " 555 "},
89     {" 666 ", "6    ", "6    ", "6666 ", "6   6", "6   6", " 666 "},
90     {"77777", "    7", "   7 ", "  7  ", " 7   ", "7    ", "7    "},
91     {" 888 ", "8   8", "8   8", " 888 ", "8   8", "8   8", " 888 "},
92     {" 9999", "9   9", "9   9", " 9999", "    9", "    9", "    9"},
93 }
答案源码

 

posted on 2014-05-12 00:45  非常笑  阅读(233)  评论(0编辑  收藏  举报

导航