www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Programming test - using strings

reply Joel Christensen <joelcnz gmail.com> writes:
Any one interested in doing D versions of this program?

http://www.rubyquiz.com/quiz14.html
Dec 16 2009
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 17 Dec 2009 03:46:52 +0100, Joel Christensen <joelcnz gmail.com>  
wrote:

 Any one interested in doing D versions of this program?

 http://www.rubyquiz.com/quiz14.html
There're plenty of opportunities for making this smaller, but at 4:53 in the morning, I'm not optimizing much. :p module lcd; import std.stdio; import std.string; import std.conv; enum byte[] letters = [ 0b01110111, // 0 0b00010010, // 1 0b01011101, // 2 0b01011011, // 3 0b00111010, // 4 0b01101011, // 5 0b01011111, // 6 0b01010010, // 7 0b01111111, // 8 0b01111011 // 9 ]; string linechar( char c, ubyte b, int size ) { return repeat( letters[ c - '0' ] & b ? "-" : " ", size ); } string barchar( char c, ubyte b ) { return letters[ c - '0' ] & b ? "|" : " "; } string toLCDNumber( string s, int size ) { string[] result = new string[ size * 2 + 3 ]; foreach ( ch; s ) { result[ 0 ] ~= " " ~ linechar( ch, 0b01000000, size ) ~ " "; result[ size + 1 ] ~= " " ~ linechar( ch, 0b00001000, size ) ~ " "; result[ 2*size + 2 ] ~= " " ~ linechar( ch, 0b00000001, size ) ~ " "; foreach ( i; 0..size ) { result[ 1 + i ] ~= barchar( ch, 0b00100000 ) ~ repeat( " ", size ) ~ barchar( ch, 0b00010000 ) ~ " "; result[ 2 + i + size ] ~= barchar( ch, 0b00000100 ) ~ repeat( " ", size ) ~ barchar( ch, 0b00000010 ) ~ " "; } } return join( result, newline ); } void main( string[] args ) { int size = 2; uint stringArg = 1; foreach ( i, elem; args[ 0..$ ] ) { if ( elem == "-s" ) { size = to!( int )( args[ i+1 ] ); if ( i == stringArg ) { stringArg = i+2; } break ; } } writeln( toLCDNumber( args[ stringArg ], size ) ); } -- Simen
Dec 16 2009
parent Joel Christensen <joelcnz gmail.com> writes:
Thanks for the reply, :-) I haven't finished mine yet. Then I can look 
at yours etc.
Dec 18 2009