This commit is contained in:
Curt Spark 2024-07-18 21:12:46 +01:00
parent d38053870d
commit 6260318c30
12 changed files with 108 additions and 40 deletions

0
.gitignore vendored Normal file → Executable file
View File

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
gcc main.c lib/*.c gcc -g main.c lib/*.c
./a.out ./a.out

18
dap.el Normal file
View File

@ -0,0 +1,18 @@
;; Eval Buffer with `M-x eval-buffer' to register the newly created template.
(dap-register-debug-template
"LLDB::Run launch"
(list :type "lldb-vscode"
:cwd "/home/cspark/Documents/C"
:request "launch"
:program "a.out"
:stopOnEntry 'nil
:name "LLDB::Run launch"))
(dap-register-debug-template
"LLDB::Run connect"
(list :type "lldb-vscode"
:request "attach"
:program "a.out"
:stopAtEntry 'nil
:name "LLDB::Run connect"))

44
lib/array.c Normal file → Executable file
View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "array.h"
#include "base10.h" #include "base10.h"
int sizeofStr(char in[]) { int sizeofStr(char in[]) {
@ -21,12 +22,12 @@ void printStrArray(char in[]) {
} }
} }
void printIntArray(int in[], int inLength) { void printIntArray(intArray *in) {
int i; int i;
printf("{ "); printf("{ ");
for (i = 0; i < inLength; ++i) { for (i = 0; i < in->length; ++i) {
printf("%d", in[i]); printf("%d", in->data[i]);
if (i == inLength - 1) { if (i == in->length - 1) {
printf(" }\n"); printf(" }\n");
} else { } else {
printf(", "); printf(", ");
@ -34,10 +35,10 @@ void printIntArray(int in[], int inLength) {
} }
} }
int checkIntArraySorted(int in[], int inLength) { int checkIntArraySorted(intArray *in) {
int i = 0; int i = 0;
while(i < inLength - 1) { while(i < in->length - 1) {
if (in[i] > in[i + 1]) if (in->data[i] > in->data[i + 1])
return 0; return 0;
++i; ++i;
} }
@ -46,25 +47,28 @@ int checkIntArraySorted(int in[], int inLength) {
} }
/* Convert an integer array into a combined integer */ /* Convert an integer array into a combined integer */
int intArrayToInt(int in[], int inLength) { int intArrayToInt(intArray *in) {
int i; int i;
int out = 0; int out = 0;
int currentDigit = inLength; int currentDigit;
for (i = 1; i <= inLength; ++i) { for (i = 0, currentDigit = in->length; i <= in->length; ++i, --currentDigit)
out += in[i - 1] * intDigitExpand(currentDigit); out += in->data[i] * intDigitExpand(currentDigit);
--currentDigit;
}
return out; return out;
} }
/* Split an integer into an integer array */ /* Split an integer into an integer array */
void intToIntArray(int in, int out[]) { intArray intToIntArray(int in) {
int inLength = intDigitCount(in);
int outData[inLength];
intArray out;
out.data = outData;
out.length = inLength;
int i; int i;
int j = 0; int j;
int currentDigit; for(i = inLength, j = 0; i >= 1; --i, ++j) {
for(i = intDigitCount(in); i >= 1; --i) { out.data[j] = in / intDigitExpand(i);
out[j] = in / intDigitExpand(i); in -= intDigitExpand(i) * out.data[j];
in -= intDigitExpand(i) * out[j];
++j;
} }
return out;
} }

13
lib/array.h Normal file → Executable file
View File

@ -1,16 +1,21 @@
#ifndef ARRAY_H #ifndef ARRAY_H
#define ARRAY_H #define ARRAY_H
typedef struct IntArray {
unsigned int length;
int *data;
} intArray;
int sizeofStr(char in[]); int sizeofStr(char in[]);
void printStrArray(char in[]); void printStrArray(char in[]);
void printIntArray(int in[], int inLength); void printIntArray(intArray *in);
int checkIntArraySorted(int in[], int inLength); int checkIntArraySorted(intArray *in);
void intToIntArray(int in, int out[]); intArray intToIntArray(int in);
int intArrayToInt(int in[], int inLength); int intArrayToInt(intArray *in);
#endif #endif

0
lib/base10.c Normal file → Executable file
View File

0
lib/base10.h Normal file → Executable file
View File

16
lib/math.c Normal file
View File

@ -0,0 +1,16 @@
#include "array.h"
intArray findDivisors(int in) {
int i;
intArray out;
for (i = 1; i <= in; ++i) {
if (in % i == 0) {
}
}
//out.data = outData;
//out.length = inLength;
return out;
};

8
lib/math.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef MATH_H
#define MATH_H
#include "array.h"
intArray findDivisors();
#endif

0
lib/temperature.c Normal file → Executable file
View File

0
lib/temperature.h Normal file → Executable file
View File

47
main.c Normal file → Executable file
View File

@ -2,27 +2,44 @@
#include "lib/temperature.h" #include "lib/temperature.h"
#include "lib/array.h" #include "lib/array.h"
#include "lib/base10.h" #include "lib/base10.h"
// #include "lib/math.h"
void swapInt(int *pX, int *pY) { void swapInt(int *pX, int *pY) {
int temp = *pX; int temp = *pX;
*pX = *pY; *pX = *pY;
*pY = temp; *pY = temp;
} }
#define ALLOCSIZE 10000
char allocbuff[ALLOCSIZE];
char* allocp = allocbuff;
char* alloc(int n) {
if (allocbuff + ALLOCSIZE - allocp >= n) {
allocp += n;
return allocp - n;
} else
return 0;
}
int main() { int main() {
int unsortedArray[] = { 1, 5, 2, 1, 4, 8, 3, 2, 6, 7 };
int sortedArray[] = { 1, 2, 4, 8, 9, 9, 9 }; intArray unsortedArray;
int unsortedArrayData[] = { 1, 5, 2, 1, 4, 8, 3, 2, 6, 7 };
printf("Unsorted Array : "); unsortedArray.length = sizeof(unsortedArrayData) / sizeof(int);
printIntArray(unsortedArray, sizeof(unsortedArray) / sizeof(int)); unsortedArray.data = unsortedArrayData;
printf("Unsorted Array Sorted?: %d\n", checkIntArraySorted(unsortedArray, sizeof(unsortedArray) / sizeof(int))); intArray sortedArray;
printf("Sorted Array : "); int sortedArrayData[] = { 1, 2, 4, 8, 9, 9, 9 };
printIntArray(sortedArray, sizeof(sortedArray) / sizeof(int)); sortedArray.length = sizeof(sortedArrayData) / sizeof(int);
printf("Sorted Array Sorted?: %d\n", checkIntArraySorted(sortedArray, sizeof(sortedArray) / sizeof(int))); sortedArray.data = sortedArrayData;
printf("40C == %dF", (int) celsiusToFarenheit(40.0)); printf("Unsorted Array : ");
printIntArray(&unsortedArray);
printf("Unsorted Array Sorted?: %d\n", checkIntArraySorted(&unsortedArray));
printf("Sorted Array : ");
printIntArray(&sortedArray);
printf("Sorted Array Sorted?: %d\n", checkIntArraySorted(&sortedArray));
printf("40C == %dF\n", (int) celsiusToFarenheit(40.0));
} }