首页 > 代码库 > Ada 换硬币小程序

Ada 换硬币小程序


with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;


procedure Main is

   type Coin is ( Penny, Nickel, Dime, Quarter, Half_Dollar, Dollar );
   
   Coin_Value : constant array ( Penny .. Dollar ) of Integer := 
     ( Penny => 1, 
       Nickel => 5,
       Dime => 10, 
       Quarter => 25, 
       Half_Dollar => 50, 
       Dollar => 100 );
   
   total_change,
   coin_count,
   num_dollars, 
   num_cents : Integer;
   
begin
   
   total_change := 0;
   
   Put_Line( "Enter the number of each coin : " );
   
   for next_coin in Penny .. Dollar loop
      Get( coin_count );
      total_change := total_change + coin_count * Coin_Value( next_coin );
   end loop;
   
   num_dollars := total_change / 100;
   num_cents := total_change rem 100;
   
   Put( "Total change is $" ); Put( num_dollars );
   if num_cents < 10 then
      Put( ".0" ); Put( num_cents );
   else
      Put( "." ); Put( num_cents );
   end if;
     
end Main;


Ada 换硬币小程序