1 / 16

構造 (structure)

構造 (structure). データとデータの操作法をひとまとまりにしたもの オブジェクト指向プログラムで使うオブジェクトの簡単なものである. 構造を用いるプログラム例. グラフィックスの基本プログラムを作る 点をピクセルとよぶ ピクセルは x 座標と y 座標からなる ピクセル は structure で定義されている ピクセルを表す structure を posn と定義する. グラフィックスの例. (require htdp/draw) (start 500 500) (draw-circle (make-posn 100 100) 50 'black)

nedra
Download Presentation

構造 (structure)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 構造(structure) • データとデータの操作法をひとまとまりにしたもの • オブジェクト指向プログラムで使うオブジェクトの簡単なものである

  2. 構造を用いるプログラム例 グラフィックスの基本プログラムを作る • 点をピクセルとよぶ • ピクセルはx座標とy座標からなる • ピクセルはstructureで定義されている • ピクセルを表すstructureをposnと定義する

  3. グラフィックスの例 (require htdp/draw) (start 500 500) (draw-circle (make-posn 100 100) 50 'black) (sleep-for-a-while 1) (draw-solid-disk (make-posn 100 100) 50 'red) (sleep-for-a-while 1) (clear-solid-disk (make-posn 100 100) 50 'red) (draw-circle (make-posn 100 100) 50 'black) ;(sleep-for-a-while 1) (draw-solid-disk (make-posn 100 210) 50 'green) (sleep-for-a-while 1) (clear-solid-disk (make-posn 100 210) 50 'green) (draw-circle (make-posn 100 210) 50 'black) (draw-solid-disk (make-posn 100 100) 50 'red) ;; (stop)

  4. make-posn • make-posnでピクセルを作る • (make-posn a b)で • x座標がa • y座標がbのピクセルを作る • 構造posnが定義されたときに, • posnのフィールドをアクセスする関数posn-x, posn-y, • 構造かどうかを検査する関数posn?  が同時に定義される

  5. 構造posnにアクセスする関数 • (posn-x p)はピクセルpのx座標を返す • (posn-y p)はピクセルpのy座標を返す • (posn? obj)はobjがピクセルの時,true,さもなければ,falseを返す

  6. ピクセルと原点の距離を求める (define (distance-to-0 apixel) (sqrt (+ (square (posn-x apixel)) (square (posn-y apixel)))))

  7. 構造の定義:posnの例 • (define-struct posn (x y)) posnは構造(structure)の名前,x,yはフィールドの名前 • make-posnstructureposn を作成 • posn-x x座標を選択 • posn-y y座標を選択 • posn? posn構造を判定する

  8. distance-to-0の見直し (define (distance-to-0 apixel) (cond [(number? apixel) ] [(posn? apixel) ] ))

  9. distance-to-0の見直し(続き) (define (distance-to-0 obj) (cond [(number? obj) obj] [(posn? obj) (sqrt (+ (square (posn-x obj)) (square (posn-y obj)))) ))

  10. studentという構造を定義する • studentは次の3つのフィールドからなる構造とする • first 名 • last 姓 • teacher  姓 • (define-struct student (first last teacher))

  11. studentという構造を作る (define x (make-student 'Taro 'Suzuki 'Saito)) (define y (make-student 'Jiro 'Yamada 'Tanaka)) (student-teacher x) (student-last y)

  12. 構造を用いてプログラムをわかりやすくする • 次のプログラムは時:分を時間に変換するプログラムであった(define min-in-hour 60)(define (h:m-to-h hour min) (+ hour (/ min min-in-hour))) • mytimeというstructureを定義することにより,このプログラムをよりわかりやすくすることができる

  13. 構造 timeを用いる (define-struct mytime (hour minute)) (define (time->minutes atime) (+ (* (mytime-hour atime) 60) (mytime-minute atime) )) (define (minutes->time min) (make-mytime (quotient min 60) (remainder min 60)))

  14. 構造 timeを用いる (続き) (define (time-diff time1 time2) (minutes->time (- (time->minutes time1) (time->minutes time2)))) (define t1 (make-mytime 3 20)) (define t2 (make-mytime 0 40)) (time-diff t1 t2)

  15. 様々なデータが混在したプログラム データを判別する関数が必要 • number? • boolean? • symbol? • pというstructureに対して • p? p は(define-struct p (… ))で定義された構造 • struct? という構造自体を判別する関数もあるが,これは使わないでください(言語レベルで仕様が異なるため)

  16. データの種類の判別の例 (number? (make-posn 2 3)) (number? (+ 12 10)) (posn? 23) (posn? (make-posn 23 3))

More Related