Utoljára aktív 1 hour ago

Calculator implementing Bogue

capitalex's Avatar capitalex gist felülvizsgálása 1 hour ago. Revízióhoz ugrás

1 file changed, 13 insertions, 22 deletions

main.ml

@@ -58,30 +58,21 @@ let calculator () =
58 58 update_display ()
59 59 | _ -> () in
60 60
61 - let op_button name op =
61 + let make_button name action =
62 62 let button = W.button name in
63 - W.on_click ~click:(op_clicked op) button ;
63 + W.on_click ~click:action button ;
64 64 button in
65 65
66 - let push_button () =
67 - let button = W.button "PSH" in
68 - W.on_click ~click:push_clicked button ;
69 - button in
70 -
71 - let swap_button () =
72 - let button = W.button "SWP" in
73 - W.on_click ~click:swap_clicked button ;
74 - button in
66 + let op_button name op = make_button name @@ op_clicked op in
67 + let push_button = make_button "PSH" push_clicked in
68 + let swap_button = make_button "SWP" swap_clicked in
69 + let pop_button = make_button "POP" pop_clicked in
70 + let digit_button digit = make_button digit @@ digit_clicked digit in
75 71
76 - let pop_button () =
77 - let button = W.button "POP" in
78 - W.on_click ~click:pop_clicked button ;
79 - button in
80 -
81 - let digit_button digit =
82 - let button = W.button digit in
83 - W.on_click ~click:(digit_clicked digit) button ;
84 - button in
72 + let (//) x y =
73 + match x, y with
74 + | (_, 0) -> 0
75 + | (x, y) -> x / y in
85 76
86 77 let layout = L.tower ~align:Draw.Center [
87 78 L.resident ~w:400 display ;
@@ -89,8 +80,8 @@ let calculator () =
89 80 L.flat_of_w [ digit_button "4" ; digit_button "5" ; digit_button "6" ] ;
90 81 L.flat_of_w [ digit_button "7" ; digit_button "8" ; digit_button "9" ] ;
91 82 L.flat_of_w [ digit_button "0" ] ;
92 - L.flat_of_w [ push_button () ; swap_button () ; pop_button () ] ;
93 - L.flat_of_w [ op_button "ADD" (+) ; op_button "MUL" ( * ) ; op_button "SUB" (-) ; op_button "DIV" (/) ] ;
83 + L.flat_of_w [ push_button; swap_button; pop_button] ;
84 + L.flat_of_w [ op_button "ADD" (+) ; op_button "MUL" ( * ) ; op_button "SUB" (-) ; op_button "DIV" (//) ] ;
94 85 ] in
95 86
96 87 let calculator = Bogue.make [] [layout] in

capitalex's Avatar capitalex gist felülvizsgálása 12 hours ago. Revízióhoz ugrás

1 file changed, 2 insertions, 2 deletions

main.ml

@@ -27,7 +27,7 @@ let calculator () =
27 27
28 28 let swap_clicked _ =
29 29 match Stack.length stack with
30 - | 2 ->
30 + | n when n >= 2 ->
31 31 let y = Stack.pop stack in
32 32 let x = Stack.pop stack in
33 33 Stack.push y stack ;
@@ -51,7 +51,7 @@ let calculator () =
51 51
52 52 let op_clicked op = fun _ ->
53 53 match Stack.length stack with
54 - | 2 ->
54 + | n when n >= 2 ->
55 55 let y = Stack.pop stack |> int_of_string in
56 56 let x = Stack.pop stack |> int_of_string in
57 57 Stack.push (op x y |> string_of_int) stack ;

capitalex's Avatar capitalex gist felülvizsgálása 13 hours ago. Revízióhoz ugrás

1 file changed, 102 insertions

main.ml(fájl létrehozva)

@@ -0,0 +1,102 @@
1 + open Bogue
2 +
3 + module W = Widget
4 + module L = Layout
5 +
6 +
7 + let calculator () =
8 + let stack = Stack.create () in
9 + Stack.push "0" stack ;
10 +
11 + let stack_show () =
12 + Stack.fold (fun acc number -> number ^ " " ^ acc) "" stack in
13 +
14 + let display = W.label "" in
15 + let update_display () =
16 + W.set_text display (stack_show ()) in
17 +
18 + let digit_pressed digit =
19 + match Stack.pop stack with
20 + | "0" -> Stack.push digit stack ;
21 + | number -> Stack.push (number ^ digit) stack ;
22 + update_display () in
23 +
24 + let push_clicked _ =
25 + Stack.push "0" stack ;
26 + update_display () in
27 +
28 + let swap_clicked _ =
29 + match Stack.length stack with
30 + | 2 ->
31 + let y = Stack.pop stack in
32 + let x = Stack.pop stack in
33 + Stack.push y stack ;
34 + Stack.push x stack ;
35 + update_display ()
36 + | _ -> () in
37 +
38 + let pop_clicked _ =
39 + match Stack.length stack with
40 + | 1 ->
41 + Stack.pop stack |> ignore ;
42 + Stack.push "0" stack ;
43 + update_display ()
44 + | _ ->
45 + Stack.pop stack |> ignore ;
46 + update_display () in
47 +
48 + let digit_clicked digit = fun _ ->
49 + digit_pressed digit ;
50 + update_display () in
51 +
52 + let op_clicked op = fun _ ->
53 + match Stack.length stack with
54 + | 2 ->
55 + let y = Stack.pop stack |> int_of_string in
56 + let x = Stack.pop stack |> int_of_string in
57 + Stack.push (op x y |> string_of_int) stack ;
58 + update_display ()
59 + | _ -> () in
60 +
61 + let op_button name op =
62 + let button = W.button name in
63 + W.on_click ~click:(op_clicked op) button ;
64 + button in
65 +
66 + let push_button () =
67 + let button = W.button "PSH" in
68 + W.on_click ~click:push_clicked button ;
69 + button in
70 +
71 + let swap_button () =
72 + let button = W.button "SWP" in
73 + W.on_click ~click:swap_clicked button ;
74 + button in
75 +
76 + let pop_button () =
77 + let button = W.button "POP" in
78 + W.on_click ~click:pop_clicked button ;
79 + button in
80 +
81 + let digit_button digit =
82 + let button = W.button digit in
83 + W.on_click ~click:(digit_clicked digit) button ;
84 + button in
85 +
86 + let layout = L.tower ~align:Draw.Center [
87 + L.resident ~w:400 display ;
88 + L.flat_of_w [ digit_button "1" ; digit_button "2" ; digit_button "3" ] ;
89 + L.flat_of_w [ digit_button "4" ; digit_button "5" ; digit_button "6" ] ;
90 + L.flat_of_w [ digit_button "7" ; digit_button "8" ; digit_button "9" ] ;
91 + L.flat_of_w [ digit_button "0" ] ;
92 + L.flat_of_w [ push_button () ; swap_button () ; pop_button () ] ;
93 + L.flat_of_w [ op_button "ADD" (+) ; op_button "MUL" ( * ) ; op_button "SUB" (-) ; op_button "DIV" (/) ] ;
94 + ] in
95 +
96 + let calculator = Bogue.make [] [layout] in
97 + update_display () ;
98 + Bogue.run calculator ;;
99 +
100 + let _ =
101 + calculator () ;
102 + Draw.quit()
Újabb Régebbi