Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Spiral

.cpp
Скачиваний:
0
Добавлен:
19.06.2023
Размер:
5.9 Кб
Скачать
#include <iostream>
#include <time.h>
#include <conio.h>

using namespace std;

void sleepcp(int milliseconds)
{
	clock_t time_end = clock() + milliseconds;
	while (clock() < time_end) {}
}

int main()
{
	int height(5), width(5), clockwise(1), center_start(0), cursor(0);
	bool quit(0);

	// Ввод данных
	printf("SPIRAL GENERATOR\n");

	/*printf("\nEnter its height (integer in range 1-10): ");
	scanf_s("%d", &height);
	while (height <= 0 || height > 10)
	{
		printf("Wrong value. Try again: ");
		cin.clear();
		cin.ignore(10000, '\n');
		scanf_s("%d", &height);
	}

	printf("\nEnter its width (integer in range 1-10): ");
	cin.clear();
	cin.ignore(10000, '\n');
	scanf_s("%d", &width);
	while (width <= 0 || width > 10)
	{
		printf("Wrong value. Try again: ");
		cin.clear();
		cin.ignore(10000, '\n');
		scanf_s("%d", &width);
	}*/

	//printf("\nIs it clockwise? \nEnter 1 if it is, enter 0 if it isn't: ");
	//cin.clear();
	//cin.ignore(10000, '\n');
	//scanf_s("%d", &clockwise);
	//while (clockwise < 0 || clockwise > 1)
	//{
	//	printf("Wrong value. Try again: ");
	//	cin.clear();
	//	cin.ignore(10000, '\n');
	//	scanf_s("%d", &clockwise);
	//}

	//printf("\nDoes it start inside? \nEnter 1 if it does, enter 0 if it doesn't: ");
	//cin.clear();
	//cin.ignore(10000, '\n');
	//scanf_s("%d", &center_start);
	//while (center_start < 0 || center_start > 1)
	//{
	//	printf("Wrong value. Try again: ");
	//	cin.clear();
	//	cin.ignore(10000, '\n');
	//	scanf_s("%d", &center_start);
	//}

	while (!quit)
	{
		
		system("cls");

		printf("SPIRAL GENERATOR\n\n");

		if (cursor == 0) {
			printf("-->  Spiral height:");
			if (height == 1)
				printf("   %2d > ", height);
			else if (height == 10)
				printf(" < %2d   ", height);
			else
				printf(" < %2d > ", height);
		}
		else printf("     Spiral height:   %2d", height);
		printf("\n");

		if (cursor == 1) {
			printf("-->  Spiral width: ");
			if (width == 1)
				printf("   %2d > ", width);
			else if (width == 10)
				printf(" < %2d   ", width);
			else
				printf(" < %2d > ", width);
		}
		else printf("     Spiral width:    %2d", width);
		printf("\n");

		printf((cursor == 2)? "-->" : "   ");
		if (center_start) printf(" [Inside Out] |  Outside In  \n");
		else printf("  Inside Out  | [Outside In] \n");

		printf((cursor == 3) ? "-->" : "   ");
		if (clockwise) printf(" [Clockwise]  |  Counterclockwise  \n");
		else printf("  Clockwise   | [Counterclockwise] \n");



		// Создание таблицы и обнуление её области
		int table[12][12];
		for (int y = 0; y < height/*+2*/; y++)
		{
			for (int x = 0; x < width/*+2*/; x++)
			{
				table[y][x] = 0;
			}
		}

		// Создание спирали
		int size = height * width;
		/*int x(1), y(1), new_x(0), new_y(0), temp;
		if ((clockwise == 1 && center_start == 0) || (clockwise == 0 && center_start == 1))
		{
			/*int dx(1), dy(0);
			for (int k = 0; k < size; k++)
			{
				if (center_start == 0) table[y][x] = k + 1;
				else table[y][x] = size - k;
				new_x = x + dx;
				new_y = y + dy;
				if (!(0 < new_x && new_x < width+1)
					|| !(0 < new_y && new_y < height+1)
					|| table[new_y+dy][new_x+dx] != 0)
				{
					temp = dx;
					dx = -dy;
					dy = temp;
				}
				x += dx;
				y += dy;
			}
		}*/
		int x(0), y(0), new_x(0), new_y(0), temp;
		if ((clockwise == 1 && center_start == 0) || (clockwise == 0 && center_start == 1))
		{
			int dx(1), dy(0);
			for (int k = 0; k < size; k++)
			{
				if (center_start == 0) table[y][x] = k + 1;
				else table[y][x] = size - k;
				new_x = x + dx;
				new_y = y + dy;
				if (!(-1 < new_x && new_x < width)
					|| !(-1 < new_y && new_y < height)
					|| table[new_y][new_x] != 0)
				{
					temp = dx;
					dx = -dy;
					dy = temp;
				}
				x += dx;
				y += dy;
			}
		}
		else
		{
			int dx(0), dy(1);
			for (int k = 0; k < size; k++)
			{
				if (center_start == 0) table[y][x] = k + 1;
				else table[y][x] = size - k;
				new_x = x + dx;
				new_y = y + dy;
				if (!(-1 < new_x && new_x < width)
					|| !(-1 < new_y && new_y < height)
					|| table[new_y][new_x] != 0)
				{
					temp = dy;
					dy = -dx;
					dx = temp;
				}
				x += dx;
				y += dy;
			}
		}

		// Вывод спирали
		//system("cls");
		printf("\nThe generated spiral: \n\t");
		int spaces = 4;
		if (size < 10)
			spaces = 2;
		else if (size < 100)
			spaces = 3;
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				/*if (table[i+1][j+1] != 0) printf("%-*d", spaces, table[i+1][j+1]);
				else printf("%-*s", spaces, "");*/
				printf("%-*d", spaces, table[i][j]);
			}
			printf("\n\t");
		}

		printf("\nPress Esc to quit");


		// 224: left = 75  right = 77  down = 80  up = 72 ;;;;; tab = 9  enter = 13  escape = 27  backspace = 8 ;;;;; 0-9 = 48-57
		int input = _getch();
		switch (input)
		{
		// arrows
		case 224:
			switch (_getch())
			{
			// arrow up
			case 72:
				cursor--;
				break;
			// arrow down
			case 80:
				cursor++;
				break;
			// arrow left
			case 75:
				if (cursor == 0 && height > 1) height--;
				else if (cursor == 1 && width > 1) width--;
				else if (cursor == 2) center_start = 1;
				else if (cursor == 3) clockwise = 1;
				break;
			// arrow right
			case 77:
				if (cursor == 0 && height < 10) height++;
				else if (cursor == 1 && width < 10) width++;
				else if (cursor == 2) center_start = 0;
				else if (cursor == 3) clockwise = 0;
				break;
			}
			if (cursor > 3) cursor = 0;
			if (cursor < 0) cursor = 3;
			break;
		// escape
		case 27:
			quit = true;
			break;
		}
		//cin.clear();
		//cin.ignore(10000, '\n');
		//cin.get();
	}
	return 0;
}
Соседние файлы в предмете Программирование