code.jam - pixelcanvas (二)

本文通过C++实现了一个模拟行走路径的程序,该程序读取输入文件中的指令,如前进和转向,并根据这些指令更新行走的方向和位置。程序使用二维数组来记录行走路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接上文题目,解二,c++的c语言实现
#include <iostream>

#include <string>

#include <cstdio>

#pragma warning(disable : 4786) //Disable warning messages

#include <vector>

#include <fstream>



using namespace std;





int main(int argc, char *argv[])

{

	char map[20][20];

	char command[10];

	int pox = 0;

	int poy = 0;

	int direction = 270;

	int i, j;



	for (i = 0; i < 20; i++)

		for (j = 0; j < 20; j++)

			map[i][j] = '.';



	freopen("input", "r", stdin);



	while( scanf("%s", &command) != -1)

	{

		if (strcmp(command, "FORWARD") == 0)

		{

			map[poy][pox] = '*';

			int len;

			scanf("%d", &len);



			switch(direction)

			{

			case 0:

				while( len-- > 0)

				{

					if (pox == 19) break;

					pox++;

					map[poy][pox] = 'X';

				}

				break;

			case 90:

				while( len-- > 0)

				{

					if (poy == 0) break;

					poy--;

					map[poy][pox] = 'X';

				}

				break;

			case 180:

				while( len-- > 0)

				{

					if (pox == 0) break;

					pox--;

					map[poy][pox] = 'X';

				}

				break;

			case 270:

				while( len-- > 0)

				{

					if (poy == 19) break;

					poy++;

					map[poy][pox] = 'X';

				}

				break;

			}

		}

		else{

			direction+=90;

			direction= direction%360;

		}

	};



	for (i = 0; i < 20; i++)

	{

		for (j = 0; j < 20; j++)

		{

			printf("%c", map[i][j]);

		}

		printf("/n", map[i][j]);

	}

	return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值