zoukankan      html  css  js  c++  java
  • 随手而写的玉兔

    今天逛了下博客园,发现了一个有趣的命题,就打算测试一下. 

    题目与原案在此

    我算是才疏学浅,希望各位高手能够指点一下.http://www.cnblogs.com/yudeyinji/p/3581008.html

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace DateTest
      8 {
      9     //    题目:
     10     //假设我们是中国国家航天局人员,当玉兔号离开嫦娥三号之后,我们需要能够控制玉兔号在月球上开展探测工作。我们先假定虹湾区是一个很大的平原,我们在虹湾区建立一个坐标轴,如下图:
     11 
     12     //         玉兔号离开嫦娥三号后,根据自身安装的定位系统可以知道自己的初始位置,我们记为 X0 , Y0 ; 同时玉兔号也可以知道当前它的朝向,如东、西、南、北(暂时只考虑这四个方向)。
     13 
     14     //中国国家航天局会向玉兔号发送指令,我们先暂定为3种:
     15 
     16     //F : 当玉兔号接收到这条指令之后,会向前移动一个坐标单位的距离
     17     //L : 当玉兔号接受到这条指令之后,会原地向左旋转90度
     18     //R : 当玉兔号接收到这条指令之后,会原地向右旋转90度
     19     //要求:
     20     //一)设计一个玉兔号的主程序,能够接收中国国家航天局发送过来的指令序列(如FFLFRFLL),执行该指令序列之后,玉兔号能够走到正确的位置,并知道当前正确的位置。(如:玉兔号初始位置为 (0,0),方向朝东,执行指令 FFLFRFLL之后,位置为 (3,1) 方向朝西)
     21     //二)主程序中,不允许出现switch case语句,也不允许出现else关键字,也不允许使用三元表达式,if关键字出现的次数要求在5次以下(0-4次)
     22     //三)主程序可以用任何语言编写,如Java、C#、Ruby、Python、PHP等
     23 
     24     public class Orientation
     25     {
     26         public string Name { get; set; }
     27         public Action<YuTu> Walk { get; set; }
     28 
     29         public Orientation(string name, Action<YuTu> work)
     30         {
     31             Name = name;
     32             Walk = work;
     33         }
     34 
     35         public static List<Orientation> Orientations = new List<Orientation>();
     36         static Orientation()
     37         {
     38             Orientations = new List<Orientation>();
     39             Orientations.Add(new Orientation("", (yutu) => { yutu.X = yutu.X + 1; }));
     40             Orientations.Add(new Orientation("", (yutu) => { yutu.Y = yutu.Y - 1; }));
     41             Orientations.Add(new Orientation("西", (yutu) => { yutu.X = yutu.X - 1; }));
     42             Orientations.Add(new Orientation("", (yutu) => { yutu.Y = yutu.Y + 1; }));
     43         }
     44     }
     45 
     46     public class YuTu
     47     {
     48         public int X { get; set; }
     49         public int Y { get; set; }
     50         public Orientation CurrentOrientation { get; set; }
     51         public List<Command> Commands { get; set; }
     52 
     53         public YuTu()
     54         {
     55             X = 0;
     56             Y = 0;
     57             CurrentOrientation = Orientation.Orientations[0];
     58             Commands = new List<Command>();
     59             Commands.Add(new FCommand());
     60             Commands.Add(new LCommand());
     61             Commands.Add(new RCommand());
     62         }
     63 
     64         public void ShowOrientation()
     65         {
     66             Console.WriteLine(string.Format("({0},{1}),方向朝{2}", X, Y, CurrentOrientation.Name));
     67         }
     68 
     69         public void ExecuteCommand(string commands)
     70         {
     71             char[] cmds = commands.ToCharArray();
     72             foreach (var cmd in cmds)
     73             {
     74                 ExecuteCommand(cmd);
     75             }
     76         }
     77 
     78         public void ExecuteCommand(char command)
     79         {
     80             foreach (var cmd in Commands)
     81             {
     82                 if (cmd.Name == command)
     83                 {
     84                     cmd.Execute(this);
     85                     return;
     86                 }
     87             }
     88             Console.WriteLine("无效的指令" + command);
     89             // throw new ApplicationException("无效的指令"+command);
     90         }
     91     }
     92 
     93     public abstract class Command
     94     {
     95         public char Name;
     96         public abstract void Execute(YuTu yutu);
     97     }
     98 
     99     public class FCommand : Command
    100     {
    101         public FCommand()
    102         {
    103             this.Name = 'F';
    104         }
    105 
    106         public override void Execute(YuTu yutu)
    107         {
    108             yutu.CurrentOrientation.Walk(yutu);
    109         }
    110     }
    111 
    112     public class LCommand : Command
    113     {
    114         public LCommand()
    115         {
    116             this.Name = 'L';
    117         }
    118 
    119         public override void Execute(YuTu yutu)
    120         {
    121             int index = Orientation.Orientations.IndexOf(yutu.CurrentOrientation);
    122             int maxLength = Orientation.Orientations.Count;
    123             //其实这里应该做判断是否小于0的 但是题目限制 只写出逻辑
    124             index = (index + maxLength - 1) % maxLength;
    125             yutu.CurrentOrientation = Orientation.Orientations[index];
    126         }
    127     }
    128 
    129     public class RCommand : Command
    130     {
    131         public RCommand()
    132         {
    133             this.Name = 'R';
    134         }
    135 
    136         public override void Execute(YuTu yutu)
    137         {
    138             int index = Orientation.Orientations.IndexOf(yutu.CurrentOrientation);
    139             int maxLength = Orientation.Orientations.Count;
    140             //其实这里应该做判断是否小于0的 但是题目限制 只写出逻辑
    141             index = (index + maxLength + 1) % maxLength;
    142             yutu.CurrentOrientation = Orientation.Orientations[index];
    143         }
    144     }
    145 
    146     //单元测试
    147     public class YuTuTest
    148     {
    149         public void ShowOrientationTest()
    150         {
    151             YuTu yt = new YuTu();
    152             yt.X = 1;
    153             yt.Y = 2;
    154             yt.CurrentOrientation = Orientation.Orientations[3];
    155             yt.ShowOrientation();
    156             Console.WriteLine("理论结果:" + "位置为 (1,2) 方向朝北");
    157         }
    158 
    159         public void ExecuteCommandTest()
    160         {
    161             YuTu yt = new YuTu();
    162             yt.ExecuteCommand('F');
    163             yt.ShowOrientation();
    164             Console.WriteLine("理论结果:" + "位置为 (1,0) 方向朝东");
    165         }
    166 
    167         public void ExecuteCommandsTest()
    168         {
    169             YuTu yt = new YuTu();
    170             yt.ExecuteCommand("FFLFRFLL");
    171             yt.ShowOrientation();
    172             Console.WriteLine("理论结果:" + "位置为 (3,1) 方向朝西");
    173         }
    174 
    175     }
    176 }
    Base

    而调用在这里

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DateTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                YuTuTest test = new YuTuTest();
    
                Console.WriteLine("测试1");
                test.ShowOrientationTest();
                Console.WriteLine("测试2");
                test.ExecuteCommandTest();
                Console.WriteLine("测试3");
                test.ExecuteCommandsTest();
                Console.ReadLine();
            }
        }
    }
    Test
  • 相关阅读:
    embarcadero radstudio xe5 正式版 下载地址
    Delphi 中 动态创建的Panel无法改变颜色的解决办法
    MySQL密码正确却无法本地登录-1045 Access denied for user 'root'@'localhost' (using password:YES)
    brew安装mongodb报错Error: No available formula with the name 'mongodb'
    PHP Fatal error: Class ‘Redis’ not found in 错误】windows下为PHP安装redis扩展操作redis
    mysql IFNULL
    安装Conda并在Conda下安装jupyter notebook
    Redis命令
    Python代码转换为exe可执行程序详解
    python机器学习包 Windows下 pip安装 scikit-learn numpy scipy
  • 原文地址:https://www.cnblogs.com/FishTank/p/3584333.html
Copyright © 2011-2022 走看看