題目以下:c++
題目:函數
假設咱們是中國國家航天局人員,當玉兔號離開嫦娥三號以後,咱們須要可以控制玉兔號在月球上開展探測工做。咱們先假定虹灣區是一個很大的平原,咱們在虹灣區創建一個座標軸,以下圖:單元測試
玉兔號離開嫦娥三號後,根據自身安裝的定位系統能夠知道本身的初始位置,咱們記爲 X0 , Y0 ; 同時玉兔號也能夠知道當前它的朝向,如東、西、南、北(暫時只考慮這四個方向)。測試
中國國家航天局會向玉兔號發送指令,咱們先暫定爲3種:this
F : 當玉兔號接收到這條指令以後,會向前移動一個座標單位的距離spa
L : 當玉兔號接受到這條指令以後,會原地向左旋轉90度設計
R : 當玉兔號接收到這條指令以後,會原地向右旋轉90度指針
要求:orm
一)設計一個玉兔號的主程序,可以接收中國國家航天局發送過來的指令序列(如FFLFRFLL),執行該指令序列以後,玉兔號可以走到正確的位置,並知道當前正確的位置。(如:玉兔號初始位置爲 (0,0),方向朝東,執行指令 FFLFRFLL以後,位置爲 (3,1) 方向朝西)get
二)主程序中,不容許出現switch case語句,也不容許出現else關鍵字,也不容許使用三元表達式,if關鍵字出現的次數要求在5次如下(0-4次)
三)主程序能夠用任何語言編寫,如Java、C#、Ruby、Python、PHP等
四)在所選語言容許的狀況下,請編寫相應的單元測試
思路:通常有多條件的,咱們會選擇使用if/else、switch /case ,但題目明確規定 不能使用,if也限定次數,很天然就想到委託(c++裏面的函數指針),還有怎麼實現根據輸入自動選擇哪一種操做,這就想到了字典(鍵/值).
//窗口程序代碼實現====================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace YutuControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public class Move
{
delegate void DelegateFront();
//字典實現
private Dictionary<string, DelegateFront> Dictionary = new Dictionary<string, DelegateFront>();
private int direction;
private const int MaxDirection = 4;//常量表示當前有4個方向
public int x,y;
public void move(int x, int y, int direction)
{
//Turn_Direction = 0; //北
//Turn_Direction = 1; //東
//Turn_Direction = 2; //南
//Turn_Direction = 3; //西
this.x = x;
this.y = y;
direction = direction;
//使用委託+字典實現
Dictionary["0"] = new DelegateFront(NorthAdd);
Dictionary["1"] = new DelegateFront(EastAdd);
Dictionary["2"] = new DelegateFront(SouthAdd);
Dictionary["3"] = new DelegateFront(WestAdd);
}
/// <summary>
/// 逆時針
/// </summary>
public void DealLeft()
{
direction = (direction - 1 + MaxDirection) % MaxDirection;
}
/// <summary>
/// 順時針
/// </summary>
public void DealRight()
{
direction = (direction + 1) % MaxDirection;
}
public void DealFront()
{
//沒有使用委託實現
//if (direction == (int)Diretion.North) { ++y; return; }
//if (direction == (int)Diretion.South) { --y; return; }
//if (direction == (int)Diretion.West) { --x; return; }
//++x;
//使用委託+字典實現
if (Dictionary.ContainsKey(direction.ToString()))
{
//調用委託
Dictionary[direction.ToString()]();
}
}
private void NorthAdd()
{
++y;
}
private void SouthAdd()
{
--y;
}
private void WestAdd()
{
--x;
}
private void EastAdd()
{
++x;
}
public string Print()
{
return "x:" + x + ",y:" + y;
// Console.WriteLine("x:" + x + ",y:" + y);
// Console.WriteLine("Direction:" + (Diretion)direction);
// Console.ReadKey();
}
// Singleton模式:
private static Move instance;
private Move() { }
public static Move Instance
{
get
{
if (instance == null)
{
instance = new Move();
}
return instance;
}
}
}
class Direction
{
int north;
int south;
int west;
int east;
public int North
{
get{return north;}
set{ north = 3;}
}
public int East
{
get { return east; }
set { east = 2; }
}
public int South
{
get { return south; }
set { south = 1; }
}
public int West
{
get { return west; }
set { west = 0; }
}
}
public delegate void OperatorDelegate();
public int X , Y ;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//字典實現
Direction direction = new Direction();
var Key = new System.Collections.Generic.Dictionary<string, OperatorDelegate>();
// Move YuTu3 = new Move(0, 0, direction.North);
// Move YuTu3 = new Move();
Move.Instance.move(X, Y, direction.North);
Key["F"] = new OperatorDelegate(Move.Instance.DealFront);
Key["L"] = new OperatorDelegate(Move.Instance.DealLeft);
Key["R"] = new OperatorDelegate(Move.Instance.DealRight);
string key = e.KeyValue.ToString();
if (key=="70")
{
string instruct = "F";
if (Key.ContainsKey(instruct))
{
Key[instruct]();
}
Y = Move.Instance.y;
X = Move.Instance.x;
label1.Text = string.Format("x: {0}"+"y: {1}:",X,Y);//Y.ToString();
}
}
}
}