-
-
Save bencz/91a2987b8d7b479e04e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define system. | |
#define extensions. | |
#class MyNumber | |
{ | |
#field theValue. | |
#constructor new : aValue | |
[ | |
theValue := aValue. | |
] | |
#method add : aValue | |
[ | |
#var x := theValue. | |
#var y := aValue. | |
#loop (y != 0)? | |
[ | |
#var carry := x && y. | |
x := x ^^ y. | |
y := carry shift &index:-1. | |
]. | |
^ MyNumber new:x. | |
] | |
#method not | |
[ | |
#var temp := MyNumber new:(theValue not). | |
^ MyNumber new:(temp + 1). | |
] | |
#method subtract : aValue | |
[ | |
#var x := $self. | |
#var y := MyNumber new:(aValue) not. | |
^x + y. | |
] | |
// Impar | |
#method isOdd | |
[ | |
#var result := (theValue && 1). | |
^result. | |
] | |
// Par | |
#method isEven | |
[ | |
#var result := MyNumber new:((theValue && 1)) not + 1. | |
^(result value). | |
] | |
#method multiplyByTwo | |
[ | |
^(theValue shift &index:-1). | |
] | |
#method multiply : aValue | |
[ | |
#var x := theValue. | |
#var y := aValue. | |
#var product := 0. | |
^product. | |
] | |
#method divideByTwo | |
[ | |
^(theValue shift &index:1). | |
] | |
#method divide : aValue | |
[ | |
#var divisor := aValue. | |
#var dividendo := theValue. | |
#var temp := 1. | |
#var result := 0. | |
#var quociente := MyNumber new:(0). | |
#var dividendoTemp := MyNumber new:(dividendo). | |
#loop (divisor <= dividendo)? | |
[ | |
divisor := divisor shift &index:-1. | |
temp := temp shift &index:-1. | |
]. | |
#loop (temp > 1)? | |
[ | |
divisor := divisor shift &index:1. | |
temp := temp shift &index:1. | |
(dividendoTemp >= divisor)? | |
[ | |
dividendoTemp := dividendoTemp - divisor. | |
quociente := quociente + temp. | |
]. | |
]. | |
^quociente. | |
] | |
#method value = theValue. | |
#method => theValue. | |
} | |
#symbol Program = | |
[ | |
consoleEx writeLine:"Sum............: 20+20=":(MyNumber new:20 + 20). | |
consoleEx writeLine:"Subtract.......: 30-30=":(MyNumber new:30 - 30). | |
consoleEx writeLine:"Not............: ~10=":((MyNumber new:10) not). | |
consoleEx writeLine:"Division.......: 36/06=":(MyNumber new:36 / 6). | |
consoleEx writeLine:"Multiply.......: -2*-2=":(MyNumber new:-2*-2). | |
consoleEx writeLine:"Multiply.......: 08*04=":(MyNumber new:8*4). | |
consoleEx writeLine:"". | |
consoleEx writeLine:"7 is odd.......: ":((MyNumber new:7) isOdd). | |
consoleEx writeLine:"2 is odd.......: ":((MyNumber new:2) isOdd). | |
consoleEx writeLine:"8 is even......: ":((MyNumber new:8) isEven). | |
consoleEx writeLine:"7 is even......: ":((MyNumber new:7) isEven). | |
consoleEx writeLine:"". | |
consoleEx writeLine:"Multiply by 2: 48*2 = ":((MyNumber new:48) multiplyByTwo). // 96 | |
consoleEx writeLine:"Devide by 2..: 36/2 = ":((MyNumber new:36) divideByTwo). // 18 | |
consoleEx readChar. | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment