#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);
}
}