From 2256df389f3b13902a95b0bad6b363e3ca2e7882 Mon Sep 17 00:00:00 2001 From: AnthonyMichaelTDM <68485672+AnthonyMichaelTDM@users.noreply.github.com> Date: Fri, 11 Mar 2022 13:45:24 -0800 Subject: [PATCH] started rust port of 95_weekday --- 95_Weekday/rust/Cargo.toml | 8 ++ 95_Weekday/rust/README.md | 3 + 95_Weekday/rust/src/main.rs | 144 ++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 95_Weekday/rust/Cargo.toml create mode 100644 95_Weekday/rust/README.md create mode 100644 95_Weekday/rust/src/main.rs diff --git a/95_Weekday/rust/Cargo.toml b/95_Weekday/rust/Cargo.toml new file mode 100644 index 00000000..1ec69633 --- /dev/null +++ b/95_Weekday/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/95_Weekday/rust/README.md b/95_Weekday/rust/README.md new file mode 100644 index 00000000..7e85f9a1 --- /dev/null +++ b/95_Weekday/rust/README.md @@ -0,0 +1,3 @@ +Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) + +Conversion to [Rust](https://www.rust-lang.org/) by Anthony Rubick [AnthonyMichaelTDM](https://github.com/AnthonyMichaelTDM) diff --git a/95_Weekday/rust/src/main.rs b/95_Weekday/rust/src/main.rs new file mode 100644 index 00000000..7f2a143b --- /dev/null +++ b/95_Weekday/rust/src/main.rs @@ -0,0 +1,144 @@ +use std::io::{self, stdout, Write}; + +struct DATE { + month: u32, + day: u32, + year:u32, + day_of_week:u32, +} +impl DATE { + //create one from user input + fn new_from_input(prompt:&str) -> DATE { + //DATA + let mut raw_date: Vec; + //get date + //input loop + loop { + //get user input, + raw_date = get_str_from_user(prompt) + .split(',')//split it up by ',''s + .filter_map(|s| s.parse::().ok()).collect();//convert it to numbers, ignore things that fail + + //if they didn't give enough data + if raw_date.len() == 3 { //is it long enough? (3 elements) + //are each ones valid things? + if (1..=12).contains(&raw_date[0]) { //valid month + if (1..=31).contains(&raw_date[1]) { //valid day + break; + } + } + } + //otherwise, print error message and go again + println!("Invalid date, try again!"); + } + + //create date + let mut date =DATE{ + month:raw_date[0], + day:raw_date[1], + year:raw_date[2], + day_of_week: 0 + }; + date.update_day_of_week(); + //return date + return date + } + + /** + * caluclates the day of the week (1-7) + */ + fn update_day_of_week(&mut self) { + let month_table:[u32;12] = [0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5]; + let i1 = (self.year - 1500) / 100; + let a = i1 * 5 + (i1 + 3)/4; + let i2 = a - (a/7) * 7; + let y2 = self.year/100; + let y3 = self.year - y2*100; + let a = y3 / 4 + y3 + self.day + month_table[(self.month - 1) as usize] + i2; + let b = a - (a/7) * 7; + + //adjust weekday and set it + if self.month <= 2 && self.is_leap_year() { + self.day_of_week = b-1; + } else { + self.day_of_week = b; + } + if self.day_of_week == 0 {self.day_of_week = 7;} + + } + + /** + * is the year a leap_year + */ + fn is_leap_year(&self) -> bool{ + if self.year % 4 != 0 { + return false; + } else if self.year %100 != 0 { + return true; + } else if self.year % 400 != 0 { + return false; + } else { + return true; + } + } +} +fn main() { + //DATA + let todays_date: DATE; + let other_date:DATE; + + //print welcome + welcome(); + + //get todays date + todays_date = DATE::new_from_input("ENTER TODAY'S DATE IN THE FORM: 3,24,1979 "); + //check todays date + if todays_date.year < 1582 { + println!("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII."); + return; + } + + //get other date + other_date = DATE::new_from_input("ENTER DAY OF BIRTH (OR OTHER DAY OF INTEREST) (like MM,DD,YYYY)"); + //check other date + if other_date.year < 1582 { + println!("NOT PREPARED TO GIVE DAY OF WEEK PRIOR TO MDLXXXII."); + return; + } + +} + + +/** + * print welome message + */ +fn welcome() { + println!(" + WEEKDAY + CREATIVE COMPUTING MORRISTOWN, NEW JERSEY + + + + WEEKDAY IS A COMPUTER DEMONSTRATION THAT + GIVES FACTS ABOUT A DATE OF INTEREST TO YOU. + "); +} + +/** + * gets a string from user input + */ +fn get_str_from_user(prompt:&str) -> String { + //DATA + let mut raw_input = String::new(); + + //print prompt + print!("{}",prompt); + //flust std out //allows prompt to be on same line as input + stdout().flush().expect("failed to flush"); + + //get input and trim whitespaces + io::stdin().read_line(&mut raw_input).expect("Failed to read input"); + + //return raw input + return raw_input.trim().to_string(); +}