nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
tests: add engine `nitg-sg` for --semi-global
[nit.git]
/
examples
/
calculator.nit
diff --git
a/examples/calculator.nit
b/examples/calculator.nit
index
071ea20
..
541f4d2
100644
(file)
--- a/
examples/calculator.nit
+++ b/
examples/calculator.nit
@@
-57,6
+57,7
@@
class CalculatorContext
fun switch_to_decimals
do
fun switch_to_decimals
do
+ if self.current == null then current = 0.0
if after_point != null then return
after_point = -1
if after_point != null then return
after_point = -1
@@
-96,6
+97,7
@@
class CalculatorGui
var lbl_disp : GtkLabel
var but_eq : GtkButton
var lbl_disp : GtkLabel
var but_eq : GtkButton
+ var but_dot : GtkButton
var context = new CalculatorContext
var context = new CalculatorContext
@@
-111,15
+113,17
@@
class CalculatorGui
if user_data isa Char then # is an operation
var c = user_data
if c == '.' then
if user_data isa Char then # is an operation
var c = user_data
if c == '.' then
+ but_dot.sensitive= false
context.switch_to_decimals
lbl_disp.text = "{context.current.to_i}."
else
context.switch_to_decimals
lbl_disp.text = "{context.current.to_i}."
else
+ but_dot.sensitive= true
context.push_op( c )
var s = context.result.to_precision_native(6)
var index : nullable Int = null
for i in s.length.times do
context.push_op( c )
var s = context.result.to_precision_native(6)
var index : nullable Int = null
for i in s.length.times do
- var chiffre = s[i]
+ var chiffre = s.chars[i]
if chiffre == '0' and index == null then
index = i
else if chiffre != '0' then
if chiffre == '0' and index == null then
index = i
else if chiffre != '0' then
@@
-128,14
+132,14
@@
class CalculatorGui
end
if index != null then
s = s.substring(0, index)
end
if index != null then
s = s.substring(0, index)
- if s[s.length-1] == ',' then s = s.substring(0, s.length-1)
+ if s.chars[s.length-1] == ',' then s = s.substring(0, s.length-1)
end
lbl_disp.text = s
end
else if user_data isa Int then # is a number
var n = user_data
context.push_digit( n )
end
lbl_disp.text = s
end
else if user_data isa Int then # is a number
var n = user_data
context.push_digit( n )
- lbl_disp.text = context.current.to_precision(after_point)
+ lbl_disp.text = context.current.to_precision_native(after_point)
end
end
end
end
@@
-178,7
+182,7
@@
class CalculatorGui
container.attach( but_eq, 4, 3, 1, 2 )
# .
container.attach( but_eq, 4, 3, 1, 2 )
# .
- var but_dot = new GtkButton.with_label( "." )
+ but_dot = new GtkButton.with_label( "." )
but_dot.request_size( 64, 64 )
but_dot.signal_connect( "clicked", self, '.' )
container.attach( but_dot, 1, 4, 1, 1 )
but_dot.request_size( 64, 64 )
but_dot.signal_connect( "clicked", self, '.' )
container.attach( but_dot, 1, 4, 1, 1 )