Created
December 31, 2017 05:38
-
-
Save segfo/07b84de9318b995b75d9d0364161bbca to your computer and use it in GitHub Desktop.
登録された値でFizzbuzzする(登録が面倒なのでマクロも作る)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
struct FizzBuzzData{ | |
num:u32, | |
id:&'static str | |
} | |
#[derive(Debug)] | |
struct FizzBuzzN{ | |
data:Vec<FizzBuzzData>, | |
} | |
impl FizzBuzzN{ | |
fn new(data:Vec<FizzBuzzData>)->Self{ | |
FizzBuzzN{ | |
data | |
} | |
} | |
fn run(self){ | |
for i in 1..100{ | |
let mut is_multiple=false; | |
for k in 0..self.data.len(){ | |
if(i%self.data[k].num==0){ | |
print!("({}/{}) ",self.data[k].id,i); | |
is_multiple=true; | |
} | |
} | |
if(is_multiple==false){ | |
print!("{} ",i); | |
} | |
} | |
} | |
} | |
macro_rules! fizzbuzzdata_create { | |
($($num:expr,$id:expr),*) => { | |
{ | |
let mut tmp = Vec::<FizzBuzzData>::new(); | |
$( | |
tmp.push(FizzBuzzData{num:$num,id:$id}); | |
)* | |
tmp | |
} | |
} | |
} | |
fn main() { | |
let f = FizzBuzzN::new( | |
fizzbuzzdata_create!( | |
3,"Fizz",5,"Buzz",7,"Fuga",11,"Hoge" | |
) | |
); | |
f.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment