Commit 37935f93 authored by joel.guest's avatar joel.guest

Initial commit

parents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node //node structure linked list is declared
//variables are set
{
int data;
char* modTitle;
char* year;
char* semester;
struct node* left;
struct node* right;
} node;
//declaration for node insertion
typedef void (*callback)(node*);
node* insert_node(node *root, char **nodeData);
//the function that reads the file is declared
node* readFile(node *root) {
static const char filename[] = "data.txt"; //filename to be read
FILE *file = fopen ( filename, "r" );
if ( file != NULL )
{
char line [ 128 ]; // or other suitable maximum line size
while ( fgets ( line, sizeof line, file ) != NULL ) // read a line
{
int i = 0;
char *array[4]; //sets array to 4 due to having 4 printable values
char* token = strtok(line, ","); //each line is tokanised with a ","
while (token != NULL) {
array[i] = malloc(sizeof(token)*10); //allocates memory siz of 10x the size of the token
strcpy(array[i], token); //copies array into token
token = strtok(NULL, ",");
i++;
}
root = insert_node(root, array); //inserts the root value into the array
}
fclose ( file );
}
else
{
perror ( filename ); // why didn't the file open?
}
return root;
}
//the next value is compaed to the ight and left value
int compare(int left,int right)
{
if(left > right)
return 1;
if(left < right)
return -1;
return 0;
}
//the function that creates the node is declared
node* createNode(char **nodeData){
//new node is allocated enough memory to store the size of node
node *new_node = (node*)malloc(sizeof(node));
if(new_node == NULL)
{
fprintf (stderr, "Out of memory!!! (create_node)\n");
exit(1);
}
//required key values from within the binary tree nodes
new_node->data = atoi(nodeData[0]);
new_node->modTitle = malloc(sizeof(nodeData[1]));
new_node->modTitle = nodeData[1];
new_node->year = malloc(sizeof(nodeData[2]));
new_node->year = nodeData[2];
new_node->semester = malloc(sizeof(nodeData[3]));
new_node->semester = nodeData[3];
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
//the function that inserts into the node is declared
node* insert_node(node *root, char **nodeData)
{
if(root == NULL)
{
root = createNode(nodeData); //root node is created if root==null
}
else
{
int is_left = 0;
int r = 0;
node* cursor = root;
node* prev = NULL;
//cursor checks whether next value is requied to be inserted to the left o right of the previous node
while(cursor != NULL)
{
r = compare(atoi(nodeData[0]),cursor->data);
prev = cursor;
if(r < 0)
{
is_left = 1;
cursor = cursor->left; //if left =1 then the cursor points to left
}
else if(r > 0)
{
is_left = 0;
cursor = cursor->right; //if left =0 then the cursor points to right
}
}
if(is_left)
prev->left = createNode(nodeData); // if curser points to left then a node is inserted to the left
else
prev->right = createNode(nodeData); // if curser points to right then a node is inserted to the left
}
return root;
}
//the function that writes to the file is declared
void writeFile(node* myNode) {
char* final = malloc(128); //final binary tree is allocated 128bits of memory
char num[10];
//each value within each node is contatinated to the fnal string
sprintf(num, "%d", myNode->data);
strcpy(final, num);
strcat(final, ",");
strcat(final, myNode->modTitle);
strcat(final, ",");
strcat(final, myNode->year);
strcat(final, ",");
strcat(final, myNode->semester);
FILE *out = fopen("SortedData.txt", "a"); //the final data is written to a file called SortedData.txt
fprintf(out, "%s", final);
fclose(out);
}
//the function that traverses the binary tree is declared
void inOrderTraversal(node* myNode) {
//if myNode pointing to left doesnt equil null the node data is written to the left node in the write file function
if (myNode->left!=NULL)
inOrderTraversal(myNode->left);
printf("Node:%d ", myNode->data);
writeFile(myNode);
//if myNode pointing to left doesnt equil null the node data is written to the left node otherwise left becomes null and a new line is printed
if (myNode->left!=NULL)
printf("(Left:%d)", myNode->left->data);
else
printf("(Left:NULL)");
//if myNode pointing to right doesnt equil null the node data is written to the right node otherwise right becomes null and a new line is printed
if (myNode->right!=NULL)
printf("(Right:%d)\n", myNode->right->data);
else
printf("(Right:NULL)\n");
if (myNode->right!=NULL)
inOrderTraversal(myNode->right);
}
//main function
int main (int argc, char *argv[]) {
node* root=NULL;
root = readFile(root);
inOrderTraversal(root);
}
1101, Programming 01, Year 01, Semester 01
1102, Mathematics, Year 01, Semester 01
1103, Computer Fundamentals, Year 01, Semester 01
1105, Programming 02, Year 01, Semester 02
1106, Software Engineering, Year 01, Semester 02
1107, Technology In Context, Year 01, Semester 02
2101, Programmming 03, Year 02, Semester 01
2102, Databases, Year 02, Semester 01
2103, Networking, Year 02, Semester 01
2107, Mobile Programming, Year 02, Semester 02
2108, Professional Project, Year 02, Semester 02
2109, Philosophies of Technology, Year 02, Semester 02
3101, Major Project, Year 03, Semester 01
3104, Human Computer Interaction, Year 03, Semester 01
3105, The Internet Of Things, Year 03, Semester 01
3107, Artificial Intelligence, Year 03, Semester 02
3108, Cybercrime Security, Year 03, Semester 02
3109, Advanced Web Development, Year 03, Semester 02
2103, Networking, Year 02, Semester 01
3109, Advanced Web Development, Year 03, Semester 02
1101, Programming 01, Year 01, Semester 01
2107, Mobile Programming, Year 02, Semester 02
1103, Computer Fundamentals, Year 01, Semester 01
3101, Major Project, Year 03, Semester 01
1105, Programming 02, Year 01, Semester 02
2108, Professional Project, Year 02, Semester 02
1106, Software Engineering, Year 01, Semester 02
2102, Databases, Year 02, Semester 01
3105, The Internet Of Things, Year 03, Semester 01
3107, Artificial Intelligence, Year 03, Semester 02
3108, Cybercrime Security, Year 03, Semester 02
1102, Mathematics, Year 01, Semester 01
1107, Technology In Context, Year 01, Semester 02
2101, Programmming 03, Year 02, Semester 01
2109, Philosophies of Technology, Year 02, Semester 02
3104, Human Computer Interaction, Year 03, Semester 01
section .data
codes: db '0123456789ABCDEF' ;data for hex output
fizz: db 'fizz', 10 ;fizz variable set
bang: db 'bang', 10 ;bang variable set
fizzbang: db 'fizzbang', 10 ;fizzbang variable set
newline: db 10 ;newline variable set
section .text
global _start
_start:
xor r14, r14 ;stores fizz counter
xor r15, r15 ;store buzz counter
xor r9,r9 ; loop counter
mov r8,20 ; loop max
xor rcx, rcx
start_loop:
inc r9
cmp r9, 21 ;compares loop counter to loop max to make
je fizzcounter
cmp r9, 22
je bangcounter
cmp r9, 23
je end ;increments loop counter by 1
main:
;sets up test for fizzbang
xor rdx,rdx ;sets all value of rdx to 0
mov rax, r9
mov r12, 15 ; r9 = loop counter
DIV r12 ; RDX:RAX/3->RAX Remainder ->RDX
CMP rdx, 0 ; compared rdx to r12
jz test15 ; jump not zero test 15
;sets uptest for bang
xor rdx,rdx ;sets all value of rdx to 0
mov rax, r9
mov r12, 5 ; r9 = loop counter
DIV r12 ; RDX:RAX/3->RAX Remainder ->RDX
CMP rdx, 0 ;compared rdx to r12
jz test5 ; jump not zero test 5
;sets uptest for fizz
xor rdx,rdx ;sets all value of rdx to 0
mov rax, r9
mov r12, 3 ; r9 = loop counter
DIV r12 ; RDX:RAX/3->RAX Remainder ->RDX
CMP RDX, 0 ;compared rdx to r12
Jz test3 ; jump no zero test 5
jmp number ;jumps to number
test15:
;output bang
inc r14
inc r15
mov rax,1
mov rdi,1
mov rsi,fizzbang
mov rdx,9
syscall
jmp start_loop
test5:
inc r15
;output bang
mov rax,1
mov rdi,1
mov rsi,bang
mov rdx,5
syscall
jmp start_loop
test3:
inc r14
;output fizz
mov rax,1
mov rdi,1
mov rsi,fizz
mov rdx,5
syscall
jmp start_loop
fizzcounter:
mov rax, r14
mov rdi,1
mov rdx,1
mov rcx, 64
jmp loop
bangcounter:
mov rax,r15
mov rdi, 1
mov rdx,1
mov rcx, 64
jmp loop
number:
mov rax,r9
mov rdi,1
mov rdx,1
mov rcx, 64
jmp loop
loop:
push rax
sub rcx, 4
; cl is a register, smallest part of rcx
; rax -- eax -- ax -- ah + al
; rcx -- ecx -- cx -- ch + cl
sar rax, cl
and rax, 0xf
lea rsi, [codes + rax]
mov rax, 1
; syscall leaves rcx and r11 changed
push rcx
syscall
pop rcx
pop rax
; test can be used for the fastest 'is it a zero?' check
test rcx, rcx
jnz loop
;output bang
mov rax,1
mov rdi,1
mov rsi,newline
mov rdx,1
syscall
jmp start_loop ;jumps to start_loop
end:
mov rax, 60 ; invoke 'exit' system call
xor rdi, rdi
syscall
#include <stdio.h>
int main(void)
{
int i;
int countfizz=0; //counter increments when i/3=0
int countbang=0; //counter increments when i/5=0
for(i=1; i<=20; i++) //loops between numbers 1 and 20
{
if (i % 3 == 0){
printf("Fizz"); //Fizz is printed when i/3=0
countfizz ++; //countfizz is incremented by 1
}
if (i % 5 == 0){
printf("Bang"); //Bang is printed when i/5=0
countbang ++; //countbang is incremented by 1
}
if ((i % 3 != 0) && (i % 5 != 0)){
printf("%d", i); //FizzBang is printed when i/3=0 and i/5=0
}
printf("\n");
}
printf("%d\n",countfizz); //prints the value of countfizz
printf("%d\n",countbang); // prints the value of countbang
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment