Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- // rekurzív konverzió
- // C# stringátadás alapértelmezetten ÉRTÉK SZERINTI... WTF?!
- bool ell;
- int BaseNumber;
- int BaseNumberSystem;
- String NewNumber = "";
- do {
- Console.Write("Kérem a konvertálni kívánt számot: ");
- ell = IntManualParse(Console.ReadLine(), out BaseNumber);
- } while (!ell);
- do
- {
- Console.Write("Kérem a számrendszert: ");
- ell = IntManualParse(Console.ReadLine(), out BaseNumberSystem);
- } while (!ell || BaseNumberSystem < 1 || BaseNumberSystem > 9);
- ConvertToNewNumberSystem(BaseNumber, ref NewNumber, BaseNumberSystem);
- Console.WriteLine("\n" + BaseNumber + " tízes számrendszerbeli szám alakja " + NewNumber + " a " + BaseNumberSystem + "-as számrendszerben.\n");
- }
- static void ConvertToNewNumberSystem( int Number, ref String Output, int NumberSystem)
- {
- if (Number < 0)
- {
- Number = Number * -1;
- Output += '-';
- }
- int i = Number % NumberSystem;
- if ((Number / NumberSystem) != 0)
- {
- ConvertToNewNumberSystem(Number/NumberSystem, ref Output, NumberSystem);
- }
- Output = Output + i;
- }
- static bool IntManualParse( String Input, out int Output) {
- Output = 0;
- if (Input.Length == 0) { return false; }
- int i = 0;
- if (Input[0] == '-' || Input[0] == '+')
- {
- i++;
- }
- if (Input.Length - i > 8)
- {
- return false;
- }
- for (; i < Input.Length; i++)
- {
- if (Input[i] <= '9' && Input[i] >= '0')
- {
- Output = Output * 10;
- Output += (Input[i] - '0');
- }
- else
- {
- return false;
- }
- }
- if (Input[0] == '-')
- {
- Output = Output*-1;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement