How to resolve the algorithm Arithmetic/Integer step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic/Integer step by step in the FutureBasic programming language

Table of Contents

Problem Statement

Get two integers from the user,   and then (for those two integers), display their:

Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.

Bonus: Include an example of the integer divmod operator. For example: as in #Haskell, #Python and #ALGOL 68

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic/Integer step by step in the FutureBasic programming language

Source code in the futurebasic programming language

window 1, @"Integer Arithmetic", ( 0, 0, 400, 300 )

NSInteger a = 25
NSInteger b = 53

print "addition        "a" + "b" = " (a + b)
print "subtraction     "a" - "b" = " (a - b)
print "multiplication  "a" * "b" = " (a * b)
print "division        "a" / "b" = " (a / b)
printf @"float division  %ld / %ld = %f", a, b, (float)a / (float)b
print "modulo          "a" % "b" = " (a mod b)
print "power           "a" ^ "b" = " (a ^ b)

HandleEvents

_window = 1
begin enum 1
_int1Label
_int1Field
_int2Label
_int2Field
_calcResults
_calcBtn
end enum

void local fn BuildWindow
CGRect r

r = fn CGRectMake( 0, 0, 480, 360 )
window _window, @"Integer Arithmetic", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable + NSWindowStyleMaskMiniaturizable

r = fn CGRectMake( 240, 320, 150, 24 )
textlabel _int1Label, @"Enter first integer:", r, _window
ControlSetAlignment( _int1Label, NSTextAlignmentRight )
r = fn CGRectMake( 400, 322, 60, 24 )
textfield _int1Field, YES, @"25", r, _window
ControlSetAlignment( _int1Field, NSTextAlignmentCenter )
ControlSetUsesSingleLineMode( _int1Field, YES )
ControlSetFormat( _int1Field, @"0123456789-", YES, 5, NULL )

r = fn CGRectMake( 240, 290, 150, 24 )
textlabel _int2Label, @"Enter second integer:", r, _window
ControlSetAlignment( _int2Label, NSTextAlignmentRight )
r = fn CGRectMake( 400, 292, 60, 24 )
textfield _int2Field, YES, @"53", r, _window
ControlSetAlignment( _int2Field, NSTextAlignmentCenter )
ControlSetUsesSingleLineMode( _int2Field, YES )
ControlSetFormat( _int2Field, @"0123456789-", YES, 5, NULL )

r = fn CGRectMake( 50, 60, 380, 200 )
textview _calcResults, r,,, _window
TextViewSetTextContainerInset( _calcResults, fn CGSizeMake( 10, 20 ) )
TextSetFontWithName( _calcResults, @"Menlo", 13.0 )
TextViewSetEditable( _calcResults, NO )

r = fn CGRectMake( 370, 13, 100, 32 )
button _calcBtn,,, @"Calculate", r
end fn

local fn PerformCalculations
CFStringRef tempStr

NSInteger i1 = fn ControlIntegerValue( _int1Field )
NSInteger i2 = fn ControlIntegerValue( _int2Field )

CFMutableStringRef mutStr = fn MutableStringWithCapacity( 0 )

// Display inout integers
tempStr = fn StringWithFormat( @"Number 1: %ld\nNumber 2: %ld\n\n", i1, i2 )
MutableStringAppendString( mutStr, tempStr )

// Add
tempStr = fn StringWithFormat( @"Addition: %ld + %ld = %ld\n", i1, i2, i1 + i2 )
MutableStringAppendString( mutStr, tempStr )

// Subtract
tempStr = fn StringWithFormat( @"Subtraction: %ld - %ld = %ld\n", i1, i2, i1 - i2 )
MutableStringAppendString( mutStr, tempStr )

// Multiply
tempStr = fn StringWithFormat( @"Multiplication: %ld * %ld = %ld\n", i1, i2, i1 * i2 )
MutableStringAppendString( mutStr, tempStr )

if ( i2 != 0 )

// Divide
tempStr = fn StringWithFormat( @"Integer Division: %ld / %ld = %ld\n", i1, i2, i1 / i2 )
MutableStringAppendString( mutStr, tempStr )

// Float Divide
tempStr = fn StringWithFormat( @"Float Division: %ld / %ld = %f\n", i1, i2, (float)i1 / (float)i2 )
MutableStringAppendString( mutStr, tempStr )

// mod
tempStr = fn StringWithFormat( @"Modulo: %ld mod %ld = %ld remainder\n", i1, i2, i1 mod i2 )
MutableStringAppendString( mutStr, tempStr )

// power
tempStr = fn StringWithFormat( @"Power: %ld ^ %ld = %e\n", i1, i2, i1 ^ i2 )
MutableStringAppendString( mutStr, tempStr )

else

MutableStringAppendString( mutStr, @"Cannot divide by zero." )

end if

TextSetString( _calcResults, mutStr )
end fn

void local fn DoDialog( ev as long, tag as long, wnd as long )
'~'1
select ( ev )
case _btnClick
select ( tag )
case _calcBtn : fn PerformCalculations
end select
case _windowWillClose : end
end select
end fn

on dialog fn DoDialog

fn BuildWindow

HandleEvents

  

You may also check:How to resolve the algorithm Fork step by step in the Raku programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the M4 programming language
You may also check:How to resolve the algorithm Factorial step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Comal programming language
You may also check:How to resolve the algorithm Same fringe step by step in the Wren programming language