parent
509750b2ab
commit
f851eb004b
@ -1,75 +1,78 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"fmt" |
"fmt" |
||||||
"flag" |
"flag" |
||||||
"strconv" |
"strconv" |
||||||
"log" |
"log" |
||||||
"math" |
"math" |
||||||
"bufio" |
"bufio" |
||||||
"os" |
"os" |
||||||
) |
) |
||||||
|
|
||||||
type Opts struct { |
type Opts struct { |
||||||
From string |
From string |
||||||
To string |
To string |
||||||
|
Numbers []string |
||||||
} |
} |
||||||
|
|
||||||
func parse_opts() (Opts, []string) { |
func ParseOpts() Opts { |
||||||
var opts Opts |
var opts Opts |
||||||
|
|
||||||
flag.StringVar(&opts.From, "from", "", "Convert from") |
flag.StringVar(&opts.From, "from", "", "Convert from") |
||||||
flag.StringVar(&opts.To, "to", "", "Convert to") |
flag.StringVar(&opts.To, "to", "", "Convert to") |
||||||
flag.Parse() |
flag.Parse() |
||||||
|
|
||||||
return opts, flag.Args() |
opts.Numbers = flag.Args() |
||||||
|
|
||||||
|
return opts |
||||||
} |
} |
||||||
|
|
||||||
func to_si(num string) string { |
func to_si(num string) string { |
||||||
number, err := strconv.ParseFloat(num, 64) |
number, err := strconv.ParseFloat(num, 64) |
||||||
if err != nil { log.Fatal("that's not a number") } |
if err != nil { log.Fatal("that's not a number") } |
||||||
mag := math.Floor(math.Log10(number)) |
mag := math.Floor(math.Log10(number)) |
||||||
|
|
||||||
switch { |
switch { |
||||||
case mag < 3: |
case mag < 3: |
||||||
return num |
return num |
||||||
case mag == 3: |
case mag == 3: |
||||||
// need to separate k from hundres
|
// need to separate k from hundres
|
||||||
as_k := math.Floor(float64(number) / 1000.0) |
as_k := math.Floor(float64(number) / 1000.0) |
||||||
mod := math.Ceil(float64(int(number) % 1000)) |
mod := math.Ceil(float64(int(number) % 1000)) |
||||||
return fmt.Sprintf("%d.%dk", int(as_k), int(mod)) |
return fmt.Sprintf("%d.%dk", int(as_k), int(mod)) |
||||||
case mag > 3 && mag < 6:
|
case mag > 3 && mag < 6: |
||||||
as_m := math.Ceil(float64(number) / 1000.0) |
as_m := math.Ceil(float64(number) / 1000.0) |
||||||
return fmt.Sprintf("%dk", int(as_m)) |
return fmt.Sprintf("%dk", int(as_m)) |
||||||
case mag == 6: |
case mag == 6: |
||||||
// need to separate mil from k
|
// need to separate mil from k
|
||||||
as_m := math.Floor(float64(number) / 1000000.0) |
as_m := math.Floor(float64(number) / 1000000.0) |
||||||
mod := math.Ceil(float64(int(number) % 1000000) / 1000.0) |
mod := math.Ceil(float64(int(number) % 1000000) / 1000.0) |
||||||
return fmt.Sprintf("%d.%dM", int(as_m), int(mod)) |
return fmt.Sprintf("%d.%dM", int(as_m), int(mod)) |
||||||
case mag > 6 && mag <= 9: |
case mag > 6 && mag <= 9: |
||||||
as_m := math.Ceil(float64(number) / 1000000.0) |
as_m := math.Ceil(float64(number) / 1000000.0) |
||||||
return fmt.Sprintf("%dM", int(as_m)) |
return fmt.Sprintf("%dM", int(as_m)) |
||||||
default: |
default: |
||||||
return fmt.Sprintf("%sbesos", num) |
return fmt.Sprintf("%sbesos", num) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
func main() { |
func main() { |
||||||
opts, nums := parse_opts() |
opts := ParseOpts() |
||||||
|
|
||||||
if opts.From != "" { |
if opts.From != "" { |
||||||
log.Fatal("you should implement this") |
log.Fatal("you should implement this") |
||||||
} |
} |
||||||
|
|
||||||
if len(nums) == 0 { |
if len(opts.Numbers) == 0 { |
||||||
scanner := bufio.NewScanner(os.Stdin) |
scanner := bufio.NewScanner(os.Stdin) |
||||||
for scanner.Scan() { |
for scanner.Scan() { |
||||||
num := scanner.Text() |
num := scanner.Text() |
||||||
fmt.Println(to_si(num)) |
fmt.Println(to_si(num)) |
||||||
} |
} |
||||||
} else { |
} else { |
||||||
for _, num := range nums { |
for _, num := range opts.Numbers { |
||||||
fmt.Println(to_si(num)) |
fmt.Println(to_si(num)) |
||||||
} |
|
||||||
} |
} |
||||||
|
} |
||||||
} |
} |
||||||
|
|||||||
@ -1,45 +1,45 @@ |
|||||||
package main |
package main |
||||||
|
|
||||||
import ( |
import ( |
||||||
"fmt" |
"fmt" |
||||||
"flag" |
"flag" |
||||||
"os" |
"os" |
||||||
"log" |
"log" |
||||||
"crypto/sha512" |
"crypto/sha512" |
||||||
) |
) |
||||||
|
|
||||||
type Opts struct { |
type Opts struct { |
||||||
Inputs []string |
Inputs []string |
||||||
} |
} |
||||||
|
|
||||||
func parse_opts() Opts { |
func ParseOpts() Opts { |
||||||
var opts Opts |
var opts Opts |
||||||
|
|
||||||
flag.Parse() |
flag.Parse() |
||||||
opts.Inputs = flag.Args() |
opts.Inputs = flag.Args() |
||||||
|
|
||||||
return opts |
return opts |
||||||
} |
} |
||||||
|
|
||||||
func to_hex(hash [sha512.Size]byte) string { |
func ToHex(hash [sha512.Size]byte) string { |
||||||
result := "" |
result := "" |
||||||
|
|
||||||
for _, b := range hash { |
for _, b := range hash { |
||||||
result += fmt.Sprintf("%x", b) |
result += fmt.Sprintf("%x", b) |
||||||
} |
} |
||||||
|
|
||||||
return result |
return result |
||||||
} |
} |
||||||
|
|
||||||
func main() { |
func main() { |
||||||
opts := parse_opts() |
opts := ParseOpts() |
||||||
|
|
||||||
for _, fname := range opts.Inputs { |
for _, fname := range opts.Inputs { |
||||||
in_data, err := os.ReadFile(fname) |
in_data, err := os.ReadFile(fname) |
||||||
if err != nil { log.Fatal(err) } |
if err != nil { log.Fatal(err) } |
||||||
|
|
||||||
hash := sha512.Sum512(in_data) |
hash := sha512.Sum512(in_data) |
||||||
|
|
||||||
fmt.Println(to_hex(hash), fname) |
fmt.Println(ToHex(hash), fname) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue