C++ (연습문제 1-2)

Program_Language/C++ 2011. 7. 25. 02:21 Posted by Request

문제 1]
다음 main 함수에서 필요로 하는 swap함수를 구현하라.
int main()
{
     int num1=20, num2=30;
     swap(&num1, &num2);

     std::cout<<num1<<'   '<<num2<<std::endl;

     char ch1='a' , ch2='z';
     swap(&ch1, &ch2);
     std::cout<<num1<<'    '<< num2<<std::endl;

    return 0;
}

=============================================
요구되는 출력 결과
  30 20
   z  a
=============================================

void swap(int *a, int *b);
void swap(char *a, char *b);


void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b= temp;
}

void swap(char *a, char *b)
{
            char temp = *a;
               *a = *b;
               *b=temp;
}