initial commit

This commit is contained in:
Baipyrus 2022-05-15 16:42:11 +02:00
commit 6c0b71e9a2
5 changed files with 156 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/*
obj/*

View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>

BIN
ConvertNumberSystems.exe Normal file

Binary file not shown.

143
Program.cs Normal file
View File

@ -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;
}
}
}

3
README.md Normal file
View File

@ -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.