Advertisement
Guest User

fibonacci(w__hex_and_formatkeeping).java

a guest
Jun 12th, 2012
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.04 KB | None | 0 0
  1. package fib;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.Clipboard;
  5. import java.awt.datatransfer.StringSelection;
  6. import java.math.BigInteger;
  7. import java.util.NoSuchElementException;
  8. import java.util.Scanner;
  9.  
  10. public class Main {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner sc = new Scanner(System.in);
  14.         int term = 0;
  15.         boolean formPrev = false, barePrev = false, copyPrev = true, printPrev = false, hexPrev = false;
  16.         while (true) {
  17.             try {
  18.                 String s = sc.nextLine().trim().toLowerCase();
  19.                 boolean form = false, bare = false, copy = true, print = false, hex = false;
  20.                 if (s.startsWith("f ")) {
  21.                     form = true;
  22.                     s = s.substring(s.indexOf(" ") + 1);
  23.                 } else if (s.startsWith("b ")) {
  24.                     bare = true;
  25.                     s = s.substring(s.indexOf(" ") + 1);
  26.                 } else if (s.startsWith("x ")) {
  27.                     hex = true;
  28.                     s = s.substring(s.indexOf(" ") + 1);
  29.                 }
  30.                 if (s.startsWith("p ")) {
  31.                     print = true;
  32.                     copy = false;
  33.                     s = s.substring(s.indexOf(" ") + 1);
  34.                 } else if (s.startsWith("h ")) {
  35.                     print = true;
  36.                     s = s.substring(s.indexOf(" ") + 1);
  37.                 }
  38.                 if (s.equals("") || s.equals("n")) {
  39.                     form = formPrev;
  40.                     bare = barePrev;
  41.                     copy = copyPrev;
  42.                     print = printPrev;
  43.                     hex = hexPrev;
  44.                     term++;
  45.                 } else if (s.equals("r")) {
  46.                 } else if (s.equals("s")) {
  47.                     term += 2;
  48.                 } else if (s.equals("p")) {
  49.                     term--;
  50.                 } else if (s.startsWith("c ")) {
  51.                     String[] parts = s.split(" ");
  52.                     try {
  53.                         clip(addBig(parts[1], parts[2]).toString());
  54.                     } catch (ArrayIndexOutOfBoundsException e) {
  55.                         System.out.println("You must put two numbers to add!");
  56.                     }
  57.                     continue;
  58.                 } else if (s.equals("m")) {
  59.                     System.out.println(term);
  60.                     continue;
  61.                 } else if (s.equals("help")) {
  62.                     System.out.println("Number: Clip the nth term.");
  63.                     System.out.println("'n' or nothing: Calculate the next term.");
  64.                     System.out.println("'r': recalculate the current term.");
  65.                     System.out.println("'p': calculate the previous term.");
  66.                     System.out.println("'s': calculate the one after the next term.");
  67.                     System.out.println("'c [NUMBER] [NUMBER]': add two big numbers.");
  68.                     System.out.println("'b [COMMAND]': calculate the nth term, but do not format it.");
  69.                     System.out.println("'f [COMMAND]': format the result with spaces.");
  70.                     System.out.println("'x [COMMAND]': calculate nth term in  hexadecimal.");
  71.                     System.out.println("'b' and 'f' can preceed any command that calculates fibonacci numbers.");
  72.                     System.out.println("'p': print out the result.");
  73.                     System.out.println("'h': copy and print out the result.");
  74.                     System.out.println("Options 'p' and 'h' must come after 'b', 'f' or 'x' if they are used.");
  75.                     System.out.println("'m': print out the current term.");
  76.                     continue;
  77.                 } else {
  78.                     try {
  79.                         term = Integer.parseInt(s);
  80.                     } catch (NumberFormatException e) {
  81.                         System.out.println("Invalid input! Enter 'help' for valid inputs!");
  82.                     }
  83.                 }
  84.                
  85.                
  86.                 if (bare) {
  87.                     proc(calc(term).toString(), print, copy);
  88.                 } else if (form){
  89.                     proc(calcFormatted(term), print, copy);
  90.                 } else if (hex){
  91.                     proc(convertToHex(calc(term)), print, copy);
  92.                 }else {
  93.                     proc(calcLine(term), print, copy);
  94.                 }
  95.                
  96.                 //Carry the settings over to the next command so that pressing 'n'
  97.                 //keeps the settings previously indicated
  98.                 formPrev = form;
  99.                 barePrev = bare;
  100.                 copyPrev = copy;
  101.                 printPrev = print;
  102.                 hexPrev = hex;
  103.             } catch (NoSuchElementException e) {
  104.             }
  105.         }
  106.     }
  107.  
  108.     private static void proc(String result, boolean print, boolean copy) {
  109.         if (print) {
  110.             System.out.println(result);
  111.         }
  112.         if (copy) {
  113.             clip(result);
  114.         }
  115.     }
  116.     private static Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
  117.  
  118.     private static void clip(String s) {
  119.         StringSelection ss = new StringSelection(s);
  120.         c.setContents(ss, null);
  121.     }
  122.  
  123.     public static BigInteger addBig(String number1, String number2) {
  124.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  125.         return num1.add(num2);
  126.     }
  127.  
  128.     public static BigInteger skipStep(String number1, String number2) {
  129.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  130.         BigInteger firstStep = num1.add(num2);
  131.         return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
  132.     }
  133.  
  134.     public static BigInteger calc(int term) {
  135.         BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
  136.         for (int x = 3; x < term; x++) {
  137.             num1 = num1.add(num2);
  138.             x++;
  139.             if (x == term) {
  140.                 return num1;
  141.             }
  142.             num2 = num2.add(num1);
  143.         }
  144.         return num2;
  145.     }
  146.  
  147.     public static String calcLine(int term) {
  148.         return "F(" + term + ") =\n\n" + calc(term);
  149.     }
  150.    
  151.     public static String calcFormatted(int term){
  152.         StringBuilder sb = new StringBuilder();
  153.         sb.append("F(");
  154.         sb.append(term);
  155.         sb.append(") =\n\n");
  156.         char[] chars = calc(term).toString().toCharArray();
  157.         for (int x = 0; x < chars.length; x++){
  158.             sb.append(chars[x]);
  159.             if ((x + 1) % 8 == 0){
  160.                 sb.append(' ');
  161.             }
  162.         }
  163.         return sb.toString();
  164.     }
  165.      
  166.      //Hexadecimal converter added by Chickendodo
  167.     public static String convertToHex(BigInteger num1){
  168.         BigInteger quotient = new BigInteger("0"), remainder = new BigInteger("0");
  169.         BigInteger[] numAndRemainder = num1.divideAndRemainder(new BigInteger("16"));
  170.         StringBuffer numInHex = new StringBuffer();
  171.            
  172.         do{
  173.         quotient = numAndRemainder[0];
  174.         remainder = numAndRemainder[1];
  175.                
  176.         numInHex = toHex(remainder).append(numInHex);
  177.                
  178.         numAndRemainder = quotient.divideAndRemainder(new BigInteger("16"));
  179.         } while (quotient.compareTo(new BigInteger("16")) != -1);
  180.            
  181.         numInHex = new StringBuffer(quotient.toString()).append(numInHex);
  182.            
  183.         return numInHex.toString();
  184.     }
  185.    
  186.     public static StringBuffer toHex(BigInteger num1){
  187.         StringBuffer num1Hex = new StringBuffer("");
  188.            
  189.         for (int i = 0; i < 10; i++){
  190.             if (num1.intValue() == i){
  191.                 num1Hex.append(i);
  192.             }
  193.         }
  194.         if (num1.intValue() == 10){
  195.             num1Hex.append("A");
  196.         }
  197.         else if (num1.intValue() == 11){
  198.             num1Hex.append("B");
  199.         }
  200.         else if (num1.intValue() == 12){
  201.             num1Hex.append("C");
  202.         }
  203.         else if (num1.intValue() == 13){
  204.             num1Hex.append("D");
  205.         }
  206.         else if (num1.intValue() == 14){
  207.             num1Hex.append("E");
  208.         }
  209.         else if (num1.intValue() == 15){
  210.             num1Hex.append("F");
  211.         }
  212.                    
  213.         return num1Hex;
  214.     }
  215.    
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement