From 20b90a21568af03403610f31fab7ef5632b1127c Mon Sep 17 00:00:00 2001 From: marc Date: Sat, 19 Oct 2024 12:21:52 +0200 Subject: [PATCH] Added last 2 functions --- .vscode/tasks.json | 28 +++++++ assignment/pyramid_of_numbers | Bin 0 -> 16352 bytes assignment/pyramid_of_numbers.c | 125 ++++++++++++++++++++++++++++++-- 3 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 assignment/pyramid_of_numbers diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/assignment/pyramid_of_numbers b/assignment/pyramid_of_numbers new file mode 100644 index 0000000000000000000000000000000000000000..e74d8bb018dc7b17fa579984791c7376a43f074b GIT binary patch literal 16352 zcmeHOeQ;FO6~DU)7!*iQP(G|YElfcsOZX6j6bXeK?x4Ei&dLHoOav6&LU{BqDC9p{?2{(yu7?_ z?2OZCXF7W`dFP&w-?`_Wci-Fl-n;i9eZxAh$0N8@h}#8m{WX@PK`FF)RRL)b^+g>cbJWPrldi6fV}TB#Pfm7{5w%1e);j3btCd4oK01Gc||#& zloR@|SNU{P4LT~CJPP5)ZB;loZB+J5sSPSy-bn<$OnaLY&d{VA;ifH?STN=Gz6G2* zMatrf37_W8s=cw!L!HV$QQnJgX(j#f_pa*8?ECrrQwy$rtM|b7zxT|ChxW4%!jKIWvgaW| zJk_tnKiOFQ--DVZBYGzIS~tO`)0(yBV&AQHZOKx*y4ZJ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$wg0hA)hw@?`pgU24E~1>l4^3u=vA z3&C}?fEWJlXg-=C&j^kNg%2k}_;BrXGz#WX)~;(30;RAWO$-S}@q{xRl& z96!&CnR5F)FLuZ82cG7{jOTeX(<+rPijk^}=lN?0GU_tpc^=M`<{Qr9&j0;T(j1rZ zJg;S{9vkd=J=-ze1w74}ndf;k(+1VJJANe=dr_gBGM?w{OtZ?+oj>mXK80VS?D#o= zDL*$5o-pqG{S-3F3H!Hyuhj1rKVQH^7Wer-0I3tNDIC*n7R)~K&mq$%UHIKf!Ia}= z2h1~l&V}ds0@Ks{fTkR9yX@};7oO))OljWkEbji(_@e&v`4gJ5R@6`|6F2?@6i&Rs zI7M3+sHhwNI^<6LL4{-5LP|&Bys_wQR3_tJ-8Ss`J?|%V+cVF!0Bp4b&(9NswGg7P zoyCptEWZQ=jXiFk=lS;jU==@IKU5gU=kXVikqP5@|6X<>Sqm}GBKDE-Oh1RliRb6; zg2Ff0NUOp)rsDy2;(4Atug + * HTBLA-Leonding / Class: 2IHIF * --------------------------------------------------------- * Exercise Number: 0 * Title: Pyramid of Numbers - * Author: + * Author: Marc Tismonar * ---------------------------------------------------------- * Description: * Calculates a pyramid of numbers, i.e., it multiplies a big @@ -13,6 +13,7 @@ * ---------------------------------------------------------- */ #include +#include /// The maximum number of digits allowed in a big int. #define MAX_DIGITS 80 @@ -36,32 +37,99 @@ struct BigInt { *** @param *big_int The converted string now as BigInt. * @return The number of characters converted. */ -int strtobig_int(const char* str, int len, struct BigInt* big_int); +int strtobig_int(const char* str, int len, struct BigInt* big_int) { + int conversions = 0; + for (int i = 0; i < len; i++) + { + int converted = str[i] - '0'; + if (converted >= 0 && converted <= 9) + { + big_int->the_int[conversions] = converted; + conversions++; + } + else + { + big_int->digits_count = conversions; + return conversions; + } + } + big_int->digits_count = conversions; + return conversions; +} /** print_big_int() prints a BigInt. *** @param *big_int The BigInt to be printed. */ -void print_big_int(const struct BigInt *big_int); +void print_big_int(const struct BigInt *big_int) { + for (int i = 0; i < big_int->digits_count; i++) + { + printf("%d", big_int->the_int[i]); + } +} /** multiply() multiplies a BigInt by an int. *** @param big_int The BigInt to be multiplied. *** @param factor The int value which is multiplied by BigInt. *** @param *big_result The result of the multiplication. */ -void multiply(const struct BigInt* big_int, int factor, struct BigInt* big_result); +void multiply(const struct BigInt* big_int, int factor, struct BigInt* big_result) { + int overflow = 0; + int sizeOfResult = big_int->digits_count + 1; + char big_str_result[sizeOfResult]; + for(int i = 0; i < big_int->digits_count; i++) + { + int result = big_int->the_int[i] * factor + overflow; + if (result > 9) + { + overflow = result / 10; + result = result - overflow; + } + big_str_result[i] = result + '0'; + } + if (big_str_result[sizeOfResult - 1] == '0') + { + sizeOfResult--; + } + + strtobig_int(big_str_result, sizeOfResult, big_result); +} /** divide() multiplies a BigInt by an int. *** @param big_int The BigInt to be divided. *** @param divisor The int value by which we want to devide big_int. *** @param *big_result The result of the division. */ -void divide(const struct BigInt* big_int, int divisor, struct BigInt* big_result); +void divide(const struct BigInt* big_int, int divisor, struct BigInt* big_result) { + int remainder = 0; + int sizeOfResult = big_int->digits_count; + char str_result[sizeOfResult]; + for (int i = 0; i < big_int->digits_count; i++) + { + int calculate = remainder * 10; + calculate += big_int->the_int[i]; + int result = calculate / divisor; + if (result == 0) { + remainder = big_int->the_int[i]; + } + str_result[i] = result + '0'; + } + if (str_result[sizeOfResult - 1] == '0') + { + sizeOfResult--; + } + strtobig_int(str_result, sizeOfResult, big_result); +} /** copy_big_int() copies a BigInt to another BigInt. *** @param from The source where we want to copy from. *** @param *to The target where we want to copy to. */ -void copy_big_int(const struct BigInt* from, struct BigInt* to); +void copy_big_int(const struct BigInt* from, struct BigInt* to) { + to->digits_count = from->digits_count; + for (int i = 0; i < from->digits_count; i++) { + to->the_int[i] = from->the_int[i]; + } +} /** *** main() reads the base number from which the pyramid has to be calculated @@ -76,5 +144,48 @@ void copy_big_int(const struct BigInt* from, struct BigInt* to); */ int main(int argc, char* argv[]) { + printf("Pyramid of Numbers\n\n"); + + printf("Please enter a number: "); + char input_str[MAX_DIGITS]; + scanf("%s", input_str); + + struct BigInt original_big_int; + int len = strlen(input_str); + strtobig_int(input_str, len, &original_big_int); + + struct BigInt changed_big_int; + copy_big_int(&original_big_int, &changed_big_int); + + //multiply + for (int i = 2; i < 10; i++) + { + //create temp BigInt & multiply + struct BigInt temp; + multiply(&changed_big_int, i, &temp); + + print_big_int(&changed_big_int); + printf(" * %d = ", i); + print_big_int(&temp); + printf("\n"); + + copy_big_int(&temp, &changed_big_int); + } + + //divide + for (int i = 9; i > 1; i--) + { + //create temp BigInt & multiply + struct BigInt temp; + divide(&changed_big_int, i, &temp); + + print_big_int(&changed_big_int); + printf(" / %d = ", i); + print_big_int(&temp); + printf("\n"); + + copy_big_int(&temp, &changed_big_int); + } + return 0; }