From cd1d8981bb0cb1311f2a75b7e8b5f1fae526e249 Mon Sep 17 00:00:00 2001 From: un-lock-able Date: Mon, 21 Apr 2025 20:08:46 +0800 Subject: [PATCH] read customer data --- lab1/src/main.rs | 37 ++++++++++++++++++++++++++++++++++++- lab1/test_data/1.txt | 3 +++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 lab1/test_data/1.txt diff --git a/lab1/src/main.rs b/lab1/src/main.rs index 7fc98ff..80bac21 100644 --- a/lab1/src/main.rs +++ b/lab1/src/main.rs @@ -10,8 +10,43 @@ struct CmdArgs { file: String, } +#[derive(Debug)] +struct Customer { + id: u32, + arrival_time: u32, + service_time: u32, +} + +fn read_customers_from_file(path: &Path) -> Vec { + let file = File::open(path).expect(&format!("Open file {} failed.", path.to_str().expect("Invalid path"))); + let reader = io::BufReader::new(file); + + let mut customers = Vec::new(); + + for line in reader.lines() { + let line = line.expect("Read lien failed"); + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() != 3 { + continue; // or handle the error + } + + let customer = Customer { + id: parts[0].parse().expect("Invalid ID"), + arrival_time: parts[1].parse().expect("Invalid arrival time"), + service_time: parts[2].parse().expect("Invalid service time"), + }; + + customers.push(customer); + } + customers +} + fn main() { let args = CmdArgs::parse(); - println!("File name: {}", args.file); + let customers = read_customers_from_file(Path::new(&args.file)); + + for cus in &customers { + println!("{:?}", cus); + } } diff --git a/lab1/test_data/1.txt b/lab1/test_data/1.txt new file mode 100644 index 0000000..ffbc809 --- /dev/null +++ b/lab1/test_data/1.txt @@ -0,0 +1,3 @@ +1 1 10 +2 5 2 +3 6 3 \ No newline at end of file