Skip to content

Instantly share code, notes, and snippets.

@nikoncode
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save nikoncode/fc8337a659e5e742565d to your computer and use it in GitHub Desktop.

Select an option

Save nikoncode/fc8337a659e5e742565d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct teams {
char team[20];
struct teams * next;
struct comp * down;
} teams_list;
typedef struct {
char last_name[20];
char first_name[20];
char team[20];
char sports_type[20];
int all_pts;
int penalty_pts;
} fields;
typedef struct comp {
fields f;
struct comp * next;
} competition;
char file_name[] = "backup.txt";
teams_list * head = NULL;
void swap_elements(competition * el1, competition * el2) {
fields swap = el1->f;
el1->f = el2->f;
el2->f = swap;
}
void new_sort(int (*compare)(competition *, competition * )) {
if (head == NULL) {
printf("WARNING: List is empty.\n");
return;
}
teams_list * current_parent = head;
competition * temp;
do {
if (current_parent->down == NULL) continue;
competition * current = current_parent->down, * next;
int length=0;
do {
length++;
} while ((current = current->next) != NULL);
printf("INFO: team '%s' length is '%d'.\n",current_parent->team, length);
int i, swap_cnt;
for (i=0;i<length;++i) {
swap_cnt = 0;
current = current_parent->down;
if (current->next == NULL) {
printf("INFO: ARRAY CONTAINS ONE ELEMENT.\n");
break;
}
next = current->next;
do {
if (compare(current, next) > 0) {
swap_elements(current, next);
}
current = current->next;
next = current->next;
} while (next != NULL);
if (swap_cnt == 0) break;
}
} while ((current_parent = current_parent->next) != NULL);
}
teams_list * find_parent(char query[20]) {
printf("INFO: called find_parent with '%s'.\n", query);
teams_list * new_team = malloc(sizeof(teams_list));
strcpy(new_team->team, query);
new_team->next = NULL;
new_team->down = NULL;
printf("INFO: new team with value '%s' has created.\n", new_team->team);
if (head == NULL) {
printf("INFO: List is empty. Modify head.\n");
head = new_team;
return new_team;
} else {
printf("INFO: List is not empty. Attempt to search.\n");
//printf("Current head team value is '%s'.\n", head->team);
teams_list * current = head, * previous;
do {
if (strcmp(current->team, query) == 0) {
printf("INFO: team '%s' already exists.\n", query);
free(new_team);
return current;
}
previous = current;
current = current->next;
} while (current != NULL);
printf("INFO: team '%s' is not exists. Adding to list.\n", query);
previous->next = new_team;
return new_team;
}
}
void f_add(competition * el) {
teams_list * parent;
parent = find_parent(el->f.team);
if (parent->down == NULL) {
parent->down = el;
} else {
competition * temp = parent->down;
while (temp->next != NULL) temp = temp->next;
temp->next = el;
}
}
void f_input() {
while (1) {
competition * new_element = malloc(sizeof(competition));
printf("Enter first name: \n");
scanf(" %[^\n]", new_element->f.first_name);
printf("Enter last name: \n");
scanf(" %[^\n]", new_element->f.last_name);
printf("Enter team: \n");
scanf(" %[^\n]", new_element->f.team);
printf("Enter sports type: \n");
scanf(" %[^\n]", new_element->f.sports_type);
printf("Enter summary pts: \n");
scanf(" %d", &new_element->f.all_pts);
printf("Enter penalty pts: \n");
scanf(" %d", &new_element->f.penalty_pts);
new_element->next = NULL;
f_add(new_element); //add element to list
printf("DO YOU HAVE AGAIN?!! (y/n)\n");
char answer;
scanf(" %c", &answer);
if (answer != 'y') return;
}
}
void f_print() {
if (head == NULL) {
printf("WARNING: List is empty. Plz add new one.\n");
return;
}
int limit = 10;
int index = 0;
teams_list * current_parent = head;
do {
if (current_parent->down == NULL) continue;
competition * temp = current_parent->down;
do {
++index;
printf("%02d|%10s|%10s|%10s|%10s|%5d|%5d|\n",
index,
temp->f.first_name,
temp->f.last_name,
temp->f.team,
temp->f.sports_type,
temp->f.all_pts,
temp->f.penalty_pts);
if (index % limit == 0) {
system("pause");
}
} while ((temp = temp->next) != NULL);
} while ((current_parent = current_parent->next) != NULL);
}
void f_delete(void) { //TO-DO: fix remove parent
if (head == NULL) {
printf("WARNING: List is empty. Plz add new one.\n");
return;
}
int removed;
printf("Enter number element: \n");
scanf(" %d", &removed);
int index = 0;
teams_list * current_parent = head, * previous_parent;
do {
if (current_parent->down == NULL) continue;
competition * temp = current_parent->down, * previous;
do {
++index;
if (index == removed) {
if (current_parent->down == temp && temp->next == NULL) {
free(temp);
free(current_parent);
if (head == current_parent && current_parent->next == NULL) {
head = NULL;
} else if (head == current_parent && current_parent->next != NULL) {
head = current_parent->next;
} else {
previous_parent->next = NULL;
}
}
if (current_parent->down == temp) {
current_parent->down = temp->next;
free(temp);
} else {
previous->next = temp->next;
free(temp);
}
return;
}
previous = temp;
temp = temp->next;
} while (temp != NULL);
previous_parent = current_parent;
current_parent = current_parent->next;
} while (current_parent != NULL);
printf("WARNING: ELEMENT NOT FOUND.\n");
}
void f_save(void) {
if (head == NULL) {
printf("List is empty. Nothing to save.\n");
return;
}
FILE * backup = fopen(file_name, "w");
teams_list * current_parent = head;
do {
if (current_parent->down == NULL) continue;
competition * temp = current_parent->down;
do {
fprintf(backup, "%s|%s|%s|%s|%d|%d|\n",
temp->f.first_name,
temp->f.last_name,
temp->f.team,
temp->f.sports_type,
temp->f.all_pts,
temp->f.penalty_pts);
} while ((temp = temp->next) != NULL);
} while ((current_parent = current_parent->next) != NULL);
fclose(backup);
}
void f_restore(void) {
FILE * backup = fopen(file_name, "r");
head = NULL;
if (backup == NULL) {
printf("File '%s' not found.\n", file_name);
return;
}
fields temporary_field;
competition * previously;
while (fscanf(backup, " %[^|]| %[^|]| %[^|]| %[^|]| %d| %d|\n", temporary_field.first_name, temporary_field.last_name, temporary_field.team, temporary_field.sports_type, &temporary_field.all_pts, &temporary_field.penalty_pts) != EOF) {
competition * new_element = malloc(sizeof(competition));
new_element->next = NULL;
new_element->f = temporary_field;
teams_list * parent = find_parent(new_element->f.team);
if (parent->down == NULL) {
parent->down = new_element;
} else {
competition * temp = parent->down;
while (temp->next != NULL) temp = temp->next;
temp->next = new_element;
}
}
fclose(backup);
}
void f_change(void) {
if (head == NULL) {
printf("WARNING: List is empty. Plz add new one.\n");
return;
}
int changed;
printf("Enter number element: \n");
scanf(" %d", &changed);
int index = 0;
teams_list * current_parent = head;
do {
if (current_parent->down == NULL) continue;
competition * temp = current_parent->down, * previous;
do {
++index;
if (index == changed) {
printf("Enter field name to change :\n");
char field_name[50];
scanf(" %[^\n]", field_name);
if (strcmp(field_name, "first_name") == 0) {
printf("Old value of '%s' param is '%s'\n", field_name, temp->f.first_name);
printf("Enter new value of '%s': \n", field_name);
scanf(" %[^\n]", temp->f.first_name);
} else if (strcmp(field_name, "last_name") == 0) {
printf("Old value of '%s' param is '%s'\n", field_name, temp->f.last_name);
printf("Enter new value of '%s': \n", field_name);
scanf(" %[^\n]", temp->f.last_name);
} else if (strcmp(field_name, "team") == 0) {
printf("Old value of '%s' param is '%s'\n", field_name, temp->f.team);
printf("Enter new value of '%s': \n", field_name);
scanf(" %[^\n]", temp->f.team);
f_add(temp);
previous->next = temp->next;
printf("INFO: RELOCATE DONE.\n");
} else if (strcmp(field_name, "sports_type") == 0) {
printf("Old value of '%s' param is '%s'\n", field_name, temp->f.sports_type);
printf("Enter new value of '%s': \n", field_name);
scanf(" %[^\n]", temp->f.sports_type);
} else if (strcmp(field_name, "all_pts") == 0) {
printf("Old value of '%s' param is '%d'\n", field_name, temp->f.all_pts);
printf("Enter new value of '%s': \n", field_name);
scanf(" %d", &temp->f.all_pts);
} else if (strcmp(field_name, "penalty_pts") == 0) {
printf("Old value of '%s' param is '%d'\n", field_name, temp->f.penalty_pts);
printf("Enter new value of '%s': \n", field_name);
scanf(" %d", &temp->f.penalty_pts);
} else {
printf("Field name '%s' is not member of (first_name/last_name/team/sports_type/all_pts/penalty_pts) set.\n", field_name);
}
return;
}
previous = temp;
temp = temp->next;
} while (temp != NULL);
} while ((current_parent = current_parent->next) != NULL);
printf("WARNING: ELEMENT NOT FOUND.\n");
}
void terminate() {
exit(0);
}
int compare_ints(competition * el1, competition * el2) {
if (el1->f.all_pts > el2->f.all_pts) return 1;
if (el1->f.all_pts < el2->f.all_pts) return -1;
return 0;
}
int compare_strings(competition * el1, competition * el2) {
return strcmp(el1->f.first_name, el2->f.first_name);
}
void sort_1() {
new_sort(compare_ints);
}
void sort_2() {
new_sort(compare_strings);
}
int main() {
while (1) {
struct mn {
char desc[100];
void (*go)();
} menu[] = {
{"Add to list (console I/O)", f_input},
{"View list (console I/O)", f_print},
{"Save list", f_save},
{"Restore list", f_restore},
{"Delete element", f_delete},
{"Sort by index field", sort_1},
{"Sort by text field", sort_2},
{"Change values", f_change},
{"Exit", terminate}
};
int menu_cnt = 9, i;
system("cls");
for (i=0;i<menu_cnt;++i) {
printf("%d : %s\n", i, menu[i].desc);
}
printf("Choose your destiny: ");
int choose;
;
if (scanf(" %d", &choose) != 1 || choose >= menu_cnt) {
printf("ERROR! Invalid menu point.\n");
system("pause");
}
system("cls");
menu[choose].go();
system("pause");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment