entet-examples
(require :cl-ppcre)
(use-package :cl-ppcre)
(defun recupfile (file)
"Reads the file line by line;ignores blank lines and puts the results in the data variable"
(let ((data nil) (i 0))
(with-open-file (stream file)
(do ((line (read-line stream nil) (read-line stream nil)))
((null line))
(incf i)
(cond ((not (equal "" line))
(push (cl-ppcre:split "\\s+" line) data))
(t nil))))
data))
(defun repchar (flux c nb)
"repetition of character c nb times"
(format flux "~v@{~A~:*~}" nb c))
(defun dupli (lst nb &optional (ini nb))
"Repeat nb times the character which is in the list lst in a list"
(cond ((eql lst nil) nil)
((<= nb 0) (dupli (cdr lst) ini ini))
(t (cons (car lst) (dupli lst (1- nb) ini )))))
(defun dataform (fl data)
"Shaping the raw data"
(loop for i from 0 to (1- (length data)) do
(format fl " ~{~a | ~} ~%~{~a~}~%" (nth i data) (dupli '(-) 68))))
(defun entete (flux)
(let((nomdate)(cote))
(format t "Enter first name, last name and date in the form dd-mm-yy")
(setf nomdate(read-line))
(terpri)
(setf cote (/ (- 68 (length nomdate)) 2))
(repchar flux "=" 68)
(terpri flux)
(format flux "~v@{~A~:*~}" cote " ")
(format flux "~a~%" nomdate)
(repchar flux "=" 68)
(terpri flux)
(repchar flux " " 10)
(format flux "| syst | diast | puls | H | mn | day | month | ~%")
(repchar flux "=" 68)
(terpri flux)))
(defun calcmL (k lst)
"Average of the values recorded at the awakening"
(if (= k 0) (format t "k should not be null")
(progn
(let ((sum 0) (lg (length lst)) (cpt 0))
(do ((i 0 (1+ i)))
((= i lg))
(cond ((equal (nth 0 (nth i lst)) "L")
(incf cpt)
(setf sum (+ sum (parse-integer (nth k (nth i lst))))))
(t nil)))
(setf sum (/ sum cpt))
sum))))
(defun calcmC (k lst)
"Average of the values recorded at the bedtime"
(if (= k 0) (format t "k should not be null")
(progn
(let ((sum 0) (lg (length lst)) (cpt 0))
(do ((i 0 (1+ i )))
((= i lg))
(cond ((equal (nth 0 (nth i lst)) "C")
(incf cpt)
(setf sum (+ sum (parse-integer (nth k (nth i lst))))))
(t nil )))
(setf sum (/ sum cpt))
sum))))
(defun calcm (k lst)
"Overall average"
(let ((sum 0) (lg (length lst)))
(do ((i 0 (1+ i)))
((= i lg))
(setf sum (+ sum (parse-integer (nth k (nth i lst))))))
(setf sum (/ sum lg))
sum))
(defun displaymoy (flux mot &optional cmoy)
"Shaping the display of the averages"
(repchar flux " " 1)
(cond ((equal cmoy "C")
(format flux "~{| ~,1f | ~,1f | ~,1f | ~}" `(,(calcmC 1 mot) ,(calcmC 2 mot) ,(calcmC 3 mot))))
((equal cmoy "L")
(format flux "~{| ~,1f | ~,1f | ~,1f | ~}" `(,(calcmL 1 mot) ,(calcmL 2 mot) ,(calcmL 3 mot))))
(t (format flux "~{| ~,1f | ~,1f | ~,1f | ~}" `(,(calcm 1 mot) ,(calcm 2 mot) ,(calcm 3 mot))))))
(defun formatage (inputfile outputfile)
"Function that reads the raw data file (inputfile) and
writes in the file outputfile the formatted data"
(with-open-file (fl outputfile :direction :output :if-exists :supersede)
(let ((data (recupfile inputfile)))
(entete fl)
(dataform fl data)
(repchar fl "=" 36)
(terpri fl)
(format fl "moy matin")
(displaymoy fl data "L")
(terpri fl)
(repchar fl "-" 36)
(terpri fl)
(format fl "moy soir ")
(displaymoy fl data "C")
(terpri fl)
(repchar fl "-" 36)
(terpri fl)
(format fl "moy m+s ")
(displaymoy fl data)
(terpri fl)
(repchar fl "=" 36)
(terpri fl))))
(defun transfdata ()
(let ((data) (form) (rep) (old))
(setf old (write-to-string (get-universal-time)))
(format t "specify the path of the data file~%")
(setf data (read-line))
(format t "specify the path of the formatted file~%")
(setf form (read-line))
(cond ((not (probe-file form))
(formatage data form))
(t (format t "the ~a file exists ~%if you want to save it type save and then enter~%otherwise type overwrite then enter" form)
(terpri)
(setf rep (read))
(if (equal rep 'overwrite)
(formatage data form)
(progn
(rename-file form (concatenate 'string form old ".txt"))
(formatage data form)))))))