我正在研究一個問題,要求我計算每個學生在課堂上的平均分。計算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