1+ ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2+ ; 作者:evilbinary on 11/19/16.
3+ ; 邮箱:rootdebug@163.com
4+ ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5+ (import (scheme)
6+ (glfw glfw)
7+ (gui duck)
8+ (gui draw)
9+ (gui window)
10+ (gui widget))
11+ (define (infix-prefix lst )
12+ (if (list? lst)
13+ (if (null? (cdr lst))
14+ (car lst)
15+ (list (cadr lst)
16+ (infix-prefix (car lst))
17+ (infix-prefix (cddr lst))))
18+ lst))
19+ (define exp-result 0)
20+ (define exp "")
21+ (define clear #t)
22+
23+ (define (app-calc )
24+ (set! window (window-create 600 420 " 鸭子gui" ))
25+ (let ((d (dialog 40.0 20.0 250.0 380.0 " 计算器" ))
26+ (result (button 224.0 60.0 " " ))
27+ (cls (button 108.0 50.0 " 清除" ))
28+ (percent (button 50.0 50.0 " % " ))
29+ (div (button 50.0 50.0 " / " ))
30+ (num7 (button 50.0 50.0 " 7" ))
31+ (num8 (button 50.0 50.0 " 8" ))
32+ (num9 (button 50.0 50.0 " 9" ))
33+ (num6 (button 50.0 50.0 " 6" ))
34+ (num5 (button 50.0 50.0 " 5" ))
35+ (num4 (button 50.0 50.0 " 4" ))
36+ (num3 (button 50.0 50.0 " 3" ))
37+ (num2 (button 50.0 50.0 " 2" ))
38+ (num1 (button 50.0 50.0 " 1" ))
39+ (num0 (button 108.0 50.0 " 0" ))
40+ (mul (button 50.0 50.0 " * " ))
41+ (sub (button 50.0 50.0 " - " ))
42+ (add (button 50.0 50.0 " + " ))
43+ (ret (button 50.0 50.0 " = " ))
44+ (dot (button 50.0 50.0 " ." )))
45+ (widget-set-attrs sub 'background #xfff79231 )
46+ (widget-set-attrs mul 'background #xfff79231 )
47+ (widget-set-attrs add 'background #xfff79231 )
48+ (widget-set-attrs ret 'background #xfff79231 )
49+ (widget-set-attrs div 'background #xfff79231 )
50+ (let loop ((btn (list result
51+ cls percent div
52+ num7 num8 num9 mul
53+ num4 num5 num6 sub
54+ num1 num2 num3 add
55+ num0 dot ret)))
56+ (if (pair? btn)
57+ (begin
58+ (widget-set-attrs (car btn) 'text-align 'center )
59+ (widget-set-margin (car btn) 4.0 4.0 4.0 4.0 )
60+ (widget-set-attrs (car btn) 'font-size 24.0 )
61+ (widget-set-events
62+ (car btn)
63+ 'click
64+ (lambda (widget p type data )
65+ (let ((text (widget-get-attr widget %text)))
66+ (case text
67+ [" = "
68+ (if (and (> (string-length exp) 0 ) clear)
69+ (begin
70+ (printf " exp:~a\n " (format " (~a)" exp) )
71+ (set! exp-result
72+ (eval (infix-prefix
73+ (read (open-input-string
74+ (format " ( ~a )" exp))))))
75+ (set! exp (format " ~a" exp-result ))
76+ (widget-set-attr result %text exp)
77+ (set! clear #f ))
78+ )]
79+ [" 清除"
80+ (set! exp " " )
81+ (set! exp-result " " )
82+ (set! clear #t )
83+ (widget-set-attr result %text exp)
84+ ]
85+ [else
86+ (set! exp (string-append exp (format " ~a" text) ))
87+ (widget-set-attr result %text exp)
88+ ])
89+
90+ )))
91+ (widget-add d (car btn))
92+ (loop (cdr btn))
93+ )))
94+ (widget-set-attrs result 'text-align 'left )
95+ (widget-set-attrs result 'background #x66cccccc )
96+ (widget-set-attrs result 'font-size 50.0 )
97+ )
98+ (window-loop window)
99+ (window-destroy window))
100+ (app-calc)
0 commit comments