Advertisement
mrevilca31

TP 2 Punto 6 (2018)

Sep 24th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 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 TP2Punto6
  8. {
  9.     class Program
  10.     {
  11.        
  12.         static void cambioBase(int num)
  13.         {
  14.             Stack<int> valor = new Stack<int>();
  15.             int Base = 2, aux = num;
  16.  
  17.             while (aux>0)
  18.             {
  19.                 valor.Push(aux % Base);
  20.                 aux = aux / Base;
  21.             }
  22.  
  23.             Console.WriteLine("\nEn Base 10: {0}", num);
  24.             Console.Write("\nEn Base 2: ");
  25.             while (valor.Count>0)
  26.             {
  27.                 Console.Write(valor.Pop());
  28.             }
  29.         }
  30.  
  31.         static bool validarEntero(string cadena)
  32.         {
  33.             bool val = false;
  34.             try
  35.             {
  36.                 int aux = int.Parse(cadena);
  37.                 return !val;
  38.             }
  39.             catch (FormatException f)
  40.             {
  41.                 Console.WriteLine("\n\nERROR: El dato ingresado es incorrecto..");
  42.             }
  43.             return val;
  44.         }
  45.  
  46.  
  47.         static void Main(string[] args)
  48.         {
  49.             string cadena;
  50.             int resp=0;
  51.  
  52.             do
  53.             {
  54.                 Console.Clear();
  55.                 Console.Write("Ingrese Número en Base 10: ");
  56.                 cadena = Console.ReadLine();
  57.                 if (validarEntero(cadena))
  58.                 {
  59.                     int num = int.Parse(cadena);
  60.                     cambioBase(num);
  61.                 }
  62.  
  63.                 Console.WriteLine("\n\n1- Volver a ingresar número.");
  64.                 Console.WriteLine("2- Salir.");
  65.                 cadena = Console.ReadLine();
  66.                 if (validarEntero(cadena))
  67.                 {
  68.                     resp = int.Parse(cadena);  
  69.                 }
  70.                
  71.             } while (resp!=2);
  72.  
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement