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 73 74 75 76 77 78 79 80 81 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> typedef struct phonebook { char name[10]; // name char phone[20]; // phone number } phonebook; phonebook mybook[10] = { {"È«±æµ¿","010-1234-5678"}, {"±èö¼ö","010-3456-1111"}, {"¾ç±Íºñ","010-5634-2390"}, {"±èöȣ","019-1111-2222"}, {"¹ÚÂùÈ£","011-3434-1290"} }; int mystrstr(char str[], char substr[]) { int i, j; for (i = 0; str[i] != 0; i++) { for (j = 0; substr[j] != 0; j++) { if (substr[j] != str[i + j]) break; } if (substr[j] == 0) return i; } return -1; } void swap(phonebook s[], int i, int j) { phonebook tmp; tmp = s[i]; s[i] = s[j]; s[j] = tmp; } void selection_sort_byname(phonebook s[], int n) { int i, j, least; for (i = 0; i < n - 1; i++) { least = i; for (j = i + 1; j < n; j++) { if (strcmp(s[j].name, s[least].name) < 0) least = j; } swap(s, i, least); } } void print_book(phonebook s[], int n) { for (int i = 0; i < n; i++) { printf("%s: %s \n", s[i].name, s[i].phone); } printf("\n"); } int find_phone(phonebook s[], char* fstr, int n) { for (int i = 0; i < n; i++) { if (mystrstr(s[i].name, fstr) != -1) printf("%s: %s \n", s[i].name, s[i].phone); if (mystrstr(s[i].phone, fstr) != -1) printf("%s: %s \n", s[i].name, s[i].phone); } } int main(void) { char find_str[20]; int n; selection_sort_byname(mybook, 5); print_book(mybook, 5); while (1) { printf("ã°í ½ÍÀº Á¤º¸(À̸§À̳ª ÀüȹøÈ£ ÀϺΠÀÔ·Â)(Á¾·á´Â q):"); scanf("%s", find_str); if (find_str[0] == 'q') break; n = find_phone(mybook, find_str, 5); } } | cs |