Created
June 22, 2015 23:55
-
-
Save khult/ea466051796a0ec8f24a to your computer and use it in GitHub Desktop.
C Coding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Combine Tables by Kristofer Hult */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
// how many characters can be used on each line | |
#define LINE_LEN 200 | |
// Removes /n from a string. | |
void trim(char *string) | |
{ | |
size_t len = strlen(string) - 1; | |
if(string[len] == '\n') | |
string[len] = '\0'; | |
} | |
int main() | |
{ | |
// get data from .txt files | |
FILE *wiki = fopen("wiki.txt", "r"); | |
FILE *authors = fopen("authors.txt", "r"); | |
FILE *wikiAuthor = fopen("wikiauthor.txt", "w"); | |
// initialize | |
char read_line[LINE_LEN], search_line[LINE_LEN]; | |
char *wantedIDPoint, *checkingForPoint; | |
int i; | |
// get needed data from wiki.txt. | |
while(fgets(read_line, sizeof(read_line), wiki) != NULL) | |
{ | |
trim(read_line); | |
wantedIDPoint = strtok(read_line, ","); | |
for (i=0; i<2; i++) | |
{ | |
fprintf(wikiAuthor, "%s,", wantedIDPoint); | |
wantedIDPoint = strtok(NULL, ","); | |
} | |
while(fgets(search_line, sizeof(search_line), authors) != NULL) | |
{ | |
trim(search_line); | |
checkingForPoint = strtok(search_line, ","); | |
// add ID if found | |
if(strcmp(wantedIDPoint, checkingForPoint) == 0) | |
{ | |
checkingForPoint = strtok(NULL, ","); | |
fprintf(wikiAuthor, "%s,", checkingForPoint); | |
checkingForPoint = strtok(NULL, ","); | |
fprintf(wikiAuthor, "%s", checkingForPoint); | |
break; | |
} | |
} | |
fprintf(wikiAuthor, "\n"); | |
// reopen authors file to continue search for ID's | |
authors = fopen("authors.txt", "r"); | |
} | |
// Close all the files. | |
int fclose(FILE *wiki); | |
int fclose(FILE *authors); | |
int fclose(FILE *wikiAuthor); | |
fclose(wiki); | |
fclose(authors); | |
fclose(wikiAuthor); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Fibonacci Series, Prints the First 20 numbers in the series, by Kristofer Hult */ | |
#include<stdio.h> | |
int main() | |
{ | |
int MAX, x = 0, y = 1, z, i; | |
MAX = 20; | |
for ( i = 0 ; i < MAX ; i++ ) | |
{ | |
if ( i <= 1 ) | |
z = i; | |
else | |
{ | |
z = x + y; | |
x = y; | |
y = z; | |
} | |
printf("%d\n",z); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Linked list implementation, by Kristofer Hult */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include "list.h" | |
// Initializes head and tail of the linked list. | |
void init(slist_t *listPointer) { | |
listPointer->head = NULL; | |
listPointer->tail = NULL; | |
} | |
void add(slist_t *listPointer, int value) { | |
list_item_t *listVal = (list_item_t *) malloc(sizeof(list_item_t)); | |
listVal->value = value; | |
listVal->next = NULL; | |
// If the list is empty, new listVal = head and tail. | |
if(listPointer->head == NULL) { | |
listPointer->head = listVal; | |
listPointer->tail = listVal; | |
listVal->prev = NULL; | |
} else { | |
listVal->prev = listPointer->tail; | |
listPointer->tail->next = listVal; | |
listPointer->tail = listVal; | |
} | |
} | |
// Prints the generated linked list | |
void print(slist_t *listPointer) { | |
printf("List: "); | |
list_item_t *iter = listPointer->head; | |
while(iter != NULL) { | |
printf("%d ", iter->value); | |
iter = iter->next; | |
} | |
printf("\n"); | |
} | |
void empty(slist_t *listPointer) { | |
list_item_t *prev = listPointer->head; | |
list_item_t *iter = prev; | |
listPointer->head = NULL; | |
listPointer->tail = NULL; | |
while(iter != NULL) { | |
iter = prev->next; | |
free(prev); | |
prev = iter; | |
} | |
} | |
void swap(slist_t *listPointer, list_item_t *current, list_item_t *advance) { | |
current->next = advance->next; | |
advance->next = current; | |
advance->prev = current->prev; | |
current->prev = advance; | |
if(advance->prev == NULL) { | |
listPointer->head = advance; | |
} else { | |
advance->prev->next = advance; | |
} | |
if(current->next == NULL) { | |
listPointer->tail = current; | |
} else { | |
current->next->prev = current; | |
} | |
} | |
// Sorts using the bubble sort algorithm. | |
void bublesort(slist_t *listPointer) { | |
list_item_t *current; | |
list_item_t *advance; | |
int isSorted = 0; | |
while(isSorted == 0) { | |
isSorted = 1; | |
current = listPointer->head; | |
advance = current->next; | |
while(advance != NULL) { | |
if(current->value > advance->value) { | |
isSorted = 0; | |
swap(listPointer, current, advance); | |
advance = current->next; | |
} else { | |
current = advance; | |
advance = advance->next; | |
} | |
} | |
} | |
} | |
int main() { | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Numbers divisible by N, by Kristofer Hult */ | |
/* Takes an input number and returns all the numbers that are divisible by it*/ | |
#include<stdio.h> | |
int main(){ | |
int N, i, PrintThis, x=0; | |
printf("Enter a number N\n"); | |
scanf("%d",&N); | |
for ( i = 1 ; i < N+1 ; i++ ) | |
{ | |
if ( N % i == 0) | |
PrintThis = i; | |
else { | |
} | |
printf("%d\n",PrintThis); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment