1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> typedef unsigned char uchar; int mystrcmp(const char* str1, const char* str2) { for (int i = 0; ; i++) { if ((uchar) str1[i] - (uchar) str2[i] < 0) return -1; else if ((uchar) str1[i] - (uchar) str2[i] > 0) return +1; if (str1[i] == 0 && str2[i] == 0) // || µµ µÊ (¿Ö µÉ±î »ý°¡Çغ¼ °Í) return 0; } } int main(void) { int result; char str[6][10] = { "ÇÏÇÏÇÏ", "ȣȣȣ", "ÇÏÇÏÇÏÇÏ", "¾È³ç", "ȣȣȣ", "ÇÏÇÏ"}; result = mystrcmp(str[1], str[4]); printf("% s - % s = %d\n", str[1], str[4], result); result = mystrcmp(str[0], str[5]); printf("% s - % s = %d\n", str[0], str[5], result); result = mystrcmp(str[0], str[2]); printf("% s - % s = %d\n", str[0], str[2], result); result = mystrcmp(str[1], str[3]); printf("% s - % s = %d\n", str[1], str[3], result); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #define N_STUDENTS 4 typedef unsigned char uchar; typedef struct student_info { char name[10]; int height; float weight; } student_info; student_info students[N_STUDENTS] = { {"À̼ø½Å", 172, 83.4}, {"È«±æµ¿", 167, 72.5}, {"±èÀ¯½Å", 159, 70.8}, {"À¯°ü¼ø", 163, 58.4} }; int mystrcmp(const char* str1, const char* str2) { for (int i = 0; ; i++) { if ((uchar)str1[i] - (uchar)str2[i] < 0) return -1; else if ((uchar)str1[i] - (uchar)str2[i] > 0) return +1; if (str1[i] == 0 && str2[i] == 0) // || µµ µÊ (¿Ö µÉ±î »ý°¡Çغ¼ °Í) return 0; } } int find_min_byName(student_info data[], int start, int n) { int i, least; least = start; for (i = start + 1; i < n; i++) { if (mystrcmp(data[i].name, data[least].name) < 0) least = i; } return least; } void sort_byName(student_info data[], int n) { student_info tmp; int least; for (int i = 0; i < n; i++) { least = find_min_byName(data, i, n); tmp = data[i]; data[i] = data[least]; data[least] = tmp; } } void print_data(student_info s[], int n) { for (int i = 0; i < n; i++) { printf("%s %d %f \n", s[i].name, s[i].height, s[i].weight); } } int main(void) { printf("-----[Á¤·Ä Àü]-------\n"); print_data(students, N_STUDENTS); sort_byName(students, N_STUDENTS); printf("-----[Á¤·Ä ÈÄ]-------\n"); print_data(students, N_STUDENTS); } | cs |