Last active
September 23, 2016 09:25
-
-
Save olecksamdr/98b94bd6dcaadddb6f59db7e2b1e167b 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; | |
class binaryCalc { | |
static char[] operations = {'+', '-', '*', '/'}; | |
static string getOperation(string str) { | |
foreach (char operation in operations) { | |
if (str.IndexOf(operation) != -1) { | |
return new String(operation, 1); | |
} | |
} | |
return null; | |
} | |
static int Main() { | |
string input, operation; | |
string[] ab; | |
int a, b, result = 0; | |
bool imposibleOperation = false; | |
Console.Write(">"); | |
while (true) { | |
input = Console.ReadLine(); | |
if (input != "") { | |
operation = getOperation(input); | |
ab = input.Split(operation[0]); | |
a = Convert.ToInt32(ab[0], 2); | |
b = Convert.ToInt32(ab[1], 2); | |
switch(operation) { | |
case "+" : result = a + b; break; | |
case "-" : result = a - b; break; | |
case "*" : result = a * b; break; | |
case "/" : | |
if (b != 0) { | |
result = a / b; | |
} else { | |
Console.WriteLine(); | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine("can't divide by zero"); | |
Console.ForegroundColor = ConsoleColor.White; | |
imposibleOperation = true; | |
} | |
break; | |
} | |
if (!imposibleOperation) { | |
Console.WriteLine(); | |
Console.ForegroundColor = ConsoleColor.Cyan; | |
Console.WriteLine(Convert.ToString(result, 2) + " | " + result); | |
} | |
Console.ForegroundColor = ConsoleColor.White; | |
Console.WriteLine(); | |
Console.Write(">"); | |
imposibleOperation = false; | |
} else { | |
Console.Write(">"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment