본문 바로가기
my_lesson/_C++

C++ - For 반복문 별그리기 ver 1.0 , ver 1.1

by boolean 2019. 4. 24.
728x90

CPP - For 반복문  별그리기 ver 1.0 , ver 1.1

별그리기 Ver 1.0

// Make a diamond by *
/*
 ......*......
 .....***.....
 ....*****....
 ...*******...
 ....*****....
 .....***.....
 ......*......
*/
#include <iostream>

using namespace std;

void makeDiamond(int iIput);
int main()
{
	long long iInput, iCount;
	while (true)
	{
		cout << "Please enter odd number of layer : ";
		cin >> iInput;
		if (iInput % 2 == 1)
		{
			cout << "\nYour number is " << iInput << endl;
			makeDiamond(iInput);
			break;
		}
		else
		{
			cout << "Your Number is wrong number\n";
			cin.ignore(INT_MAX, '\n');
			continue;
		}
	}
	return 0;
}

void makeDiamond(int iInput)
{
	cout << "======= This is " << iInput << " layers diamond !! =======" << endl;
	// space iInput/2 iInput/2-1 iInput/2-2 ... 0 ... iInput/2-2 iInput/2-1 iInput/2
	// *     1  2 ... iInput/2-2 iInput/2-1  iInpu7   iInput/2-1 iInput/2-1  ... 2 1
	int dotSpace = (iInput < 20) ? 4 : (iInput / 10) * (iInput / 10);
	for (int i = 0; i < iInput / (2); ++i)
	{
		for (int j = 0; j < iInput / (2) - i - 1 + dotSpace; ++j)
		{
			cout << ".";
		}
		for (int k = iInput / (i + 2); k > iInput / (i + 2) - i * 2 - 1; --k)
		{
			cout << "*";
		}
		for (int j = iInput / (2) - i - 1 + dotSpace; j > 0; --j)
		{
			cout << ".";
		}

		cout << endl;
	}

	for (int i = iInput / 2; i >= 0; --i)
	{

		for (int j = iInput / (2) - i - 1 + dotSpace; j > 0; --j)
		{
			cout << ".";
		}
		for (int k = iInput / (i + 2) - i * 2 - 1; k < iInput / (i + 2); ++k)
		{
			cout << "*";
		}

		for (int j = 0; j < iInput / (2) - i - 1 + dotSpace; ++j)
		{
			cout << ".";
		}

		cout << endl;
	}
}

 

별그리기 Ver 1.1.

 

// Create a for_star_diamond ver 1.1
// iLayer = input star number of line
// iCount = counter of layer
// dotSpace = number of space's dot

#include <iostream>

using namespace std;
void makeDiamond(int iLayer, int iCount);
#define EVEN 2
int main()
{
	int iLayer, iCount = 0;

	while (true)
	{
		cout << " Please input a oddynary number :";
		cin >> iLayer;

		if (iLayer % EVEN == 1)
		{
			makeDiamond(iLayer, iCount);
			break;
		}
		else
		{
			cout << "It is not a oddynary number !" << endl;
			cin.ignore(INT_MAX, '\n');
			continue;
		}
	}
	//system("pause");
	return 0;
}

void makeDiamond(int iLayer, int iCount)
{
	cout << "****** This is " << iLayer << "s layer star Diamond ******" << endl;
	int dotSpace = (iLayer < 20) ? 4 : (iLayer / 10) * (iLayer / 10);
	for (int i = 0; i < iLayer; ++i)
	{
		iCount = i;
		if (iCount > iLayer / EVEN)
		{
			iCount = iLayer - i - 1;
		}
			for (int j = 0; j <iLayer/EVEN - iCount + dotSpace; ++j)
			{
				cout << ".";
			}
			for (int k = 0; k < iCount * EVEN + 1; ++k)
			{
				cout << "*";
			}
			for (int j = 0; j <iLayer/EVEN - iCount + dotSpace; ++j)
			{
				cout << ".";
			}
			cout << endl;

		
	}
}

댓글