From 9639c6453e4cdcb500a21bb8992bc4ee465d0101 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Thu, 29 Oct 2020 21:47:51 -0400 Subject: [PATCH] Added simple implementation of hex/octal conversion --- ch_03/exercise_03-05/compile.sh | 19 +++ ch_03/exercise_03-05/main.c | 31 ++++ experiments/call_library/compile.sh | 11 ++ experiments/call_library/main.c | 19 +++ libraries/hex_octal_conv/compile.sh | 20 ++- libraries/hex_octal_conv/include/hex.h | 20 +-- libraries/hex_octal_conv/include/octal.h | 0 libraries/hex_octal_conv/src/hex.c | 190 +++++++++++++++-------- 8 files changed, 239 insertions(+), 71 deletions(-) create mode 100755 ch_03/exercise_03-05/compile.sh create mode 100755 experiments/call_library/compile.sh create mode 100644 experiments/call_library/main.c delete mode 100644 libraries/hex_octal_conv/include/octal.h diff --git a/ch_03/exercise_03-05/compile.sh b/ch_03/exercise_03-05/compile.sh new file mode 100755 index 0000000..ac167b9 --- /dev/null +++ b/ch_03/exercise_03-05/compile.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +if [ ! -d "bin" ]; then + mkdir bin + echo "Created bin directory" +fi + +current_dir=`pwd` +hex_octal_conv_lib_root_dir="../../libraries/hex_octal_conv" +hex_octal_conv_lib_include_dir="$hex_octal_conv_lib_root_dir/include" +hex_octal_conv_lib_dir="$hex_octal_conv_lib_root_dir/bin/static" + +gcc -c main.c -I"$hex_octal_conv_lib_include_dir" -o bin/main.o -O3 + +cd $hex_octal_conv_lib_root_dir +./compile.sh "release" +cd $current_dir + +gcc bin/main.o -L"$hex_octal_conv_lib_dir" -lhexoctalconv -o bin/a.out -O3 diff --git a/ch_03/exercise_03-05/main.c b/ch_03/exercise_03-05/main.c index 5e9e76e..54cd39a 100644 --- a/ch_03/exercise_03-05/main.c +++ b/ch_03/exercise_03-05/main.c @@ -10,8 +10,39 @@ #include +#include "hex.h" + +void test(); + int main() { + test(); return 0; } + + +void test() +{ + char *number = "21"; + char hex_number[1024]; + + HexOctalConv_number_converted_to_hex(hex_number, number); + + printf("%s converted to hex is %s\n", number, hex_number); + + number = "55"; + HexOctalConv_number_converted_to_hex(hex_number, number); + printf("%s converted to hex is %s\n", number, hex_number); + + const int numerical_value = HexOctalConv_hex_converted_to_number(hex_number); + printf("Hexidecimal %s is %d\n", hex_number, numerical_value); + + char octal_number[1024]; + HexOctalConv_number_converted_to_octal(octal_number, number); + printf("%s converted to octal is %s\n", number, octal_number); + + const int o_numberical_value = HexOctalConv_octal_converted_to_number(octal_number); + printf("Octal %s converted to a numerical value is %d\n", octal_number, o_numberical_value); + +} diff --git a/experiments/call_library/compile.sh b/experiments/call_library/compile.sh new file mode 100755 index 0000000..9d19f1a --- /dev/null +++ b/experiments/call_library/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +gcc -c main.c -I../../libraries/test_library1/include -o bin/main.o + +cd ../../libraries/test_library1 +./compile.sh + +cd ../../experiments/call_library + + +gcc bin/main.o -L../../libraries/test_library1/bin/static -ltestlibrary -o bin/call_library diff --git a/experiments/call_library/main.c b/experiments/call_library/main.c new file mode 100644 index 0000000..cb52d84 --- /dev/null +++ b/experiments/call_library/main.c @@ -0,0 +1,19 @@ +#include + +#include "test.h" + + +int main() +{ + printf("Going to print test value from TestLibrary\n"); + + const double test_value = TestLibrary_test_value(); + + printf("%.0f\n", test_value); + + const double new_test_value = TestLibrary_multiply_by_one_point_five(test_value); + + printf("Multiplied by 1.5: %.2f\n", new_test_value); + + return 0; +} diff --git a/libraries/hex_octal_conv/compile.sh b/libraries/hex_octal_conv/compile.sh index 6830d28..9eaef60 100755 --- a/libraries/hex_octal_conv/compile.sh +++ b/libraries/hex_octal_conv/compile.sh @@ -1,3 +1,12 @@ +#!/bin/bash + +if [ -n "$1" ]; then + compile_type=$1 +else + compile_type="debug" +fi + + if [ ! -d "bin" ]; then mkdir bin echo "created bin directory" @@ -8,6 +17,15 @@ if [ ! -d "bin/static" ]; then echo "created bin/static directory" fi -gcc -c src/hex.c -Iinclude -o bin/static/hex.o +if [ "$compile_type" = "debug" ]; then + echo "Compiling for debug version" + gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O0 +elif [ "$compile_type" = "release" ]; then + echo "Compiling for release version" + gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O3 +else + echo "Compiling for debug by default" + gcc -c src/hex.c -Iinclude -o bin/static/hex.o -O0 +fi 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 index 5bf8751..025cf30 100644 --- a/libraries/hex_octal_conv/include/hex.h +++ b/libraries/hex_octal_conv/include/hex.h @@ -3,16 +3,18 @@ #define BUFFER_LIMIT 1024 -char hex_value[BUFFER_LIMIT]; - -char *HexOctalConv_number_converted_to_hex(char *value); - +int HexOctalConv_hex_converted_to_number(char *value); +int HexOctalConv_octal_converted_to_number(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); +void HexOctalConv_number_converted_to_hex(char *hex_value, char *value); +void HexOctalConv_number_converted_to_octal(char *octal_value, char *value); + + +static long rudy_pow(long value, int power); + +static int size_of_string(char *value); + +static void reverse_order(char *value); diff --git a/libraries/hex_octal_conv/include/octal.h b/libraries/hex_octal_conv/include/octal.h deleted file mode 100644 index e69de29..0000000 diff --git a/libraries/hex_octal_conv/src/hex.c b/libraries/hex_octal_conv/src/hex.c index 63d83f7..e6b562a 100644 --- a/libraries/hex_octal_conv/src/hex.c +++ b/libraries/hex_octal_conv/src/hex.c @@ -4,45 +4,37 @@ #include -char *HexOctalConv_number_converted_to_hex(char *value) +int HexOctalConv_hex_converted_to_number(char *value) { - int number_value = atoi(value); - int string_size = HexOctalConv_size_of_string(value); + int number = 0; + int power = 0; + int string_size = size_of_string(value); - extern char hex_value[]; - - int hex_start = 0; - - for (double number = number_value;;) + for (int i = (string_size - 1); i >= 0; --i) { - 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 = value[i]; + int number_part = HexOctalConv_hex_part_to_number(hex_part); + number += number_part * rudy_pow(16, power); - char hex_part = HexOctalConv_convert_hex_part(number_hex_part); - hex_value[hex_start++] = hex_part; - - /** - if (number >= 16) - { - } - else - { - } - */ - - number = quotient_trunecated; + ++power; } - hex_value[hex_start] = '\0'; + return number; +} - if (hex_start > 1) +int HexOctalConv_octal_converted_to_number(char *value) +{ + int octal_converted_to_number = 0; + int power_value = 0; + + for (int a = size_of_string(value) - 1; a >= 0; --a) { - HexOctalConv_reverse_order(hex_value); + int octal_part = value[a] - '0'; + int octal_power = octal_part * rudy_pow(8, power_value++); + octal_converted_to_number += octal_power; } - return hex_value; + return octal_converted_to_number; } int HexOctalConv_hex_part_to_number(char hex_part) @@ -78,36 +70,6 @@ int HexOctalConv_hex_part_to_number(char hex_part) 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) { @@ -142,9 +104,115 @@ char HexOctalConv_convert_hex_part(int value) } -void HexOctalConv_reverse_order(char *value) + +void HexOctalConv_number_converted_to_hex(char *hex_value, char *value) { - int string_size = HexOctalConv_size_of_string(value); + printf("Starting process to convert %s to hexidecimal\n", value); + int number_value = atoi(value); + int string_size = size_of_string(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; + // printf("%s\n", hex_value); + + if (number < 16) + { + break; + } + + number = quotient_trunecated; + } + + hex_value[hex_start] = '\0'; + + if (hex_start > 1) + { + reverse_order(hex_value); + } +} + +void HexOctalConv_number_converted_to_octal(char *octal_value, char *value) +{ + int value_number = atoi(value); + int index = 0; + + for (double quotient_number = value_number; (quotient_number >= 0);) + { + if (quotient_number == 0) + { + octal_value[index] = '0'; + break; + } + else + { + quotient_number /= 8; + int trunecated_quotient_number = quotient_number; + double remaining = quotient_number - trunecated_quotient_number; + int octal_part = remaining * 8; + + octal_value[index] = octal_part + '0'; + quotient_number = trunecated_quotient_number; + + ++index; + } + } + + octal_value[index] = '\0'; + + for (int a = 0; a <= (index / 2); ++a) + { + char tmp = octal_value[a]; + octal_value[a] = octal_value[(index - 1) - a]; + octal_value[(index - 1)] = tmp; + } +} + + + + +static long rudy_pow(long value, int power) +{ + if (power > 2) + { + return value * rudy_pow(value, --power); + } + else if (power == 2) + { + return value * value; + } + else if (power == 1) + { + return value; + } + else + { + return 1; + } +} + + +static int size_of_string(char *value) +{ + int size = 0; + + for (; value[size] != '\0'; ++size); + + return size; +} + + +static void reverse_order(char *value) +{ + int string_size = size_of_string(value); for (int i = 0;; ++i) {