Advertisement
Blizzardo1

BaseCalculator.cs

Jul 30th, 2021
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Text;
  5.  
  6. namespace BaseCalculator
  7. {
  8.     internal static class Program
  9.     {
  10.         private record Base(int BaseNum, string BaseName, string HelpFile, Func<string, NumberRecord> func);
  11.         private record NumberRecord(string Number, int Base)
  12.         {
  13.             public string Binary => Number.ConvertTo(Base, 2);
  14.             // public string Ternary => Number.ConvertTo(Base, 3);
  15.             // public string Quaternary => Number.ConvertTo(Base, 4);
  16.             // public string Quinary => Number.ConvertTo(Base, 5);
  17.             // public string Senary => Number.ConvertTo(Base, 6);
  18.             // public string Septernary => Number.ConvertTo(Base, 7);
  19.             public string Octal => Number.ConvertTo(Base, 8);
  20.             // public string Nonary => Number.ConvertTo(Base, 9);
  21.             public string Decimal => Number.ConvertTo(Base, 10);
  22.             // public string Undecimal => Number.ConvertTo(Base, 11);
  23.             // public string Duodecimal => Number.ConvertTo(Base, 12);
  24.             public string Hexadecimal => Number.ConvertTo(Base, 16);
  25.             // public string Vigesimal => Number.ConvertTo(Base, 20);
  26.             // public string Sexagesimal => Number.ConvertTo(Base, 60);
  27.             public override string ToString()
  28.             {
  29.                 var sb = new StringBuilder();
  30.                 sb.AppendLine($"Base  2: {Binary}");
  31.                 // sb.AppendLine($"Base  3: {Ternary}");
  32.                 // sb.AppendLine($"Base  4: {Quaternary}");
  33.                 // sb.AppendLine($"Base  5: {Quinary}");
  34.                 // sb.AppendLine($"Base  6: {Senary}");
  35.                 // sb.AppendLine($"Base  7: {Septernary}");
  36.                 sb.AppendLine($"Base  8: {Octal}");
  37.                 // sb.AppendLine($"Base  9: {Nonary}");
  38.                 sb.AppendLine($"Base 10: {Decimal}");
  39.                 // sb.AppendLine($"Base 11: {Undecimal}");
  40.                 // sb.AppendLine($"Base 12: {Duodecimal}");
  41.                 sb.AppendLine($"Base 16: {Hexadecimal}");
  42.                 //sb.AppendLine($"Base 20: {Vigesimal}");
  43.                 //sb.AppendLine($"Base 60: {Sexagesimal}");
  44.  
  45.                 return sb.ToString();
  46.             }
  47.         }
  48.  
  49.         private static int ToInt32(this string s, int from) => Convert.ToInt32(s, from);
  50.         private static string ConvertTo(this string s, int from, int to) => Convert.ToString(s.ToInt32(from), to);
  51.  
  52.         private static Base[] s_menu = {
  53.             new( 2, "bin" ,"Converts from Binary.", (b) => new(b, 2)),
  54.             new( 8, "oct" ,"Converts from Octal.", (o) =>  new(o, 8)),
  55.             new( 10, "dec","Converts from Decimal.", (d)=> new(d, 10)),
  56.             new( 16, "hex","Converts from Hexadecimal.", (h) => new(h, 16)),
  57.             new( 0, "quit", "Quits the program", (q) => null)
  58.         };
  59.  
  60.         private static void Menu()
  61.         {
  62.             foreach (var mi in s_menu)
  63.             {
  64.                 Console.WriteLine($"{mi.BaseName}\t- {mi.HelpFile}\r\n\t{mi.BaseNum} can also be used.");
  65.             }
  66.         }
  67.  
  68.         private static void Main(string[] args)
  69.         {
  70.             Menu();
  71.             while (true)
  72.             {
  73.                 try
  74.                 {
  75.                     Console.Write("Enter Base to convert from followed by the number: ");
  76.                     string[] input = Console.ReadLine().Split(' ');
  77.  
  78.                     foreach (Base b in s_menu)
  79.                     {
  80.                         string cmd = input[0].ToLower();
  81.                         if (cmd != b.BaseName && cmd != b.BaseNum.ToString())
  82.                             continue;
  83.  
  84.                         NumberRecord n = b.func(input.Length > 1 ? input[1] : null);
  85.                         if (n == null)
  86.                         {
  87.                             Console.WriteLine("Goodbye! :(");
  88.                             return;
  89.                         }
  90.                         Console.WriteLine(n.ToString());
  91.                     }
  92.                 }
  93.                 catch (Exception ex)
  94.                 {
  95.                     Console.WriteLine(ex.Message);
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement