;;change.lsp
;;aventari CS351 4-25-2001 3:47 AM;;change() is a recursive function to give the best change for a given amount.
;; "best change" is defined as the smallest number of coins.
;; you pass in the amount to make change for, and a list of the denominations available
;; to make change out of.
(defun change(input list)
(cond
((eql nil list) nil)
((= input input) (cons (divide input (car list)) (change (mod input (car list)) (cdr list))))
)
)
;;main1() of course is the starting function, it sets the coin types, then prints messages to the user
;; main1 reads the user input and calls the correct functions to make the change. It has some error
;; handling ability as well if the user enters a bad numeric value
(defun main1()
(setf coins '(50 25 10 5 1))
(format t "~\%~\% This program will make change for you.~\% press 1 to enter a bill amount greater than one (no 2 dollar bills)~\% press 2 to enter a coin amount or 1 dollar~\% : ")
(setf x (read))
(cond
((= x 2) (format t "~\% input value of coinage to make change for (in cents, enter 100 for dollar bill) :") (setf x (read)) (format t "~\%Best change is on top~\%")(output(change x coins)) (variations x (change x coins) coins 0))
((= x 1) (format t "~\% input value of bill(s) to make change for (in dollars) :") (setf x (read)) (format t "~\%Best change is on top~\%") (output(change (* 100 x) coins)) (variations (* 100 x) (change (* 100 x) coins) coins 0))
((not (= x 2)) (format t "~\%~\% input error.. enter a 1 or 2") )
((not (= x 1)) (format t "~\%~\% input error.. enter a 1 or 2") )
)
)
;;variations() is my function that will make 9 different change combinations for a given total.
;; it takes a lot of arguments, the first is the total amount we are making change for, the
;; second is the "best change" list of coins, the 3rd is the list of coins we are making
;; use of, and the 4th is a counter so we will only make 9 more combinations. The counter steps
;; in increments of 5 because it is used in the calculation of different change.
;;The coins list in here is wierd because it is used to make the change function give all
;; pennys for a given input instead of the 'best change' which is usually does.
(defun variations(total input list counter)
(setq x (car(cdr(cdr(cdr(cdr input))))) )
(setq pennys (+ x (+ counter 1)) )
(setf coins '(1000 1000 1000 1000 1))
(cond
((<= counter 40)
(cond
((>= (- total pennys) 0) (output (addLists (change (- total pennys) list) (change (+ x (+ counter 1)) coins)) ) (variations total input list (+ counter 5)) )
((< (- total pennys) 0) nil)
)
)
)
)
;;output() is a simple function that outputs a list of coins to the user in a pretty format
(defun output (money)
(format t "~a 50('s), ~a quarter(s), ~a dime(s), ~a nickel(s), and ~a penny(s)~\%"
(car money) (car(cdr money)) (car(cdr(cdr money))) (car(cdr(cdr(cdr money)))) (car(cdr(cdr(cdr(cdr money)))))
)
)
;;recursive function that will add the individual elements of 2 lists together into 1 list
(defun addLists(list1 list2)
(cond
((atom list1) nil)
((atom list2) nil)
((= 1 1) (cons (+ (car list1) (car list2)) (addLists (cdr list1) (cdr list2)) ) )
)
)
;;recursive divide function that truncates remainder instead of giving fraction or floating point
(defun divide (in divis)
(if (< (- in divis) 0) 0
(+ (divide (- in divis) divis) 1)
)
)
;;the end!