Subversion Repositories programming

Rev

Rev 69 | Details | Compare with Previous | Last modification | View Log | RSS feed

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