139 lines
3.1 KiB
Markdown
139 lines
3.1 KiB
Markdown
# Calculator
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func calculate(a, b int, operator rune) int {
|
|
switch operator {
|
|
case '+':
|
|
return a + b
|
|
case '-':
|
|
return a - b
|
|
case '*':
|
|
return a * b
|
|
case '/':
|
|
if b != 0 {
|
|
return a / b
|
|
}
|
|
fmt.Println("Division durch Null ist nicht erlaubt.")
|
|
return 0
|
|
case '%':
|
|
if b != 0 {
|
|
return a % b
|
|
}
|
|
fmt.Println("Modulo durch Null ist nicht erlaubt.")
|
|
return 0
|
|
default:
|
|
fmt.Println("Ungültiger Operator.")
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(calculate(2, 5, '+')) // 7
|
|
fmt.Println(calculate(2, 5, '-')) // -3
|
|
fmt.Println(calculate(2, 5, '*')) // 10
|
|
fmt.Println(calculate(10, 5, '/')) // 2
|
|
fmt.Println(calculate(5, 3, '%')) // 2
|
|
}
|
|
|
|
```
|
|
|
|
# Student
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// Student repräsentiert einen Studenten
|
|
type Student struct {
|
|
Vorname string
|
|
Nachname string
|
|
Matrikelnummer string
|
|
Alter int
|
|
}
|
|
|
|
// Konstruktor für die Klasse Student
|
|
func NewStudent(vorname, nachname, matrikelnummer string, alter int) *Student {
|
|
return &Student{
|
|
Vorname: vorname,
|
|
Nachname: nachname,
|
|
Matrikelnummer: matrikelnummer,
|
|
Alter: alter,
|
|
}
|
|
}
|
|
|
|
// Greet gibt eine Begrüßungsnachricht zurück
|
|
func (s *Student) Greet() string {
|
|
return fmt.Sprintf("Hallo, mein Name ist %s %s.", s.Vorname, s.Nachname)
|
|
}
|
|
|
|
// Überschreiben der String-Methode für eine lesbare Darstellung
|
|
func (s *Student) String() string {
|
|
return fmt.Sprintf("Student: %s %s, Matrikelnummer: %s, Alter: %d", s.Vorname, s.Nachname, s.Matrikelnummer, s.Alter)
|
|
}
|
|
|
|
func main() {
|
|
// Erstellen eines neuen Studenten mit dem Konstruktor
|
|
student := NewStudent("Franz", "Herta", "123456", 20)
|
|
|
|
// Aufrufen der Greet-Methode
|
|
fmt.Println(student.Greet()) // Hallo, mein Name ist Franz Herta.
|
|
|
|
// Ausgabe der String-Methode
|
|
fmt.Println(student) // Student: Franz Herta, Matrikelnummer: 123456, Alter: 20
|
|
}
|
|
|
|
```
|
|
|
|
# R-P-S
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
ROCK = 'r'
|
|
PAPER = 'p'
|
|
SCISSORS = 's'
|
|
)
|
|
|
|
func main() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
playerScore, computerScore := 0, 0
|
|
choices := []rune{ROCK, PAPER, SCISSORS}
|
|
|
|
for round := 1; round <= 3; round++ {
|
|
var playerChoice rune
|
|
fmt.Printf("Runde %d: Dein Zug ([r]rock, [p]aper, [s]cissors)? ", round)
|
|
fmt.Scanf("%c\n", &playerChoice)
|
|
|
|
computerChoice := choices[rand.Intn(len(choices))]
|
|
|
|
switch {
|
|
case playerChoice == computerChoice:
|
|
fmt.Printf("Unentschieden - Du: %c, Computer: %c - [%d:%d]\n", playerChoice, computerChoice, playerScore, computerScore)
|
|
case (playerChoice == ROCK && computerChoice == SCISSORS) || (playerChoice == PAPER && computerChoice == ROCK) || (playerChoice == SCISSORS && computerChoice == PAPER):
|
|
playerScore++
|
|
fmt.Printf("Du hast gewonnen - Du: %c, Computer: %c - [%d:%d]\n", playerChoice, computerChoice, playerScore, computerScore)
|
|
default:
|
|
computerScore++
|
|
fmt.Printf("Der Computer hat gewonnen - Du: %c, Computer: %c - [%d:%d]\n", playerChoice, computerChoice, playerScore, computerScore)
|
|
}
|
|
}
|
|
|
|
fmt.Printf("Ergebnis - Du %d Punkt(e), Computer %d Punkt(e)\n", playerScore, computerScore)
|
|
}
|
|
|
|
``` |