67 |
irasnyd |
1 |
;Written By: Ira Snyder
|
|
|
2 |
;Due Date: 02-16-2005
|
|
|
3 |
;Homework #: hw08
|
|
|
4 |
|
68 |
irasnyd |
5 |
;ASSUMPTIONS: I chose to use the print format specified in the table
|
|
|
6 |
; on the document that was handed out in class for hw08.
|
|
|
7 |
; The difference is in the spaces between the coefficient
|
|
|
8 |
; and the "x" that is printed. In the table there is a space
|
|
|
9 |
; between the coefficient and the "x", whereas in the example
|
|
|
10 |
; runs on the bottom of the page, there is not a space.
|
|
|
11 |
; The change to make it the same as the example runs is to delete
|
|
|
12 |
; the space between "~A" and "x" in the format statements on
|
|
|
13 |
; lines 111 and 112 (the last two lines in the cond of WRITE-NUM).
|
|
|
14 |
;
|
|
|
15 |
; Also, I was forced to assume that the last printed line on the
|
|
|
16 |
; the hw08 handout is incorrect, since the terms are unsorted
|
|
|
17 |
; and have un-combined coefficients with the same exponent.
|
|
|
18 |
; Mine is correct and combines terms with the same exponent
|
|
|
19 |
; and sorts the list from highest to lowest exponent.
|
|
|
20 |
;
|
|
|
21 |
|
67 |
irasnyd |
22 |
;;; Given a list in unsorted, unreduced Polynomial
|
|
|
23 |
;;; Normal Form, and returns a list in perfect PNF
|
|
|
24 |
(defun PNF (L)
|
|
|
25 |
(do ((L L (cdr L))
|
|
|
26 |
(RET nil))
|
|
|
27 |
((null L) RET)
|
|
|
28 |
(setf RET (INSERT (car L) RET))
|
|
|
29 |
(setf RET (DEL-ZERO-COEF RET))
|
|
|
30 |
)
|
|
|
31 |
)
|
|
|
32 |
|
|
|
33 |
;;; Given a pair of numbers (in a list) and a list of pairs
|
|
|
34 |
;;; this inserts the pair into the list in it's correct place
|
|
|
35 |
;;; including combining coefficients
|
|
|
36 |
(defun INSERT (PAIR LIST)
|
|
|
37 |
(cond
|
|
|
38 |
((null LIST) (cons PAIR LIST))
|
|
|
39 |
((> (cadr PAIR) (cadar LIST)) (cons PAIR LIST))
|
68 |
irasnyd |
40 |
((= (cadr PAIR) (cadar LIST)) (progn
|
|
|
41 |
(setf (caar LIST) (+ (car PAIR) (caar LIST)))
|
|
|
42 |
LIST
|
|
|
43 |
))
|
67 |
irasnyd |
44 |
(t (cons (car LIST ) (INSERT PAIR (cdr LIST))))
|
|
|
45 |
)
|
|
|
46 |
)
|
|
|
47 |
|
|
|
48 |
;;; Deletes all of the pairs that have zero coefficients in
|
|
|
49 |
;;; a PNF list
|
|
|
50 |
(defun DEL-ZERO-COEF (L)
|
|
|
51 |
(cond
|
|
|
52 |
((null L) nil)
|
|
|
53 |
((= 0 (caar L)) (DEL-ZERO-COEF (cdr L)))
|
|
|
54 |
(t (cons (car L) (DEL-ZERO-COEF (cdr L))))
|
|
|
55 |
)
|
|
|
56 |
)
|
|
|
57 |
|
|
|
58 |
;;; This takes a non-negative integer, and returns a string
|
|
|
59 |
;;; containing the number of spaces specified by the integer
|
|
|
60 |
(defun SPACES (N)
|
|
|
61 |
(do ((N N (1- N))
|
|
|
62 |
(STR (format nil "")))
|
|
|
63 |
((zerop N) STR)
|
|
|
64 |
(setf STR (concatenate 'string STR " "))
|
|
|
65 |
)
|
|
|
66 |
)
|
|
|
67 |
|
68 |
irasnyd |
68 |
;;; This checks if nil was passed. If it was, print a blank line, then
|
|
|
69 |
;;; a "0" line. Else pass the list off to the WRITE-POLY1 function.
|
67 |
irasnyd |
70 |
(defun WRITE-POLY (L)
|
68 |
irasnyd |
71 |
(cond
|
|
|
72 |
((null L) (format t "~%0~%"))
|
|
|
73 |
(t (WRITE-POLY1 L))
|
|
|
74 |
)
|
|
|
75 |
)
|
|
|
76 |
|
|
|
77 |
;;; Given a list in Polynomial Normal Form, this will print
|
|
|
78 |
;;; the human readable form of the list on two lines.
|
|
|
79 |
;;; Example:
|
|
|
80 |
;;; INPUT: ((1 1) (2 3) (-10 0) (3 2) (2 1))
|
|
|
81 |
;;; OUTPUT: 3 2
|
|
|
82 |
;;; OUTPUT: + 2 x + 3 x + 3 x - 10
|
|
|
83 |
(defun WRITE-POLY1 (L)
|
67 |
irasnyd |
84 |
(do ((L (PNF L) (cdr L))
|
|
|
85 |
(EXP (format nil ""))
|
|
|
86 |
(COEF (format nil "")))
|
|
|
87 |
((null L) (format t "~A~%~A~%" EXP COEF))
|
|
|
88 |
(setf COEF (concatenate 'string COEF (WRITE-NUM (caar L) (cadar L))))
|
|
|
89 |
(MAKE-EQUAL EXP COEF)
|
|
|
90 |
(setf EXP (concatenate 'string EXP (WRITE-EXP (caar L) (cadar L))))
|
|
|
91 |
(MAKE-EQUAL EXP COEF)
|
|
|
92 |
)
|
|
|
93 |
)
|
|
|
94 |
|
68 |
irasnyd |
95 |
;;; Given a coefficient and an exponent, output the correct expression
|
|
|
96 |
;;; to represent it.
|
|
|
97 |
;;; Examples:
|
|
|
98 |
;;; INPUT: 3 4
|
|
|
99 |
;;; OUTPUT: + 3 x
|
|
|
100 |
;;;
|
|
|
101 |
;;; INPUT: 3 0
|
|
|
102 |
;;; OUTPUT: + 3
|
|
|
103 |
;;;
|
|
|
104 |
;;; INPUT: -3 0
|
|
|
105 |
;;; OUTPUT: - 3
|
67 |
irasnyd |
106 |
(defun WRITE-NUM (NUM EXP)
|
|
|
107 |
(cond
|
68 |
irasnyd |
108 |
; don't output an x if we have a zero exponent
|
|
|
109 |
((zerop EXP) (if (plusp NUM) (format nil " + ~A" NUM)
|
|
|
110 |
(format nil " - ~A" (abs NUM))))
|
67 |
irasnyd |
111 |
((plusp NUM) (format nil " + ~A x" NUM))
|
|
|
112 |
(t (format nil " - ~A x" (abs NUM)))
|
|
|
113 |
)
|
|
|
114 |
)
|
|
|
115 |
|
68 |
irasnyd |
116 |
;;; Given a coefficient and an exponent, output the exponent.
|
|
|
117 |
;;; NOTE: I chose to have the syntax of WRITE-EXP match the syntax of WRITE-NUM
|
|
|
118 |
;;; even though I do not use the parameter NUM in the code. This was for
|
|
|
119 |
;;; uniformity reasons.
|
67 |
irasnyd |
120 |
(defun WRITE-EXP (NUM EXP)
|
|
|
121 |
(cond
|
|
|
122 |
((zerop EXP) nil)
|
|
|
123 |
((= EXP 1) nil)
|
|
|
124 |
(t (format nil "~A" EXP))
|
|
|
125 |
)
|
|
|
126 |
)
|
|
|
127 |
|
68 |
irasnyd |
128 |
;;; When called with two strings as arguments, this macro expands to
|
|
|
129 |
;;; make them the same length, regardless of which is longer.
|
67 |
irasnyd |
130 |
(defmacro MAKE-EQUAL (S1 S2)
|
|
|
131 |
`(cond
|
|
|
132 |
((> (length ,S1) (length ,S2)) (let ((DIFF (- (length ,S1) (length ,S2))))
|
|
|
133 |
(setf ,S2 (concatenate 'string ,S2 (SPACES DIFF)))))
|
|
|
134 |
((< (length ,S1) (length ,S2)) (let ((DIFF (- (length ,S2) (length ,S1))))
|
|
|
135 |
(setf ,S1 (concatenate 'string ,S1 (SPACES DIFF)))))
|
|
|
136 |
(t nil)
|
|
|
137 |
)
|
|
|
138 |
)
|
|
|
139 |
|
68 |
irasnyd |
140 |
|