aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--integral.c32
-rw-r--r--linkedlist/list.c46
-rw-r--r--linkedlist/list.h16
-rw-r--r--linkedlist/main.c165
-rw-r--r--linkedlist/stack.c38
-rw-r--r--linkedlist/stack.h18
-rw-r--r--linkedlist/test.c28
-rw-r--r--socket.h168
-rw-r--r--sort.c56
9 files changed, 567 insertions, 0 deletions
diff --git a/integral.c b/integral.c
new file mode 100644
index 0000000..9509624
--- /dev/null
+++ b/integral.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <math.h>
+
+#define PI 3.14159
+
+
+const float range_bottom = PI / 2;
+const float range_top = 2 * PI / 3;
+const float intervals = 100000;
+
+float f(float x)
+{
+ return (1 / sin(x));
+}
+
+int main(void)
+{
+ float oversum, undersum, average, steps;
+ steps = (range_top - range_bottom) / intervals;
+
+ for(float i = range_bottom; i <= range_top; i += steps)
+ {
+ oversum += steps * f(i);
+ undersum += steps * f(i - steps);
+ }
+
+ average = (oversum + undersum) / 2;
+
+ printf("Area under f(x) between %f and %f:\n", range_bottom, range_top);
+ printf("%f\n", average);
+ printf("Interval: %f\n", steps);
+}
diff --git a/linkedlist/list.c b/linkedlist/list.c
new file mode 100644
index 0000000..701cd5f
--- /dev/null
+++ b/linkedlist/list.c
@@ -0,0 +1,46 @@
+#include "list.h"
+
+void list_init(struct list** _listroot)
+{
+ if(*_listroot)
+ while(list_popFront(_listroot));
+
+ *_listroot = NULL;
+}
+
+void list_add(int _data, struct list** _listroot)
+{
+ struct list **current = _listroot;
+
+ while(*current)
+ current = &(*current)->next;
+
+ *current = malloc(sizeof(**current));
+ (*current)->data = _data;
+}
+
+int list_popFront(struct list** _listroot)
+{
+ if(!*_listroot)
+ return 0;
+
+ int data = (*_listroot)->data;
+ struct list *oldRoot = *_listroot;
+
+ *_listroot = (*_listroot)->next;
+ free(oldRoot);
+
+ return data;
+}
+
+int list_get(int _index, struct list** _listroot)
+{
+ struct list* current = *_listroot;
+
+ for(int i = 0; i < _index; i++)
+ {
+ current = current->next;
+ }
+
+ return current->data;
+}
diff --git a/linkedlist/list.h b/linkedlist/list.h
new file mode 100644
index 0000000..db9162b
--- /dev/null
+++ b/linkedlist/list.h
@@ -0,0 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+struct list
+{
+ struct list *next;
+ int data;
+};
+
+void list_init(struct list** _listroot);
+
+void list_add(int _data, struct list** _listroot);
+
+int list_popFront(struct list** _listroot);
+
+int list_get(int _index, struct list** _listroot);
diff --git a/linkedlist/main.c b/linkedlist/main.c
new file mode 100644
index 0000000..1d17923
--- /dev/null
+++ b/linkedlist/main.c
@@ -0,0 +1,165 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "stack.h"
+#include "list.h"
+
+struct node
+{
+ struct node* nextS;
+ struct node* nextB;
+ int data;
+};
+
+struct node* root;
+
+struct node** getSmallest(struct node** _root)
+{
+ struct node** current = _root;
+
+ while((*current)->nextS)
+ current = &((*current)->nextS);
+
+ return current;
+}
+
+struct node removeNode(int _data)
+{
+ struct node **current = &root;
+ struct node *removeNode = NULL;
+ struct node **newRoot = NULL;
+ struct node returnNode;
+
+ while((*current)->data != _data)
+ current = (*current)->data < _data ? &(*current)->nextB : &(*current)->nextS;
+
+
+ printf("node to remove:%i\n", (*current)->data);
+ removeNode = *current;
+
+ if(removeNode->nextB)
+ {
+ newRoot = getSmallest(&removeNode->nextB);
+ *current = *newRoot;
+ *newRoot = (*newRoot)->nextB;
+
+ (*current)->nextS = removeNode->nextS;
+ (*current)->nextB = removeNode->nextB;
+ }
+ else
+ *current = (*current)->nextS;;
+
+ returnNode = *removeNode;
+ free(removeNode);
+
+ return returnNode;
+}
+
+void insert(int _data)
+{
+ struct node** current = &root;
+ struct node* new = malloc(sizeof(*new));
+
+ new->nextS = NULL;
+ new->nextB = NULL;
+ new->data=_data;
+
+ while (*current)
+ current = (*current)->data > _data ? &((*current)->nextS) : &((*current)->nextB);
+
+ *current = new;
+}
+
+void draw(void)
+{
+ struct node* current = root;
+ struct node* stackTop = NULL;
+ struct stack* stackpointer = NULL;
+ int depth = 0;
+
+ stack_init(&stackpointer);
+
+start:
+
+ while (current)
+ {
+ stack_add( (void*) current , &stackpointer);
+
+ int i;
+ for(i = 0; i < depth; i++)
+ printf(" ");
+
+ printf("|:%i\n", current->data);
+
+ current = current->nextS;
+
+ depth++;
+ }
+nextinstack:
+
+ stackTop = (struct node*)stack_get(&stackpointer);
+ if(!stackTop)
+ return;
+
+ current = stackTop->nextB;
+
+ if(current)
+ goto start;
+
+ depth --;
+ goto nextinstack;
+}
+
+void balance()
+{
+ struct node *current = root;
+ struct node *stackTop = NULL;
+ struct stack *stackpointer = NULL;
+ struct list *listroot = NULL;
+
+
+ stack_init(&stackpointer);
+ list_init(&listroot);
+
+ //Create Inorder List
+start:
+ while(current)
+ {
+ stack_add( (void*) current, &stackpointer);
+ current = current->nextS;
+ }
+
+ stackTop = (struct stack*) stack_pop(&stackpointer);
+ stackTop ? current = stackTop->nextB :;
+
+ if(current)
+ {
+ list_add(current->data, &listroot);
+ goto start;
+ }
+
+ //TODO Balance
+
+}
+
+int main(int argc, char* argv[])
+{
+ root = NULL;
+
+ insert(7);
+ insert(5);
+ insert(10);
+ insert(11);
+ insert(1);
+
+ //draw();
+ balance();
+
+ printf("balanced");
+ //removeNode(7);
+
+ //draw();
+
+ printf("%i is the smallest obejct\n", (*getSmallest(&root))->data);
+ return 0;
+}
diff --git a/linkedlist/stack.c b/linkedlist/stack.c
new file mode 100644
index 0000000..de77b72
--- /dev/null
+++ b/linkedlist/stack.c
@@ -0,0 +1,38 @@
+#include "stack.h"
+
+void stack_add(void* _data, struct stack** _stackpointer)
+{
+ struct stack* newsp = malloc(sizeof(*newsp));
+
+ newsp->data = _data;
+ newsp->next = *_stackpointer;
+ *_stackpointer = newsp;
+}
+
+void* stack_get(struct stack** _stackpointer)
+{
+ void* return_data;
+ struct stack* oldsp; //Old stackpointer
+
+ if(!*_stackpointer)
+ return NULL;
+
+ return_data = (*_stackpointer)->data;
+ oldsp = *_stackpointer;
+ *_stackpointer = (*_stackpointer)->next;
+ free(oldsp);
+
+ return return_data;
+}
+
+void stack_init(struct stack** _stackpointer)
+{
+ stack_flush(_stackpointer);
+ *_stackpointer = NULL;
+}
+
+void stack_flush(struct stack** _stackpointer)
+{
+ while(*_stackpointer)
+ stack_get(_stackpointer);
+}
diff --git a/linkedlist/stack.h b/linkedlist/stack.h
new file mode 100644
index 0000000..d579da9
--- /dev/null
+++ b/linkedlist/stack.h
@@ -0,0 +1,18 @@
+//stack.h
+//
+
+#include <stdlib.h>
+
+struct stack
+{
+ struct stack* next;
+ void* data;
+};
+
+void stack_add(void* _data, struct stack** _stackpointer);
+
+void* stack_get(struct stack** _stackpointer);
+
+void stack_init(struct stack** _stackpointer);
+
+void stack_flush(struct stack** _stackpointer);
diff --git a/linkedlist/test.c b/linkedlist/test.c
new file mode 100644
index 0000000..9c138d4
--- /dev/null
+++ b/linkedlist/test.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "list.h"
+
+int main(void)
+{
+ struct list* listroot = malloc(sizeof(*listroot));
+
+ list_init(&listroot);
+
+ for(int i = 0; i < 30; i++)
+ {
+ list_add(i, &listroot);
+ }
+
+ printf("list created, printing\n");
+
+ for(int i = 0; i < 30; i++)
+ {
+ printf("Round %i, ", i);
+ printf("Element %i\n", list_get(i, &listroot));
+ }
+
+ printf("\n");
+
+ return 0;
+}
diff --git a/socket.h b/socket.h
new file mode 100644
index 0000000..6c64816
--- /dev/null
+++ b/socket.h
@@ -0,0 +1,168 @@
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <vector>
+#include <string>
+#include <string.h>
+#include <sstream>
+#include <unistd.h>
+using namespace std;
+
+#define _CONNECT 0
+#define _BIND 1
+
+#define _BUFF_SIZE 512 //bytes
+
+//ERROR CODES
+#define _ERR 1
+#define _ERR_ACC 3
+#define _ERR_SOCK 4
+#define _ERR_CONNECT 5
+#define _ERR_BIND 6
+#define _ERR_RECV 7
+#define _ERR_SND 8
+
+class cSocket
+{
+public:
+ cSocket(char *host, int port, unsigned int mode);
+
+ int init(); //connects or binds the socket
+
+ int Send(int socket, char *data); //Send with char*
+ int Send(int socket, string data); //Send with string
+
+ string recieve(int socket);
+ int waitConn(int *socketNum, sockaddr_in *client);
+
+ int Close(int socket);
+ int quit();
+
+private:
+ long getIP(char *_host); //resolves hostname
+
+ int main_sock; //main Socket
+ struct sockaddr_in server; //sockadddr_in for main_sock
+
+ char buffer[_BUFF_SIZE]; //Output Buffer
+ //std::vector<int> sockets; //stores sockets
+ unsigned int socketMode; //stores Mode
+ int connected;
+};
+
+cSocket::cSocket(char *host, int port, unsigned int mode)
+{
+ main_sock = socket(AF_INET, SOCK_STREAM, 0);
+ socketMode = mode;
+ server.sin_port = htons(port);
+ server.sin_family = AF_INET;
+
+ if(mode == _CONNECT)
+ server.sin_addr.s_addr = getIP(host);
+
+ else if (mode == _BIND)
+ server.sin_addr.s_addr = INADDR_ANY; // accept on any incoming address
+
+ connected = 0;
+}//cSocket
+
+int cSocket::init()
+{
+ if (main_sock == -1)
+ return _ERR_SOCK;
+
+ if (socketMode == _CONNECT)
+ {
+ if (connect(main_sock , (struct sockaddr *)&server , sizeof(server)) < 0)
+ return _ERR_CONNECT;
+ }
+ else if (socketMode == _BIND)
+ {
+ if( bind(main_sock,(struct sockaddr *)&server , sizeof(server)) < 0)
+ return _ERR_BIND;
+
+ listen(main_sock , 3);
+ }
+ else
+ return 3;
+
+ connected = 1;
+ return 0;
+}//init
+
+int cSocket::waitConn(int *socket, sockaddr_in *client)
+{
+ int sz = sizeof(struct sockaddr_in);
+ int tmpSocket = accept(main_sock, (struct sockaddr *)client, (socklen_t*)&sz);
+
+ if (tmpSocket < 0)
+ return _ERR_ACC;
+
+ *socket = tmpSocket;
+ return 0;
+}//waitConn
+
+int cSocket::Send(int socket, char *data)
+{
+ if (main_sock == -1 || connected < 1)
+ return _ERR_SOCK;
+
+ if( send(socket, data , strlen(data) , 0) < 0)
+ return _ERR_SND;
+
+ return 0;
+}//Send
+
+int cSocket::Send(int socket, string data)
+{
+ const char *cData = data.c_str(); //Convert to char*
+ return Send(socket, cData);
+}//Send
+
+string cSocket::recieve(int socket)
+{
+ for (int i = 0; i < _BUFF_SIZE; i++)
+ {
+ buffer[i] = 0;
+ }
+
+ if( recv(socket, buffer , _BUFF_SIZE , 0) < 0)
+ return "";
+
+ string ret (buffer);
+ return ret;
+}//recieve
+
+int cSocket::Close(int socket)
+{
+ close(socket);
+ return 0;
+}//close
+
+int cSocket::quit()
+{
+ Close(main_sock);
+
+ main_sock = -1;
+ connected = 0;
+
+ return 0;
+}//quit
+
+long cSocket::getIP(char *_host)
+{
+ char ip[100];
+ struct hostent *he;
+ struct in_addr **addr_list;
+ int i;
+
+ if ( (he = gethostbyname( _host ) ) == NULL)
+ return 1;
+
+ addr_list = (struct in_addr **) he->h_addr_list;
+
+ for(i = 0; addr_list[i] != NULL; i++)
+ strcpy(ip , inet_ntoa(*addr_list[i]) );
+
+ return inet_addr(ip);
+}//getIP
diff --git a/sort.c b/sort.c
new file mode 100644
index 0000000..5584867
--- /dev/null
+++ b/sort.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define _OUTPUT
+
+int main(int argc, char* argv[])
+{
+ int count = 1000;
+ int *list;
+ int swaps = 0;
+ int runs = 0;
+ int biggest = 0;
+ int smallest = 0;
+
+ printf("Random list sorting\nLenght of List:%i \n\n", count);
+
+ list = malloc(sizeof(int) * count);
+
+ srandom(list);
+
+ for(int i = 0; i < count; i++)
+ {
+ list[i] = random()/361913;
+#ifdef _OUTPUT
+ printf("%i,", list[i]);
+#endif
+ }
+
+ printf("\n==========Sorting==========\n");
+
+ do {
+ swaps = 0;
+ runs++;
+ for( int i = 1; i < count; i++ )
+ {
+ if(list[i - 1] > list [i])
+ {
+ int tmp = list[i - 1];
+ list[i - 1] = list[i];
+ list[i] = tmp;
+ swaps++;
+ }
+ }
+ } while(swaps);
+
+#ifdef _OUTPUT
+ for(int i = 0; i < count; i++)
+ {
+ printf("%i,",list[i]);
+ }
+#endif
+
+ printf("\n\nNeeded runs: %i\n", runs);
+ free(list);
+ return 0;
+}