U E D R , A S I H C RSS

aekae/code

별표(*)

~cpp 
#include <iostream>
using namespace std;

int main()
{
	int i;
	
	int a,b,c,d;
	
	for (i=1; i<11; i++)
	{
		for (a=1; a<1+i; a++)
			cout << "*";
		for (b=1; b<12-i; b++)
			cout << " ";
		for (c=1; c<12-i; c++)
			cout << "*";
		for (d=1; d<2*i; d++)
			cout << " ";
		for (c=1; c<12-i; c++)
			cout << "*";
		for (b=1; b<12-i; b++)
			cout << " ";
		for (a=1; a<1+i; a++)
			cout << "*";
		cout << endl;
	}

	return 0;
}

리팩토링 전

~cpp 
#include <iostream> 
using namespace std; 
#include <ctime> 

int table[5][5] = {0,}; 
bool IsExistZero(); 

int main() 
{ 
	srand(time(0));         // 그냥 쓴다. 
	
	int x, y; 
	
	cin >> x >> y; 
	table[y][x]++; 	

	while( IsExistZero() ) 
	{                
		int a = rand() % 3 - 1;              
		x += a; 
		if(x == 5) 
			x = 0; 
		else if(x == -1) 
			x = 4; 

		int b = rand() % 3 - 1;              
		y += b; 
		if(y == 5) 
			y = 0; 
		else if(y == -1) 
			y = 4; 
	
		table[y][x]++; 	
	} 

	for (int i=0; i<5; i++) 
	{ 
		for (int j=0; j<5; j++) 
			cout << table[i][j] << "\t"; 
		cout << endl; 
	}                        
	
	return 0; 
} 

bool IsExistZero() 
{ 
	for (int i=0; i<5; i++) 
	{ 
		for (int j=0; j<5; j++) 
		{ 
			if(table[i][j] == 0) 
				return true; 
		} 
	} 
	return false; 
} 

리팩토링 후

~cpp 
#include <iostream>
using namespace std;
#include <ctime>

bool IsExistZero(int table[5][5]);
void ShowTable(int table[5][5]);
void SetCoordinate(int& coord);

int main()
{
	srand(time(0));		// 그냥 쓴다.
	int table[5][5] = {0,};

	int x, y;

	cin >> x >> y;
	
	while( IsExistZero(table) )
	{		
		table[y][x]++;
		SetCoordinate(x);	
		SetCoordinate(y);	
	}
	ShowTable(table);

	return 0;
}

bool IsExistZero(int table[5][5])
{
	for (int i=0; i<5; i++)
	{
		for (int j=0; j<5; j++)
		{
			if(table[i][j] == 0)
				return true;
		}
	}
	return false;
}

void ShowTable(int table[5][5])
{
	for (int i=0; i<5; i++)
	{
		for (int j=0; j<5; j++)
			cout << table[i][j] << "\t";
		cout << endl;
	}			
}

void SetCoordinate(int& coord)
{
	int delta = rand() % 3 - 1;		
	coord += delta;
	if(coord == 5)
		coord = 0;
	else if(coord == -1)
		coord = 4;
}
DeleteMe - ~cpp IsExitZero 에서 일일이 갔던곳을 세기 보다는.. 새로운 곳을 갔을 때 변수를 증가시켜주는 방법은 어떨까? 임인택
~cpp 
for( ; ; )
{
 // roach moves, if unvisited place, count it!
 if( ThisIsFirstVisit )
  NumberOfVisitedPlace++;
}
bool IsExitZero()
{
 return (ArrSize*ArrSize==NumberOfVisitedPlace);
}
 

스택

~cpp 
#include <iostream>
using namespace std;

void MainMenu();
void push();
void pop();
void show();
void exit();

int i=0;
int in[10];

int main()
{
	MainMenu();

	return 0;
}

void MainMenu()
{
	cout << "1.입력\n"
			"2.삭제\n"
			"3.보여주기\n"
			"4.끝내기\n"
			"선택하세요 : ";
	int input;
	cin >> input;
	switch (input)
	{
	case 1 : push();
			 break;
	case 2 : pop();
			 break;
	case 3 : show();
			 break;
	case 4 : exit();
			 break;
	}
}

void push()
{
	if(i == 10)
		cout << "값을 초과하였습니다." << endl;

	for (i; i<10; i++)
	{
		cout << i+1 << "번째 값: ";
		cin >> in[i];
		i++;
		break;
	}
	MainMenu();
}

void pop()
{
	in[i-1] = ' ';
	cout << char(in[i-1]) << endl;
	cout << i << "번째 값 삭제" << endl;
	i--;
	MainMenu();
}

void show()
{
	for (int j=0; j<i; j++)
	{
		cout << j+1 << "번째 값: ";
		cout << in[j] << endl;
	}
	MainMenu();
}

void exit()
{
	return;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:35
Processing time 0.0183 sec