Advertisement
stoianpp

Numeral 7

Dec 27th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤  16).
  8.  
  9. class ConvertAnyToAny
  10. {
  11.     static void Main()
  12.     {
  13.         Console.Write("Enter the entrance number base: ");
  14.         int s = int.Parse(Console.ReadLine());
  15.         Console.Write("Enter a number in the chosen numeral system: ");
  16.         string numberInput = Console.ReadLine();
  17.         Console.Write("Enter the result number base: ");
  18.         int d = int.Parse(Console.ReadLine());
  19.         int decimalInput = SToDecimal(numberInput, s);
  20.         DecimalToD(decimalInput, d);
  21.     }
  22.  
  23.     static void DecimalToD(int number,int d)
  24.     {
  25.         StringBuilder printNum = new StringBuilder();
  26.         while (number != 0)
  27.         {
  28.             char addingChar = ' ';
  29.             switch (number % d)
  30.             {
  31.                 case 0: addingChar = '0'; break;
  32.                 case 1: addingChar = '1'; break;
  33.                 case 2: addingChar = '2'; break;
  34.                 case 3: addingChar = '3'; break;
  35.                 case 4: addingChar = '4'; break;
  36.                 case 5: addingChar = '5'; break;
  37.                 case 6: addingChar = '6'; break;
  38.                 case 7: addingChar = '7'; break;
  39.                 case 8: addingChar = '8'; break;
  40.                 case 9: addingChar = '9'; break;
  41.                 case 10: addingChar = 'A'; break;
  42.                 case 11: addingChar = 'B'; break;
  43.                 case 12: addingChar = 'C'; break;
  44.                 case 13: addingChar = 'D'; break;
  45.                 case 14: addingChar = 'E'; break;
  46.                 case 15: addingChar = 'F'; break;
  47.             }
  48.             printNum.Insert(0,addingChar);
  49.             number /= d;
  50.         }
  51.         Console.WriteLine(printNum.ToString());
  52.     }
  53.  
  54.     static int SToDecimal(string input,int s)
  55.     {
  56.         int result = 0;
  57.         for (int i = 0; i < input.Length; i++)
  58.         {
  59.             result += Powing(input[input.Length-(i+1)], i, s);
  60.         }
  61.         return result;
  62.     }
  63.  
  64.     static int Powing(char inputInt, int i, int s)
  65.     {
  66.         if (inputInt != '0')
  67.         {
  68.             int result = 1;
  69.             switch (inputInt)
  70.             {
  71.                 case '0': result = 0; break;
  72.                 case '1': result = 1; break;
  73.                 case '2': result = 2; break;
  74.                 case '3': result = 3; break;
  75.                 case '4': result = 4; break;
  76.                 case '5': result = 5; break;
  77.                 case '6': result = 6; break;
  78.                 case '7': result = 7; break;
  79.                 case '8': result = 8; break;
  80.                 case '9': result = 9; break;
  81.                 case 'A': result = 10; break;
  82.                 case 'B': result = 11; break;
  83.                 case 'C': result = 12; break;
  84.                 case 'D': result = 13; break;
  85.                 case 'E': result = 14; break;
  86.                 case 'F': result = 15; break;
  87.             }
  88.             for (int g = 0; g < i; g++)
  89.             {
  90.                 result *= s;
  91.             }
  92.             return result;
  93.         }
  94.         return 0;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement