forked from steger/pr3-sose2026
Compare commits
No commits in common. "cff08c274c27036cc8451ccae24b279b49328a18" and "b83f1365e4d33e012f3adbd2df445af4a7230666" have entirely different histories.
cff08c274c
...
b83f1365e4
|
|
@ -5,46 +5,18 @@ import (
|
|||
)
|
||||
|
||||
func main() {
|
||||
const (
|
||||
add = "add"
|
||||
subtract = "subtract"
|
||||
multiply = "multiply"
|
||||
divide = "divide"
|
||||
exit = "exit"
|
||||
)
|
||||
|
||||
fmt.Println("Welcome to the Simple Calculator!")
|
||||
for {
|
||||
var operation string
|
||||
fmt.Print("\nEnter operation (add, subtract, multiply, divide, exit): ")
|
||||
fmt.Scan(&operation)
|
||||
//TODO: implement according to README.md
|
||||
|
||||
if operation == exit {
|
||||
fmt.Println("Goodbye!")
|
||||
break
|
||||
}
|
||||
//the following code just demonstrates how to use fmt.Scan
|
||||
|
||||
var num1, num2 float64
|
||||
fmt.Print("Enter the first number: ")
|
||||
fmt.Scan(&num1)
|
||||
fmt.Print("Enter the second number: ")
|
||||
fmt.Scan(&num2)
|
||||
var str string
|
||||
fmt.Print("Please enter a string: ")
|
||||
fmt.Scan(&str)
|
||||
|
||||
switch operation {
|
||||
case add:
|
||||
fmt.Printf("Result: %.2f\n", num1+num2)
|
||||
case subtract:
|
||||
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.")
|
||||
}
|
||||
}
|
||||
var x float64
|
||||
fmt.Print("Please enter a float: ")
|
||||
fmt.Scan(&x)
|
||||
|
||||
fmt.Printf("You entered '%s' and %f\n", str, x)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
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))
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ type Product struct {
|
|||
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 product_categories = map[prod_category]string{
|
||||
|
|
@ -53,8 +53,8 @@ func DisplayInventory() {
|
|||
fmt.Println("Kategorie: |Name: |Preis: |Menge: |")
|
||||
|
||||
for i := 0; i < len(inventory); i++ {
|
||||
|
||||
fmt.Printf("%s | %s | %.2f | %.2f |\n", product_categories[inventory[i].category], inventory[i].name, inventory[i].price, inventory[i].quantitiy)
|
||||
product := inventory[i]
|
||||
fmt.Printf("%s | %s | %.2f | %.2f |\n", product_categories[product.category], product.name, product.price, product.quantitiy)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue