1

Приведите пoжалуйста пример кода простейшего калькулятора с GUI на Racket'e.

1 ответ 1

3
#lang racket
(require racket/class racket/gui/base)
(define frame (new frame% [label "Calculator"]
                   [width 300]
                   [height 200]))
(define field1 (new text-field% [parent frame]
                    [label "Number 1:"]
                    [stretchable-width #f]))
(define operation (new choice% [parent frame]
                       [label "Operation"]
                       [choices '("+" "-" "*")]))
(define field2 (new text-field% [parent frame]
                    [label "Number 2:"]
                    [stretchable-width #f]))
(define result (new message% [parent frame]
                    [label ""]
                    [auto-resize #t]))
(new button% [parent frame]
     [label "Calculate"]
     (callback (lambda (button event)
                 (define n1 (string->number (send field1 get-value)))
                 (define n2 (string->number (send field2 get-value)))
                 (define op (send operation get-selection))
                 (define res (if (= op 0)
                                 (number->string (+ n1 n2))
                                 (if (= op 1)
                                     (number->string (- n1 n2))
                                     (number->string (* n1 n2)))))
                 (send result set-label res))))
(send frame show #t)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.