Commit 6074f0df authored by norbert.dajnowski's avatar norbert.dajnowski

Final Commit

parents
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXCHAR 1000
FILE* file;
char data[MAXCHAR];
struct node
{
char record [200];
char key [50];
char module [50];
char year [50];
char semester [50];
struct node* left;
struct node* right;
};
struct node* createNode(char record [200]){
struct node* newNode = malloc(sizeof(struct node));
strcpy(newNode->record, record);
char* token = strtok(record, ",");
strcpy(newNode->key, token);
token = strtok(NULL, ",");
strcpy(newNode->module, token);
token = strtok(NULL, ",");
strcpy(newNode->year, token);
token = strtok(NULL, ",");
strcpy(newNode->semester, token);
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* input(struct node* root, char record [200])
{
if (root == NULL) return createNode(record);
if (strcmp(record, root->record) < 0){
root->left = input(root->left, record);
}
else if (strcmp(record, root->record) > 0){
root->right = input(root->right, record);
}
return root;
}
void traverse_order(struct node* root){
if(root == NULL) return;
traverse_order(root->left);
printf("%s (Left: ", root->key);
if (root->left != NULL) printf("%s)", root->left->key);
else printf("NULL)");
if (root->right != NULL) printf(" (Right: %s)\n", root->right->key);
else printf(" (Right: NULL)\n");
fflush(stdout);
fputs(root->record, file);
traverse_order(root->right);
}
int main() {
char* filenameread = "pg03data.txt";
char* filenamewrite ="pg03sorted.txt";
int counter = 1;
struct node *root = NULL;
file = fopen(filenameread, "r");
if (file == NULL) printf("Could not open read file %s", filenameread);
while (fgets(data, MAXCHAR, file) != NULL){
if (counter == 1){
root = input(root, data);
counter = counter + 1;
}else input(root,data);
}
fclose(file);
file= fopen(filenamewrite, "w");
if (file == NULL) printf("Could not open file to write to %s", filenamewrite);
traverse_order(root);
fclose(file);
return 0;
}
\ No newline at end of file
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
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