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

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

@ -1,16 +1,21 @@
#ifndef ARRAY_H
#define ARRAY_H
typedef struct IntArray {
unsigned int length;
int *data;
} intArray;
int sizeofStr(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

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

33
main.c Normal file → Executable file
View File

@ -2,6 +2,7 @@
#include "lib/temperature.h"
#include "lib/array.h"
#include "lib/base10.h"
// #include "lib/math.h"
void swapInt(int *pX, int *pY) {
int temp = *pX;
@ -9,20 +10,36 @@ void swapInt(int *pX, int *pY) {
*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 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 };
unsortedArray.length = sizeof(unsortedArrayData) / sizeof(int);
unsortedArray.data = unsortedArrayData;
intArray sortedArray;
int sortedArrayData[] = { 1, 2, 4, 8, 9, 9, 9 };
sortedArray.length = sizeof(sortedArrayData) / sizeof(int);
sortedArray.data = sortedArrayData;
printf("Unsorted Array : ");
printIntArray(unsortedArray, sizeof(unsortedArray) / sizeof(int));
printf("Unsorted Array Sorted?: %d\n", checkIntArraySorted(unsortedArray, sizeof(unsortedArray) / sizeof(int)));
printIntArray(&unsortedArray);
printf("Unsorted Array Sorted?: %d\n", checkIntArraySorted(&unsortedArray));
printf("Sorted Array : ");
printIntArray(sortedArray, sizeof(sortedArray) / sizeof(int));
printf("Sorted Array Sorted?: %d\n", checkIntArraySorted(sortedArray, sizeof(sortedArray) / sizeof(int)));
printf("40C == %dF", (int) celsiusToFarenheit(40.0));
printIntArray(&sortedArray);
printf("Sorted Array Sorted?: %d\n", checkIntArraySorted(&sortedArray));
printf("40C == %dF\n", (int) celsiusToFarenheit(40.0));
}