Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3个月前关闭。
                                                                                            
                
        
我对编程和所有事物都还很陌生,自从我们最近开始使用C以来,我们就一直在假设的无限网格上使用以下三个可能的动作来创建程序(机器人模拟器):并前进。输出只需要是x和y坐标(x,y)。现在的事情是,我只能通过所有四个运动(上,下,左,右-北,南,西,东,东)来理解并实现(假设没有向左转或向右转的选项,向左意味着移动1位置向左,向右表示向右移动1个位置等))。因此,仅需简单地减去它们即可得出最终坐标,但我根本不知道如何为带有2个转弯选项并前进的规则制定规则。我会将代码留给已经完成的任务,如果有人认为我可以升级该代码,或者对如何使其工作有任何建议或想法,请告诉我。

#include<stdio.h>
static void position(int n, char* string)
{
    int left = 0, right = 0, up = 0, down = 0;
    for (int i = 0; i < n; i++) {
        if (string[i] == 'L')
            left++;
        else if (string[i] == 'R')
            right++;
        else if (string[i] == 'U')
            up++;
        else if (string[i] == 'D')
            down++;
    }
    printf("(%d,%d)", (right - left), (up - down));
}
int main()
{
    printf("Enter your movement: ");
    char string[1000];
    scanf("%s", string);
    int n;
    for (n = 0; string[n] != '\0'; ++n);
    string[100] = string[n];
    position(n, string);
}

最佳答案

解决此问题的一种方法是使用笛卡尔坐标系。

c - 机器人路径模拟器-LMLPHP

而不是尝试最后计算所有内容,我只是跟踪运动的处理过程。

#include<stdio.h>

enum {NORTH/*UP*/,SOUTH/*DOWN*/, EAST/*LEFT*/, WEST/*RIGHT*/};
static void position(int n, char* string)
{                           // let's use a Cartesian coordinate system
    // assume a 10x10 grid
    int X=0,Y=0;            // start at this place within the grid (the center)
    int direction = NORTH;  // we are facing NORTH
    int i;
    for (i = 0; i < n; i++) {
        if (string[i] == 'L') // TURN LEFT, no step
        {   // pivot
            switch (direction)
            {
            case NORTH:
                direction = EAST;   // face EAST, but do not step
                break;
            case SOUTH:
                direction = WEST;   // face WEST, but do not step
                break;
            case EAST:
                direction = SOUTH;  // face SOUTH, but do not step
                break;
            case WEST:
                direction = NORTH;  // face NORTH, but do not step
                break;
            }
        }
        else if (string[i] == 'R') // TURN RIGHT, no step
        {   // pivot
            switch (direction)
            {
            case NORTH:
                direction = WEST;   // face WEST, but do not step
                break;
            case SOUTH:
                direction = EAST;   // face EAST, but do not step
                break;
            case EAST:
                direction = NORTH;  // face NORTH, but do not step
                break;
            case WEST:
                direction = SOUTH;  // face SOUTH, but do not step
                break;
            }
        }
        else if (string[i] == 'A')
        {   // advance
            switch (direction)
            {
            case NORTH:
                if (Y == 5)
                    printf("you cannot go NORTH any more.\n");
                else
                    Y++;
                break;
            case SOUTH:
                if (Y == -5)
                    printf("you cannot go SOUTH any more.\n");
                else
                    Y--;
                break;
            case EAST:
                if (X == -5)
                    printf("you cannot go EAST any more.\n");
                else
                    X--;
                break;
            case WEST:
                if (X == 5)
                    printf("you cannot go WEST any more.\n");
                else
                    X++;
                break;
            }
        }
        else
        {
            printf("%c is not a valid command.\n", string[i]);
        }
    }
    printf("(%d,%d)", X, Y);
}
int main()
{
    char string[1000];
    int n;
    for(;;)
    {   // only valid movements are turn Right,turn Left and Advance. (RLA)
        printf("Enter your movement: ");
        scanf("%s", string);
        for (n = 0; string[n] != '\0'; ++n);
        string[100] = string[n];
        position(n, string);
    }
}


样品I / O

输入您的动作:@

@不是有效的命令。

(0,0)

输入您的动作:LLLA

(1,0)

输入您的动作:LLLA

(1,0)

输入您的动作:LLLAA

(3,0)

输入您的动作:LLARRRAA

(2,-1)

输入您的动作:LRLRLRA

(0,1)

07-24 13:56