1
0
Fork 0

Compare commits

...

20 Commits

Author SHA1 Message Date
Oliver Stolle cff08c274c fixed some comment & a little refactoring 2026-04-04 13:39:20 +00:00
Oliver Stolle 6af376c1c4 Merge remote-tracking branch 'upstream/main' 2026-04-04 13:31:04 +00:00
Sebastian Steger c11a7dfcfc Merge branch 'go-basics-slice-exercise' of https://gitty.informatik.th-mannheim.de/steger/pr3-code 2026-03-31 15:08:59 +00:00
Sebastian Steger a6f88eeafd slice exercise 2026-03-31 15:08:13 +00:00
Sebastian Steger ce34567ee0 add code comments 2026-03-31 16:59:21 +02:00
Sebastian Steger e8388ef484 structs 2026-03-31 16:59:21 +02:00
Sebastian Steger 9c6b07fb61 functions 2026-03-31 16:59:21 +02:00
Sebastian Steger 01686901e3 enums 2026-03-31 16:59:21 +02:00
Sebastian Steger 0777a631a7 maps 2026-03-31 16:59:21 +02:00
Sebastian Steger e4cc4d76a3 slices 2026-03-31 16:59:21 +02:00
Sebastian Steger bda5a2203f pointers 2026-03-31 16:59:21 +02:00
Sebastian Steger 9125300735 arrays 2026-03-31 16:59:21 +02:00
Sebastian Steger dc666aa93b switch 2026-03-31 16:59:21 +02:00
Sebastian Steger be518dba65 ifelse 2026-03-31 16:59:21 +02:00
Sebastian Steger bf182eaefb for 2026-03-31 16:59:21 +02:00
Sebastian Steger e99201a8d6 constants 2026-03-31 16:59:21 +02:00
Sebastian Steger 8c76e7d705 variables 2026-03-31 16:59:21 +02:00
Sebastian Steger c964dff5bb values 2026-03-31 16:59:21 +02:00
Sebastian Steger 8bbadc4b5e Merge branch 'go-basics-calculator' of https://gitty.informatik.th-mannheim.de/steger/pr3-code 2026-03-31 14:57:28 +00:00
Sebastian Steger b376482644 calculator solution 2025-08-20 06:32:48 +00:00
3 changed files with 72 additions and 13 deletions

View File

@ -5,18 +5,46 @@ import (
) )
func main() { func main() {
const (
add = "add"
subtract = "subtract"
multiply = "multiply"
divide = "divide"
exit = "exit"
)
//TODO: implement according to README.md fmt.Println("Welcome to the Simple Calculator!")
for {
var operation string
fmt.Print("\nEnter operation (add, subtract, multiply, divide, exit): ")
fmt.Scan(&operation)
//the following code just demonstrates how to use fmt.Scan if operation == exit {
fmt.Println("Goodbye!")
break
}
var str string var num1, num2 float64
fmt.Print("Please enter a string: ") fmt.Print("Enter the first number: ")
fmt.Scan(&str) fmt.Scan(&num1)
fmt.Print("Enter the second number: ")
fmt.Scan(&num2)
var x float64 switch operation {
fmt.Print("Please enter a float: ") case add:
fmt.Scan(&x) fmt.Printf("Result: %.2f\n", num1+num2)
case subtract:
fmt.Printf("You entered '%s' and %f\n", str, x) fmt.Printf("Result: %.2f\n", num1-num2)
case multiply:
fmt.Printf("Result: %.2f\n", num1*num2)
case divide:
if num2 == 0 {
fmt.Println("Error: Division by zero is not allowed!")
} else {
fmt.Printf("Result: %.2f\n", num1/num2)
}
default:
fmt.Println("Invalid operation. Please try again.")
}
}
} }

View File

@ -0,0 +1,31 @@
package main
import (
"fmt"
"runtime"
)
func main() {
//create an array with a lot of data
massiveData := make([]int, 1_000_000)
for i := range massiveData {
massiveData[i] = i
}
// Task: Extract the first 8 elements safely
//1. attempt: use the slice operator
//smallSlice := massiveData[:8]
// consequence -> smallSlice and massiveData point to the same underlying array.
// Therefore, setting massiveData to nil is not sufficient to release that big array.
//2. attempt: create a new slice (with a new underlying array) and copy data
smallSlice := make([]int, 8)
copy(smallSlice, massiveData[:8])
massiveData = nil //don't use massive data anymore: Underlying array shall be freed.
runtime.GC() //run the garbage collector to make sure that all unused data is actually freed
//cap(smallSlice) should be 8 or similar, but not close to 1_000_000. The majority of the data should have been released by now
fmt.Printf("Small Slice: %v, Len: %d, Cap: %d\n", smallSlice, len(smallSlice), cap(smallSlice))
}

View File

@ -17,7 +17,7 @@ type Product struct {
price float32 price float32
} }
var initial_inventory = [1]Product{{ELECTRONICS, "Laptop", 10, 100.0}} //Subtask 2 => Did you mean "product(s)" instead of "product names" var initial_inventory = [1]Product{{ELECTRONICS, "Laptop", 10, 100.0}} //Subtask 2 => Did you mean "product(s)" instead of "product names"?
var inventory = initial_inventory[0:] //Subtask 3 var inventory = initial_inventory[0:] //Subtask 3
var product_categories = map[prod_category]string{ var product_categories = map[prod_category]string{
@ -53,8 +53,8 @@ func DisplayInventory() {
fmt.Println("Kategorie: |Name: |Preis: |Menge: |") fmt.Println("Kategorie: |Name: |Preis: |Menge: |")
for i := 0; i < len(inventory); i++ { for i := 0; i < len(inventory); i++ {
product := inventory[i]
fmt.Printf("%s | %s | %.2f | %.2f |\n", product_categories[product.category], product.name, product.price, product.quantitiy) fmt.Printf("%s | %s | %.2f | %.2f |\n", product_categories[inventory[i].category], inventory[i].name, inventory[i].price, inventory[i].quantitiy)
} }
fmt.Println() fmt.Println()
} }