From 72906bac35e4f830c16cf5130f06bb62c3d3e339 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 25 Oct 2020 22:51:28 -0400 Subject: [PATCH] Working on exercise 3-5 but I'm creating some libraries --- ch_03/exercise_03-05/main.c | 17 +++ libraries/hex_octal_conv/compile.sh | 13 ++ libraries/hex_octal_conv/include/hex.h | 18 +++ libraries/hex_octal_conv/include/octal.h | 0 libraries/hex_octal_conv/src/hex.c | 166 +++++++++++++++++++++++ libraries/test_library1/compile.sh | 16 +++ libraries/test_library1/include/test.h | 13 ++ libraries/test_library1/src/test.c | 26 ++++ 8 files changed, 269 insertions(+) create mode 100644 ch_03/exercise_03-05/main.c create mode 100755 libraries/hex_octal_conv/compile.sh create mode 100644 libraries/hex_octal_conv/include/hex.h create mode 100644 libraries/hex_octal_conv/include/octal.h create mode 100644 libraries/hex_octal_conv/src/hex.c create mode 100755 libraries/test_library1/compile.sh create mode 100644 libraries/test_library1/include/test.h create mode 100644 libraries/test_library1/src/test.c diff --git a/ch_03/exercise_03-05/main.c b/ch_03/exercise_03-05/main.c new file mode 100644 index 0000000..5e9e76e --- /dev/null +++ b/ch_03/exercise_03-05/main.c @@ -0,0 +1,17 @@ +/* + * + * Exercise 3-5. Write a function itob(n,s,b that converts the integer + * m into a base b character representation in the string s. In + * particular, itob(n,s,16) formats s as a hexadecimal integer in s. + * + * + * Author: Kun Deng + */ + +#include + +int main() +{ + + return 0; +} diff --git a/libraries/hex_octal_conv/compile.sh b/libraries/hex_octal_conv/compile.sh new file mode 100755 index 0000000..6830d28 --- /dev/null +++ b/libraries/hex_octal_conv/compile.sh @@ -0,0 +1,13 @@ +if [ ! -d "bin" ]; then + mkdir bin + echo "created bin directory" +fi + +if [ ! -d "bin/static" ]; then + mkdir bin/static + echo "created bin/static directory" +fi + +gcc -c src/hex.c -Iinclude -o bin/static/hex.o + +ar rcs bin/static/libhexoctalconv.a bin/static/hex.o diff --git a/libraries/hex_octal_conv/include/hex.h b/libraries/hex_octal_conv/include/hex.h new file mode 100644 index 0000000..5bf8751 --- /dev/null +++ b/libraries/hex_octal_conv/include/hex.h @@ -0,0 +1,18 @@ +#include + +#define BUFFER_LIMIT 1024 + + +char hex_value[BUFFER_LIMIT]; + +char *HexOctalConv_number_converted_to_hex(char *value); + +int HexOctalConv_hex_part_to_number(char hex_part); + +long HexOctalConv_rudy_pow(long value, int power); + +int HexOctalConv_size_of_string(char *value); + +char HexOctalConv_convert_hex_part(int value); + +void HexOctalConv_reverse_order(char *value); diff --git a/libraries/hex_octal_conv/include/octal.h b/libraries/hex_octal_conv/include/octal.h new file mode 100644 index 0000000..e69de29 diff --git a/libraries/hex_octal_conv/src/hex.c b/libraries/hex_octal_conv/src/hex.c new file mode 100644 index 0000000..63d83f7 --- /dev/null +++ b/libraries/hex_octal_conv/src/hex.c @@ -0,0 +1,166 @@ +#include "hex.h" + +#include +#include + + +char *HexOctalConv_number_converted_to_hex(char *value) +{ + int number_value = atoi(value); + int string_size = HexOctalConv_size_of_string(value); + + extern char hex_value[]; + + int hex_start = 0; + + for (double number = number_value;;) + { + const double quotient = number / 16.0; + const int quotient_trunecated = number / 16; + const double remaining = quotient - quotient_trunecated; + int number_hex_part = remaining * 16; + + char hex_part = HexOctalConv_convert_hex_part(number_hex_part); + hex_value[hex_start++] = hex_part; + + /** + if (number >= 16) + { + } + else + { + } + */ + + number = quotient_trunecated; + } + + hex_value[hex_start] = '\0'; + + if (hex_start > 1) + { + HexOctalConv_reverse_order(hex_value); + } + + return hex_value; +} + +int HexOctalConv_hex_part_to_number(char hex_part) +{ + const char hex_part_lowered = tolower(hex_part); + int result = 0; + + switch (hex_part_lowered) + { + case 'f': + result = 15; + break; + case 'e': + result = 14; + break; + case 'd': + result = 13; + break; + case 'c': + result = 12; + break; + case 'b': + result = 11; + break; + case 'a': + result = 10; + break; + default: + result = hex_part_lowered - '0'; + break; + } + + return result; +} + +long HexOctalConv_rudy_pow(long value, int power) +{ + if (power > 2) + { + return value * HexOctalConv_rudy_pow(value, --power); + } + else if (power == 2) + { + return value * value; + } + else if (power == 1) + { + return value; + } + else + { + return 1; + } +} + + +int HexOctalConv_size_of_string(char *value) +{ + int size = 0; + + for (; value[size] != '\0'; ++size); + + return size; +} + + +char HexOctalConv_convert_hex_part(int value) +{ + char result; + + switch (value) + { + case 15: + result = 'F'; + break; + case 14: + result = 'E'; + break; + case 13: + result = 'D'; + break; + case 12: + result = 'C'; + break; + case 11: + result = 'B'; + break; + case 10: + result = 'A'; + break; + default: + result = value + '0'; + break; + } + + return result; +} + + +void HexOctalConv_reverse_order(char *value) +{ + int string_size = HexOctalConv_size_of_string(value); + + for (int i = 0;; ++i) + { + char tmp = value[string_size - i - 1]; + value[string_size - i - 1] = value[i]; + value[i] = tmp; + + int is_odd = string_size % 2; + + if (is_odd == 1 && (string_size / 2) == ((i + 1))) + { + break; + } + else if (is_odd == 0 && (string_size / 2) == (i + 1)) + { + break; + } + } +} diff --git a/libraries/test_library1/compile.sh b/libraries/test_library1/compile.sh new file mode 100755 index 0000000..ffaa7e7 --- /dev/null +++ b/libraries/test_library1/compile.sh @@ -0,0 +1,16 @@ +if [ ! -d "bin" ]; then + mkdir bin + echo "created bin directory" +fi + +if [ ! -d "bin/static" ]; then + mkdir bin/static + echo "created bin/static directory" +fi + +gcc -c src/test.c -Iinclude -o bin/static/test.o + +# gcc -fPIC src/test.c -Iinclude -o bin/shared/test.o + +ar rcs bin/static/libtestlibrary.a bin/static/test.o + diff --git a/libraries/test_library1/include/test.h b/libraries/test_library1/include/test.h new file mode 100644 index 0000000..4c40946 --- /dev/null +++ b/libraries/test_library1/include/test.h @@ -0,0 +1,13 @@ +#include + +// Returns a test value +int TestLibrary_test_value(); + +// Multiplies the value by 1.5 +double TestLibrary_multiply_by_one_point_five(const double value); + + +void __attribute__((constructor)) initLibrary(void); + +void __attribute__((destructor)) cleanUpLibrary(void); + diff --git a/libraries/test_library1/src/test.c b/libraries/test_library1/src/test.c new file mode 100644 index 0000000..960c97e --- /dev/null +++ b/libraries/test_library1/src/test.c @@ -0,0 +1,26 @@ + + +// #include "../include/test.h" +#include "test.h" + +int TestLibrary_test_value() +{ + return 48; +} + + +double TestLibrary_multiply_by_one_point_five(const double value) +{ + return value * 1.5; +} + + +void __attribute__((constructor)) initLibrary(void) +{ + printf("TestLibrary Initialized\n"); +} + +void __attribute__((destructor)) cleanUpLibrary(void) +{ + printf("TestLibrary is exiting\n"); +}