Main.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef unsigned int uint;
  5. typedef char* string;
  6. uint getNumberOfDigits(uint number) {
  7. uint counter = 1;
  8. while(number >= 10) {
  9. number /= 10;
  10. counter++;
  11. }
  12. return counter;
  13. }
  14. int convertNumberToDigits(uint number, char* buffer, uint bufferLength, const string* digits, uint dWidth, uint dHeight) {
  15. uint digitAmount = getNumberOfDigits(number);
  16. uint lineLength = digitAmount * dWidth + 1;
  17. uint length = lineLength * dHeight;
  18. if(length >= bufferLength) {
  19. return 1;
  20. }
  21. for(uint i = 0; i < digitAmount; i++) {
  22. uint digit = number % 10;
  23. uint offset = (digitAmount - 1 - i) * dWidth;
  24. for(uint line = 0; line < dHeight; line++) {
  25. memcpy(buffer + offset + lineLength * line, digits[digit] + dWidth * line, dWidth);
  26. }
  27. number /= 10;
  28. }
  29. for(uint line = 1; line < dHeight; line++) {
  30. buffer[lineLength * line - 1] = '\n';
  31. }
  32. buffer[length - 1] = '\0';
  33. return 0;
  34. }
  35. int convertNumber(uint number, char* buffer, uint bufferLength) {
  36. const string digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
  37. return convertNumberToDigits(number, buffer, bufferLength, digits, 1, 1);
  38. }
  39. void printSmallDigits(uint number) {
  40. char buffer[128];
  41. if(convertNumber(number, buffer, 128)) {
  42. return;
  43. }
  44. puts(buffer);
  45. }
  46. void printBigDigits(uint number) {
  47. const uint digitWidth = 3;
  48. const uint digitHeight = 3;
  49. const string digits[] = {
  50. " _ | ||_|", // 0
  51. " | |", // 1
  52. " _ _||_ ", // 2
  53. " _ _| _|", // 3
  54. " |_| |", // 4
  55. " _ |_ _|", // 5
  56. " _ |_ |_|", // 6
  57. " _ | |", // 7
  58. " _ |_||_|", // 8
  59. " _ |_| _|" // 9
  60. };
  61. char buffer[128];
  62. if(convertNumberToDigits(number, buffer, 128, digits, digitWidth, digitHeight)) {
  63. return;
  64. }
  65. puts(buffer);
  66. }
  67. int main() {
  68. printSmallDigits(1234567890);
  69. printBigDigits(1234567890);
  70. return EXIT_SUCCESS;
  71. }