void sort_by_name( Taxonomy& tax, bool recursive, bool case_sensitive ) { // Make two functions for case sensitive and insensitive comparison. auto comp_by_name_cs = []( Taxon const& lhs, Taxon const& rhs ) { return lhs.name() < rhs.name(); }; auto comp_by_name_ci = []( Taxon const& lhs, Taxon const& rhs ) { return utils::to_lower( lhs.name() ) < utils::to_lower( rhs.name() ); }; // Sort. if( case_sensitive ) { // std::sort( tax.begin(), tax.end(), comp_by_name_cs ); tax.sort( comp_by_name_cs ); } else { // std::sort( tax.begin(), tax.end(), comp_by_name_ci ); tax.sort( comp_by_name_ci ); } // Run recursion if necessary. if( recursive ) { for( auto& child : tax ) { sort_by_name( child, true, case_sensitive ); } } }
int main (int argc, char *argv[]) { char in[10]; int id = 1; User *s; unsigned num_users; while (1) { printf ("1. add user\n"); printf ("2. find user\n"); printf ("3. delete user\n"); printf ("4. delete all users\n"); printf ("5. sort items by name\n"); printf ("6. sort items by id\n"); printf ("7. print users\n"); printf ("8. count users\n"); gets (in); switch (atoi (in)) { case 1: printf ("name?\n"); add_user (id++, gets (in)); break; case 2: printf ("id?\n"); s = find_user (atoi (gets (in))); printf ("user: %s\n", s ? s->name : "unknown"); break; case 3: printf ("id?\n"); s = find_user (atoi (gets (in))); if (s) delete_user (s); else printf ("id unknown\n"); break; case 4: delete_all (); break; case 5: sort_by_name (); break; case 6: sort_by_id (); break; case 7: print_users (); break; case 8: num_users = HASH_COUNT (users); printf ("there are %u users\n", num_users); break; } } }
int process_for_result() { // Number of found hotels int found = 0; // Current processing HOTEL HOTEL hotel; for (i = 0; i < 50; i++) { // Clear out existing search result hotel_search_results[i] = empty_hotel; hotel = hotels[i]; // Check for criteria... if (StrStrI(hotel.name, userinput) != 0 || StrStrI(hotel.location, userinput) != 0 // Search // // string // matches? ) { // We found it. Now store it hotel_search_results[found++] = hotel; } } // Sort the results switch (sortby) { case SORT_NONE: break; case SORT_NAME: sort_by_name(hotel_search_results, found); break; case SORT_STAR: sort_by_star(hotel_search_results, found); break; case SORT_RATING: sort_by_rating(hotel_search_results, found); break; case SORT_POPS: sort_by_pops(hotel_search_results, found); break; case SORT_PRICE: sort_by_price(hotel_search_results, found); break; } return found; }
int sort(int argc, char *argv[]) { printf("sort cmd\n"); if (strcmp(argv[1], "name") == 0) sort_by_name(); if (strcmp(argv[1], "age") == 0) sort_by_age(); return 0; }
/* runs the program */ int main(int argc, char ** argv) { char i_name[MAXNAME + 1] ; /* name of current entry in file */ int i_grade ; /* grade of current entry in file */ int i; /* counter */ /* file i/o to read in file for grades */ FILE *inputF ; char *mode = "r" ; /* store the file handle */ inputF = fopen(argv[1], mode) ; /* if cannot read file */ if(inputF == NULL) { fprintf(stderr, "Invalid input file.") ; exit(1) ; } /* pass in the address of "grade" */ while(fscanf(inputF, "%s %d", i_name, &i_grade) != EOF) { i_name[0] = toupper(i_name[0]) ; struct grade_entry entry ; entry.name = calloc( strlen( i_name ) + 1, sizeof( char ) ) ; strncpy(entry.name, i_name, strlen( i_name ) ) ; entry.grade = i_grade ; grade_list[numgrades] = entry ; numgrades++ ; } printf("\nUnsorted data: \n"); print_grades(); printf("\nSort by name: \n"); sort_by_name(); print_grades(); printf("\nSort by grade: \n"); sort_by_grade(); print_grades(); printf("\nMean: %d\n", calcMean(grade_list, numgrades)); printf("Median: %d\n", calcMedian(grade_list, numgrades)); return 0; }
int main(void) { int i; int cont = 1; student std[5]; // 5人のデータを入力する for(i=0;i<5;i++){ scanf("%s",std[i].name); scanf("%d",&std[i].height); scanf("%f",&std[i].weight); scanf("%ld",&std[i].schols); } while (cont) { printf("please select: sort by <1: height, 2:weight, 3:name, 0:quit>\n "); scanf("%d", &cont); switch (cont) { case 1: printf("height\n"); sort_by_height(std, NUMBER); break; case 2: printf("weight\n"); sort_by_weight(std, NUMBER); break; case 3: printf("name\n"); sort_by_name(std, NUMBER); break; } if (cont) { puts("-----------------------------\n"); for (i = 0; i < NUMBER; i++) printf("%-8s %6d%6.1f%7ld\n", std[i].name, std[i].height, std[i].weight, std[i].schols); puts("-----------------------------\n"); } } return (0); }
static void read_afms(FILE *f_c, FILE *f_h) { DIR *d = opendir("."); struct dirent *de; fputs( "/*\n" " * Built-in font metrics\n" " */\n" "\n" "const AFM *const PSDRV_BuiltinAFMs[] =\n" "{\n", f_c); if (d == NULL) { fprintf(stderr, "Error opening current directory\n"); exit(__LINE__); } while ((de = readdir(d)) != NULL) { FILE *f; char *cp, linebuf[256], font_family[128]; int i, num_metrics; cp = strrchr(de->d_name, '.'); /* Does it end in */ if (cp == NULL || strcasecmp(cp, ".afm") != 0) /* .afm or .AFM? */ continue; f = fopen(de->d_name, "r"); if (f == NULL) { fprintf(stderr, "Error opening %s\n", de->d_name); exit(__LINE__); } while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "FontName not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "FontName ", 9) == 0) break; } sscanf(linebuf, "FontName %[^\r\n]", font_family); for (i = 0; font_family[i] != '\0'; ++i) if (font_family[i] == '-') font_family[i] = '_'; fprintf(f_h, "extern const AFM PSDRV_%s;\n", font_family); fprintf(f_c, " &PSDRV_%s,\n", font_family); while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "FamilyName not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "FamilyName ", 11) == 0) break; } sscanf(linebuf, "FamilyName %[^\r\n]", font_family); while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "StartCharMetrics not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "StartCharMetrics ", 17) == 0) break; } sscanf(linebuf, "StartCharMetrics %i", &num_metrics); for (i = 0; i < num_metrics; ++i) { char namebuf[128]; if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "Unexpected EOF after %i glyphs in %s\n", i, de->d_name); exit(__LINE__); } cp = strchr(linebuf, 'N'); if (cp == NULL || strlen(cp) < 3) { fprintf(stderr, "Parse error after %i glyphs in %s\n", i, de->d_name); exit(__LINE__); } sscanf(cp, "N %s", namebuf); if (search_by_name(namebuf) != NULL) continue; sprintf(linebuf, "FONT FAMILY;%s", font_family); glyphs[num_glyphs].UV = -1; glyphs[num_glyphs].name = strdup(namebuf); glyphs[num_glyphs].comment = strdup(linebuf); if (glyphs[num_glyphs].name == NULL || glyphs[num_glyphs].comment == NULL) { fprintf(stderr, "Memory allocation failure\n"); exit(__LINE__); } ++num_glyphs; sort_by_name(); } fclose(f); } closedir(d); fputs(" NULL\n};\n", f_c); }