pascal编程题目,求大师解答。用最简单的方法:描述机器人严格按照指令行走,由于是原始装置。

2024-11-22 21:10:06
推荐回答(1个)
回答(1):

var
   x,y,direction:integer;
   c:char;

procedure goStraight();
begin
    case direction of
        0:inc(y);
        1:inc(x);
        2:dec(y);
        3:dec(x);
    end;
end;

procedure turnLeft();
begin
    dec(direction);
    if direction=-1 then direction:=3;
    goStraight();
end;

procedure turnRight();
begin
    inc(direction);
    if(direction)=4 then direction:=0;
    goStraight();
end;

procedure stop();
begin
    case direction of
    0:writeln('(',x,',',y,'), N');
    1:writeln('(',x,',',y,'), E');
    2:writeln('(',x,',',y,'), S');
    3:writeln('(',x,',',y,'), W');
    end;
end;

procedure walk(order:char);
begin
   case order of
       'G','g':goStraight();
       'L','l':turnLeft();
       'R','r':turnRight();
       'S','s':stop();
   end;
end;

begin
    repeat
        read(c);
        walk(c);
    until (c='S') or (c='s');
end.