Created
April 4, 2014 04:07
-
-
Save nikoncode/9967939 to your computer and use it in GitHub Desktop.
123123
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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| 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; | |
| //definitions | |
| competition * list_head = NULL; | |
| char file_name[] = "backup.txt"; | |
| void f_print(void); | |
| void f_input(void); | |
| void f_add(competition *); | |
| void f_delete(void); | |
| void f_change(void); | |
| //void f_sort_1(); | |
| //void f_sort_2(); | |
| void f_save(void); | |
| void f_restore(void); //not_working | |
| //end of definitions | |
| void f_sort_1() { | |
| if (list_head == NULL) { | |
| printf("List is empty.\n"); | |
| return; | |
| } | |
| competition * current = list_head, * next; | |
| int length=0; | |
| do { | |
| length++; | |
| } while ((current = current->next) != NULL); | |
| int i; | |
| for (i=0;i<length;++i) { | |
| current = list_head; | |
| next = current->next; | |
| do { | |
| if (current->f.all_pts > next->f.all_pts) { | |
| fields swap = current->f; | |
| current->f = next->f; | |
| next->f = swap; | |
| } | |
| current = current->next; | |
| next = current->next; | |
| } while (next != NULL); | |
| } | |
| } | |
| 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_add(competition * el) { | |
| if (list_head == NULL) { | |
| list_head = el; | |
| } else { | |
| competition * temp = list_head; | |
| while (temp->next != NULL) temp = temp->next; | |
| temp->next = el; | |
| } | |
| } | |
| void f_print() { | |
| if (list_head == NULL) { | |
| printf("List is empty.\n"); | |
| return; | |
| } | |
| int limit = 10; | |
| int index = 0; | |
| competition * temp = list_head; | |
| do { | |
| ++index; | |
| printf("%10s|%10s|%10s|%10s|%5d|%5d|\n", | |
| 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) { | |
| index = 0; | |
| system("pause"); | |
| } | |
| } while ((temp = temp->next) != NULL); | |
| } | |
| void f_delete(void) { | |
| if (list_head == NULL) { | |
| printf("List is empty.\n"); | |
| return; | |
| } | |
| int id, cur_pos = 0; | |
| printf("Enter number element: \n"); | |
| scanf(" %d", &id); | |
| competition * current = list_head; | |
| competition * previously = NULL; | |
| if (id == 0) { | |
| if (current->next != NULL) | |
| list_head = current->next; | |
| else | |
| list_head = NULL; | |
| free(current); | |
| return; | |
| } | |
| do { | |
| if (id == cur_pos) { | |
| previously->next = current->next; | |
| free(current); | |
| break; | |
| } | |
| previously = current; | |
| current = current->next; | |
| cur_pos++; | |
| } while (current != NULL); | |
| if (id != cur_pos) { | |
| printf("ERR: NOT FOUND ELEMENT WITH ENTERED ID.\n"); | |
| } | |
| } | |
| void f_change(void) { | |
| if (list_head == NULL) { | |
| printf("List is empty.\n"); | |
| return; | |
| } | |
| int id, cur_pos = 0; | |
| printf("Enter number element: \n"); | |
| scanf(" %d", &id); | |
| competition * current = list_head; | |
| do { | |
| if (id == cur_pos) { | |
| printf("Old values: \n"); | |
| printf("%10s|%10s|%10s|%10s|%5d|%5d|\n", | |
| current->f.first_name, | |
| current->f.last_name, | |
| current->f.team, | |
| current->f.sports_type, | |
| current->f.all_pts, | |
| current->f.penalty_pts); | |
| system("pause"); | |
| printf("Enter new values: \n\n"); | |
| printf("Enter first name: \n"); | |
| scanf(" %[^\n]s", current->f.first_name); | |
| printf("Enter last name: \n"); | |
| scanf(" %[^\n]s", current->f.last_name); | |
| printf("Enter team: \n"); | |
| scanf(" %[^\n]s", current->f.team); | |
| printf("Enter sports type: \n"); | |
| scanf(" %[^\n]s", current->f.sports_type); | |
| printf("Enter summary pts: \n"); | |
| scanf(" %d", ¤t->f.all_pts); | |
| printf("Enter penalty pts: \n"); | |
| scanf(" %d", ¤t->f.penalty_pts); | |
| break; | |
| } | |
| current = current->next; | |
| cur_pos++; | |
| } while (current != NULL); | |
| if (id != cur_pos) { | |
| printf("ERR: NOT FOUND ELEMENT WITH ENTERED ID.\n"); | |
| } | |
| } | |
| void f_save(void) { | |
| if (list_head == NULL) { | |
| printf("List is empty. Nothing to save.\n"); | |
| return; | |
| } | |
| FILE * backup = fopen(file_name, "w"); | |
| competition * temp = list_head; | |
| 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); | |
| fclose(backup); | |
| } | |
| void f_restore(void) { | |
| list_head == NULL; //TO-DO: free all list | |
| FILE * backup = fopen(file_name, "r"); | |
| competition * temp = malloc(sizeof(competition)); | |
| while (fscanf(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) != EOF) { | |
| printf("%10s|%10s|%10s|%10s|%5d|%5d|\n", | |
| temp->f.first_name, | |
| temp->f.last_name, | |
| temp->f.team, | |
| temp->f.sports_type, | |
| temp->f.all_pts, | |
| temp->f.penalty_pts); | |
| system("pause"); | |
| } | |
| fclose(backup); | |
| } | |
| 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", f_sort_1}, | |
| {"Change values", f_change} | |
| }; | |
| int menu_cnt = 6, i; | |
| for (i=0;i<menu_cnt;++i) { | |
| printf("%d : %s\n", i, menu[i].desc); | |
| } | |
| printf("Choose your destiny: "); | |
| int choose; | |
| scanf(" %d", &choose); | |
| menu[choose].go(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment