题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <stdio.h>
typedef struct
{
int x;
int y;
}point_t;
static int isPointFlag(char c)
{
if(c == 'A' || c == 'D'
|| c == 'W' || c == 'S')
{
return 1;
}
return 0;
}
static int isPoint(char c)
{
if(c >= '0' && c <= '9')
{
return 1;
}
return 0;
}
int main()
{
point_t point = {0};
char str[10000] = {0};
gets(str);
char *p = str;
char pointFlag = 0; //查找到坐标方向标志
int distance = 0; //移动距离
while(*p != '\0')
{
//1、未匹配到方向
if(!pointFlag)
{
//寻找坐标方向
if(isPointFlag(*p))
{
pointFlag = *p; //把方向赋值给pointFlag
p++;
//continue;
}
else
{
//匹配不到就继续匹配方向
while(*p != '\0' && *p != ';')
{
p++;
}
}
}
//2、匹配到方向,处理移动距离
if(pointFlag)
{
if(isPoint(*p))
{
distance *= 10;
distance += *p - '0';
}
else if(*p == ';')//表示距离的数字结束
{
switch(pointFlag)
{
case 'A':
{
point.x -= distance;
}
break;
case 'D':
{
point.x += distance;
}
break;
case 'W':
{
point.y += distance;
}
break;
case 'S':
{
point.y -= distance;
}
break;
default:
printf("failed -->[%c]", pointFlag);
break;
}
distance = 0;
pointFlag = 0;
}
else
{
//若出现问题,则清空数据
distance = 0;
pointFlag = 0;
}
}
p++;
}
printf("%d,%d\n", point.x, point.y);
return 0;
}