Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- using System.Text;
- namespace BaseCalculator
- {
- internal static class Program
- {
- private record Base(int BaseNum, string BaseName, string HelpFile, Func<string, NumberRecord> func);
- private record NumberRecord(string Number, int Base)
- {
- public string Binary => Number.ConvertTo(Base, 2);
- // public string Ternary => Number.ConvertTo(Base, 3);
- // public string Quaternary => Number.ConvertTo(Base, 4);
- // public string Quinary => Number.ConvertTo(Base, 5);
- // public string Senary => Number.ConvertTo(Base, 6);
- // public string Septernary => Number.ConvertTo(Base, 7);
- public string Octal => Number.ConvertTo(Base, 8);
- // public string Nonary => Number.ConvertTo(Base, 9);
- public string Decimal => Number.ConvertTo(Base, 10);
- // public string Undecimal => Number.ConvertTo(Base, 11);
- // public string Duodecimal => Number.ConvertTo(Base, 12);
- public string Hexadecimal => Number.ConvertTo(Base, 16);
- // public string Vigesimal => Number.ConvertTo(Base, 20);
- // public string Sexagesimal => Number.ConvertTo(Base, 60);
- public override string ToString()
- {
- var sb = new StringBuilder();
- sb.AppendLine($"Base 2: {Binary}");
- // sb.AppendLine($"Base 3: {Ternary}");
- // sb.AppendLine($"Base 4: {Quaternary}");
- // sb.AppendLine($"Base 5: {Quinary}");
- // sb.AppendLine($"Base 6: {Senary}");
- // sb.AppendLine($"Base 7: {Septernary}");
- sb.AppendLine($"Base 8: {Octal}");
- // sb.AppendLine($"Base 9: {Nonary}");
- sb.AppendLine($"Base 10: {Decimal}");
- // sb.AppendLine($"Base 11: {Undecimal}");
- // sb.AppendLine($"Base 12: {Duodecimal}");
- sb.AppendLine($"Base 16: {Hexadecimal}");
- //sb.AppendLine($"Base 20: {Vigesimal}");
- //sb.AppendLine($"Base 60: {Sexagesimal}");
- return sb.ToString();
- }
- }
- private static int ToInt32(this string s, int from) => Convert.ToInt32(s, from);
- private static string ConvertTo(this string s, int from, int to) => Convert.ToString(s.ToInt32(from), to);
- private static Base[] s_menu = {
- new( 2, "bin" ,"Converts from Binary.", (b) => new(b, 2)),
- new( 8, "oct" ,"Converts from Octal.", (o) => new(o, 8)),
- new( 10, "dec","Converts from Decimal.", (d)=> new(d, 10)),
- new( 16, "hex","Converts from Hexadecimal.", (h) => new(h, 16)),
- new( 0, "quit", "Quits the program", (q) => null)
- };
- private static void Menu()
- {
- foreach (var mi in s_menu)
- {
- Console.WriteLine($"{mi.BaseName}\t- {mi.HelpFile}\r\n\t{mi.BaseNum} can also be used.");
- }
- }
- private static void Main(string[] args)
- {
- Menu();
- while (true)
- {
- try
- {
- Console.Write("Enter Base to convert from followed by the number: ");
- string[] input = Console.ReadLine().Split(' ');
- foreach (Base b in s_menu)
- {
- string cmd = input[0].ToLower();
- if (cmd != b.BaseName && cmd != b.BaseNum.ToString())
- continue;
- NumberRecord n = b.func(input.Length > 1 ? input[1] : null);
- if (n == null)
- {
- Console.WriteLine("Goodbye! :(");
- return;
- }
- Console.WriteLine(n.ToString());
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement