(load "gcdlcm.txt")
;
;
(print "assignment")
; Reba Orton
; Programming Assignment 2
; Due March 11, 1987
;
; This assignment is an partial implementation of rational numbers.
; Six funtions are created to make a rational number, change an 
; integer to a rational number, add and mutiply two rational numbers,
; reduce fractions (rational numbers) to simplist terms, and print.
;
;
; Makerat makes a rational number from two integers in a list 
; of the form (int . int), the 1st the numerator and 
; the 2nd the denominator, and reduces them into lowest terms.
;
(print "makerat")
(defun makerat (int-num int-denom)
   (cons int-num int-denom))
;
;           
; Inttorat changes a integer into a rational number simply by putting
; 1 in the denominator.
;  
(print "inttorat")
(defun inttorat (int)
   (makerat int 1))
;
;
; Addrat adds two rational numbers and reduces the resultant rational 
; number to lowest terms.
;
(print "addrat")
(defun addrat (rat1 rat2)
   (reducerat (makerat (+
                         (* (car (reducerat rat1))
                            (lcm (cdr (reducerat rat1))
                                 (cdr (reducerat rat2)))
                         )
                         (* (car (reducerat rat2))
                            (lcm (cdr (reducerat rat1)) 
                                 (cdr (reducerat rat2)))
                         )
                       )
                       (* (cdr (reducerat rat1)) 
                          (lcm (cdr (reducerat rat1))
                               (cdr (reducerat rat2)))
                       )
              )
   )
)
;
;
; Multrat multiplies two rational numbers and reduces the resultant 
; rational number to lowest terms.
;
(print "multrat")
(defun multrat (rat1 rat2)
   (reducerat (makerat (* (car (reducerat rat1)) (car (reducerat rat2)))
                    (* (cdr (reducerat rat1)) (cdr (reducerat rat2))))))
;
;
; Reducerat reduces a rational number to simplist terms by using
; external functions in the file gcdlcm.txt.
;
(print "reducerat")
(defun reducerat (rat)
   (cons  (/ (car rat) (gcd (car rat) (cdr rat)))
         (/ (cdr rat) (gcd (car rat) (cdr rat)))))
;
;
; Printrat formats a rational number into a fraction represented by 
; a list with the form (int / int).
;
(print "printrat")
(defun printrat (rat)     
   (print (list (car rat) (setq b '/) (cdr rat)) f1))
;
(print "testing begin")
;
;
;
; Test setup courtesy of W E B.
; Everything following a semi-colon is a comment.
; This comment extends to the end of the line.
;
;
; Make some set constants to use as test data.
;
(setq f1 (openo "testing.txt"))
; output file now open.  Write test results
; Don't do anything to change f1.  It belongs to the I/O system now.
;
; The labeling of output could be improved.  I leave that to each user.
;
(print "Reba Orton" f1) ; Change the string to your name.
;
(print "Programming assignment 2" f1)
;
(setq int-num '9999)
(setq int-denom '10000)
(setq rat1 (makerat int-num int-denom))
(setq rat2 (makerat 9000 int-denom))
(setq rat3 (makerat 3333 int-denom))
(setq rat4 (makerat 3000 int-denom))
(print "Data" f1)
(print "int-num - 9999 int-denom - 10000" f1)
(print "rat1 9999/10000, rat2 9000/10000" f1)
(print "rat3 3333/10000, rat4 3000/10000" f1)  
(print "" f1)
(print "testing makerat" f1)
(print int-num f1)
(print int-denom f1)
(print (makerat int-num int-denom) f1)
(print "" f1)
(print "testing inttorat" f1)
(print int-num f1)
(print (inttorat int-num) f1)
(print " " f1)
(print "testing reducerat" f1)
(print rat4 f1)
(print (reducerat rat4) f1)
(print " " f1)
(print "testing addrat" f1)
(print rat2 f1)
(print rat4 f1)
(print (addrat rat2 rat4) f1)
(print " " f1)
(print "testing multrat" f1)
(print rat2 f1)
(print rat4 f1)
(print (multrat rat2 rat4) f1) 
(print " " f1)
(print "testing printrat" f1)
(print rat3 f1)
(printrat rat3) 
(print " " f1)
(print "End of processing" f1)
;
; Processing of test cases is completed;  close the file.
(close f1)
; Don't use f1 after this.
; Leave Xlisp.
(print "Exiting xlisp.") ; This comes to the screen.
(exit)


1