2016년 6월 9일 목요일

이것이 c언어다. 15장 프로필교환

프로필 교환하기. 교환할때 int형과 double형을 하나의 함수로 스왑해주기.

#include <stdio.h>
#include <string.h>

void swap(char *pc, void* a, void* b);
void printProfile(int a, double t);

int main(int argc, char *argv[])
{
    int firstAge;
    int secondAge;
    double firstTall;
    double secondTall;

    printf("enter the age and tall of first person. : ");
    scanf("%d %lf",&firstAge,&firstTall);

    printf("enter the age and tall of second person. : ");
    scanf("%d %lf",&secondAge,&secondTall);

    printProfile(firstAge,firstTall);
    printProfile(secondAge,secondTall);

    swap("int",(void*)&firstAge,(void*)&secondAge);
    swap("double",(void*)&firstTall,(void*)&secondTall);

    printProfile(firstAge,firstTall);
    printProfile(secondAge,secondTall);
    return 0;
}
void swap(char *pc, void* a, void* b)
{
    if( strcmp(pc,"int") == 0)
    {
        int t;
        t = *(int*)a;
        *(int*)a = *(int*)b;
        *(int*)b = t;
    }
    if( strcmp(pc,"double") == 0)
    {
        double t;
        t = *(double*)a;
        *(double*)a = *(double*)b;
        *(double*)b = t;
    }

}
void printProfile(int a, double t)
{
    printf("age : %d, tall : %.2lf \n",a,t);

}


댓글 없음:

댓글 쓰기