Working on exercise 3-5 but I'm creating some libraries
This commit is contained in:
@@ -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 <stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
+13
@@ -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
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
#include "hex.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Executable
+16
@@ -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
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
@@ -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");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user