我正在研究一个问题,要求我计算每个学生在课堂上的平均分。计算Lisp中的平均分和平均分
输入是一个Lisp文件格式如下:
(((name studentname) (class hour grade) (class hour grade) ...)
((name studentname) (class hour grade) (class hour grade) ...) ...)
对于输出:我需要打印的平均成绩作为排序的学生的名字和他们的GPA(平均成绩为学生)以及班级平均分数(每个独特班级的平均分数)。
到目前为止,这是我
(setq class '(((name Seymore) (eng 3 4.0) (mat 3 3.0) (his 3 4.0) (bio 3 2.0) (biol 1 4.0))
((name Ichahbod) (cs 3 3.0) (mat 3 4.0) (spe 2 4.0) (che 3 4.0) (chel 1 3.0) (lit 3 3.0))
((name Zackery) (mat 5 3.0) (eng 3 3.0) (jou 2 3.0) (phy 3 3.0) (phyl 1 4.0) (lit 2 4.0))
((name Tukerville) (soc 4 3.0) (mus 2 4.0) (jou 3 4.0) (geo 4 4.0) (geol 1 3.0) (eng 3 3.0))
((name Simonsays) (css 3 3.0) (ast 3 4.0) (spe 3 4.0) (cs 3 4.0) (spe 2 3.0) (dan 4 4.0))
((name Snicker) (eng 3 4.0) (phy 4 4.0) (css 3 2.0) (csl 1 4.0) (ped 2 3.0) (mat 3 3.0))
((name Glass) (mat 3 1.0) (eng 3 1.0) (ped 1 1.0) (bio 3 1.0) (biol 1 0.0) (che 3 1.0) (chel 1 1.0))))
;this function multiplies the hours * the grades
(defun product (hours grades)
(* hours grades)
)
;this function multiplies a set of grades
(defun sumofGrades (L)
(cond
((null L) 0) ;check if it is first
(t (+ (product (cdr (cdadar L)) (caddar L)))) ;first val then the second val
(sumofGrades (cdr L)) ;the rest of one
)
)
;to get the total , same as sum of grades but sum the second variables
(defun totalHours (L)
(cond
((null L) 0) ;check if it is first
(t (+ (product (caddar L) (caddar L)))) ;first val then the second val
(totalHours() (cdr L)) ;the rest of one
)
)
(defun gradepoint (L)
(/ (sumofGrades L) (totalHours L))
)
我试图启动与辅助方法,因为我认为这会是最好的方法,它可能没有。当我运行sumofGrades时,我从第一个条目中获得了我需要的4.0,但它说它不是一个数字。我写了这些方法的基本数学,我需要做的数字,但在这一点上,我很困惑下一步该怎么做。
如果我需要倒带和去一个不同的例程我倒了,任何帮助将不胜感激。
下次请粘贴完整的错误消息和函数调用。 – sds
好吧,会对此表示歉意。 – user2762848