language: Ada (gnat-4.3.2)
date: 126 days 11 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- Calculator3
 
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Calculator3 is
        First, Second, Result : Integer;
        Operator : Character;
        Valid : boolean;
--Skip-spaces   
        procedure Skip_spaces is
                Next : Character;
                EndLine : boolean;
        begin
                Look_ahead (Next, EndLine);
                while Next=' '  or Next=ASCII.ht or Endline=true loop
                if Endline=true then
                        Skip_Line;
                else
                        Get (Next);
                end if;
                Look_ahead (Next, Endline);
                end loop;
        end Skip_spaces;
--Look_for_digit
        procedure Look_for_digit is
                Next : character;
                Endline : boolean;
        begin
        skip_spaces;
        Look_ahead (Next, Endline);
        If not (next in '0' .. '9') then
                Put ("Character is invalid, number is expected. ");
                Valid := false;
                Skip_Line;
        end if;
        end look_for_digit;
--Look_for_operator
        procedure look_for_operator is
                Next : character;
                Endline : boolean;
        begin
        skip_spaces;
        Look_ahead (Next, Endline);
        If not (Next = '+' or Next = '-' or Next = '*' or Next = 'x' 
                or Next = '/') then
                Put ("Operator is invalid. ");
                Valid :=false;
                Skip_Line;
        end if;
        end look_for_operator;
--body
begin
        valid:=false;
        while valid=false loop
        Put ("What do you want me to calculate? ");
        valid := true;
        Look_for_digit;
        while Valid = true loop
                Get (First);
                Look_for_operator;
                Get (Operator);
                Look_for_digit;
                Get (Second);
        end loop;
        end loop;
        if Operator = '+' then
                Result := First+Second;
        elsif Operator = '-' then
                Result := First-Second;
        elsif Operator = '*' or Operator = 'x' then
                Result := First*Second;
        elsif Operator = '/' then
                Result := First/Second;
        else
                Put ("Invalid operator ");
                Put (Operator);
                New_Line;
                valid:=false;
        end if;
        if valid=true then              
                Put (First, width => 3);
                Put (Operator);
                Put (Second, width => 0);
                Put ("=");
                Put (Result, width =>0);
                New_Line;
        end if;
end Calculator3;