Advertisement
CarlBjorklund

Tenta Uppgift 2: Chips

Oct 31st, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 2.32 KB | None | 0 0
  1. with Ada.Text_IO;                       use Ada.Text_IO;
  2. with Ada.Integer_Text_IO;               use Ada.Integer_Text_IO;
  3.  
  4. procedure Chips is
  5.    Value_Names   : constant array(1..6) of String(1..5) :=
  6.      ("white", "red  ", "blue ", "green", "black", "gold ");
  7.    Value_Weights : constant array(1..6) of Positive :=
  8.      (   1   ,   5    ,   10   ,   20   ,   100   ,  200 );
  9.  
  10.    function Get_Value_For(Color : in String) return Positive is
  11.    begin
  12.       for I in 1..6 loop
  13.          if Color = Value_Names(I) then
  14.             return Value_Weights(I);
  15.          end if;
  16.       end loop;
  17.       -- crash
  18.    end Get_Value_For;
  19.  
  20.    function Get_Color_For(Value : in Positive) return String is
  21.    begin
  22.       for I in 1..6 loop
  23.          if Value = Value_Weights(I) then
  24.             return Value_Names(I);
  25.          end if;
  26.       end loop;
  27.       -- crash
  28.    end Get_Color_For;
  29.  
  30.  
  31.    -- Lägg till din datatypsdefinition
  32.    -- och dina underprogram här nedan.
  33.    type Chips_Type is
  34.       record
  35.          Antal: Integer;
  36.          Color: String(1..5);
  37.       end record;
  38.  
  39.    C1, C2, Sum : Chips_Type;
  40.  
  41.    procedure Get(Chips: out Chips_Type) is
  42.       C: Character;
  43.    begin
  44.       Get(Chips.Color);
  45.       Get(C);
  46.       Get(Chips.Antal);
  47.       end Get;
  48.  
  49.  
  50.       function "+"(C1,C2: in Chips_Type) return Chips_Type is
  51.  
  52.          A,B,C : Integer;
  53.          Sum: Chips_Type;
  54.          Sant_Falskt: Boolean;
  55.       begin
  56.         A:=Get_Value_For(C1.Color);
  57.         B:=Get_Value_For(C2.Color);
  58.         Sant_Falskt:=True;
  59.         if B < A then
  60.            C:=A;
  61.            A:=B;
  62.            B:=C;
  63.            Sant_Falskt:=False;
  64.         end if;
  65.         if Sant_Falskt = True then
  66.         Sum.Color:=Get_Color_For(A);
  67.         Sum.Antal:=C1.Antal+(C2.Antal*(B/A));
  68.         elsif Sant_Falskt = False then
  69.            Sum.Color:=Get_Color_For(A);
  70.            Sum.Antal:=C2.Antal+(C1.Antal*(B/A));
  71.         end if;
  72.  
  73.  
  74.          return Sum;
  75.       end "+";
  76.  
  77.       procedure Put(Sum: in Chips_Type) is
  78.       begin
  79.          Put(Sum.Color);
  80.          Put(" ");
  81.          Put(Sum.Antal,0);
  82.       end Put;
  83.  
  84. begin
  85.    Put("Input some chips: ");
  86.    Get(C1);
  87.    Skip_Line;
  88.    Put("Input some more chips: ");
  89.    Get(C2);
  90.    Skip_Line;
  91.  
  92.    Sum := C1 + C2;
  93.  
  94.    New_Line;
  95.    Put("The sum is ");
  96.    Put(Sum);
  97.    Put_Line(".");
  98. end Chips;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement