Created
January 12, 2015 20:24
-
-
Save lelandbatey/d25fae86c1816c21bc44 to your computer and use it in GitHub Desktop.
tiny_tree.rs -- A Rust port of my original tiny_tree.c: https://gist.github.com/lelandbatey/99f3c4b99c621c12631d
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
use std::os; | |
use std::num::Int; | |
// Helper function to print out single characters repeatedly. | |
fn repeat_char(ch: char, mut n: i32){ | |
while n > 0 { | |
print!("{}", ch.to_string()); | |
n = n-1; | |
} | |
} | |
fn main() { | |
let args = os::args(); | |
// The user can specify a command line argument for the `height`, and | |
// without an argument `height` defaults to a value of `5` | |
let mut height = | |
if args.len() > 1 { | |
let temp : i32 = match args[1].parse() { | |
Some(value) => value, | |
None => panic!("Argument given needs to be integer.") | |
}; | |
temp | |
} else { | |
5i32 | |
}; | |
// Instead of using bit shifting for powers of two, we use the power | |
// system, since it's clearer what's going on | |
height = 2.pow(height as usize); | |
let mut base = height; | |
while base > 0 { | |
base /= 2; | |
// Determine number of "nodes" to print on a line | |
let mut i = | |
if base > 0 { | |
height / base | |
} else { | |
0i32 | |
}; | |
// Print the nodes on a line. | |
while i > 0 { | |
repeat_char(' ', base-1); | |
let mod_i = i%2; | |
let mystery_char = | |
if mod_i == 0 { | |
'@' | |
} else { | |
' ' | |
}; | |
repeat_char(mystery_char, 1); | |
i = i-1; | |
} | |
print!("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment