A+B: Difference between revisions

1,868 bytes added ,  1 year ago
no edit summary
(Added Modula-3)
No edit summary
Line 2,766:
Item #1 = 11
Sum = 31
</pre>
 
 
 
 
 
 
=={{header|FutureBasic}}==
The input statement was removed from FB several years ago. However, it's not complicated to write our own input field which compiles as a stand-alone Macintosh application.
<lang futurebasic>
_window = 1
begin enum 1
_label
_input
_result
end enum
 
void local fn BuildWindow
window _window, @"A + B", ( 0, 0, 260, 200 )
textlabel _label, @"Enter two signed integers separated by a comma, space/s or plus sign. Enter return to calculate.", ( 20, 130, 220, 48 ), _window
textfield _input,,,( 20, 90, 220, 24 ), _window
TextFieldSetMaximumNumberOfLines( _input, 1 )
ControlSetFormat( _input, @"0123456789 ,+-", YES, 0, NULL )
ControlSetAlignment( _input, NSTextAlignmentCenter )
textlabel _result,, ( 20, 50, 220, 24 ), _window
ControlSetAlignment( _result, NSTextAlignmentRight )
WindowMakeFirstResponder( _window, _input )
end fn
 
local fn DoCalc
NSInteger value1, value2
CFStringRef calcStr = fn ControlStringValue( _input )
calcStr = fn StringByReplacingOccurrencesOfString( calcStr, @",", @"\t" )
calcStr = fn StringByReplacingOccurrencesOfString( calcStr, @"+", @"\t" )
calcStr = fn StringByReplacingOccurrencesOfString( calcStr, @" ", @"\t" )
CFArrayRef calcArr = fn StringComponentsSeparatedByString( calcStr, @"\t" )
value1 = fn StringIntegerValue( fn ArrayFirstObject( calcArr ) )
value2 = fn StringIntegerValue( fn ArrayLastObject( calcArr ) )
ControlSetStringValue( _result, fn StringWithFormat( @"%ld + %ld = %ld", value1, value2, value1 + value2 ) )
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long )
select ( ev )
case _textFieldDidEndEditing : fn DoCalc
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
fn BuildWindow
 
HandleEvents
</lang>
{{output}}
<pre>
-250 1000 = 750
or
-250,1000 = 750
or
-250 + 1000 = 750
</pre>
 
715

edits