📄 club.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_NAME_SIZE 26struct hero{ char name[MAX_NAME_SIZE]; char alias[MAX_NAME_SIZE]; int debut;};void printRecords(struct hero *club);void sortRecords(char *key, struct hero *club);int main(int argc, char *argv[]){ struct hero heroes[] = { // name alias debut {"Spider-Man", "Peter Parker", 1962}, {"Wonder Woman", "Diana Prince", 1941}, {"The Tick", "The Tick", 1986}, {"Superman", "Clark Kent", 1938}, {"Buffy the Vampire Slayer", "Buffy Summers", 1992}, {"Zorro", "Don Diego de la Vega", 1919}, {"", "", 0000} }; if (argc == 1) { fprintf(stderr, "Usage: %s sort-key ...\n", argv[0]); return 1; } printf("*** Unsorted:\n"); printRecords(heroes); for (int i=1; i<argc; i++) { char *key = argv[i]; sortRecords(key, heroes); printf("\n*** Sorted by \"%s\":\n", key); printRecords(heroes); } return 0;}// Print the records, with a nice header.void printRecords(struct hero *club){int numClub = 0;printf("%-27s%-28s%s\n", "Name","Alias","Debut"); //print off a nice headerint j;while(club[numClub].debut != 0){ numClub++; //figure out how many real lines are being read}for(j=0; j<numClub; j++){printf("%-27s%-28s%d\n", club[j].name, club[j].alias, club[j].debut); //print off the results with the select spacing of 27 and 28.}}void sortRecords(char *key, struct hero *club){struct hero temp; int i;int j;int clubSize = 0;while(club[clubSize].debut != 0){ clubSize++; //agian continue reading in a line until it finds nothing left to read.}int caseKey = 0; //I am setting the cases here by assisgning them numerical values. if(strcmp(key, "hero") == 0) caseKey = 1;else if(strcmp(key, "alias") == 0) caseKey = 2;else if(strcmp(key, "debut") == 0) caseKey = 3;switch (caseKey) { case 1: /* organize the words by alphabetical order and the main difference between this one and the other 2 is you have to compare strings. if case 2 is greater then case 1 it is going to give you a -1 so to alphabetize them, it will run and print off the words that satisfy the condition.*/ for( int i=1; i<clubSize; i++ ) //BUBBLE SORT { for( int j=0; j<clubSize-i; j++ ) { if ( strcmp (club[j].name, club[j+1].name ) > 0) { temp = club[j]; //replace the cases allowing for the first value to "bubble" up so to speak club[j] = club[j+1]; club[j+1] = temp; } } } break; case 3:/*What this does is as an insertion sort, it takes the entire line and finds the largest value and places it in the first position available. Then it goes through again and finds the next smallestand places it in the 2nd spot so on and so forth until it reaches the end. */ for(i=1; i<clubSize; i++) //INSERTION SORT { int ind = club[i].debut; j=i; while((j>0) && (club[j-1].debut<ind)) { club[j].debut = club[j-1].debut; j = j-1; } club[j].debut = ind; } break; case 2:/* For the Alias case her I used the final sort, a selection sort and what that does is it establishes 2 arrays. It places one line in one of the array and the rest in another. It then goes through comparingstrings and it places them one at a time into the 1st array in the propper spot given the strcmp. */ for(i=0; i<clubSize-1; i++) //SELECTION SORT { int min = i; for(j = i+1; j< clubSize; j++) { if(strcmp(club[j].alias, club[min].alias)<0) min = j; } temp = club[i]; club[i] = club[min]; club[min] = temp; } break; default: //DEFAULT: not needed but just in case... good coding practice. printf("\nImprorper key. Use hero, alias, or debut.\n"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -