From ff449cfd633dc975475f618c1fd949f2d2f1a51a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Sun, 3 Aug 2014 10:54:14 -0400 Subject: [PATCH 1/1] calculator: move to a project folder MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- .../src/calculator_gtk.nit} | 158 ++------------------ examples/calculator/src/calculator_logic.nit | 91 +++++++++++ examples/calculator/src/calculator_test.nit | 87 +++++++++++ 3 files changed, 189 insertions(+), 147 deletions(-) rename examples/{calculator.nit => calculator/src/calculator_gtk.nit} (50%) create mode 100644 examples/calculator/src/calculator_logic.nit create mode 100644 examples/calculator/src/calculator_test.nit diff --git a/examples/calculator.nit b/examples/calculator/src/calculator_gtk.nit similarity index 50% rename from examples/calculator.nit rename to examples/calculator/src/calculator_gtk.nit index 37792bd..d4edbdb 100644 --- a/examples/calculator.nit +++ b/examples/calculator/src/calculator_gtk.nit @@ -1,6 +1,6 @@ # This file is part of NIT ( http://www.nitlanguage.org ). # -# Copyright 2013 Alexis Laferrière +# Copyright 2013-2014 Alexis Laferrière # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,80 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import gtk - -class CalculatorContext - var result : nullable Float = null - - var last_op : nullable Char = null - - var current : nullable Float = null - var after_point : nullable Int = null - - fun push_op( op : Char ) - do - apply_last_op_if_any - if op == 'C' then - self.result = 0.0 - last_op = null - else - last_op = op # store for next push_op - end - - # prepare next current - after_point = null - current = null - end +# GTK calculator +module calculator_gtk - fun push_digit( digit : Int ) - do - var current = current - if current == null then current = 0.0 - - var after_point = after_point - if after_point == null then - current = current * 10.0 + digit.to_f - else - current = current + digit.to_f * 10.0.pow(after_point.to_f) - self.after_point -= 1 - end +import calculator_logic - self.current = current - end - - fun switch_to_decimals - do - if self.current == null then current = 0.0 - if after_point != null then return - - after_point = -1 - end - - fun apply_last_op_if_any - do - var op = last_op - - var result = result - if result == null then result = 0.0 - - var current = current - if current == null then current = 0.0 - - if op == null then - result = current - else if op == '+' then - result = result + current - else if op == '-' then - result = result - current - else if op == '/' then - result = result / current - else if op == '*' then - result = result * current - end - self.result = result - self.current = null - end -end +import gtk class CalculatorGui super GtkCallable @@ -104,12 +36,12 @@ class CalculatorGui redef fun signal( sender, user_data ) do var after_point = context.after_point - if after_point == null then + if after_point == null then after_point = 0 else after_point = (after_point.abs) end - + if user_data isa Char then # is an operation var c = user_data if c == '.' then @@ -119,7 +51,7 @@ class CalculatorGui 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 @@ -197,76 +129,8 @@ class CalculatorGui end end -# context tests -var context = new CalculatorContext -context.push_digit( 1 ) -context.push_digit( 2 ) -context.push_op( '+' ) -context.push_digit( 3 ) -context.push_op( '*' ) -context.push_digit( 2 ) -context.push_op( '=' ) -var r = context.result.to_precision( 2 ) -assert r == "30.00" else print r - -context = new CalculatorContext -context.push_digit( 1 ) -context.push_digit( 4 ) -context.switch_to_decimals -context.push_digit( 1 ) -context.push_op( '*' ) -context.push_digit( 3 ) -context.push_op( '=' ) -r = context.result.to_precision( 2 ) -assert r == "42.30" else print r - -context.push_op( '+' ) -context.push_digit( 1 ) -context.push_digit( 1 ) -context.push_op( '=' ) -r = context.result.to_precision( 2 ) -assert r == "53.30" else print r - -context = new CalculatorContext -context.push_digit( 4 ) -context.push_digit( 2 ) -context.switch_to_decimals -context.push_digit( 3 ) -context.push_op( '/' ) -context.push_digit( 3 ) -context.push_op( '=' ) -r = context.result.to_precision( 2 ) -assert r == "14.10" else print r - -#test multiple decimals -context = new CalculatorContext -context.push_digit( 5 ) -context.push_digit( 0 ) -context.switch_to_decimals -context.push_digit( 1 ) -context.push_digit( 2 ) -context.push_digit( 3 ) -context.push_op( '+' ) -context.push_digit( 1 ) -context.push_op( '=' ) -r = context.result.to_precision( 3 ) -assert r == "51.123" else print r - -#test 'C' button -context = new CalculatorContext -context.push_digit( 1 ) -context.push_digit( 0 ) -context.push_op( '+' ) -context.push_digit( 1 ) -context.push_digit( 0 ) -context.push_op( '=' ) -context.push_op( 'C' ) -r = context.result.to_precision( 1 ) -assert r == "0.0" else print r - # graphical application +if "NIT_TESTING".environ == "true" then exit 0 -if "NIT_TESTING".environ != "true" then - var app = new CalculatorGui - run_gtk -end +var app = new CalculatorGui +run_gtk diff --git a/examples/calculator/src/calculator_logic.nit b/examples/calculator/src/calculator_logic.nit new file mode 100644 index 0000000..9666d14 --- /dev/null +++ b/examples/calculator/src/calculator_logic.nit @@ -0,0 +1,91 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2013-2014 Alexis Laferrière +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Business logic of a calculator +module calculator_logic + +class CalculatorContext + var result : nullable Float = null + + var last_op : nullable Char = null + + var current : nullable Float = null + var after_point : nullable Int = null + + fun push_op( op : Char ) + do + apply_last_op_if_any + if op == 'C' then + self.result = 0.0 + last_op = null + else + last_op = op # store for next push_op + end + + # prepare next current + after_point = null + current = null + end + + fun push_digit( digit : Int ) + do + var current = current + if current == null then current = 0.0 + + var after_point = after_point + if after_point == null then + current = current * 10.0 + digit.to_f + else + current = current + digit.to_f * 10.0.pow(after_point.to_f) + self.after_point -= 1 + end + + self.current = current + end + + fun switch_to_decimals + do + if self.current == null then current = 0.0 + if after_point != null then return + + after_point = -1 + end + + fun apply_last_op_if_any + do + var op = last_op + + var result = result + if result == null then result = 0.0 + + var current = current + if current == null then current = 0.0 + + if op == null then + result = current + else if op == '+' then + result = result + current + else if op == '-' then + result = result - current + else if op == '/' then + result = result / current + else if op == '*' then + result = result * current + end + self.result = result + self.current = null + end +end diff --git a/examples/calculator/src/calculator_test.nit b/examples/calculator/src/calculator_test.nit new file mode 100644 index 0000000..2d47acb --- /dev/null +++ b/examples/calculator/src/calculator_test.nit @@ -0,0 +1,87 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2013-2014 Alexis Laferrière +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Test the business logic +module calculator_test + +import calculator_logic + +# context tests +var context = new CalculatorContext +context.push_digit( 1 ) +context.push_digit( 2 ) +context.push_op( '+' ) +context.push_digit( 3 ) +context.push_op( '*' ) +context.push_digit( 2 ) +context.push_op( '=' ) +var r = context.result.to_precision( 2 ) +assert r == "30.00" else print r + +context = new CalculatorContext +context.push_digit( 1 ) +context.push_digit( 4 ) +context.switch_to_decimals +context.push_digit( 1 ) +context.push_op( '*' ) +context.push_digit( 3 ) +context.push_op( '=' ) +r = context.result.to_precision( 2 ) +assert r == "42.30" else print r + +context.push_op( '+' ) +context.push_digit( 1 ) +context.push_digit( 1 ) +context.push_op( '=' ) +r = context.result.to_precision( 2 ) +assert r == "53.30" else print r + +context = new CalculatorContext +context.push_digit( 4 ) +context.push_digit( 2 ) +context.switch_to_decimals +context.push_digit( 3 ) +context.push_op( '/' ) +context.push_digit( 3 ) +context.push_op( '=' ) +r = context.result.to_precision( 2 ) +assert r == "14.10" else print r + +#test multiple decimals +context = new CalculatorContext +context.push_digit( 5 ) +context.push_digit( 0 ) +context.switch_to_decimals +context.push_digit( 1 ) +context.push_digit( 2 ) +context.push_digit( 3 ) +context.push_op( '+' ) +context.push_digit( 1 ) +context.push_op( '=' ) +r = context.result.to_precision( 3 ) +assert r == "51.123" else print r + +#test 'C' button +context = new CalculatorContext +context.push_digit( 1 ) +context.push_digit( 0 ) +context.push_op( '+' ) +context.push_digit( 1 ) +context.push_digit( 0 ) +context.push_op( '=' ) +context.push_op( 'C' ) +r = context.result.to_precision( 1 ) +assert r == "0.0" else print r -- 1.7.9.5