Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Core
- {
- public static string ConvertTo(int value, int newBase)
- {
- string Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string result = "";
- int remainder = 0; //the rest from dividing
- if (value == 0)
- return "0";
- while (value > 0)
- {
- result = Chars[value % newBase] + result;
- Console.WriteLine("\t\t"+value + "\t/\t" + newBase + " | " + result[remainder]);
- value /= newBase;
- }
- return result;
- }
- public static int ConvertFrom(string number, int oldBase)
- {
- const int ASCII_LETTER_TO_VALUE = 55;
- const int ASCII_DIGIT_TO_VALUE = 48;
- int power = 0;
- int sum = 0;
- foreach (char digit in number)
- {
- if (!(char.IsUpper(digit) || char.IsDigit(digit)))
- throw new Exception($"Character {digit} is out of conversion range! Use digits or upper letters.");
- int digitValue = char.IsDigit(digit) ? digit - ASCII_DIGIT_TO_VALUE : digit - ASCII_LETTER_TO_VALUE;
- if (digitValue > (oldBase - 1))
- throw new Exception($"Digit value: {digitValue} is greater than numeric base!");
- sum = sum + ((int)Math.Pow(oldBase, power) * digitValue);
- Console.WriteLine($"\t\t | {oldBase} ^ {power} * {digit} = {Math.Pow(oldBase, power) * digitValue} \t||{sum}");
- power = power + 1;
- }
- Console.WriteLine("\t\n\nWynik pośredni, dziesiętnie: {0}\n\n",sum);
- return sum;
- }
- public static void Input(int firstBase, int secondBase)
- {
- Console.Clear();
- Console.WriteLine("\n\nPodaj liczbę w systemie "+ firstBase +"-owym do przeliczenia: ");
- String nvalue = Console.ReadLine();
- int jeden = ConvertFrom(nvalue, firstBase);
- string dwa = ConvertTo(jeden, secondBase);
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine(dwa);
- }
- public static void Main()
- {
- bool status = false;
- while (!status)
- {
- Console.WriteLine("\nPrzelicznik systemów liczbowych obejmujący system trójkowy i siódemkowy");
- Console.WriteLine("\n\n\t1 - Z trójkowego na siódemkowy.\n\t2 - Z siódemkowego na trójkowy.\n\t3 - Koniec programu.");
- ConsoleKeyInfo button = Console.ReadKey();
- switch (button.Key)
- {
- case ConsoleKey.D1:
- Input(3, 7);
- break;
- case ConsoleKey.D2:
- Input(7, 3);
- break;
- case ConsoleKey.D3:
- status = true;
- break;
- default:
- Console.WriteLine("\nNieprawidłowy wybór");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement