Advertisement
Otisz

Szöveg

Apr 14th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 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. namespace ConsoleApplication1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             byte opcio = Beolvasás();
  14.  
  15.             Binárisból(opcio);
  16.  
  17.             Oktálisból(opcio);
  18.  
  19.             Decimálisból(opcio);
  20.  
  21.             Hexadecimálisból(opcio);
  22.  
  23.             Bezárás(opcio);
  24.            
  25.             Kilépés();            
  26.         }
  27.  
  28.         private static byte Beolvasás()
  29.         {
  30.             byte opcio = 0;
  31.             do
  32.             {
  33.                 Console.Clear();
  34.                 Console.ForegroundColor = ConsoleColor.White;
  35.                 Console.WriteLine(" 1: Binárisból");
  36.                 Console.WriteLine(" 2: Oktálisból");
  37.                 Console.WriteLine(" 3: Decimálisból");
  38.                 Console.WriteLine(" 4: Hexadecimálisból");
  39.                 Console.ResetColor();
  40.                 Console.ForegroundColor = ConsoleColor.Red;
  41.                 Console.WriteLine(" 5: Bezárás");
  42.                 Console.ResetColor();
  43.                 Console.ForegroundColor = ConsoleColor.White;
  44.                 Console.Write("\n Adja meg (csak) a számát, hogy melyik átváltást szeretné használni: ");
  45.                 try
  46.                 {
  47.                     opcio = byte.Parse(Console.ReadLine());
  48.                 }
  49.                 catch (Exception e)
  50.                 {
  51.                     opcio = Beolvasás();
  52.                 }
  53.                 Console.ResetColor();
  54.  
  55.                 if (opcio > 5)
  56.                 {
  57.                     Console.ForegroundColor = ConsoleColor.Magenta;
  58.                     Console.Write("\n Nincs ilyen opció, nyomjon entert az újrakezdéshez...");
  59.                     Console.ResetColor();
  60.                     Console.ReadLine();
  61.                 }
  62.  
  63.             } while (opcio > 5);
  64.             return opcio;
  65.         }
  66.  
  67.         private static void Binárisból(byte opcio)
  68.         {
  69.             try
  70.             {
  71.                 if (opcio == 1)
  72.                 {
  73.                     string bin;
  74.                     Console.ForegroundColor = ConsoleColor.Cyan;
  75.                     Console.Write("\n" + " Adja meg a bináris számot: ");
  76.                     Console.ResetColor();
  77.                     Console.ForegroundColor = ConsoleColor.Yellow;
  78.                     bin = Convert.ToString(Console.ReadLine());
  79.                     int integer = Convert.ToInt32(bin, 2);
  80.                     Console.ForegroundColor = ConsoleColor.Green;
  81.                     Console.WriteLine("\n" + " A(z) {1} oktálisba átváltva:          {0}", Convert.ToString(integer, 8), bin);
  82.                     Console.WriteLine(" A(z) {1} decimálisba átváltva:        {0}", Convert.ToString(integer, 10), bin);
  83.                     Console.WriteLine(" A(z) {1} hexadecimálisba átváltva:    {0}", Convert.ToString(integer, 16), bin);
  84.                     Console.ResetColor();
  85.                 }
  86.             }
  87.             catch
  88.             {
  89.                 Console.ForegroundColor = ConsoleColor.Red;
  90.                 Console.WriteLine("\n" + " Hibás adat...");
  91.                 Console.ResetColor();
  92.             }
  93.         }
  94.  
  95.         private static void Oktálisból(byte opcio)
  96.         {
  97.             try
  98.             {
  99.                 if (opcio == 2)
  100.                 {
  101.                     string oct;
  102.                     Console.ForegroundColor = ConsoleColor.Cyan;
  103.                     Console.Write("\n" + " Adja meg az oktális számot: ");
  104.                     Console.ResetColor();
  105.                     Console.ForegroundColor = ConsoleColor.Yellow;
  106.                     oct = Convert.ToString(Console.ReadLine());
  107.                     int integer = Convert.ToInt32(oct, 8);
  108.                     Console.ForegroundColor = ConsoleColor.Green;
  109.                     Console.WriteLine("\n" + " A(z) {1} binárisba átváltva:          {0}", Convert.ToString(integer, 2), oct);
  110.                     Console.WriteLine(" A(z) {1} decimálisba átváltva:        {0}", Convert.ToString(integer, 10), oct);
  111.                     Console.WriteLine(" A(z) {1} hexadecimálisba átváltva:    {0}", Convert.ToString(integer, 16), oct);
  112.                     Console.ResetColor();
  113.                 }
  114.             }
  115.             catch
  116.             {
  117.                 Console.ForegroundColor = ConsoleColor.Red;
  118.                 Console.WriteLine("\n" + " Hibás adat...");
  119.                 Console.ResetColor();
  120.             }
  121.         }
  122.  
  123.         private static void Decimálisból(byte opcio)
  124.         {
  125.             try
  126.             {
  127.                 if (opcio == 3)
  128.                 {
  129.                     string dec;
  130.                     Console.ForegroundColor = ConsoleColor.Cyan;
  131.                     Console.Write("\n Adja meg a decimális számot: ");
  132.                     Console.ResetColor();
  133.                     Console.ForegroundColor = ConsoleColor.Yellow;
  134.                     dec = Convert.ToString(Console.ReadLine());
  135.                     int integer = Convert.ToInt32(dec, 10);
  136.                     Console.ForegroundColor = ConsoleColor.Green;
  137.                     Console.WriteLine("\n A(z) {1} binárisba átváltva:          {0}", Convert.ToString(integer, 2), dec);
  138.                     Console.WriteLine(" A(z) {1} oktálisba átváltva:          {0}", Convert.ToString(integer, 8), dec);
  139.                     Console.WriteLine(" A(z) {1} hexadecimálisba átváltva:    {0}", Convert.ToString(integer, 16), dec);
  140.                     Console.ResetColor();
  141.                 }
  142.             }
  143.             catch
  144.             {
  145.                 Console.ForegroundColor = ConsoleColor.Red;
  146.                 Console.WriteLine("\n Hibás adat...");
  147.                 Console.ResetColor();
  148.             }
  149.         }
  150.  
  151.         private static void Hexadecimálisból(byte opcio)
  152.         {
  153.             try
  154.             {
  155.                 if (opcio == 4)
  156.                 {
  157.                     string hex;
  158.                     Console.ForegroundColor = ConsoleColor.Cyan;
  159.                     Console.Write("\n Adja meg a hexadecimális számot: ");
  160.                     Console.ResetColor();
  161.                     Console.ForegroundColor = ConsoleColor.Yellow;
  162.                     hex = Convert.ToString(Console.ReadLine());
  163.                     Console.ResetColor();
  164.                     int integer = Convert.ToInt32(hex, 16);
  165.                     Console.ForegroundColor = ConsoleColor.Green;
  166.                     Console.WriteLine("\n" + " A(z) {1} binárisba átváltva:     {0}", Convert.ToString(integer, 2), hex);
  167.                     Console.WriteLine(" A(z) {1} oktálisba átváltva:     {0}", Convert.ToString(integer, 8), hex);
  168.                     Console.WriteLine(" A(z) {1} decimálisba átváltva:   {0}", Convert.ToString(integer, 10), hex);
  169.                     Console.ResetColor();
  170.                 }
  171.             }
  172.             catch
  173.             {
  174.                 Console.ForegroundColor = ConsoleColor.Red;
  175.                 Console.WriteLine("\n Hibás adat...");
  176.                 Console.ResetColor();
  177.             }
  178.         }
  179.  
  180.         private static void Bezárás(byte opcio)
  181.         {
  182.             if (opcio == 5)
  183.             {
  184.                 Environment.Exit(0);
  185.             }
  186.         }
  187.  
  188.         private static void Kilépés()
  189.         {
  190.             Console.ForegroundColor = ConsoleColor.Red;
  191.             Console.Write("\n Kilépéshez nyomjon entert...");
  192.             Console.ResetColor();
  193.             Console.ReadLine();
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement