initial commit
This commit is contained in:
commit
22020f62d9
6 changed files with 381 additions and 0 deletions
104
redovalnica/redovalnica.go
Normal file
104
redovalnica/redovalnica.go
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
// Package redovalnica implements a simple grade book.
|
||||
//
|
||||
// It supports adding students and grades, listing grades, and computing final
|
||||
// status based on averages.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// studenti := make(map[string]redovalnica.Student)
|
||||
// redovalnica.DodajStudenta(studenti, "63210001", "Ana", "Novak")
|
||||
// redovalnica.DodajOceno(studenti, "63210001", 10)
|
||||
// redovalnica.IzpisVsehOcen(studenti)
|
||||
// redovalnica.IzpisiKoncniUspeh(studenti, 3, 1, 10)
|
||||
//
|
||||
// Exported functions:
|
||||
// - DodajStudenta
|
||||
// - DodajOceno
|
||||
// - IzpisVsehOcen
|
||||
// - IzpisiKoncniUspeh
|
||||
package redovalnica
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Student represents a student with personal data and grades.
|
||||
type Student struct {
|
||||
Ime string
|
||||
Priimek string
|
||||
Ocene []int
|
||||
}
|
||||
|
||||
// DodajStudenta adds a student if the ID is not already present.
|
||||
func DodajStudenta(studenti map[string]Student, vpisnaStevilka string, ime string, priimek string) {
|
||||
_, obstaja := studenti[vpisnaStevilka]
|
||||
if obstaja {
|
||||
return
|
||||
}
|
||||
studenti[vpisnaStevilka] = Student{Ime: ime, Priimek: priimek, Ocene: []int{}}
|
||||
}
|
||||
|
||||
// DodajOceno appends a grade to a student's record. If the student is missing
|
||||
// the call is a no-op.
|
||||
func DodajOceno(studenti map[string]Student, vpisnaStevilka string, ocena int) {
|
||||
student, obstaja := studenti[vpisnaStevilka]
|
||||
if !obstaja {
|
||||
return
|
||||
}
|
||||
student.Ocene = append(student.Ocene, ocena)
|
||||
studenti[vpisnaStevilka] = student
|
||||
}
|
||||
|
||||
// povprecje returns the average grade for a student, or 0 if no grades.
|
||||
func povprecje(studenti map[string]Student, vpisnaStevilka string) float64 {
|
||||
student, obstaja := studenti[vpisnaStevilka]
|
||||
if !obstaja || len(student.Ocene) == 0 {
|
||||
return 0.0
|
||||
}
|
||||
skupaj := 0
|
||||
for _, ocena := range student.Ocene {
|
||||
skupaj += ocena
|
||||
}
|
||||
return float64(skupaj) / float64(len(student.Ocene))
|
||||
}
|
||||
|
||||
// izpisStudenta returns formatted student info or an empty string if missing.
|
||||
func izpisStudenta(studenti map[string]Student, vpisnaStevilka string) string {
|
||||
student, obstaja := studenti[vpisnaStevilka]
|
||||
if !obstaja {
|
||||
return ""
|
||||
}
|
||||
return vpisnaStevilka + " - " + student.Ime + " " + student.Priimek + ": " + fmt.Sprint(student.Ocene)
|
||||
}
|
||||
|
||||
// IzpisVsehOcen prints all students and their grades.
|
||||
func IzpisVsehOcen(studenti map[string]Student) {
|
||||
fmt.Println("REDOVALNICA:")
|
||||
for vpisnaStevilka := range studenti {
|
||||
fmt.Println(izpisStudenta(studenti, vpisnaStevilka))
|
||||
}
|
||||
}
|
||||
|
||||
// vrniUspeh returns a status string based on average and grade count.
|
||||
func vrniUspeh(povprecje float64, stOcen int, minStOcen int, minOcena int, maxOcena int) string {
|
||||
if stOcen < minStOcen {
|
||||
return "Premalo ocen"
|
||||
}
|
||||
if povprecje >= float64(maxOcena)-1 {
|
||||
return "Odličen študent!"
|
||||
} else if povprecje >= float64(minOcena)+float64(maxOcena-minOcena)/2 {
|
||||
return "Povprečen študent"
|
||||
} else {
|
||||
return "Neuspešen študent"
|
||||
}
|
||||
}
|
||||
|
||||
// IzpisiKoncniUspeh prints final status for all students using provided
|
||||
// thresholds.
|
||||
func IzpisiKoncniUspeh(studenti map[string]Student, minStOcen int, minOcena int, maxOcena int) {
|
||||
for vpisnaStevilka, student := range studenti {
|
||||
povp := povprecje(studenti, vpisnaStevilka)
|
||||
stOcen := len(student.Ocene)
|
||||
uspeh := vrniUspeh(povp, stOcen, minStOcen, minOcena, maxOcena)
|
||||
fmt.Printf("%s %s: povprečna ocena %.1f (%d ocen) -> %s\n",
|
||||
student.Ime, student.Priimek, povp, stOcen, uspeh)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue