Hi
Does anyone know how to print this output to the screen instead of getting true when I run ‘test.’ I am new to Prolog, so I have never printed anything to the console. This is part of my Programming languages assignment, but seems like I can not demo my result because of this.
%import SWI-Prolog's csv_read_file_row/3
:- use_module(library(csv)).
test :- augment('HW2data.csv').
% function that reads rows from a csv file and stores it in object
% file structure: line_number,ID,Name,HW1,HW2,HW3,Midterm,Final
% extracts data from csv file and calculates score for each student
% Score = HW1 * 0.1 + HW2 * 0.1 + HW3 * 0.1 + Midterm * 0.3 + Final * 0.4 -> round to nearest integer
% Calculate the grade of each student
% Grade = (A+ = 90-100, A = 85-89, A- = 80-84, B+ = 77-79, B = 73-76, B- = 70-72, C+ = 67-69, C= 63-66, C- = 60-62, D = 50-59, E = 0-49)
% Print the score and grade of each student
% print format: StudentID Name Score Grade
% Output: StudentID, Name, Score, Grade
augment(FileName) :-
csv_read_file(FileName, Rows, []),
findall(Student, student(Student, Rows), Students),
print_students(Students).
student(Student, Rows) :-
nth1(LineNumber, Rows, [ID, Name, HW1, HW2, HW3, Midterm, Final]),
nth1(LineNumber, Rows, [ID, Name, HW1, HW2, HW3, Midterm, Final]),
calculate_score(LineNumber, ID, Name, HW1, HW2, HW3, Midterm, Final, Score, Grade),
Student = [LineNumber, ID, Name, Score, Grade].
print_students([]).
print_students([Student | Students]) :-
print_student(Student),
print_students(Students).
print_student([LineNumber, ID, Name, Score, Grade]) :-
format('~w,~w,~w,~w,~w~n', [ID, Name, Score, Grade]).
% Calculate the score of a student and store it in the object
calculate_score(LineNumber, ID, Name, HW1, HW2, HW3, Midterm, Final, Score, Grade) :-
Score is round((HW1 * 0.1 + HW2 * 0.1 + HW3 * 0.1 + Midterm * 0.3 + Final * 0.4)),
calculate_grade(Score, Grade).
% Calculate the grade of a student and store it in the object
calculate_grade(Score, Grade) :-
(Score >= 90 -> Grade = 'A+';
Score >= 85 -> Grade = 'A';
Score >= 80 -> Grade = 'A-';
Score >= 77 -> Grade = 'B+';
Score >= 73 -> Grade = 'B';
Score >= 70 -> Grade = 'B-';
Score >= 67 -> Grade = 'C+';
Score >= 63 -> Grade = 'C';
Score >= 60 -> Grade = 'C-';
Score >= 50 -> Grade = 'D';
Grade = 'E').