2015-04-08 58 views
1

我試圖在case語句中使用字符串,但它給了我expected a discrete type. Found type Standard.String我明白字符串不是離散的。我想知道是否有工作。這裏是我的代碼:Ada與字符串的case語句

function Is_Valid_Direction(Direction_To_Go : in String) return Integer is 
    Room : Integer := 0; 
    begin 
     --if (Direction_To_Go = "NORTH" or Direction_To_Go = "N") then 
     -- Room := Building(currentRoom).exits(NORTH); 
     --elsif (Direction_To_Go = "SOUTH" or Direction_To_Go = "S") then 
     -- Room := Building(currentRoom).exits(SOUTH); 
     --elsif (Direction_To_Go = "EAST" or Direction_To_Go = "E") then 
     -- Room := Building(currentRoom).exits(EAST); 
     --elsif (Direction_To_Go = "WEST" or Direction_To_Go = "W") then 
     -- Room := Building(currentRoom).exits(WEST); 
     --elsif (Direction_To_Go = "UP" or Direction_To_Go = "U") then 
     -- Room := Building(currentRoom).exits(UP); 
     --elsif (Direction_To_Go = "DOWN" or Direction_To_Go = "D") then 
     -- Room := Building(currentRoom).exits(DOWN); 
     --end if; 
     case Direction_To_Go is 
     when "NORTH" | "N" => Room := Building(currentRoom).exits(NORTH); 
     when "SOUTH" | "S" => Room := Building(currentRoom).exits(SOUTH); 
     when "EAST" | "E" => Room := Building(currentRoom).exits(EAST); 
     when "WEST" | "W" => Room := Building(currentRoom).exits(WEST); 
     when "UP" | "U" => Room := Building(currentRoom).exits(UP); 
     when "DOWN" | "D" => Room := Building(currentRoom).exits(DOWN); 
     when others => Room := 0; 
     end case; 
     return Room; 
    end Is_Valid_Direction; 

評論部分正在做我想要的,但與if語句。我只是想看看是否可以用case語句。

+0

否.Ada不是Java。 – ajb

+0

這可能有助於思考「其他方式」 - 從內部開始,然後進入用戶界面 - 更具體地說,就類型而言思考問題。 http://blog.kickin-the-darkness.com/2007/08/fundamental-theory-of-ada.html – Shark8

回答

5

您可以將您的字符串映射到離散類型。最簡單的是一個枚舉類型:

procedure Light (Colour : in  String) is 
    type Colours is (Red, Green, Blue); 
begin 
    case Colours'Value (Colour) is -- ' <- magic ;-) 
     when Red => 
     Switch_Red_LED; 
     when Green => 
     Switch_Green_LED; 
     when Blue => 
     Switch_Blue_LED; 
    end case; 
exception 
    when Constraint_Error => 
     raise Constraint_Error with "There is no " & Colour & " LED."; 
end Light; 
3

我經常使用的實際map做這種映射的,因爲它可以讓你比枚舉更大的靈活性。您的「名稱」不必符合枚舉語法,並且您可以輕鬆提供所有映射到單個值的變體。

對於所期望的功能的定義,如在包中提供:

package Case_Map is 

    function Is_Valid_Direction(Direction_To_Go : in String) return Integer; 

end Case_Map; 

該(非編譯由於缺少針對遊戲的聲明)實現使用字符串的映射到一個枚舉是依次case表達式:

with Ada.Characters.Handling; 
with Ada.Containers.Indefinite_Ordered_Maps; 

package body Case_Map is 

    use Ada.Characters.Handling; 

    type Directions is (Go_North, Go_South, Go_East, Go_West, Go_Up, Go_Down); 

    package Direction_Management is new Ada.Containers.Indefinite_Ordered_Maps 
    (String, Directions); 

    Direction_Map : Direction_Management.Map; 

    function Is_Valid_Direction(Direction_To_Go : in String) return Integer is 
     Room : Integer := 0; 
    begin 
     case Direction_Map(To_Upper(Direction_To_Go)) is 
     when Go_North => Room := Building(CurrentRoom).Exits(NORTH); 
     when Go_South => Room := Building(CurrentRoom).Exits(SOUTH); 
     when Go_East => Room := Building(CurrentRoom).Exits(EAST); 
     when Go_West => Room := Building(CurrentRoom).Exits(WEST); 
     when Go_Up => Room := Building(CurrentRoom).Exits(UP); 
     when Go_Down => Room := Building(CurrentRoom).Exits(DOWN); 
     end case; 
     return Room; 
    exception 
     when Constraint_Error => 
     return 0; 
    end Is_Valid_Direction; 

begin 
    Direction_Map.Insert("NORTH", Go_North); 
    Direction_Map.Insert("N", Go_North); 
    Direction_Map.Insert("SOUTH", Go_South); 
    Direction_Map.Insert("S", Go_South); 
    Direction_Map.Insert("EAST", Go_East); 
    Direction_Map.Insert("E", Go_East); 
    Direction_Map.Insert("WEST", Go_West); 
    Direction_Map.Insert("W", Go_West); 
    Direction_Map.Insert("UP", Go_Up); 
    Direction_Map.Insert("U", Go_Up); 
    Direction_Map.Insert("DOWN", Go_Down); 
    Direction_Map.Insert("D", Go_Down); 
end Case_Map; 
0

GNAT編譯器本身使用一個散列表,它將字符串(標識符,關鍵字......)映射爲整數。這是包namet.ads,和GNATCOLL.Symbolic提供了一個類似的API。這簡化了許多事情(比如字符串比較快得多),並允許使用case語句,如你的例子。因此,如果您使用的是有限(或至少是增長緩慢)的字符串列表,則GNATCOLL.Symbolic可能是一種合適的方法。