SICP ex2-1 modified rational arithmetic

经过十多天煎熬,本宝宝终于顺利进入第二章,傲娇脸

首先,本章章首简要回顾了上一章的基本内容以及总体概述了data abstraction 的原因重要性,以及介绍了本章将会学习的一些东西

2.1.1通过一个rational arithmetic的例子来引出data abstraction 并且介绍基础pair 语法

ex2-1要求修改书中所给的make-rat使之能够在给定参数为-时,将符号放在分子出输出,两种思路

一 修改gcd 使之能够处理负数

二 利用abs 将gcd 参数约束为正数,并保留原先符号,在结果出进行修正

我们由于比较懒,所以选择第二种方式

以下为代码

(define (Gcd a b)
	(define (reminder x y) (if (< x y) x (reminder (- x y) y)))
	(if (= b 0)  a  (Gcd b (reminder a b)))
)
(define (make-rat n d)
	(let ((g (Gcd (abs n) (abs d)))
		(symbol
			((lambda (a b)
				(cond ((< (* a b) 0) -1)
					((> (* a b) 0) 1)
					((= a 0) 0)))
				 n d)
		)
		)
		(cons (* symbol (abs (/ n g))) (abs (/ d g)))))

(define (rat+ x y)
	(/ (+ (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y))))
(define (rat* x y)
	(/ (* (car x) (car y)) (* (cdr x) (cdr y))))
(define (rat/ x y)
	(/ (* (car x) (cdr y)) (* (cdr x) (car y))))
(define (rat- x y)
	(/ (- (* (car x) (cdr y)) (* (car y) (cdr x))) (* (cdr x) (cdr y))))
(define (rat= x y)
	(= (* (car x) (cdr y)) (* (car y) (cdr x))))
(define (print-rat x)
	(newline)
	(display (car x))
	(display "/")
	(display (cdr x))
	0
)

时间: 2024-10-30 09:18:14

SICP ex2-1 modified rational arithmetic的相关文章

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi

pat1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Spe

PAT1088. Rational Arithmetic

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

A1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

A.1088 Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

PAT (Advanced Level) 1088. Rational Arithmetic (20)

简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algor

PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验细节处理,其它没啥好说的. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; long long numerato

Kattis之旅——Rational Arithmetic

Input The first line of input contains one integer, giving the number of operations to perform. Then follow the operations, one per line, each of the form x1 y1 op x2 y2. Here, ?109≤x1,y1,x2,y2<109 are integers, indicating that the operands are x1/y1