How to resolve the algorithm Abbreviations, simple step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abbreviations, simple step by step in the FutureBasic programming language

Table of Contents

Problem Statement

The use of   abbreviations   (also sometimes called synonyms, nicknames, AKAs, or aliases)   can be an easy way to add flexibility when specifying or using commands, sub─commands, options, etc.

For this task, the following   command table   will be used:

Notes concerning the above   command table:

For a user string of: the computer program should return the string:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abbreviations, simple step by step in the FutureBasic programming language

Source code in the futurebasic programming language

_window = 1
begin enum 1
  _userStringFld
  _validateBtn
  _resultsStringFld
end enum

void local fn BuildWindow
  window _window, @"Abbreviations, simple", (0,0,600,268)
  WindowSetContentMinSize( _window, fn CGSizeMake( 200, 268 ) )
  WindowSetContentMaxSize( _window, fn CGSizeMake( 10000, 268 ) )
  
  textfield _userStringFld,, @"riG   rePEAT copies  put mo   rest    types   fup.    6       poweRin", (20,152,560,96)
  TextFieldSetPlaceholderString( _userStringFld, @"Enter commands" )
  ViewSetAutoresizingMask( _userStringFld, NSViewWidthSizable )
  
  button _validateBtn,,, @"Validate", (259,117,83,32)
  ViewSetAutoresizingMask( _validateBtn, NSViewMinXMargin + NSViewMaxXMargin )
  
  textfield _resultsStringFld,,, (20,20,560,96)
  TextFieldSetEditable( _resultsStringFld, NO )
  TextFieldSetSelectable( _resultsStringFld, YES )
  ViewSetAutoresizingMask( _resultsStringFld, NSViewWidthSizable )
end fn

local fn Commands as CFArrayRef
  CFStringRef       cmd, string
  long              abbrLen
  CFMutableArrayRef commands = fn MutableArrayWithCapacity(0)
  ScannerRef        scanner
  
  string = @"   add 1  alter 3  backup 2  bottom 1  Cappend 2  change 1  Schange  Cinsert 2  Clast 3"
  string = fn StringByAppendingString( string, @"   compress 4 copy 2 count 3 Coverlay 3 cursor 3  delete 3 Cdelete 2  down 1  duplicate" )
  string = fn StringByAppendingString( string, @"   3 xEdit 1 expand 3 extract 3  find 1 Nfind 2 Nfindup 6 NfUP 3 Cfind 2 findUP 3 fUP 2" )
  string = fn StringByAppendingString( string, @"   forward 2  get  help 1 hexType 4  input 1 powerInput 3  join 1 split 2 spltJOIN load" )
  string = fn StringByAppendingString( string, @"   locate 1 Clocate 2 lowerCase 3 upperCase 3 Lprefix 2  macro  merge 2 modify 3 move 2" )
  string = fn StringByAppendingString( string, @"   msg  next 1 overlay 1 parse preserve 4 purge 3 put putD query 1 quit  read recover 3" )
  string = fn StringByAppendingString( string, @"   refresh renum 3 repeat 3 replace 1 Creplace 2 reset 3 restore 4 rgtLEFT right 2 left" )
  string = fn StringByAppendingString( string, @"   2  save  set  shift 2  si  sort  sos  stack 3 status 4 top  transfer 3  type 1  up 1" )
  
  scanner = fn ScannerWithString( string )
  while ( fn ScannerIsAtEnd( scanner ) == NO )
    if ( fn ScannerScanUpToCharactersFromSet( scanner, fn CharacterSetWhitespaceAndNewlineSet, @cmd ) )
      abbrLen = 0
      fn ScannerScanInteger( scanner, @abbrLen )
      MutableArrayAddObject( commands, @{@"cmd":cmd,@"len":@(abbrLen)} )
    end if
  wend
end fn = commands

void local fn Validate
  CFArrayRef         commands, words
  CFStringRef        userString, result, wd, cmd
  long               wordCount, i, wordLen, abbrLen
  CFMutableStringRef results
  CFDictionaryRef    dict
  BOOL               found
  
  commands   = fn Commands
  userString = fn ControlStringValue( _userStringFld )
  words      = fn StringComponentsSeparatedByCharactersInSet( userString, fn CharacterSetWhitespaceAndNewlineSet )
  results    = fn MutableStringWithCapacity( 0 )
  wordCount  = len( words )
  
  for i = 0 to wordCount - 1
    found = NO
    result = @"*error* "
    wd = words[i]
    wordLen = len( wd )
    if ( wordLen )
      
      for dict in commands
        cmd = dict[@"cmd"]
        abbrLen = fn NumberIntegerValue(dict[@"len"])
        
        if ( abbrLen != 0 and wordLen >= abbrLen )
          found = fn StringHasPrefix( lcase( cmd ), lcase( wd ) )
        else
          found = fn StringIsEqual( lcase( cmd ), lcase( wd ) )
        end if
        
        if ( found )
          result = fn StringWithFormat( @"%@ ",ucase( cmd ) )
          break
        end if
        
      next
      
      MutableStringAppendString( results, result )
    end if
  next
  
  ControlSetStringValue( _resultsStringFld, results )
end fn


void local fn DoDialog( ev as long, tag as long )
  select ( ev )
    case _btnClick : fn Validate
  end select
end fn


editmenu 1
fn BuildWindow

on dialog fn DoDialog

HandleEvents

  

You may also check:How to resolve the algorithm Erdös-Selfridge categorization of primes step by step in the Java programming language
You may also check:How to resolve the algorithm Repeat step by step in the Wren programming language
You may also check:How to resolve the algorithm Anagrams step by step in the MiniScript programming language
You may also check:How to resolve the algorithm String append step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Binary digits step by step in the TI-83 BASIC programming language