Ubuntu Forum beginners Challenge

' Gambas class file
'----------------------------------------------------
PUBLIC SUB Form_Open()
  ME.center 'centre form on screen
END
'----------------------------------------------------
PUBLIC SUB btnRun_Click()
  DIM iTotl AS Integer = 0 'for total of numbers
  DIM iArraySize AS Integer = 1000 'initial size of input array
  DIM hFileIn AS File 'input file handle
  DIM sChar AS String 'variable to hold the line character
  DIM iCount AS Integer = 0 'general loop counter for input
  DIM iCountOut AS Integer = 0 'general loop counter for output
  DIM myArray AS String[] 'array to hold input
  DIM letterCount AS Integer[26] 'array to hold count of individual letters
  
  IF Exist(FileChooser1.value) THEN 'check if a file has been chosen
    myArray = NEW String[iArraySize] 'instantiate the array at default size
    hFileIn = OPEN FileChooser1.value FOR INPUT 'open the text file
    WHILE NOT Eof(hFileIn) 'for each line in the text file
      LINE INPUT #hFileIn, sChar
        IF sChar = "" THEN  'trap an empty line
          sChar = "_"
        ENDIF 
        sChar = Left(sChar, 1) 'limit to first digit of line in case there is more than one digit
        myArray[iCount] = sChar 'put into the array
        INC iCount 'increment the counter by 1
        IF iCount = iArraySize - 1 THEN 'if the array has nearly reached its limits then expand
          iArraySize += 1000
          myArray.Resize(iArraySize)
        ENDIF
    WEND  
    PRINT icount
    FOR iCountOut = 0 TO icount - 2 'for each valid element in the array but ignore last line
      IF IsNumber(Val(myArray[iCountOut])) THEN 'check if it is a number
        iTotl += myArray[iCountOut] 'add to the number total if it is
      ELSE 
        IF myArray[iCountOut] >= "a" AND myArray[iCountOut] <= "z" THEN 'check if it is a valid letter as defined in spec
          INC letterCount[Asc(myArray[iCountOut]) - 97] 'add 1 to the count of that letter
        ELSE 
          ListBox1.Add("'" & myArray[iCountOut] & "' is not within specification and is ignored") 'add an error message to listbox
        ENDIF 
      ENDIF 
    NEXT 
    ListBox1.Add("The sum of numbers is " & iTotl)
    FOR iCount = 0 TO 25
      IF letterCount[icount] > 0 THEN 
        ListBox1.Add("Total number of letter '" & Chr(icount + 97) & "' is " & letterCount[icount])
      ENDIF 
    NEXT 
  ELSE 
    Message.Error("please select a file to analyse")
  ENDIF 
END
'----------------------------------------------------
PUBLIC SUB btnExit_Click()
  ME.close
END
'----------------------------------------------------
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: