2012. 7. 13. 14:50

// Listing 15.8 Without function pointers

#include <iostream>
using namespace std;

void Square (int&,int&);
void Cube (int&, int&);
void Swap (int&, int &);
void GetVals(int&, int&);
void PrintVals(void (*)(int&, int&),int&, int&);

int main()
{
        int valOne=1, valTwo=2;
        int choice;
        bool fQuit = false;

        void (*pFunc)(int&, int&);

        while (fQuit == false)
        {
                cout << "(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: ";
                cin >> choice;
                switch (choice)
                {
                case 1:        pFunc = GetVals; break;
                case 2:        pFunc = Square; break;
                case 3:        pFunc = Cube; break;
                case 4:        pFunc = Swap; break;
                default:fQuit = true; break;
                }
                if (fQuit == true)
                        break;
                PrintVals ( pFunc, valOne, valTwo);
        }

        return 0;
}

void PrintVals( void (*pFunc)(int&, int&),int& x, int& y)
{
        cout << "x: " << x << " y: " << y << endl;
        pFunc(x,y);
        cout << "x: " << x << " y: " << y << endl;
}

void Square (int & rX, int & rY)
{
        rX *= rX;
        rY *= rY;
}

void Cube (int & rX, int & rY)
{
        int tmp;

        tmp = rX;
        rX *= rX;
        rX = rX * tmp;

        tmp = rY;
        rY *= rY;
        rY = rY * tmp;
}

void Swap(int & rX, int & rY)
{
        int temp;
        temp = rX;
        rX = rY;
        rY = temp;
}

void GetVals (int & rValOne, int & rValTwo)
{
        cout << "New value for ValOne: ";
        cin >> rValOne;
        cout << "New value for ValTwo: ";
        cin >> rValTwo;
}


(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1
x: 1 y: 2
New value for ValOne: 2
New value for ValTwo: 3
x: 2 y: 3
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3
x: 2 y: 3
x: 8 y: 27
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2
x: 8 y: 27
x: 64 y: 729
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4
x: 64 y: 729
x: 729 y: 64
(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0

-------------------------------------------------------------------------

PrintVals는 세 개의 매개변수를 취하는 함수로 선언되어있다.
첫번째 매개변수는 void형을 반환하고 두개의 정수형 참조자를 매개변수로 갖는 함수에 대한
포인터이고 두번째세뻔째는 정수형 참조자이다.

'Language > C & C Plus' 카테고리의 다른 글

[C++] 멤버함수에 대한 포인터  (0) 2012.07.13
[C++] typedef를 사용한 함수 포인터  (0) 2012.07.13
[C++] 함수포인터배열  (0) 2012.07.13
[C++] 함수포인터(간단한 예제)  (0) 2012.07.13
[C] #pragma 정리  (0) 2012.07.13
Posted by 몰라욧