Advertisement
Guest User

Suma de octales

a guest
Jul 23rd, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package programa;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Programa {
  7.  
  8.  
  9.     static int a, b;
  10.     public static void main(String[] args)
  11.     {  
  12.        Scanner leer = new Scanner(System.in);
  13.  
  14.        System.out.println("Introduzca el primer octal");
  15.        String valor = leer.nextLine();
  16.        try
  17.        {
  18.             int valord = Integer.parseInt(valor, 8 );
  19.        }
  20.        catch (NumberFormatException e)
  21.        {
  22.            System.out.println("El primer número debe ser octal");
  23.        }
  24.  
  25.        System.out.println("Introduzca el segundo octal");
  26.        String valorb = leer.nextLine();
  27.  
  28.        try
  29.        {
  30.  
  31.             int valordb = Integer.parseInt(valorb, 8 );
  32.        }
  33.        catch (NumberFormatException e)
  34.        {
  35.            System.out.println("El segundo número debe ser octal");
  36.        }
  37.        String res = "";
  38.        int val;
  39.        int mayor = valor.length() >  valorb.length() ? valor.length() : valorb.length();
  40.        int acarreo = 0;
  41.        boolean seguir = true;
  42.        for (int cont = mayor-1; cont >= 0; cont--)
  43.        {
  44.            val = 0;
  45.            try
  46.            {
  47.                a = Character.getNumericValue(valor.charAt(cont));
  48.  
  49.            }
  50.            catch (StringIndexOutOfBoundsException e)
  51.            {  
  52.                 a  = 0;
  53.                 seguir = false;
  54.  
  55.            }
  56.  
  57.            try
  58.            {
  59.  
  60.                b = Character.getNumericValue(valorb.charAt(cont));
  61.  
  62.  
  63.             }
  64.            catch (StringIndexOutOfBoundsException e)
  65.            {  
  66.                 b = 0;
  67.                 seguir = false;
  68.  
  69.            }
  70.  
  71.  
  72.  
  73.                val = a + b;
  74.                if (acarreo > 0)
  75.                {  
  76.                   val += acarreo;
  77.                   acarreo = 0;
  78.                }
  79.                if (val >= 10)
  80.                   acarreo =  val/10;
  81.  
  82.                if (val > 7)
  83.  
  84.                       val -= 8;
  85.  
  86.                  res += val;
  87.  
  88.  
  89.  
  90.                if (!seguir)
  91.                {
  92.                    if (val > 0) res += val;
  93.                    else if (acarreo > 0) res += val;
  94.                }
  95.  
  96.  
  97.        }    
  98.  
  99.  
  100.        res = new StringBuffer(res).reverse().toString();
  101.        System.out.println(res);
  102.  
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement