diff --git a/Chapter02/src/main.rs b/Chapter02/src/main.rs index a20e0fe..d645deb 100644 --- a/Chapter02/src/main.rs +++ b/Chapter02/src/main.rs @@ -3,6 +3,10 @@ use std::time::Instant; use floating_duration::{TimeAsFloat, TimeFormat}; use std::time::SystemTime; use std::{thread, time}; +use std::env; +use std::fs::File; +use std::io::{self, Read}; +use std::io::prelude::*; fn main() { @@ -22,6 +26,40 @@ fn main() let mut floor_requests: Vec = Vec::new(); //4. Parse input and store as building description and floor requests + match env::args().nth(1) { + None => { + let mut buffer = String::new(); + io::stdin().read_to_string(&mut buffer) + .expect("read_to_string failed"); + + for (li,l) in buffer.lines().enumerate() { + if li==0 { + floor_count = l.parse::().unwrap(); + } else if li==1 { + floor_height = l.parse::().unwrap(); + } else { + floor_requests.push(l.parse::().unwrap()); + } + } + }, + Some(fp) => { + let mut buffer = String::new(); + File::open(fp) + .expect("File::open failed") + .read_to_string(&mut buffer) + .expect("read_to_string failed"); + + for (li,l) in buffer.lines().enumerate() { + if li==0 { + floor_count = l.parse::().unwrap(); + } else if li==1 { + floor_height = l.parse::().unwrap(); + } else { + floor_requests.push(l.parse::().unwrap()); + } + } + } + } //5. Loop while there are remaining floor requests let mut prev_loop_time = SystemTime::now(); @@ -36,8 +74,8 @@ fn main() location = location + velocity * dt; velocity = velocity + acceleration * dt; acceleration = { - let F = (up_input_voltage - down_input_voltage) * 8; - let m = 1200000; + let F = (up_input_voltage - down_input_voltage) * 8.0; + let m = 1200000.0; -9.8 + F/m }; diff --git a/Chapter02/test1.txt b/Chapter02/test1.txt new file mode 100644 index 0000000..cd1734c --- /dev/null +++ b/Chapter02/test1.txt @@ -0,0 +1,9 @@ +5 +5.67 +2 +1 +4 +0 +3 +1 +0