Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2.  
  3. public class Core
  4. {
  5.  
  6.     public static string ConvertTo(int value, int newBase)
  7.     {
  8.         string Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9.         string result = "";
  10.         int remainder = 0;      //the rest from dividing
  11.  
  12.         if (value == 0)
  13.             return "0";
  14.  
  15.         while (value > 0)
  16.         {
  17.             result = Chars[value % newBase] + result;
  18.             Console.WriteLine("\t\t"+value + "\t/\t" + newBase + " | " + result[remainder]);
  19.             value /= newBase;
  20.         }
  21.         return result;
  22.     }
  23.  
  24.     public static int ConvertFrom(string number, int oldBase)
  25.     {
  26.         const int ASCII_LETTER_TO_VALUE = 55;
  27.         const int ASCII_DIGIT_TO_VALUE = 48;
  28.         int power = 0;
  29.         int sum = 0;
  30.  
  31.         foreach (char digit in number)
  32.         {
  33.             if (!(char.IsUpper(digit) || char.IsDigit(digit)))
  34.                 throw new Exception($"Character {digit} is out of conversion range! Use digits or upper letters.");
  35.  
  36.             int digitValue = char.IsDigit(digit) ? digit - ASCII_DIGIT_TO_VALUE : digit - ASCII_LETTER_TO_VALUE;
  37.  
  38.             if (digitValue > (oldBase - 1))
  39.                 throw new Exception($"Digit value: {digitValue} is greater than numeric base!");
  40.  
  41.             sum = sum + ((int)Math.Pow(oldBase, power) * digitValue);
  42.  
  43.             Console.WriteLine($"\t\t | {oldBase} ^ {power} * {digit} = {Math.Pow(oldBase, power) * digitValue} \t||{sum}");
  44.  
  45.             power = power + 1;
  46.         }
  47.         Console.WriteLine("\t\n\nWynik pośredni, dziesiętnie: {0}\n\n",sum);
  48.  
  49.         return sum;
  50.     }
  51.     public static void Input(int firstBase, int secondBase)
  52.     {
  53.         Console.Clear();
  54.         Console.WriteLine("\n\nPodaj liczbę w systemie "+ firstBase +"-owym do przeliczenia: ");
  55.         String nvalue = Console.ReadLine();
  56.         int jeden = ConvertFrom(nvalue, firstBase);
  57.         string dwa = ConvertTo(jeden, secondBase);
  58.         Console.WriteLine();
  59.         Console.WriteLine();
  60.         Console.WriteLine(dwa);
  61.     }
  62.     public static void Main()
  63.     {
  64.        
  65.         bool status = false;
  66.         while (!status)
  67.         {
  68.             Console.WriteLine("\nPrzelicznik systemów liczbowych obejmujący system trójkowy i siódemkowy");
  69.             Console.WriteLine("\n\n\t1 - Z trójkowego na siódemkowy.\n\t2 - Z siódemkowego na trójkowy.\n\t3 - Koniec programu.");
  70.             ConsoleKeyInfo button = Console.ReadKey();
  71.             switch (button.Key)
  72.             {
  73.                 case ConsoleKey.D1:
  74.                     Input(3, 7);
  75.                     break;
  76.                 case ConsoleKey.D2:
  77.                     Input(7, 3);
  78.                     break;
  79.                 case ConsoleKey.D3:
  80.                     status = true;
  81.                     break;
  82.                 default:
  83.                     Console.WriteLine("\nNieprawidłowy wybór");
  84.                     break;
  85.             }
  86.         }      
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement