机器人的行走

#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
int startX = 0;//默认起始坐标
int startY = 0;
int start = 0;
char Location[4] = { 'N','E','S','W' };//定义一个固定坐标,北东南西
void changelocation(char s)//改变方向
{
    if (s == 'L')
    {
        if (start == 0)
            start = 3;
        else
            start -= 1;
    }
    if (s == 'R')
    {
        if (start == 3)
            start = 0;
        else
            start += 1;
    }

}
void explain(char* s)//拆分指令
{
    int l = 0;
    int i = 0;
    int len = strlen(s);
    while (i < len)
    {
        if (isalpha(s[i]))//为数字直接改变步数位置
        {
            changelocation(s[i]);
            i++;
        }
        else
        {
            l = i;
            while (i < len && isdigit(s[i]))
                i++;
            int k = i - l;
            int sum = 0;
            for (int j = k-1; j >= 0; j--)
            {
                int m = s[l++] - '0';
                sum += m * (int)pow(10, j);
            }
            //判段为那个方向,默认上和右为正,下和左为负
            if (Location[start] == 'N')
                startY += sum;
            else if (Location[start] == 'S')
                startY -= sum;
            else if (Location[start] == 'E')
                startX += sum;
            else if (Location[start] == 'W')
                startX -= sum;
        }
    }
}
int main(int argc, char* argv[])
{
    // 请在此输入您的代码  
    int n;
    char str[257];
    scanf("%d", &n);
    while (n--)
    {
        getchar();
        scanf("%s", str);
        explain(str);
        double nums = sqrt(startX * startX + startY * startY);
        printf("%.2lf\n", nums);
        startX = 0;//接受新指令
        startY = 0;
        start = 0;
    }
    return 0;
}
 

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

她没有救赎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值