Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main exposing (..)
  2.  
  3. import Browser
  4. import Html exposing (Html, text, div, h1, img, button, p)
  5. import Html.Attributes exposing (class)
  6. import Html.Events exposing (onClick)
  7.  
  8. ---- MODEL ----
  9.  
  10. type alias Model =
  11.     { operator1: Int
  12.     , operator2: Int
  13.     , operation: String
  14.     }
  15.  
  16. init : ( Model, Cmd Msg )
  17. init =
  18.     ( { operator1 = 0
  19.       , operator2 = 0
  20.       , operation = ""
  21.       }
  22.     , Cmd.none
  23.     )
  24.  
  25. getResult: Model -> String
  26. getResult model =
  27.     case model.operation of
  28.         ""  -> model.operator1 |> String.fromInt
  29.         "+" -> model.operator1 + model.operator2 |> String.fromInt
  30.         "-" -> model.operator1 - model.operator2 |> String.fromInt
  31.         "*" -> model.operator1 * model.operator2 |> String.fromInt
  32.         "/" -> if model.operator2 == 0 then
  33.                     "NaN"
  34.                else
  35.                     String.fromInt (model.operator1 // model.operator2)
  36.                     ++ " R" ++
  37.                     String.fromInt (model.operator1 - (model.operator1 // model.operator2) * model.operator2)
  38.  
  39.         _ -> "Unknown Operation: " ++ model.operation
  40.  
  41. ---- UPDATE ----
  42.  
  43. type Msg
  44.     = Number Int
  45.     | Delete
  46.     | Op String
  47.  
  48. update : Msg -> Model -> ( Model, Cmd Msg )
  49. update msg model =
  50.     case msg of
  51.         Number x ->
  52.             ( if model.operation == "" then
  53.                 { model | operator1 = model.operator1*10+x}
  54.               else
  55.                 { model | operator2 = model.operator2*10+x}
  56.             , Cmd.none
  57.             )
  58.  
  59.         Delete ->
  60.             ( if model.operator2 == 0 && model.operation == "" then
  61.                 { model | operator1 = model.operator1//10 }
  62.               else if model.operator2 == 0 then
  63.                 { model | operation = "" }
  64.               else
  65.                 { model | operator2 = model.operator2//10 }
  66.             , Cmd.none
  67.             )
  68.        
  69.         Op x ->
  70.             ( case model.operation of
  71.                 "" -> { model | operation = x }
  72.                 "/"-> { model
  73.                       | operator1 = model.operator1 // model.operator1
  74.                       , operator2 = 0
  75.                       , operation = x
  76.                       }
  77.                 _  -> { model
  78.                       | operator1 =
  79.                           getResult model
  80.                           |> String.toInt
  81.                           |> Maybe.withDefault 0
  82.                       , operator2 = 0
  83.                       , operation = x
  84.                       }
  85.             , Cmd.none
  86.             )
  87.  
  88. ---- VIEW ----
  89.  
  90. view : Model -> Html Msg
  91. view model =
  92.     div [ class "h-100" ]
  93.     [ div [ class "but-box h-100" ]
  94.         [ div [ class "output" ]
  95.             [ p [ class "calculation" ]
  96.                 [ text
  97.                     (
  98.                         if model.operation == "" then
  99.                             String.fromInt model.operator1
  100.                         else
  101.                             String.fromInt model.operator1 ++ model.operation ++ String.fromInt model.operator2
  102.                     )
  103.                 ]
  104.             , p [ class "result" ] [ text ("=" ++ getResult model) ]
  105.             ]
  106.         , button [ class "key", onClick (Number 1) ] [ text "1" ]
  107.         , button [ class "key", onClick (Number 2) ] [ text "2" ]
  108.         , button [ class "key", onClick (Number 3) ] [ text "3" ]
  109.         , button [ class "key-op", onClick (Op "+") ] [ text "➕" ]
  110.         , button [ class "key", onClick (Number 4) ] [ text "4" ]
  111.         , button [ class "key", onClick (Number 5) ] [ text "5" ]
  112.         , button [ class "key", onClick (Number 6) ] [ text "6" ]
  113.         , button [ class "key-op", onClick (Op "-") ] [ text "➖" ]
  114.         , button [ class "key", onClick (Number 7) ] [ text "7" ]
  115.         , button [ class "key", onClick (Number 8) ] [ text "8" ]
  116.         , button [ class "key", onClick (Number 9) ] [ text "9" ]
  117.         , button [ class "key-op", onClick (Op "*") ] [ p [ class "rot" ] [ text "➕" ] ]
  118.         , button [ class "key-double", onClick (Number 0) ] [ text "0" ]
  119.         , button [ class "key-del", onClick Delete ] [ text "⌫" ]
  120.         , button [ class "key-op", onClick (Op "/") ] [ text "➗" ]
  121.         ]
  122.     ]    
  123.  
  124. ---- PROGRAM ----
  125.  
  126. main : Program () Model Msg
  127. main =
  128.     Browser.element
  129.         { view = view
  130.         , init = \_ -> init
  131.         , update = update
  132.         , subscriptions = always Sub.none
  133.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement