Print the amount of characters that is given in the input

86 Views Asked by At

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) 
:
1

There are 1 best solutions below

0
On

Let's go step by step, ok?
Here: printing

PROC printer( CHAN INT  input,
              CHAN BYTE output
              )
  INT   aNUM:
  BOOL  aReadingFLAG IS TRUE:

  WHILE aReadingFLAG
    SEQ
      input ? aNUM
      IF
        aNUM  = -1                         -- num  = -1
          aReadingFLAG := FALSE               -- SET aReadingFLAG, FALSE
        aNUM >  0                          -- num >   0
          SEQ repeat FOR aNUM                 -- SEQ <-an-iterator-syntax->
              out.string( "#", 0, output )       -- OUT print the '#' character
        TRUE                               -- num <  -1
          SKIP                             -- num  =  0
: -- printer()

Next: character analysis:

PROC justnumbers( CHAN BYTE input,
                  CHAN INT  output
                  )
  BYTE  aCHAR:
  BOOL  aReadingFLAG IS TRUE:

  WHILE aReadingFLAG
    SEQ
      input ? aChar
      CASE    aChar                  -- SWITCH( aChar ):
        'q'                            -- CASE 'q':
          SEQ                             SEQ
            aReadingFLAG := FALSE         -- SET aReadingFLAG, FALSE
            output ! -1                   -- OUT -1
        '1', '2', '3', '4', '5', '6', '7', '8', '9'
          output ! ( ( INT aChar ) - ( INT '0' ) )
: -- justnumbers()