Added last 2 functions

This commit is contained in:
marc 2024-10-19 12:21:52 +02:00
parent 06d59994bf
commit 20b90a2156
3 changed files with 146 additions and 7 deletions

View file

@ -1,9 +1,9 @@
/*----------------------------------------------------------
* HTBLA-Leonding / Class: <your class>
* HTBLA-Leonding / Class: 2IHIF
* ---------------------------------------------------------
* Exercise Number: 0
* Title: Pyramid of Numbers
* Author: <your name>
* Author: Marc Tismonar
* ----------------------------------------------------------
* Description:
* Calculates a pyramid of numbers, i.e., it multiplies a big
@ -13,6 +13,7 @@
* ----------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
/// 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;
}