Created
April 25, 2010 07:44
-
-
Save rekkusu/378246 to your computer and use it in GitHub Desktop.
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
using System; | |
namespace Dec2Bin | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Dec2Bin - 10進数から2進数に変換"); | |
Console.WriteLine("プログラム能力を試すのにお薦め"); | |
while (true) | |
{ | |
Console.Write("> "); | |
string input = Console.ReadLine(); | |
try | |
{ | |
int dec = int.Parse(input); | |
if (dec < 0) throw new ApplicationException(); | |
Console.WriteLine("Result: " + input + " -> " + dec2bin(dec)); | |
} | |
catch (FormatException) | |
{ | |
break; | |
} | |
catch (ApplicationException) | |
{ | |
Console.WriteLine("負の数値は変換に対応していません"); | |
} | |
} | |
} | |
static string dec2bin(int input) | |
{ | |
if (input == 0) return "0"; | |
return dec2bin(input / 2) + input % 2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment