U E D R , A S I H C RSS

Hardcore Cpp Study/첫숙제/Overloading/임민수

함수의 중복정의라 함은

함수의 이름은 같게 하되 전달인자들의 개수나 형식에 따라 다르게 사용될 수 있도록 정의하는것을 말함..

즉.. 영어의 한 단어의 뜻이 문맥상황에 따라 다르게 해석되는것을 뜻함


  • 마방진 짜본것.. 아직은 많이 미숙하네요..


~cpp 
#include <iostream>
using namespace std;
int const arsize = 11;
void main()
{
	int num, garo=0, sero=0, cnt=1;
	cout << " 원하는 마방진의 크기를 입력하세요 (11 이하의 홀수만) : "; 
	cin >> num;
	int square[arsize][arsize]={0,};
	sero = num/2;
	square[0][sero]=1;
	while ( cnt < num*num )
	{
		garo--;
		sero++;
		if (garo < 0)
			garo = num-1;
		if (sero > num-1)
			sero = 0;
		if (square[garo][sero] !=0)
		{ garo+=2, sero-=1;}
		if (garo > num-1)
			garo = 1;
		if (sero < 0)
			sero = num-1;
		square[garo][sero]=++cnt;
	}
	
	for (int i = 0 ; i <num; i++)
	{
		for ( int j = 0 ; j < num ; j++)
		{
			cout << square[i][j] << "\t";
		}
		cout << endl;
	}
}


~cpp 

#include <iostream>
using namespace std;

const int max=20;
void main()
{
	int cnt=0, board[max][max]={0,};
	int garo,sero,x,y;
	char direction[max];
	cout << "판의 크기를 입력하세요 (행, 열) : ";
	cin >> garo >> sero;
	
	cout << "처음 시작 위치를 입력하세요 (행, 열) : ";
	cin >> x >> y;
	
	board[x][y]=1;
	
	cout << "방향을 입력하세요 (0은 북쪽방향 , 시계 방향으로 1~7 , 끝내기는 999) : ";
	for (int i=0; i<max; i++)
	{
		cin >> direction[i];
		cnt++;
		if(direction[i-2]=='9' && direction[i-1]=='9' && direction[i]=='9')
		{
			cnt-=3;
			break;
		}
	}
	
	for (int k=0; k<cnt; k++)
	{
		switch(direction[k])
		{
		case '0' : x--;
				   break;
		case '1' : x--;
			       y++;
				   break;
		case '2' : y++;
				   break;
		case '3' : x++;
			       y++;
				   break;
		case '4' : x++;
				   break;
		case '5' : x++;
			       y--;
				   break;
		case '6' : y--;
			       break;
		case '7' : x--;
			       y--;
				   break;
		}
		if(x==garo)
			x=0;
		else if(x<0)
			x=garo-1;
		if(y==sero)
			y=0;
		else if(y<0)
			y=sero-1;

		board[x][y]++;
	}

	for (int j=0 ; j<garo ; j++)
	{
		for (i=0 ; i<sero ; i++)
			cout << board[j][i] << "  ";
		cout << endl ;
	}
	cout << endl << "총 이동 횟수는 " << cnt << "입니다. ";
}




~cpp 
#include <iostream> 
using namespace std; 

const int max=100; 

void input(int &a, int &b);
void input(short &a, short &b);
void input(char array[], int &cnt);
void process(char array[], int board[][max], int garo, int sero, int cnt, int x, int y);
void output(int garo, int sero, int cnt, int board[][max]);

void main() 
{ 
        int cnt=0, board[max][max]={0,}; 
        int garo,sero;
		short x,y; 
        char direction[max]; 

        input(garo,sero);
		input(x,y);
		board[x][y]=1; 
		input(direction ,cnt);
		process(direction, board, garo, sero, cnt, x, y);
		output(garo,sero,cnt,board);
}

void input(int &a, int &b)
{
		cout << "판의 크기를 입력하세요 (행, 열) : "; 
        cin >> a >> b; 
}

void input(short &a, short &b)
{
        cout << "처음 시작 위치를 입력하세요 (행, 열) : "; 
        cin >> a >> b; 
}
void input(char array[] , int &cnt)
{
        cout << "방향을 입력하세요 (0은 북쪽방향 , 시계 방향으로 1~7 , 끝내기는 999) : "; 
        for (int i=0; i<max; i++) 
        { 
                cin >> array[i]; 
                cnt++; 
                if(array[i-2]=='9' && array[i-1]=='9' && array[i]=='9') 
                { 
                        cnt-=3; 
                        break; 
                } 
		} 
}

void process(char array[], int board[][max], int garo, int sero, int cnt, int x, int y)
{
        for (int k=0; k<cnt; k++) 
        { 
                switch(array[k]) 
                { 
                case '0' : x--; 
                                   break; 
                case '1' : x--; 
                           y++; 
                                   break; 
                case '2' : y++; 
                                   break; 
                case '3' : x++; 
                           y++; 
                                   break; 
                case '4' : x++; 
                                   break; 
                case '5' : x++; 
                           y--; 
                                   break; 
                case '6' : y--; 
				   break; 
                case '7' : x--; 
                           y--; 
                                   break; 
                } 
                if(x==garo) 
                        x=0; 
                else if(x<0) 
                        x=garo-1; 
                if(y==sero) 
                        y=0; 
                else if(y<0) 
                        y=sero-1; 
 
                board[x][y]++; 
        }
}
void output(int garo, int sero, int cnt, int board[][max])
{
        for (int j=0 ; j<garo ; j++) 
        { 
                for (int i=0 ; i<sero ; i++) 
                        cout << board[j][i] << "  "; 
                cout << endl ; 
        } 
        cout << endl << "총 이동 횟수는 " << cnt << "입니다. "; 
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:22
Processing time 0.0262 sec