Subversion Repositories programming

Rev

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

Rev Author Line No. Line
67 irasnyd 1
;Written By: Ira Snyder
2
;Due Date:   02-16-2005
3
;Homework #: hw08
108 ira 4
;License: Public Domain
67 irasnyd 5
 
68 irasnyd 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
 
67 irasnyd 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))
68 irasnyd 41
    ((= (cadr PAIR) (cadar LIST)) (progn
42
                                    (setf (caar LIST) (+ (car PAIR) (caar LIST)))
43
                                    LIST
44
                                  ))
67 irasnyd 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
 
68 irasnyd 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.
67 irasnyd 71
(defun WRITE-POLY (L)
68 irasnyd 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)
67 irasnyd 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
 
68 irasnyd 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
67 irasnyd 107
(defun WRITE-NUM (NUM EXP)
108
  (cond
68 irasnyd 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))))
67 irasnyd 112
    ((plusp NUM) (format nil " + ~A x" NUM))
113
    (t           (format nil " - ~A x" (abs NUM)))
114
  )
115
)
116
 
68 irasnyd 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.
67 irasnyd 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
 
68 irasnyd 129
;;; When called with two strings as arguments, this macro expands to
130
;;; make them the same length, regardless of which is longer.
67 irasnyd 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
 
68 irasnyd 141