update: foundations
This commit is contained in:
@ -16,10 +16,18 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
const usageMsg = `Type a couple of unique numbers.
|
||||
Separate them with spaces.`
|
||||
|
||||
// remember [1:] skips the first argument
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
main:
|
||||
for _, arg := range os.Args[1:] {
|
||||
for _, arg := range args {
|
||||
n, err := strconv.Atoi(arg)
|
||||
if err != nil {
|
||||
// skip non-numerics
|
||||
|
@ -15,31 +15,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
validOps = "%*/+-"
|
||||
|
||||
usageMsg = "Usage: [op=" + validOps + "] [size]"
|
||||
sizeMissingMsg = "Size is missing"
|
||||
validOps = "* / + - mul div add sub"
|
||||
usageMsg = "Usage: [valid ops: " + validOps + "] [size]"
|
||||
sizeMissingMsg = "Size is missing\n" + usageMsg
|
||||
invalidOpMsg = `Invalid operator.
|
||||
Valid ops one of: ` + validOps
|
||||
|
||||
invalidOp = -1
|
||||
)
|
||||
|
||||
func main() {
|
||||
// CHECK THE ARGUMENTS
|
||||
args := os.Args[1:]
|
||||
|
||||
switch l := len(args); {
|
||||
case l == 1:
|
||||
if l := len(args); l == 1 {
|
||||
fmt.Println(sizeMissingMsg)
|
||||
fallthrough
|
||||
case l < 1:
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
op := args[0]
|
||||
if strings.IndexAny(op, validOps) == invalidOp {
|
||||
fmt.Println(invalidOpMsg)
|
||||
} else if l < 1 {
|
||||
fmt.Println(usageMsg)
|
||||
return
|
||||
}
|
||||
|
||||
@ -49,12 +39,32 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// CHECK THE VALIDITY OF THE OP
|
||||
op, ops := args[0], strings.Fields(validOps)
|
||||
|
||||
var ok bool
|
||||
for _, o := range ops {
|
||||
if strings.ToLower(o) == op {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
fmt.Println(invalidOpMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// PRINT THE TABLE
|
||||
|
||||
// HEADER
|
||||
fmt.Printf("%5s", op)
|
||||
for i := 0; i <= size; i++ {
|
||||
fmt.Printf("%5d", i)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
// CELLS
|
||||
for i := 0; i <= size; i++ {
|
||||
fmt.Printf("%5d", i)
|
||||
|
||||
@ -62,22 +72,24 @@ func main() {
|
||||
var res int
|
||||
|
||||
switch op {
|
||||
case "*":
|
||||
default:
|
||||
fallthrough // default is multiplication
|
||||
case "*", "mul":
|
||||
res = i * j
|
||||
case "%":
|
||||
if j != 0 {
|
||||
res = i % j
|
||||
}
|
||||
case "/":
|
||||
if j != 0 {
|
||||
res = i / j
|
||||
}
|
||||
case "+":
|
||||
case "+", "add":
|
||||
res = i + j
|
||||
case "-":
|
||||
case "-", "sub":
|
||||
res = i - j
|
||||
case "%", "mod":
|
||||
if j == 0 {
|
||||
// continue // will continue the loop
|
||||
break // breaks the switch
|
||||
}
|
||||
res = i % j
|
||||
}
|
||||
|
||||
// break // breaks the loop
|
||||
|
||||
fmt.Printf("%5d", res)
|
||||
}
|
||||
fmt.Println()
|
||||
|
@ -44,7 +44,7 @@ func main() {
|
||||
verbose = true
|
||||
}
|
||||
|
||||
guess, err := strconv.Atoi(args[0])
|
||||
guess, err := strconv.Atoi(args[len(args)-1])
|
||||
if err != nil {
|
||||
fmt.Println("Not a number.")
|
||||
return
|
||||
@ -75,13 +75,36 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
msg := "%s Try again?\n"
|
||||
// msg, n := "%s Try again?\n", rand.Intn(5)
|
||||
// if msg, n := "%s Try again?\n", rand.Intn(5); n <= 2 {
|
||||
// fmt.Printf(msg, "☠️ YOU LOST...")
|
||||
// } else if n < 3 {
|
||||
// fmt.Printf(msg, "☠️ JUST A BAD LUCK...")
|
||||
// } else if n == 4 {
|
||||
// fmt.Printf(msg, "☠️ TRY NEXT TIME...")
|
||||
// }
|
||||
|
||||
switch rand.Intn(2) {
|
||||
case 0:
|
||||
fmt.Printf(msg, "☠️ YOU LOST...")
|
||||
case 1:
|
||||
fmt.Printf(msg, "☠️ JUST A BAD LUCK...")
|
||||
// var msg string
|
||||
// switch rand.Intn(10) {
|
||||
// // more probability
|
||||
// case 0, 1, 2, 3, 4, 5:
|
||||
// msg = "☠️ YOU LOST..."
|
||||
// case 6, 7, 8:
|
||||
// msg = "☠️ JUST A BAD LUCK..."
|
||||
// default:
|
||||
// msg = "☠️ TRY NEXT TIME..."
|
||||
// }
|
||||
// fmt.Printf("%s Try again?\n", msg)
|
||||
|
||||
var msg string
|
||||
switch n := rand.Intn(10); {
|
||||
// more probability
|
||||
case n <= 5:
|
||||
msg = "☠️ YOU LOST..."
|
||||
case n <= 8:
|
||||
msg = "☠️ JUST A BAD LUCK..."
|
||||
default:
|
||||
msg = "☠️ TRY NEXT TIME..."
|
||||
}
|
||||
|
||||
fmt.Printf("%s Try again?\n", msg)
|
||||
}
|
||||
|
@ -32,9 +32,11 @@ func main() {
|
||||
for i, w := range words {
|
||||
q, w = strings.ToLower(q), strings.ToLower(w)
|
||||
|
||||
if strings.Contains(w, q) {
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
if !strings.Contains(w, q) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("#%-2d: %q\n", i+1, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user