Huy Trần :
use std::fs;
struct Student {
name: String,
score1: f64,
score2: f64,
}
impl Student {
fn average(&self) -> f64 {
(self.score1 + self.score2) / 2.0
}
fn rank(&self) -> &str {
match self.average() {
x if x >= 8.0 => "Gioi",
x if x >= 6.5 => "Kha",
x if x >= 5.0 => "Trung binh",
_ => "Yeu",
}
}
}
fn main() {
let data = fs::read_to_string("DIEM.TXT")
.expect("Khong the mo file DIEM.TXT");
let students: Vec = data
.lines()
.map(|line| {
let mut parts = line.split_whitespace();
Student {
name: parts.next().unwrap().to_string(),
score1: parts.next().unwrap().parse().unwrap(),
score2: parts.next().unwrap().parse().unwrap(),
}
})
.collect();
let mut result = String::new();
for student in &students {
result.push_str(&format!(
"{} {:.1} {}\n",
student.name,
student.average(),
student.rank()
));
}
fs::write("KETQUA.TXT", result)
.expect("Khong the ghi file KETQUA.TXT");
}
2026-09-14 12:49:19