Browse Source

initial commit

Kajetan Johannes Hammerle 4 years ago
commit
1ed2c01fed
3 changed files with 90 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 79 0
      Main.c
  3. 10 0
      Makefile

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+number_converter

+ 79 - 0
Main.c

@@ -0,0 +1,79 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef unsigned int uint;
+typedef char* string;
+
+uint getNumberOfDigits(uint number) {
+    uint counter = 1;
+    while(number >= 10) {
+        number /= 10;
+        counter++;
+    }
+    return counter;
+}
+
+int convertNumberToDigits(uint number, char* buffer, uint bufferLength, const string* digits, uint dWidth, uint dHeight) {
+    uint digitAmount = getNumberOfDigits(number);
+    uint lineLength = digitAmount * dWidth + 1;
+    uint length = lineLength * dHeight;
+    if(length >= bufferLength) {
+        return 1;
+    }
+    for(uint i = 0; i < digitAmount; i++) {
+        uint digit = number % 10;
+        uint offset = (digitAmount - 1 - i) * dWidth;
+        for(uint line = 0; line < dHeight; line++) {
+            memcpy(buffer + offset + lineLength * line, digits[digit] + dWidth * line, dWidth);
+        }
+        number /= 10;
+    }
+    for(uint line = 1; line < dHeight; line++) {
+        buffer[lineLength * line - 1] = '\n';
+    }
+    buffer[length - 1] = '\0';
+    return 0;
+}
+
+int convertNumber(uint number, char* buffer, uint bufferLength) {
+    const string digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
+    return convertNumberToDigits(number, buffer, bufferLength, digits, 1, 1);
+}
+
+void printSmallDigits(uint number) {
+    char buffer[128];
+    if(convertNumber(number, buffer, 128)) {
+        return;
+    }
+    puts(buffer);
+}
+
+void printBigDigits(uint number) {
+    const uint digitWidth = 3;
+    const uint digitHeight = 3;
+    const string digits[] = {
+        " _ | ||_|", // 0
+        "     |  |", // 1
+        " _  _||_ ", // 2
+        " _  _| _|", // 3
+        "   |_|  |", // 4
+        " _ |_  _|", // 5
+        " _ |_ |_|", // 6
+        " _   |  |", // 7
+        " _ |_||_|", // 8
+        " _ |_| _|" //  9
+    };
+
+    char buffer[128];
+    if(convertNumberToDigits(number, buffer, 128, digits, digitWidth, digitHeight)) {
+        return;
+    }
+    puts(buffer);
+}
+
+int main() {
+    printSmallDigits(1234567890);
+    printBigDigits(1234567890);
+    return EXIT_SUCCESS;
+}

+ 10 - 0
Makefile

@@ -0,0 +1,10 @@
+all: number_converter
+
+run: number_converter
+	./number_converter
+	
+number_converter: Main.c
+	gcc -o number_converter Main.c -Wall -Wextra -Werror -pedantic
+	
+clean:
+	rm -f number_converter