Subversion Repositories programming

Rev

Go to most recent revision | Details | 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
4
 
5
;;; Given a list in unsorted, unreduced Polynomial
6
;;; Normal Form, and returns a list in perfect PNF
7
(defun PNF (L)
8
  (do ((L L (cdr L))
9
       (RET nil))
10
      ((null L) RET)
11
    (setf RET (INSERT (car L) RET))
12
    (setf RET (DEL-ZERO-COEF RET))
13
  )
14
)
15
 
16
;;; Given a pair of numbers (in a list) and a list of pairs
17
;;; this inserts the pair into the list in it's correct place
18
;;; including combining coefficients
19
(defun INSERT (PAIR LIST)
20
  (cond
21
    ((null LIST)                  (cons PAIR LIST))
22
    ((> (cadr PAIR) (cadar LIST)) (cons PAIR LIST))
23
    ((= (cadr PAIR) (cadar LIST)) (setf (caar LIST) (+ (car PAIR) (caar LIST))) LIST)
24
    (t                            (cons (car LIST ) (INSERT PAIR (cdr LIST))))
25
  )
26
)
27
 
28
;;; Deletes all of the pairs that have zero coefficients in
29
;;; a PNF list
30
(defun DEL-ZERO-COEF (L)
31
  (cond
32
    ((null L) nil)
33
    ((= 0 (caar L)) (DEL-ZERO-COEF (cdr L)))
34
    (t              (cons (car L) (DEL-ZERO-COEF (cdr L))))
35
  )
36
)
37
 
38
;;; This takes a non-negative integer, and returns a string
39
;;; containing the number of spaces specified by the integer
40
(defun SPACES (N)
41
  (do ((N N (1- N))
42
       (STR (format nil "")))
43
      ((zerop N) STR)
44
    (setf STR (concatenate 'string STR " "))
45
  )
46
)
47
 
48
(defun WRITE-POLY (L)
49
  (do ((L (PNF L) (cdr L))
50
       (EXP  (format nil ""))
51
       (COEF (format nil "")))
52
      ((null L) (format t "~A~%~A~%" EXP COEF))
53
    (setf COEF (concatenate 'string COEF (WRITE-NUM (caar L) (cadar L))))
54
    (MAKE-EQUAL EXP COEF)
55
    (setf EXP  (concatenate 'string EXP  (WRITE-EXP (caar L) (cadar L))))
56
    (MAKE-EQUAL EXP COEF)
57
  )
58
)
59
 
60
(defun WRITE-NUM (NUM EXP)
61
  (cond
62
    ((zerop EXP) (if (plusp NUM) (format nil " + ~A" NUM) (format nil " - ~A" (abs NUM))))
63
    ((plusp NUM) (format nil " + ~A x" NUM))
64
    (t           (format nil " - ~A x" (abs NUM)))
65
  )
66
)
67
 
68
(defun WRITE-EXP (NUM EXP)
69
  (cond
70
    ((zerop EXP) nil)
71
    ((= EXP 1)   nil)
72
    (t           (format nil "~A" EXP))
73
  )
74
)
75
 
76
;;; When called with two strings as arguments, this macro
77
;;; expands to make them the same length.
78
(defmacro MAKE-EQUAL (S1 S2)
79
  `(cond
80
     ((> (length ,S1) (length ,S2)) (let ((DIFF (- (length ,S1) (length ,S2))))
81
                                    (setf ,S2 (concatenate 'string ,S2 (SPACES DIFF)))))
82
     ((< (length ,S1) (length ,S2)) (let ((DIFF (- (length ,S2) (length ,S1))))
83
                                    (setf ,S1 (concatenate 'string ,S1 (SPACES DIFF)))))
84
     (t                             nil)
85
   )
86
)
87