I have to write an Occam program which reads characters from the standard input, then prints out as many '#' characters as the value of the input character. The program must have 2 processes. The first process filters the input. It accepts numbers, and the 'q' character which is the end of the input.
Here is an example input:
asdf 2 351 q
And what it pass on :
2 3 5 1
The second process has to print out as many # characters, as the value of the numbers
So the example output is this:
## ### ##### #
I tried to write it, but I'm unfamiliar with Occam.
#USE "course.lib"
PROC main(CHAN BYTE keyboard, screen, error)
CHAN INT numberinput:
BYTE character:
PROC justnumbers(CHAN BYTE input, CHAN INT output)
BYTE ch:
BOOL reading:
INT ret:
SEQ
reading := TRUE
WHILE reading
SEQ
input ? ch
IF
-- idk that if i can do the following part like this:
-- (ch>='0') AND (ch<='9')
-- ret := ch - '0'
-- ch = 'q'
-- ret := -1
-- TRUE
-- SKIP
ch = '0'
ret:=0
ch = '1'
ret:=1
ch = '2'
ret:=2
ch = '3'
ret:=3
ch = '4'
ret:=4
ch = '5'
ret:=5
ch = '6'
ret:=6
ch = '7'
ret:=7
ch = '8'
ret:=8
ch = '9'
ret:=9
ch = 'q'
SEQ
ret:=-1
reading := FALSE
TRUE
SKIP
output ! ret
:
PROC printer(CHAN INT input)
INT num:
BOOL reading:
SEQ
reading := TRUE
WHILE reading
SEQ
input ? num
IF
num <> -1
WHILE num > 0
SEQ
-- here i wanna print the '#' character
num := num - 1
num = -1
reading := FALSE
:
SEQ
PAR
justnumbers(keyboard, numberinput)
printer(numberinput)
out.string("*n",0,screen)
:
Let's go step by step, ok?
Here: printing
Next: character analysis: