Advertisement
GraionDilach

Számrendszer

Feb 22nd, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             // rekurzív konverzió
  13.             // C# stringátadás alapértelmezetten ÉRTÉK SZERINTI... WTF?!
  14.  
  15.            
  16.             bool ell;
  17.             int BaseNumber;
  18.             int BaseNumberSystem;
  19.             String NewNumber = "";
  20.  
  21.             do {
  22.                 Console.Write("Kérem a konvertálni kívánt számot: ");
  23.                 ell = IntManualParse(Console.ReadLine(), out BaseNumber);
  24.             } while (!ell);
  25.  
  26.             do
  27.             {
  28.                 Console.Write("Kérem a számrendszert: ");
  29.                 ell = IntManualParse(Console.ReadLine(), out BaseNumberSystem);
  30.             } while (!ell || BaseNumberSystem < 1 || BaseNumberSystem > 9);
  31.  
  32.             ConvertToNewNumberSystem(BaseNumber, ref NewNumber, BaseNumberSystem);
  33.  
  34.             Console.WriteLine("\n" + BaseNumber + " tízes számrendszerbeli szám alakja " + NewNumber + " a " + BaseNumberSystem + "-as számrendszerben.\n");
  35.            
  36.  
  37.         }
  38.  
  39.  
  40.        
  41.         static void ConvertToNewNumberSystem( int Number, ref String Output, int NumberSystem)
  42.         {
  43.             if (Number < 0)
  44.             {
  45.                 Number = Number * -1;
  46.                 Output += '-';
  47.             }
  48.             int i = Number % NumberSystem;
  49.             if ((Number / NumberSystem) != 0)
  50.             {
  51.                 ConvertToNewNumberSystem(Number/NumberSystem, ref Output, NumberSystem);
  52.             }
  53.  
  54.             Output = Output + i;
  55.         }
  56.  
  57.  
  58.         static bool IntManualParse( String Input, out int Output) {
  59.             Output = 0;
  60.             if (Input.Length == 0) { return false; }
  61.             int i = 0;
  62.             if (Input[0] == '-' || Input[0] == '+')
  63.             {
  64.                 i++;
  65.             }
  66.  
  67.             if (Input.Length - i > 8)
  68.             {
  69.                 return false;
  70.             }
  71.  
  72.             for (; i < Input.Length; i++)
  73.             {
  74.                 if (Input[i] <= '9' && Input[i] >= '0')
  75.                     {
  76.                         Output = Output * 10;
  77.                         Output += (Input[i] - '0');
  78.                     }
  79.                 else
  80.                     {
  81.                         return false;
  82.                     }
  83.            }
  84.             if (Input[0] == '-')
  85.             {
  86.                 Output = Output*-1;
  87.             }
  88.  
  89.             return true;
  90.         }
  91.  
  92.        
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement