From f036029205e23b15fe6175a02e2d8598c5a49f87 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Sat, 18 Jun 2022 17:21:38 +0200 Subject: [PATCH] 4.3 --- Uebung 4/Uebung4_3/Makefile | 52 +++++++++++++++++++++++++++++++++ Uebung 4/Uebung4_3/digitSum.cpp | 18 ++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Uebung 4/Uebung4_3/Makefile create mode 100644 Uebung 4/Uebung4_3/digitSum.cpp diff --git a/Uebung 4/Uebung4_3/Makefile b/Uebung 4/Uebung4_3/Makefile new file mode 100644 index 0000000..d266e29 --- /dev/null +++ b/Uebung 4/Uebung4_3/Makefile @@ -0,0 +1,52 @@ + +# Name of the binary for Development +BINARY = main +# Name of the binary for Release +FINAL = prototyp +# Object files +OBJS = digitSum.o +# Compiler flags +CFLAGS = -Werror -Wall -std=c++17 -fsanitize=address,undefined -g +# Linker flags +LFLAGS = -fsanitize=address,undefined +#Which Compiler to use +COMPILER = c++ + + +# all target: builds all important targets +all: binary + +final : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${FINAL} ${OBJS} + rm ${OBJS} + +binary : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + +# Links the binary +${BINARY} : ${OBJS} + ${COMPILER} ${LFLAGS} -o ${BINARY} ${OBJS} + + +# Compiles a source-file (any file with file extension .c) into an object-file +# +# "%" is a wildcard which matches every file-name (similar to * in regular expressions) +# Such a rule is called a pattern rule (because it matches a pattern, see https://www.gnu.org/software/make/manual/html_node/Pattern-Rules.html), +# which are a form of so called implicit rules (see https://www.gnu.org/software/make/manual/html_node/Implicit-Rules.html) +# "$@" and "$<" are so called automatic variables (see https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) +%.o : %.cpp + ${COMPILER} -c ${CFLAGS} -o $@ $< + + +# Rules can not only be used for compiling a program but also for executing a program +run: ${BINARY} + ./${BINARY} + + +# Delete all build artifacts +clean : + rm -rf ${BINARY} ${OBJS} + + +# all and clean are a "phony" targets, meaning they are no files +.PHONY : all clean diff --git a/Uebung 4/Uebung4_3/digitSum.cpp b/Uebung 4/Uebung4_3/digitSum.cpp new file mode 100644 index 0000000..873c390 --- /dev/null +++ b/Uebung 4/Uebung4_3/digitSum.cpp @@ -0,0 +1,18 @@ +#include +#include + +/* algorithm that "hashes" the through digit sum. The size needed for + * this would be 64, since telephone numbers are exactly 7 digits long and the + * maximum value a digit can have is 9 -> 7*9 = 63, plus 1 since we start at + * zero. */ +int digitSum(std::string telNr) { + uint32_t number = std::stoi(telNr); + uint8_t digitSum = 0; + while (number > 0) { + digitSum += number % 10; + number /= 10; + } + return digitSum; +} + +int main() { std::cout << digitSum("9797979") << std::endl; } \ No newline at end of file