commit 6c0b71e9a2066a88a9fd542e61af26719405c287 Author: Baipyrus Date: Sun May 15 16:42:11 2022 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dea4ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/* +obj/* \ No newline at end of file diff --git a/ConvertNumberSystems.csproj b/ConvertNumberSystems.csproj new file mode 100644 index 0000000..2082704 --- /dev/null +++ b/ConvertNumberSystems.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/ConvertNumberSystems.exe b/ConvertNumberSystems.exe new file mode 100644 index 0000000..4f11670 Binary files /dev/null and b/ConvertNumberSystems.exe differ diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..7195a2f --- /dev/null +++ b/Program.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections; + +namespace ConvertNumberSystems +{ + class Program + { + static ArrayList symbols = new ArrayList(); + static long maxCommas = 5; + + static void Main(string[] args) { + Console.Clear(); + + // Initialize Symbols + for (int i = 48; i <= 57; i++) { symbols.Add((char)i); } + for (int i = 65; i <= 90; i++) { symbols.Add((char)i); } + + + // Get Input + Console.Write("Input your first number: "); + string number1 = Console.ReadLine(); + + Console.Write("Input the base of that number: "); + long base1 = Convert.ToInt64(Console.ReadLine()); + + Console.Write("Input your second number: "); + string number2 = Console.ReadLine(); + + Console.Write("Input the base of that number: "); + long base2 = Convert.ToInt64(Console.ReadLine()); + + Console.Write("Input the operator to use on the numbers: "); + string inOp = Console.ReadLine(); + + Console.Write("Input the base your output Number: "); + long outputBase = Convert.ToInt64(Console.ReadLine()); + + + // Calculate Numbers + double aTD1 = anyToDecimal(number1, base1); + double aTD2 = anyToDecimal(number2, base2); + + string result = ""; + switch (inOp[0]) { + case '+': + result = decimalToAny(aTD1 + aTD2, outputBase); + break; + case '-': + result = decimalToAny(aTD1 - aTD2, outputBase); + break; + case '*': + result = decimalToAny(aTD1 * aTD2, outputBase); + break; + case '/': + result = decimalToAny(aTD1 / aTD2, outputBase); + break; + default: + result = "None"; + break; + } + + Console.WriteLine("Your resulting number is: {0}", result); + Console.ReadKey(); + } + + static double anyToDecimal(string number, long _base) + { + if (_base == 10) { return Convert.ToDouble(number); } + if (_base > symbols.Count || _base < 2) { return 0d; } + + string removeComma = ""; + bool foundComma = false; + long afterComma = 0; + for (int i = 0; i < number.Length; i++) + { + if (afterComma == maxCommas) { break; } + if (number[i] != ',') + { + removeComma += number[i]; + if (foundComma) + { + afterComma++; + } + } + else + { + foundComma = true; + } + } + + double value = 0; + string upperCase = removeComma.ToUpper(); + for (int i = 0; i < removeComma.Length; i++) + { + long position = removeComma.Length - i - 1 - afterComma; + char current = upperCase[i]; + for (long j = 0; j < _base; j++) + { + if (current == (char)symbols[Convert.ToInt32(j)]) + { + value += j * (double)Math.Pow(_base, position); + break; + } + } + } + return value; + } + + static string decimalToAny(double number, long _base) + { + string b = ""; + if (_base == 10) + { + return b + number; + } + if (_base > symbols.Count || _base < 2) { return b; } + long hP = (long)Math.Floor((double)Math.Log((double)number) / Math.Log((double)_base)); + if (hP < 0) { hP = 0; } + double t = number; + long index = hP; + while ((t > (double)Math.Pow(10, -maxCommas-1)) && (index > -maxCommas-1)) + { + if (index == -1) + { + b += ","; + } + double p = (double)Math.Pow((double)_base, (double)index); + double d = (double)Math.Floor((double)t / p); + if ((double)Math.Abs(p * d) <= t) + { + t -= d * p; + b += "" + symbols[Convert.ToInt32(d)]; + } + else + { + b += "0"; + } + index--; + } + return b; + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb02b4f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +This is some old version of my Number Converter which still runs in a Console Application. +I think it still works perfectly fine, so im not gonna bother to upgrade it to winforms again. +The .exe next to the code is a compiled version of said winforms program, so it's not technically lost forever. \ No newline at end of file