接上文题目,解二,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;
}