Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit f4abaf0

Browse files
committed
Add project files.
1 parent 2896934 commit f4abaf0

22 files changed

Lines changed: 924 additions & 0 deletions

NumericSystemConvertor.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35919.96 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NumericSystemConvertorConsole", "NumericSystemConvertorConsole\NumericSystemConvertorConsole.csproj", "{5BA78952-7A5F-42A2-89D8-88B81438E28B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NumericSystemConvertorGUI", "NumericSystemConvertorGUI\NumericSystemConvertorGUI.csproj", "{C4EF1CF4-681E-4F7B-AEE4-F4E6B2632B10}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{5BA78952-7A5F-42A2-89D8-88B81438E28B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{5BA78952-7A5F-42A2-89D8-88B81438E28B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{5BA78952-7A5F-42A2-89D8-88B81438E28B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{5BA78952-7A5F-42A2-89D8-88B81438E28B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{C4EF1CF4-681E-4F7B-AEE4-F4E6B2632B10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{C4EF1CF4-681E-4F7B-AEE4-F4E6B2632B10}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{C4EF1CF4-681E-4F7B-AEE4-F4E6B2632B10}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{C4EF1CF4-681E-4F7B-AEE4-F4E6B2632B10}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {F0B94485-0820-405B-AB98-BB0485880FB6}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PlatformTarget>x64</PlatformTarget>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace NumericSystemConvertor
2+
{
3+
/* Description: The class is partial, so the logics is divided in modules, but can contain methods, that are generic for some modules or methods
4+
that do not affect the main logics of conversations, such as PrintResult();. */
5+
6+
internal partial class Calculator
7+
{
8+
private string _number;
9+
private string _numberSystemName;
10+
private string _systemToConvertName;
11+
private string _result;
12+
13+
private const int MAX_ITERATIONS_FRACTIONAL_PART = 4; // It will be used, in the case of numbers with fractional part. Feel free to change the value, if you want more precision.
14+
private readonly int _numberBaseValue;
15+
private readonly int _systemToConvertBaseValue;
16+
17+
private double _decimalNumber;
18+
19+
private bool _isResultWithoutFractionalPart = false;
20+
21+
public Calculator(string number, string numberSystemName, string systemToConvertName)
22+
{
23+
_number = number;
24+
_numberSystemName = numberSystemName;
25+
_systemToConvertName = systemToConvertName;
26+
27+
_numberBaseValue = GetBaseValue(_numberSystemName);
28+
_systemToConvertBaseValue = GetBaseValue(_systemToConvertName);
29+
}
30+
31+
private int GetBaseValue(string numberSystemName)
32+
{
33+
return numberSystemName switch
34+
{
35+
"Binary" => Systems.BINARY_BASE,
36+
"Octal" => Systems.OCTAL_BASE,
37+
"Decimal" => Systems.DECIMAL_BASE,
38+
"Hexadecimal" => Systems.HEXADECIMAL_BASE,
39+
};
40+
}
41+
42+
public void Calculate()
43+
{
44+
_decimalNumber = (_numberSystemName != "Decimal") ? ConvertBaseNToDecimal() : Convert.ToDouble(_number);
45+
_result = (_systemToConvertName != "Hexadecimal") ? ConvertDecimalToBaseN() : ConvertDecimalToHex();
46+
}
47+
48+
private double HandleRemainderFromResult(double result)
49+
{
50+
double truncatedValue = Math.Truncate(result);
51+
return truncatedValue;
52+
}
53+
54+
private double HandleNewFractionalPartFromResult(double result)
55+
{
56+
string resultString = Convert.ToString(result);
57+
string[] parts = resultString.Split('.');
58+
59+
if (parts.Length > 1)
60+
return Convert.ToDouble("0." + parts[1]);
61+
62+
_isResultWithoutFractionalPart = true;
63+
return 0.0;
64+
}
65+
66+
public void PrintResult()
67+
{
68+
Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"\nThe result of conversion => {_number} ({_numberSystemName}) to {_systemToConvertName} = {_result} approximately."); Console.ResetColor();
69+
}
70+
}
71+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace NumericSystemConvertor
4+
{
5+
internal class ConsoleWindowManager
6+
{
7+
private const int SW_MAXIMIZE = 3;
8+
[DllImport("kernel32.dll", ExactSpelling = true)]
9+
private static extern IntPtr GetConsoleWindow();
10+
[DllImport("user32.dll")]
11+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
12+
13+
public static void MaximizeConsoleWindow()
14+
{
15+
IntPtr consoleWindow = GetConsoleWindow();
16+
ShowWindow(consoleWindow, SW_MAXIMIZE);
17+
}
18+
}
19+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace NumericSystemConvertor
2+
{
3+
internal partial class Calculator
4+
{
5+
private double ConvertBaseNToDecimal()
6+
{
7+
string[] parts = _number.Split('.');
8+
double decimalWholePart = ConvertBaseNToDecimalGetWholePart(parts[0]);
9+
10+
if (parts.Length > 1)
11+
decimalWholePart += ConvertBaseNToDecimalGetFractionalPart(parts[1]);
12+
13+
return decimalWholePart;
14+
}
15+
16+
private double ConvertBaseNToDecimalGetWholePart(string wholePart)
17+
{
18+
double decimalWholePart = 0;
19+
20+
for (int i = 0; i < wholePart.Length; i++)
21+
{
22+
char currentChar = wholePart[i];
23+
int digit = char.IsDigit(currentChar) ? (currentChar - '0') : Systems.HexadecimalValues[currentChar];
24+
decimalWholePart += digit * Math.Pow(_numberBaseValue, wholePart.Length - 1 - i);
25+
}
26+
27+
return decimalWholePart;
28+
}
29+
30+
private double ConvertBaseNToDecimalGetFractionalPart(string fractionalPart)
31+
{
32+
double decimalFractionalPart = 0;
33+
34+
for (int i = 0, j = -1; i < fractionalPart.Length; i++, j--)
35+
{
36+
char currentChar = fractionalPart[i];
37+
int digit = char.IsDigit(currentChar) ? (currentChar - '0') : Systems.HexadecimalValues[currentChar];
38+
decimalFractionalPart += digit * Math.Pow(_numberBaseValue, j);
39+
}
40+
41+
return decimalFractionalPart;
42+
}
43+
44+
}
45+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace NumericSystemConvertor
2+
{
3+
internal partial class Calculator
4+
{
5+
private string ConvertDecimalToBaseN()
6+
{
7+
string[] parts = Convert.ToString(_decimalNumber).Split('.');
8+
string wholePart = ConvertDecimalToBaseNGetWholePart(Convert.ToDouble(parts[0]));
9+
10+
if (parts.Length > 1)
11+
wholePart += '.' + ConvertDecimalToBaseNGetFractionalPart(Convert.ToDouble("0." + parts[1]));
12+
13+
return wholePart;
14+
}
15+
16+
private string ConvertDecimalToBaseNGetWholePart(double decimalWholePart)
17+
{
18+
List<double> remainders = new List<double>();
19+
20+
for (int i = 0; ; i++)
21+
{
22+
if (decimalWholePart < _systemToConvertBaseValue)
23+
{
24+
remainders.Add(decimalWholePart);
25+
break;
26+
}
27+
28+
remainders.Add(decimalWholePart % _systemToConvertBaseValue);
29+
decimalWholePart = (decimalWholePart - remainders[i]) / _systemToConvertBaseValue;
30+
}
31+
32+
remainders.Reverse();
33+
return string.Join("", remainders);
34+
}
35+
36+
private string ConvertDecimalToBaseNGetFractionalPart(double decimalFractionalPart)
37+
{
38+
List<double> remainders = new List<double>();
39+
40+
for (int i = 0; i < MAX_ITERATIONS_FRACTIONAL_PART; i++)
41+
{
42+
if (_isResultWithoutFractionalPart)
43+
break;
44+
45+
double result = decimalFractionalPart * _systemToConvertBaseValue;
46+
remainders.Add(HandleRemainderFromResult(result));
47+
48+
decimalFractionalPart = HandleNewFractionalPartFromResult(result);
49+
}
50+
51+
return string.Join("", remainders);
52+
}
53+
}
54+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace NumericSystemConvertor
2+
{
3+
internal partial class Calculator
4+
{
5+
private string ConvertDecimalToHex()
6+
{
7+
string[] parts = Convert.ToString(_decimalNumber).Split('.');
8+
string wholePart = ConvertDecimalToHexGetWholePart(Convert.ToDouble(parts[0]));
9+
10+
if (parts.Length > 1)
11+
wholePart += '.' + ConvertDecimalToHexGetFractionalPart(Convert.ToDouble("0." + parts[1]));
12+
13+
return wholePart;
14+
}
15+
16+
private string ConvertDecimalToHexGetWholePart(double decimalWholePart)
17+
{
18+
List<string> remainders = new List<string>();
19+
20+
for (int i = 0; ; i++)
21+
{
22+
if (decimalWholePart < Systems.HEXADECIMAL_BASE)
23+
{
24+
remainders.Add(HandleHexRemainder(decimalWholePart));
25+
break;
26+
}
27+
28+
double numericRemainder = decimalWholePart % Systems.HEXADECIMAL_BASE;
29+
remainders.Add(HandleHexRemainder(numericRemainder));
30+
decimalWholePart = (decimalWholePart - numericRemainder) / Systems.HEXADECIMAL_BASE;
31+
}
32+
33+
remainders.Reverse();
34+
return string.Join("", remainders);
35+
}
36+
37+
private string HandleHexRemainder(double remainder)
38+
{
39+
return remainder switch
40+
{
41+
10 => "A",
42+
11 => "B",
43+
12 => "C",
44+
13 => "D",
45+
14 => "E",
46+
15 => "F",
47+
_ => remainder.ToString(),
48+
};
49+
}
50+
51+
private string ConvertDecimalToHexGetFractionalPart(double decimalFractionalPart)
52+
{
53+
List<string> remainders = new List<string>();
54+
55+
for (int i = 0; i < MAX_ITERATIONS_FRACTIONAL_PART; i++)
56+
{
57+
if (_isResultWithoutFractionalPart)
58+
break;
59+
60+
double result = decimalFractionalPart * _systemToConvertBaseValue;
61+
double currentRemider = HandleRemainderFromResult(result);
62+
63+
remainders.Add(HandleHexRemainder(currentRemider));
64+
decimalFractionalPart = HandleNewFractionalPartFromResult(result);
65+
}
66+
67+
return string.Join("", remainders);
68+
}
69+
}
70+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace NumericSystemConvertor
2+
{
3+
internal class Program : ConsoleWindowManager
4+
{
5+
static void Main(string[] args)
6+
{
7+
MaximizeConsoleWindow();
8+
9+
while (true)
10+
{
11+
StartMenu.Start();
12+
13+
Console.WriteLine("\n\n");
14+
}
15+
}
16+
17+
}
18+
}

0 commit comments

Comments
 (0)