index — ti25-glauchau-code @ 6315f0b2a914eaaaa9d551fcee9cab6add5a59e3

Meine Lösungen (oder auch nicht) für die Programmieraufgaben in der TI25 an der Staatlichen Studienakademie Glauchau

discord_programmieraufgaben/2025-10-11_1/rechner.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
package main

import (
	"fmt"
	"bufio"
	"os"
	"log"
	"strconv"
)

func readInput() *string {
	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Rechnung: ")
	text, err := reader.ReadString('\n')
	if err != nil {
		log.Fatal(err)
	}
	text = text[:len(text)-1]

	return &text
}

func parseCalculation(calculation *string) *int64 {
	for i := 0; i < len(*calculation); i++ { // no range because unicode would be wasteful
		if (*calculation)[i] == '+' {
			return add(&i, calculation)
		}
	}
	singleNumber, err := strconv.ParseInt(*calculation, 10, 64)
	if err != nil {
		log.Fatal("Unbekanntes Rechenzeichen")
	}

	return &singleNumber
}

func add(i *int, calculation *string) *int64 {
	left, err := strconv.ParseInt((*calculation)[:*i], 10, 64)
	if err != nil {
		log.Fatal(err)
	}
	right, err := strconv.ParseInt((*calculation)[*i+1:], 10, 64)
	if err != nil {
		log.Fatal(err)
	}

	result := left + right

	return &result
	
}

func main() {
	inputptr := readInput()
	resultptr := parseCalculation(inputptr)

	fmt.Printf("Ergebnis: %d", *resultptr)
}