Respond to this messageReturn to Index
Original Message
  • Very well
    • AlGoreIthm (no login)
      Posted Jul 21, 2012 5:43 PM

      Our friend Lisztfr brings up an excellent point. Document what your program is doing so that others may follow the logic more easily.

      Hopefully this will pass your evaluation:
      It is as I understand pmarathe's original intention was described, but using
      a completely different method not relying on series of 'IF THEN ELSEs' or
      'SELECT CASE'. Enjoy.


      DECLARE FUNCTION changeperson$ (tochange$)

      ' Word substitution program by AlGoreIthm
      ' This version: Saturday July 21st 2012 pm
      ' NOT extensively tested, NOT guaranteed 'bug-free'.

      ' IMPORTANT NOTE: this version does not have a method of analyzing
      ' for the correct use of 'I' versus 'me'.

      ' User INPUT tochange$ will be scanned by the changeperson$() FUNCTION
      ' and certain words from a list will be substitued with their antonyms

      INPUT tochange$
      IF tochange$ <> "" THEN PRINT changeperson$(tochange$)


      FUNCTION changeperson$ (tochange$)

      ' This function scans the user INPUT tochange$ for data as per the
      ' list below, to be substituted with the second word in the string, in
      ' this case, the antonym or 'opposite' meaning.

      ' Provided that the line with DIM s$(9) is dimensioned to the correct value,
      ' FUNCTION changeperson$ will always perform the same way, regardless of
      ' the number of items in the list, no change in program code will be required

      ' Due to the nature of INSTR, this list of items must be ordered with the
      ' longest item from a group first: therefore all words beginning with 'you'
      ' must be in order of longest first -
      ' example: yourself before yours before your before you etc.

      DIM s$(9)
      s$(1) = "myself yourself"
      s$(2) = "my your"
      s$(3) = "mine yours"
      s$(4) = "me you"
      s$(5) = "yourself myself"
      s$(6) = "yours mine"
      s$(7) = "your my"
      s$(8) = "you me"
      s$(9) = "i you"

      's$(10) = "white black" ' these additional data items
      's$(11) = "black white" ' can be used to demonstrate that the logic
      's$(12) = "day night" ' will always perform the same way
      's$(13) = "night day" ' regardless of how many items appear in this list
      's$(14) = "yes no" ' remember to set the line DIM s$()
      's$(15) = "no yes" ' to the correct UPPER BOUNDARY value
      's$(16) = "wet dry"
      's$(17) = "dry wet"

      'modify tochange$ to assist the scanning process by ensuring that
      'the entire string is in completely in lowercase,
      'with only one leading space and one trailing space

      tochange$ = " " + LCASE$(LTRIM$(RTRIM$(tochange$))) + " "

      begin% = 1

      DO 'External loop scans the length of User INPUT tochange$

      ' setup to scan the INPUT tochange$ word by word

      scan% = INSTR(begin%, tochange$, " ") + 1
      found% = 0
      looper% = 0

      DO 'Internal loop scans to find a match in the data list

      looper% = looper% + 1 ' loop counter index increase
      match$ = LEFT$(s$(looper%), INSTR(s$(looper%), " ") - 1)

      ' when a word in tochange $ is found in the data list
      ' its counterpart will be set in tochange$ at the same position
      ' effectively replacing the found match$
      ' ---------------------------------------------------------------------------

      IF MID$(tochange$, scan%, LEN(match$)) = match$ THEN

      ' setup the replacement word
      with$ = RIGHT$(s$(looper%), LEN(s$(looper%)) - LEN(match$) - 1)

      ' retain the remainder of tochange$
      therest$ = MID$(tochange$, scan% + LEN(match$))

      ' modify tochange$ with the substitution
      tochange$ = LEFT$(tochange$, scan% - 1) + with$ + therest$

      found% = looper% ' will terminate the Internal DO LOOP
      ' because a match has been found
      END IF

      ' ---------------------------------------------------------------------------

      ' The Internal loop terminates if a match has been found or after
      ' the entire data list has been searched and returned no match
      ' for the current word

      LOOP UNTIL found% = looper% OR looper% = UBOUND(s$)

      'moving the search scan index towards the next word
      begin% = scan% + 2

      'once tochange$ has been completely scanned the External loop can terminate
      IF begin% >= LEN(tochange$) - 1 THEN terminate% = 1

      LOOP WHILE terminate% = 0

      changeperson$ = tochange$



      ' For more information on Table-Driven Methods,
      ' Please refer to CODE COMPLETE by Steve McConnell
      ' Microsoft Press Copyright 1993 pages 271 - ...

      'quote from page 271-272:

      ' " A table-driven method is a scheme that allows you to look up information
      ' in a table rather than using logic statements (if and case) to figure
      ' it out. Virtually anything you can select with logic statements, you
      ' can select with tables instead. " end quote -

      ' Programmer's notes:
      ' -------------------
      ' The strong point to this method is that it forces the separation of data
      ' from the programming code, ensuring that a change in data will not tamper
      ' with extensively tested code possibly causing a bug.

      ' When the data itself is included within the code, a change in data
      ' may inadvertently cause a problem with the code, or vice versa.

      ' Ideally, all the data would be 'hidden' in a disk file and not be visible
      ' in any section of the program itself.
      ' The code should be versatile enough to handle any properly formatted data.

      ' Although it may not be the optimal or fastest method, today's computers
      ' are fast enough that the human user will not notice a delay of a few extra
      ' milliseconds. It should probably be considered that 'speed' does not only
      ' consist of how fast a program runs, but also how quickly it can be
      ' modified without the consequence of having unforeseen 'bugs' appear. : )

      END SUB

    Your Name
    Message Title
    Message Text

     Copyright © 1999-2017 Network54. All rights reserved.   Terms of Use   Privacy Statement