CLU példaprogramok

 
Összegzés stream-ből

A program stream-ből (file vagy billentyűzet) olvas szóközzel elválasztott egészeket, majd kiírja a standard input-ra az összegüket.
[stream; exception] (CLU forrás)


start_up = proc()
  inp:stream := stream$primary_input()
  outp:stream := stream$primary_output()
  stream$reset(inp)
  sum:int := SumStream(inp)
  stream$putl(outp,int$unparse(sum))
  except when integer_overflow(num:string):
              stream$putl(outp,"integer overflow error: "||num);
         when bad_format(num:string):
              stream$putl(outp,"format error: "||num);
         when sum_overflow:
              stream$putl(outp,"integer overflow error at sum");
         end
end start_up

SumStream = proc(s:stream) returns (int) signals (sum_overflow,
                                                   integer_overflow(string),
                                                   bad_format(string))
  sum:int := 0
  num:string
  while true do
    c:char := stream$getc(s)
    while c=' ' do
      c:=stream$getc(s)
    end
    num:="";
    while c~=' ' do
      num:=string$append(num,c)
      c:=stream$getc(s)
    end except when end_of_file: end
    sum:=sum+int$parse(num)
  end
  except when end_of_file: return (sum)
         when integer_overflow: signal integer_overflow(num)
         when bad_format,invalid_character(*): signal bad_format(num)
         when overflow: signal sum_overflow
         end
end SumStream