...
 
Commits (1)
......@@ -3,6 +3,7 @@ module Main exposing (main)
import Browser
import Html exposing (Html, button, div, h1, text)
import Html.Attributes exposing (class)
import Html.Events exposing (onClick)
......@@ -10,26 +11,111 @@ import Html.Attributes exposing (class)
type alias Model =
{}
{ stack : List Float
, currentNum : Float
}
initialModel : Model
initialModel =
{}
{ stack = []
, currentNum = 0
}
type Msg
= NoOp
-- UPDATE
type Operator
= Div
| Mult
| Sub
| Add
-- UPDATE
operatorToString : Operator -> String
operatorToString operator =
case operator of
Div ->
"÷"
Mult ->
"×"
Sub ->
"-"
Add ->
"+"
operatorFunction : Operator -> (Float -> Float -> Float)
operatorFunction op =
case op of
Div ->
(/)
Mult ->
(*)
Sub ->
(-)
Add ->
(+)
type Msg
= InputOperator Operator
| InputNumber Float
| InputDecimal
| Clear
| ClearAll
| Back
| Enter
update : Msg -> Model -> Model
update msg model =
case msg of
NoOp ->
ClearAll ->
{ model | currentNum = 0, stack = [] }
Clear ->
{ model | currentNum = 0 }
Back ->
{ model | currentNum = toFloat <| floor <| model.currentNum / 10 }
Enter ->
{ model
| stack = model.currentNum :: model.stack
, currentNum = 0
}
InputOperator operator ->
case model.stack of
[] ->
model
x :: xs ->
let
op =
operatorFunction operator
newNum =
op model.currentNum x
in
{ model
| stack = xs
, currentNum = newNum
}
InputNumber num ->
{ model | currentNum = (model.currentNum * 10) + num }
InputDecimal ->
model
......@@ -75,12 +161,13 @@ sizeToString size =
"triple"
cell : Size -> Color -> String -> Html Msg
cell size color content =
cell : Html.Attribute Msg -> Size -> Color -> String -> Html Msg
cell attr size color content =
button
[ class <|
String.join " " <|
[ "cell", sizeToString size, colorToString color ]
, attr
]
[ text content ]
......@@ -88,25 +175,25 @@ cell size color content =
section : Html Msg
section =
div [ class "section" ]
[ cell Single Gray "←"
, cell Single Gray "C"
, cell Single Gray "CE"
, cell Single Yellow "÷"
, cell Single White "7"
, cell Single White "8"
, cell Single White "9"
, cell Single Yellow "×"
, cell Single White "4"
, cell Single White "5"
, cell Single White "6"
, cell Single Yellow "-"
, cell Single White "1"
, cell Single White "2"
, cell Single White "3"
, cell Single Yellow "+"
, cell Single White "0"
, cell Single White "."
, cell Double Yellow "Enter"
[ cell (onClick Back) Single Gray "←"
, cell (onClick Clear) Single Gray "C"
, cell (onClick ClearAll) Single Gray "CE"
, cell (onClick <| InputOperator Div) Single Yellow "÷"
, cell (onClick <| InputNumber 7) Single White "7"
, cell (onClick <| InputNumber 8) Single White "8"
, cell (onClick <| InputNumber 9) Single White "9"
, cell (onClick <| InputOperator Mult) Single Yellow "×"
, cell (onClick <| InputNumber 4) Single White "4"
, cell (onClick <| InputNumber 5) Single White "5"
, cell (onClick <| InputNumber 6) Single White "6"
, cell (onClick <| InputOperator Sub) Single Yellow "-"
, cell (onClick <| InputNumber 1) Single White "1"
, cell (onClick <| InputNumber 2) Single White "2"
, cell (onClick <| InputNumber 3) Single White "3"
, cell (onClick <| InputOperator Add) Single Yellow "+"
, cell (onClick <| InputNumber 0) Single White "0"
, cell (onClick InputDecimal) Single White "."
, cell (onClick <| Enter) Double Yellow "Enter"
]
......@@ -125,9 +212,12 @@ view model =
[ h1 [ class "h1" ] [ text "RPN Calculator" ]
, div
[ class "calculator" ]
[ inputBox 78.9
, section
]
(List.map inputBox (List.reverse model.stack)
++ [ inputBox model.currentNum
, section
]
)
, div [] [ text <| Debug.toString model ]
]
......