C Language Basic

[부록] 막대 그래프(완성본), 체스 말 움직이는 모션(완성본)

cCcode 2021. 7. 24. 23:27

1) 막대 그래프 그리는 프로그램

#include <stdio.h>
#include <stdbool.h>

int i;

enum Centimeter {
	nine_centimeter,
	eight_centimeter,
	seven_centimeter,
	six_centimeter,
	five_centimeter,
	four_centimeter,
	three_centimeter,
	two_centimeter,
	one_centimeter,
};

void PRINT_GRAPH(enum Centimeter C) // 1 ~ 9 
{
	int count;
	printf("    y\n\n");
	printf("   8%c", 24); // 3 - 5, 6 - 2
	for (i = 0; i < 9; i++)
	{
		count = 0;
		if (i != 0)
			printf("    %c", 5);    // 공간 7

		if (9 - C + 1 == 9 - i)
		{
			printf("      ");
			printf(" %c%c%c%c%c%c%c", 1, 6, 6, 6, 6, 6, 2);
		}

		if (9 - C >= 9 - i && count == 0)
		{
			printf("       ");
			printf("%c     %c", 5, 5);
			count += 1;
		}
		printf("\n");
	}

	printf("    %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c x\n", 3, 6, 6, 6, 6, 6, 6, 6, 21, 6, 6, 6, 6, 6, 21, 6, 6, 6, 6, 6, 6, 6, 6, 26);
	printf("    0\n");
}

void JUDGEMENT(int input)
{
	switch (input)
	{
	case 1:
	{
		PRINT_GRAPH(one_centimeter);
		break;
	}
	case 2:
	{
		PRINT_GRAPH(two_centimeter);
		break;
	}
	case 3:
	{
		PRINT_GRAPH(three_centimeter);
		break;
	}
	case 4:
	{
		PRINT_GRAPH(four_centimeter);
		break;
	}
	case 5:
	{
		PRINT_GRAPH(five_centimeter);
		break;
	}
	case 6:
	{
		PRINT_GRAPH(six_centimeter);
		break;
	}
	case 7:
	{
		PRINT_GRAPH(seven_centimeter);
		break;
	}
	case 8:
	{
		PRINT_GRAPH(eight_centimeter);
		break;
	}
	case 9:
	{
		PRINT_GRAPH(nine_centimeter);
		break;
	}
	// default: return 0;
	}
}

int main()
{
	int input;
	char input_2[30] = { 0 };
	bool stop = false;

	while (stop != true)
	{
		printf("\n\n\n");
		printf("    그래프의 길이를 입력해주세요. (한 줄의 길이는 1cm)\n");
		printf("    [8cm까지만 입력 가능합니다.]\n");
		printf("\n    input(cm) : ");
		scanf_s("%d", &input);
		printf("\n\n");

		if (input >= 9)
		{
			printf("    Wrong input!\n");
			printf("    Retry enter please.");
			continue;
		}

		JUDGEMENT(input);
		printf("\n\n    계속하시겠습니까?    [yes/no]\n\n");
		printf("    input : ");
		scanf_s("%s", input_2, (unsigned char)sizeof(input_2));

		for (i = 0; input_2[i] != 0; i++)
		{
			if (input_2[i] == 'n' || input_2[i] == 'N' && input_2[i + 1] == 'o' || input_2[i + 1] == 'O' && input_2[i + 2] == ' ')
				stop = true;
		}
	}

	return 0;
}
// 1 -> 
// 6 -> 
// 2 -> 

// made in cCcode

막대 그래프를 그리는 프로그램입니다.

 

프로그램을 실행시키면

그래프의 길이 입력

프로그램에서 그릴 막대 그래프의 길이를 입력하는 부분이 나옵니다. 

8cm까지만 입력 가능한 이유는 제가 사실 이 프로그램을 처음 만들 때 입력값을 염두에 두지 않고 단지 그래프의 모습만 구현하는데 치중하다보니 (그 때 그래프의 최댓값(길이)을 8로 잡았습니다.) 나중에 입력값을 이용하려고 보니 수정해야할 부분이 생각보다 많길래.. 그냥 처음 만들 때의 최댓값을 사용하기로 했습니다. 사실 좀 수정하면 더 크게 그릴수도 있긴한데,, 귀찮아서...

 

값을 입력하면 

8cm의 그래프

해당 길이의 그래프가 출력된 뒤 재실행 여부를 묻습니다. 여기서는 그래프 길이의 차이를 확연하게 보여드리기 위해 다른 길이의 그래프도 보여드리겠습니다.

6cm의 그래프
4cm의 그래프
2cm의 그래프

확연한 차이가 보이시나요?

 

2) 체스 말의 움직임을 구현한 프로그램

#include <stdio.h>
#include <stdbool.h>
#include <Windows.h>
#include <string.h>
#include <stdlib.h>

#define ONE_JUMP 9
#define i_goal 10
#define j_goal 80

int Const_size;

void PRINT_LAND();	// 전방 선언
// CHARACTER 함수가 PRINT_LAND 함수보다 먼저 정의되어 있기 때문

void PRINT_TITLE()
{
	printf("          ##########\n");
	printf("        ###                     #####        #####\n");
	printf("       ##                     ##    ##      ##    ##\n");
	printf("       ##                     ##     ##    ##     ##\n");
	printf("       ##                     ##      ##  ##      ##\n");
	printf("       ##                     ##        ##        ##\n");
	printf("       ##                     ##                  ##\n");
	printf("       ##                     ##                  ##\n");
	printf("        ###                   ##                  ##\n");
	printf("          ########## haracter ##                  ## otion\n");
	printf("\n\n\n");
}

void PRINT_STRAIGHT_CHARACTER(char* move)
{
	printf("%s             ######\n", move);
	printf("%s           #        #\n", move);
	printf("%s           #        # \n", move);
	printf("%s           #        #\n", move);
	printf("%s            #      #\n", move);
	printf("%s          #          #\n", move);
	printf("%s           #        #\n", move);
	printf("%s             #     #\n", move);
	printf("%s            #       #\n", move);
	printf("%s           #         #\n", move);
	printf("%s          #           #\n", move);
	printf("%s          #           #\n", move);
	printf("%s           #         #\n", move);
	printf("%s          #           #\n", move);
	printf("%s         ###############\n", move);
}

void PRINT_JUMPING_CHARACTER(char* move, char blank[], int size)
{
	for (int i = 0; i < size; i++)
		printf("%c\n", blank[i]);
	printf("%s             ######  \n", move);
	printf("%s           #        #\n", move);
	printf("%s           #        # \n", move);
	printf("%s           #        #\n", move);
	printf("%s            #      #\n", move);
	printf("%s          #          #\n", move);
	printf("%s           #        #\n", move);
	printf("%s             #     #\n", move);
	printf("%s            #       #\n", move);
	printf("%s           #         #\n", move);
	printf("%s          #           #\n", move);
	printf("%s          #           #\n", move);
	printf("%s           #         #\n", move);
	printf("%s          #           #\n", move);
	printf("%s         ###############\n", move);

	for (int i = 0; i < Const_size; i++)
		printf("%c\n", blank[i]);

	PRINT_LAND();
}

void PRINT_LAND()
{
	for (int i = 0; i < i_goal; i++)
	{
		for (int j = 0; j < j_goal; j++)
		{
			if (i == 0 || i == i_goal - 1)
				printf("7");
			else if (j == 0 || j == j_goal - 1)
				printf("7");
			else
				printf(" ");
		}
		printf("\n");
	}
}

int main()
{
	int decide = 0;
	int decide_2 = 10;

_continue:
	PRINT_TITLE();
	// 제목 표시
	printf("1. 체스 말 직선 모션\n");
	printf("2. 체스 말 점프 모션\n\n\n");

	printf("input : ");
	scanf_s("%d", &decide);
	Sleep(1500);
	system("cls");

	char* Move_Straight = malloc(sizeof(char) * 500);
	// 문자열 할당 시 읽기 메모리로 전환 따라서 동적할당
	if (Move_Straight != 0)
		memset(Move_Straight, 0, sizeof(Move_Straight));
	char* Move_distance = "      ";

	if (decide == 1)
	{
		// Go Straight
		if (Move_Straight)
		{
			for (int i = 0; i < 9; i++)
			{
				strcat_s(Move_Straight, _msize(Move_Straight), Move_distance);
				// 동적할당 된 포인터의 크기를 구하기 위해 _msize 함수 사용
				PRINT_STRAIGHT_CHARACTER(Move_Straight);
				PRINT_LAND();
				Sleep(500);
				system("cls");
			}
		}
	}

	char blank[ONE_JUMP] = { " ", };
	char height = sizeof(blank) / sizeof(char);
	int calculate_tool = ONE_JUMP / 2;

	if (decide == 2)
	{
		// One _ Jump
		for (int i = 1; i <= ONE_JUMP; i++)
		{
			if (Move_Straight)
			{
				strcat_s(Move_Straight, _msize(Move_Straight), Move_distance);
				PRINT_JUMPING_CHARACTER(Move_Straight, blank, height);
				Sleep(500);
				system("cls");

				if (calculate_tool >= i)
				{
					height -= 1;
					Const_size += 1;
				}
				else
				{
					height += 1;
					Const_size -= 1;
				}
			}
		}
	}
	printf("Do you want to continue?	[yes(0)/ no(1)]\n\n");
	printf("input : ");
	scanf_s("%d", &decide_2);

	if (decide_2 == 0)
	{
		height = 0;
		Const_size = 0;

		Sleep(1000);
		system("cls");
		goto _continue;
	}

	return 0;
}

// made in cCcode

사실 말만 거창하지 진짜 별거 없습니다.

 

프로그램을 시작하면

구현하고자 하는 모션

구현하고자 하는 모션을 선택하는 화면이 나옵니다.

 

1) 체스 말 직선 모션

1번을 선택한 경우
체스말 직진

체스의 말 중 하나인 (제가.. 직접 구현한거라.. 폰처럼 안보여도 이해해주세요.)이 직선으로 움직이는 모습을 볼 수 있습니다.

재실행 여부

그 후 재실행 여부를 묻습니다.

 

2) 체스 말 점프 모션

2번을 선택한 경우
체스말 점프
재실행 여부

해당 프로그램을 만든 사람으로써.. 한마디만 덧붙이자면 이 프로그램을 실행시킬 때는 되도록 콘솔 화면을 크게 만들어주시면 감사하겠습니다. (사실 큰 상관은 없지만 콘솔 화면이 작으면 아래 바닥 부분[7로 이루어진]을 그리면서 콘솔화면이 조금씩 흔들리는 현상이 일어납니다.)