“ lisp

lang racket

;: 练习2.3 定义矩形

;: 定义点
(define (make-point x y)
(cons x y))
(define (x-point point)
(car point))
(define (y-point point)
(cdr point))

(define (print-point p)
(newline)
(display “(“)
(display (x-point p))
(display “,”)
(display (y-point p))
(display “)”))

;: 计算给定矩型的周长和面积
;: 2 * (宽+高)
(define (perimeter-rect rect)
(* 2 (+ (width-rect rect) (height-rect rect))))
;: 宽*高
(define (area-rect rect)
(* (width-rect rect) (height-rect rect)))

;: 第一种表示法：用两个顶点表示,左上顶点，右下顶点
(define (make-rect pt0 pt1) (cons pt0 pt1))
(define (left-point rect) (car rect))
(define (right-point rect) (cdr rect))

;: 获取矩形的宽
(define (width-rect rect)
(let ((left (left-point rect))
(right (right-point rect)))
(- (x-point right) (x-point left))))

;: 获取矩形的高
(define (height-rect rect)
(let ((left (left-point rect))
(right (right-point rect)))
(- (y-point right) (y-point left))))

;: 制造一个矩型
(define rect (make-rect (make-point 1 2)
(make-point 3 4)))

;: 测试
rect
(area-rect rect)
(perimeter-rect rect)
 lisp

lang racket

;: 练习2.3 定义矩形
;: 平面上线段的表示问题
(define (make-segment start-point end-point)
(cons start-point end-point))

(define (start-segment seg)
(car seg))
(define (end-segment seg)
(cdr seg))

;: 定义点
(define (make-point x y)
(cons x y))
(define (x-point point)
(car point))
(define (y-point point)
(cdr point))

;: 计算给定矩型的周长和面积
;: 2 * (宽+高)
(define (perimeter-rect rect)
(* 2 (+ (width-rect rect) (height-rect rect))))
;: 宽*高
(define (area-rect rect)
(* (width-rect rect) (height-rect rect)))

;: 第二种表示法：用两条相互垂真的线段表示
(define (make-rect line01 line02) (cons line01 line02))

;: 获取矩形的宽
(define (width-rect rect)
(let ((line (car rect)))
(abs (- (x-point (start-segment line)) (x-point (end-segment line))))))

;: 获取矩形的高
(define (height-rect rect)
(let ((line (cdr rect)))
(abs (- (y-point (start-segment line)) (y-point (end-segment line))))))

;: 制造一个矩型
(define rect (make-rect (make-segment (make-point 1 4) (make-point 3 4))
(make-segment (make-point 1 2) (make-point 1 4))
))

;: 测试
(area-rect rect)
(perimeter-rect rect)



`

练习5 面向对象基础（含答案）

2015-12-04 08:31:40

【JAVA笔记】习题：尝试编写一个矩形类，将长与宽作为矩形类的属性，在构造方法中将长，宽初始化，定义一个成员方法求此矩形的面积。

2017-04-25 18:26:02

Java实验一：编写Rectangle(矩形)类......

2015-06-16 08:21:16

基础练习 矩形面积交

2015-03-11 12:42:13

蓝桥杯 BASIC-18 基础练习 矩形面积交

2016-07-22 00:32:22

类的概念矩形类

2018年01月14日 1KB 下载

JAVA蓝桥杯矩形面积交

2017-02-12 11:23:55

java学习之路 之 基本语法-方法（函数）及练习题

2016-07-25 22:30:42

python编程：从入门到实践-第二章练习

2018-03-09 01:04:05

《算法导论》第二章-第2节_练习（参考答案）

2017-07-18 15:51:07