Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  H2018
Language: C/C++
Code:
#include <stdio.h>

#include <string.h>

struct phone
{
    char name[20];  //이름
    char number[20]; //전화번호
    int age; //나이
};

void phone_sort(struct phone table[], int size);
int main()
{
    struct phone pb[100];
    int count = 0, i;

    while(1)
    {
        printf("%d번째 입력.\n", count + 1);
        printf(">> 이름 : ");
        gets(pb[count].name);

        if(strcmp(pb[count].name, "") == 0)
        {
            break;
        }

        printf(">> 전화번호 : ");
        scanf("%*c%s", pb[count].number);
        printf(">> 나이 : ");
        scanf("%d%*c", &pb[count].age);
        ++count;
    }

    phone_sort(pb, count);

    printf("%-20s  %-20s  %s\n", "이름", "전화번호", "나이");
    printf("===================================================\n");
    for(i=0; i<count; i++)
    {
        printf("%-20s  %-20s  %-3d\n", pb[i].name, pb[i].number, pb[i].age);
    }
}

void phone_sort(struct phone table[], int size)
{
    int i, j;
    struct phone temp;

    for(i=0; i<size-1; i++)
    {
        for(j=i+1; j<size; j++)
        {
            if(strcmp(table[i].name, table[j].name) > 0)
            {
                temp = table[i];
                table[i] = table[j];
                table[j] = temp;
            }
        }
    }
}
Comments: