Doop
Doop is a function that takes 3 command-line arguments: two numbers and an operator, and performs the corresponding arithmetic operation.
Functions
In this exercice you don't have access to strconv package, which contains the tools to transform int and strings.
We will need to perform arithmetic operations on our arguments, that are strings, so we will have to convert them to int.
To show the result with z01.PrintRune function, we will need to swap it back to string, so understanding of Itoa and Atoi functions are needed for this exercice.
PrintStr function
We will use a simple function called PrintStr that will allow us to print strings.
func PrintStr(s string) {
for i := 0; i < len(s); i++ {
z01.PrintRune(rune(s[i]))
}
z01.PrintRune('\n')
}
Doop step by step
First step
We check if the lenght of our argument is exactly 4 (2 numbers 1 operand and the name of the file).
If not, then we end the programm.
if len(os.Args) != 4 {
return
}
Second step
We store our numbers and operand into viariable, and convert the numbers into int.
number1, err1 := AtoiDoop(os.Args[1])
number2, err2 := AtoiDoop(os.Args[3])
operator := os.Args[2]
result := 0
if err1 != nil || err2 != nil {
return
}
If that's the case, we just end the programm.
Third step
We then check for overflows, if we don't know the int overflow values, we just use the ones given in the example.
if number1 > 9223372036854775806 || number2 > 9223372036854775806 || number1 < -9223372036854775806 || number2 < -9223372036854775806 {
return
}
Fourth step
We need now to perform the arithmetic operation on our 2 numbers.
We use the switch case, it will detect what operand we have and perform the operation accordingly.
switch operator {
case "+":
result = number1 + number2
}
We then add a condition that end the programm if that happend.
case "/":
if number2 == 0 {
PrintStr("No division by 0")
return
}
result = number1 / number2
case "%":
if number2 == 0 {
PrintStr("No modulo by 0")
return
}
result = number1 % number2
If the second argument is equal to noone of the operands, then we end the programm
default:
return
switch operator {
case "+":
result = number1 + number2
case "-":
result = number1 - number2
case "*":
result = number1 * number2
case "/":
if number2 == 0 {
PrintStr("No division by 0")
return
}
result = number1 / number2
case "%":
if number2 == 0 {
PrintStr("No modulo by 0")
return
}
result = number1 % number2
default:
return
}
Fifth and final step
We now have to print the result, for that we will use the Itoa function to tranform it to a string.
We can then print it with our PrintStr function created earlier.
PrintStr(ItoaDoop(result))
Full code
package main
import (
"os"
)
func AtoiDoop(s string) (int, int) {
// implementation of AtoiDoop function
}
func ItoaDoop(n int) string {
// implementation of ItoaDoop function
}
func PrintStr(s string) {
for i := 0; i < len(s); i++ {
z01.PrintRune(rune(s[i]))
}
z01.PrintRune('\n')
}
func Doop() {
// first step
if len(os.Args) != 4 {
return
}
// second step
number1, err1 := AtoiDoop(os.Args[1])
number2, err2 := AtoiDoop(os.Args[3])
operator := os.Args[2]
result := 0
if err1 != nil || err2 != nil {
return
}
// third step
if number1 > 9223372036854775806 || number2 > 9223372036854775806 || number1 < -9223372036854775806 || number2 < -9223372036854775806 {
return
}
// fourth step
switch operator {
case "+":
result = number1 + number2
case "-":
result = number1 - number2
case "*":
result = number1 * number2
case "/":
if number2 == 0 {
PrintStr("No division by 0")
return
}
result = number1 / number2
case "%":
if number2 == 0 {
PrintStr("No modulo by 0")
return
}
result = number1 % number2
default:
return
}
// final step
PrintStr(ItoaDoop(result))
}