Previous topic | Ada Home Page | Index

Case example

----------------------------------------------------------
-- calc.ada - Simple calculator program
-- Skansholm pp133-134
----------------------------------------------------------

with TEXT_IO; use TEXT_IO;

procedure calc is

    -- declare integer and float I/O libraries
    package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
    package float_io is new TEXT_IO.FLOAT_IO( FLOAT );
    use  int_io, float_io;

    -- declare any constants and variables required
    operand_1, operand_2 : integer;  -- values of operands
    operator : character;            -- operation to do

begin -- calc
    -- get char from user
    PUT_LINE ("Write a simple arithmetic expression");
    GET (operand_1);
    GET (operator);
    GET (operand_2);
    SKIP_LINE;

    -- select calc based on operator
    case operator is
        when '+' =>
            PUT(operand_1 + operand_2, width=>1);
        when '-' =>
            PUT(operand_1 - operand_2, width=>1);
        when '*' =>
            PUT(operand_1 * operand_2, width=>1);
        when '/' =>
            if operand_2 /= 0 then
                PUT(operand_1 / operand_2, width=>1);
            else
                PUT("division by zero is not allowed");
            end if;
        when others =>
          PUT("Faulty operator");
    end case;
    NEW_LINE;
end calc;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 13 Feb 96