#! /usr/bin/gawk -f # Last edited on 2024-01-01 10:56:40 by stolfi # Reads from {stdin} the {catsep} output of quiz and lab grade files. # Writes to stdout a consolidated file of those grades plus the overall # grade average. The first three files should be called "p{N}.cooked", # where {N} is 1, 2, or 3; the last one should be called "tp.cooked" The # first two columns in each file must be '|' and the RA number, and the # last two columns should be the total adjusted quiz or lab grade and # the name. Blank and comment lines are ignored. # # Needs "-f mc937_read_alunos.gawk" to read the file "alunos.txt". BEGIN { ngr = 0 # Number of grades included. nra = 0 # Number of distinct RAs seen split("", grs) # Element {grs[ra,igr]} is grade indexed {igr} (1,2,3,4) of student {ra}. split("", ras) # Element {ras[ira]} is the RA indexed {ira}. split("", iras) # Element {iras[ra]} is the index of RA {ra} in {ras} split("", nam) # Element {nam[ra]} is the name of student with RA {ra}. split("", niv) # Element {niv[ra]} is the level ("GD", "EP", ...) of student with RA {ra}. read_alunos("alunos.txt", nam, niv); } /^[#]FILE / { ngr++ fname = $2 pn = fname gsub(/[.]cooked$/, "", pn) if (pn ~ /^p[1-3]$/) { gsub(/^p/, "", pn) igr = pn + 0 } else if (pn == "tp") { igr = 4; } else { data_error(("invalid file name \"" fname "\"")); } if (igr != ngr) { data_error(("file out of sequence \"" fname "\"")); } next } //{ gsub(/[ ]*[#].*$/, "", $0) } /^ *$/ { next } /^ *[~]+ *$/ { next } /^[+]/ { next } /^[^|]/ { data_error(("invalid grade file format")); next } /^[|] [0-9][0-9][0-9][0-9][0-9][0-9] / { ra = $2 va = $(NF-1) name = $(NF) if (ra in iras) { ira = iras[ra] if (nam[ra] != name) { data_error(("student name mismatch \"" nam[ra] "\" : \"" name "\"")); } } else { ira = nra ras[ira] = ra nam[ra] = name iras[ra] = ira nra++ } grs[ra,igr] = va next } // { data_error("invalid line format") } END { printf "#\n" printf "# Notas e média de provas\n" printf "\n" printf "# RA " for (igr = 1; igr <= ngr-1; igr++) { printf " %3s", ("P" igr); } printf " M.P M.T M.F POS NOME\n" print_dashes() for (ira = 0; ira < nra; ira++) { ra = ras[ira] printf "| %s", ra # Compute average grade {med} and number of quizes done {nhi}: split("", nva); split("", xva); npr = 0 # Quizes made. nit = 0 # Grades obtained. for (igr = 1; igr <= ngr; igr++) { va = grs[ra,igr] xva[igr] = va; if (va == "") { prog_error(("missing grade " igr " for ra " ra "")); } else if (va == "---") { nva[igr] = 0 } else { nva[igr] = va + 0; if (igr < ngr) { npr++; } nit++ } } for (igr = 1; igr <= ngr-1; igr++) { printf " %s", xva[igr]; } mpr = int((2*nva[1] + 3*nva[2] + 4*nva[3])/9.0 + 0.5) if (npr > 0) { printf " %03d", mpr } else { if (mpr != 0) { prog_error(("duh npr mpr")); } printf " %3s", "---" } mtp = nva[4]; printf " %3s", xva[4]; mmin = (mpr < mtp ? mpr : mtp); mmax = (mpr > mtp ? mpr : mtp); mfi = int((mmax + 4*mmin)/5.0 + 0.5); if (nit > 0) { printf " %03d", mfi } else { if ((mpr != 0) || (mtp != 0) || (mfi != 0)) { prog_error(("duh nit mfi")); } printf " %3s", "---" } ispos = (niv[ra] == "EP") || (niv[ra] == "GP") || (niv[ra] == "MS") || (niv[ra] == "DR") pos = (ispos ? (mfi >= 75 ? "(A)" : (mfi >= 65 ? "(B)" : (mfi >= 50 ? "(C)" : (mfi >= 1 ? "(D)" : "(E)")))) : "---") printf " %3s", pos printf " %s\n", nam[ra] if (ra == "999999") { if (ira != 0) { data_error(("genius student not first")); } print_dashes(); } } print_dashes(); printf "\n" } function print_dashes( igr) { printf "+ %s", "------"; for (igr = 1; igr <= ngr-1; igr++) { printf " %s", "---"; } printf " %s", "---"; printf " %s", "---"; printf " %s", "---"; printf " %s", "---"; printf " %s\n", "----------------------------------------"; } function data_error(msg) { printf "%s:%d: ** %s\n", fname, FNR, msg > "/dev/stderr" printf " [%s]\n", $0 > "/dev/stderr" exit(1) } function prog_error(msg) { printf "%s:%d: ** program error - %s\n", fname, FNR, msg > "/dev/stderr" printf " [%s]\n", $0 > "/dev/stderr" exit(1) }