// golfScores.c : Questo file contiene la funzione 'main', in cui inizia e termina l'esecuzione del programma. // #include #include #include /* Maximum length of names */ #define STRMAX 20 /* Maximum length of rows */ #define MAX_ROW 80 struct dnode { int score; char player[STRMAX+1]; struct dnode* prev; struct dnode* next; }; struct dnode* first; /* pointer to the first list item */ struct dnode* last; /* pointer to the last list item */ struct dnode* createNode(char* playerName, int playerScore) { struct dnode* newNode = (struct dnode*)malloc(sizeof(struct dnode)); if (newNode != NULL) { strcpy(newNode->player, playerName); newNode->score = playerScore; newNode->next = NULL; newNode->prev = NULL; } return(newNode); } void printList(struct dnode* head) { struct dnode* current = head; while (current != NULL) { printf("%s ", current->player); printf("%d\n", current->score); current = current->next; } printf("\n"); } void printListDescending(struct dnode* head) { struct dnode* current = head; while (current != NULL) { if (current->next == NULL) break; else current = current->next; } if (current != NULL) { while (current != head) { printf("%s ", current->player); printf("%d\n", current->score); current = current->prev; } printf("%s ", current->player); printf("%d\n", current->score); } printf("\n"); } void saveData(FILE *fo, struct dnode* head) { struct dnode* current = head; while (current != NULL) { if (fprintf(fo, "%s;%d\n", current->player, current->score) < 0) { perror("Error saving data to file"); break; } current = current->next; } printf("\n"); } void printPlayersSameScore(struct dnode* pnode) { int playerScore = 0; struct dnode* current = pnode; if (pnode != NULL) { playerScore = pnode->score; } while (current != NULL && current->score == playerScore) { printf("%s ", current->player); printf("%d\n", current->score); current = current->next; } current = pnode->prev; while (current != NULL && current->score == playerScore) { printf("%s ", current->player); printf("%d\n", current->score); current = current->prev; } printf("\n"); } struct dnode* searchItem(struct dnode* head, char* playerToSearch) { while ((head != NULL) && (strcmp(head->player,playerToSearch)!=0)) head = head->next; return(head); } void head_insert(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; tmp = *ptr; *ptr = newNode; if (tmp != NULL) { tmp->prev = newNode; (*ptr)->next = tmp; } else { (*ptr)->next = NULL; (*ptr)->prev = NULL; } } void tail_insert(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; if (*ptr == NULL) { *ptr = newNode; (*ptr)->next = NULL; (*ptr)->prev = NULL; } else { tmp = *ptr; while (tmp->next != NULL) tmp = tmp->next; tmp->next = newNode; newNode->prev = tmp; newNode->next = NULL; } } void insert_x_score(struct dnode** ptr, struct dnode* newNode) { struct dnode* tmp = NULL; if (*ptr == NULL) { *ptr = newNode; (*ptr)->next = NULL; (*ptr)->prev = NULL; } else { if ((*ptr)->score >= newNode->score) { newNode->next = *ptr; newNode->prev = NULL; (*ptr)->prev = newNode; *ptr = newNode; } else { tmp = *ptr; while (tmp->next != NULL) { if ((tmp->score <= newNode->score) && (tmp->next->score >= newNode->score)) { newNode->next = tmp->next; tmp->next->prev = newNode; tmp->next = newNode; newNode->prev = tmp; break; } else { tmp = tmp->next; } } if (tmp->next == NULL) { tmp->next = newNode; newNode->next = NULL; newNode->prev = tmp; } } } } int updateData(FILE* fi, struct dnode* head) { char row[MAX_ROW + 1]; while (!feof(fi)) { if (fgets(row, MAX_ROW, fi) != row) { if (feof(fi)) { break; } else { perror("Error reading file input"); return(3); } } char player[STRMAX + 1]; int score; char* t; if ((t = strtok(row, ";")) != NULL) { strcpy(player, t); if ((t = strtok(NULL, ";")) != NULL) { score = atoi(t); } } else { printf("Error reading tokens from golf scores file"); return(4); } //printf("Player %s - score %d\n", player, score); struct dnode* playerNode = searchItem(head, player); if (playerNode != NULL) { playerNode->score += score; } else { printf("Error searching player %s: not found", player); } } return(0); } struct dnode* insertionSort(struct dnode* head) { struct dnode* headList = NULL; struct dnode* current = head; while (current != NULL) { struct dnode* newNode = createNode(current->player, current->score); if (newNode != NULL) { insert_x_score(&headList, newNode); } else { printf("Error creating newNode"); return(NULL); } current = current->next; } while (head != NULL) { current = head->next; free(head); head = current; } return(headList); } int main(int argc, char *argv[]) { char row[MAX_ROW+1]; struct dnode* headList = NULL; struct dnode* headListTail = NULL; struct dnode* headListScore = NULL; if (argc != 2) { printf("Error: usage %s golf_scores_filename\n", argv[0]); return(1); } FILE* fi = fopen(argv[1], "r"); if (fi == NULL) { perror("Error opening input file\n"); return(2); } while (!feof(fi)) { if (fgets(row, MAX_ROW, fi) != row) { if (feof(fi)) { break; } else { perror("Error reading file input"); return(3); } } char player[STRMAX+1]; int score; char* t; if ((t = strtok(row, ";")) != NULL) { strcpy(player, t); if ((t = strtok(NULL, ";")) != NULL) { score = atoi(t); } } else { printf("Error reading tokens from golf scores file"); return(4); } //printf("Player %s - score %d\n", player, score); struct dnode* newNode = createNode(player, score); if (newNode != NULL) { //head_insert(&headList, newNode); //tail_insert(&headListTail, newNode); insert_x_score(&headListScore, newNode); } else { printf("Error creating newNode"); return(5); } } fclose(fi); //printList(headListScore); bool menu = true; while (menu) { printf("[1] Print list in ascending order of scores\n"); printf("[2] Print list in decending order of scores\n"); printf("[3] Search player\n"); printf("[4] Load new scores\n"); printf("[5] Save scores\n"); printf("[6] Exit\n"); printf("\nMake your choice:"); char choice[STRMAX + 1]; if (gets_s(choice, STRMAX) == NULL) { printf("Error reading choice\n"); return(6); } /*else { printf("Choice %s\n", choice); }*/ int myChoice = atoi(choice); switch (myChoice) { case 1: printf("Printing current list\n"); printList(headListScore); break; case 2: printf("Printing current list\n"); printListDescending(headListScore); break; case 3: { char playerName[STRMAX + 1]; printf("\nInsert player name:"); if (gets_s(playerName, STRMAX) == NULL) { printf("Error reading player name\n"); return(7); } else { printf("Player to be searched %s\n", playerName); } struct dnode* playerFound = searchItem(headListScore, playerName); if (playerFound != NULL) { printPlayersSameScore(playerFound); } else { printf("Player %s not found\n", playerName); } break; } case 4: { printf("Loading new scores..\n"); char filenameToRead[MAX_ROW + 1]; printf("\nInsert filename with new scores:"); if (gets_s(filenameToRead, MAX_ROW) == NULL) { printf("Error reading file name\n"); return(9); } FILE* fi = fopen(filenameToRead, "r"); if (fi != NULL) { int retc = updateData(fi, headListScore); if (retc != 0) { printf("Error updating scores data\n"); } fclose(fi); } else { perror("Error opening file"); printf("Error opening file '%s.\n", filenameToRead); } headListScore = insertionSort(headListScore); if (headListScore == NULL) { printf("Error sorting list.\n"); return(10); } break; } case 5: { char filenameToSave[MAX_ROW + 1]; printf("\nInsert filename where saving data:"); if (gets_s(filenameToSave, MAX_ROW) == NULL) { printf("Error reading file name\n"); return(7); } FILE* fo = fopen(filenameToSave, "r"); if (fo != NULL) { // Close the file after checking existence fclose(fo); printf("File '%s' exists.\n", filenameToSave); printf("Do you want to overwrite it (Y/N):"); if (gets_s(choice, STRMAX) == NULL) { printf("Error reading choice\n"); return(11); } char c = choice[0]; if ((c == 'n') || (c == 'N')) printf("\nPlease choose a different filename\n"); else if ((c == 'y') || (c == 'Y')) { printf("\nSaving data in file '%s'.\n", filenameToSave); fo = fopen(filenameToSave, "w"); if (fo == NULL) { perror("Error opening file for saving data"); return(8); } saveData(fo, headListScore); fclose(fo); } else printf("\nBad choice. Please choose a different filename\n"); } else { printf("Saving data in file '%s'.\n", filenameToSave); fo = fopen(filenameToSave, "w"); if (fo == NULL) { perror("Error opening file for saving data"); return(8); } saveData(fo, headListScore); fclose(fo); } break; } case 6: menu = false; break; default: printf("Choice %s is a bad choice\n", choice); break; } } return(0); }