RosettaCodeData/Task/Time-a-function/FutureBasic/time-a-function.basic

63 lines
2.1 KiB
Plaintext

begin enum 1
_numLabel
_numInput
_textField
_computeTime
end enum
void local fn BuildMacInterface
window 1, @"Prime factors", ( 0, 0, 560, 120 ), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
textlabel _numLabel, @"Number to factor:", ( 155, 86, 148, 24 )
textfield _numInput, Yes, @"", ( 280, 88, 160, 24 )
TextFieldSetTextColor ( _numLabel, fn ColorGray )
ControlSetAlignment ( _numInput, NSTextAlignmentCenter )
ControlSetFormat ( _numInput, @"0123456789-", YES, 15, _formatCaseInsensitive )
ControlSetFont ( _numInput, fn FontSystemFontOfSize( 13 ) )
textfield _textField, YES, , ( 20, 46, 520, 26 )
ControlSetFontWithName ( _textField, @"Menlo", 12 )
ControlSetAlignment( _textField, NSTextAlignmentCenter )
TextFieldSetBordered( _textField, YES )
textlabel _computeTime,,( 350, 16, 188, 22 )
ControlSetAlignment( _computeTime, NSTextAlignmentRight )
filemenu 1 : menu 1, , No
editmenu 2 : menu 2, , Yes
WindowMakeFirstResponder ( 1, _numInput )
end fn
void local fn factor
SInt64 i, count = 0, num = fn ControlIntegerValue( _numInput ), n = num
if n < 0 then n = -n
while ( n mod 2 == 0 )
mda(count) = 2 : count++ : n = n/2
wend
for i = 3 to sqr(n) + 1 step 2
while ( n mod i == 0 )
mda(count) = i : count++ : n = n/i
wend
next
if (n > 2) then mda(count) = n
if num < 0 then mda(0) = -mda_integer(0)
CFStringRef s = fn StringByReplacingOccurrencesOfString( mda_text, @"\n", @" * " ) : mda_kill
s = fn StringByTrimmingCharactersInSet( s, fn CharacterSetWithCharactersInString( @" * " ) )
ControlSetStringValue( _textField, fn StringWithFormat( @"%@ = %lld", s, num ) )
end fn
void local fn DoDialog( ev as Long )
select ev
case _controlTextDidEndEditing
CFTimeInterval t = fn CACurrentMediaTime
fn Factor
ControlSetStringValue( _computeTime, fn StringWithFormat( @"Compute time: %.3f ms", (fn CACurrentMediaTime-t)*1000 ) )
WindowMakeFirstResponder( _textField, _numInput )
case _windowWillClose : end
end select
end fn
fn BuildMacInterface
on dialog fn DoDialog
HandleEvents