Advertisement
Bloodshot

Fibonacci Calculator

Jun 11th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.53 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.         while (true) {
  16.             try {
  17.                 String s = sc.nextLine().trim().toLowerCase();
  18.                 boolean form = false, bare = false, copy = true, print = false;
  19.                 if (s.startsWith("f ")) {
  20.                     form = true;
  21.                     s = s.substring(s.indexOf(" ") + 1);
  22.                 } else if (s.startsWith("b ")) {
  23.                     bare = true;
  24.                     s = s.substring(s.indexOf(" ") + 1);
  25.                 }
  26.                 if (s.startsWith("p ")) {
  27.                     print = true;
  28.                     copy = false;
  29.                     s = s.substring(s.indexOf(" ") + 1);
  30.                 } else if (s.startsWith("h ")) {
  31.                     print = true;
  32.                     s = s.substring(s.indexOf(" ") + 1);
  33.                 }
  34.                 if (s.equals("") || s.equals("n")) {
  35.                     term++;
  36.                 } else if (s.equals("r")) {
  37.                 } else if (s.equals("s")) {
  38.                     term += 2;
  39.                 } else if (s.equals("p")) {
  40.                     term--;
  41.                 } else if (s.startsWith("c ")) {
  42.                     String[] parts = s.split(" ");
  43.                     try {
  44.                         clip(addBig(parts[1], parts[2]).toString());
  45.                     } catch (ArrayIndexOutOfBoundsException e) {
  46.                         System.out.println("You must put two numbers to add!");
  47.                     }
  48.                     continue;
  49.                 } else if (s.equals("m")) {
  50.                     System.out.println(term);
  51.                     continue;
  52.                 } else if (s.equals("help")) {
  53.                     System.out.println("Number: Clip the nth term.");
  54.                     System.out.println("'n' or nothing: Calculate the next term.");
  55.                     System.out.println("'r': recalculate the current term.");
  56.                     System.out.println("'p': calculate the previous term.");
  57.                     System.out.println("'s': calculate the one after the next term.");
  58.                     System.out.println("'c [NUMBER] [NUMBER]': add two big numbers.");
  59.                     System.out.println("'b [COMMAND]': calculate the nth term, but do not format it.");
  60.                     System.out.println("'f [COMMAND]': format the result with spaces.");
  61.                     System.out.println("'b' and 'f' can preceed any command that calculates fibonacci numbers.");
  62.                     System.out.println("'p': print out the result.");
  63.                     System.out.println("'h': copy and print out the result.");
  64.                     System.out.println("Options 'p' and 'h' must come after 'b' or 'f' if they are used.");
  65.                     System.out.println("'m': print out the current term.");
  66.                     continue;
  67.                 } else {
  68.                     try {
  69.                         term = Integer.parseInt(s);
  70.                     } catch (NumberFormatException e) {
  71.                         System.out.println("Invalid input! Enter 'help' for valid inputs!");
  72.                     }
  73.                 }
  74.                 if (bare) {
  75.                     proc(calc(term).toString(), print, copy);
  76.                 } else if (form){
  77.                     proc(calcFormatted(term), print, copy);
  78.                 } else {
  79.                     proc(calcLine(term), print, copy);
  80.                 }
  81.             } catch (NoSuchElementException e) {
  82.             }
  83.         }
  84.     }
  85.  
  86.     private static void proc(String result, boolean print, boolean copy) {
  87.         if (print) {
  88.             System.out.println(result);
  89.         }
  90.         if (copy) {
  91.             clip(result);
  92.         }
  93.     }
  94.     private static Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
  95.  
  96.     private static void clip(String s) {
  97.         StringSelection ss = new StringSelection(s);
  98.         c.setContents(ss, null);
  99.     }
  100.  
  101.     public static BigInteger addBig(String number1, String number2) {
  102.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  103.         return num1.add(num2);
  104.     }
  105.  
  106.     public static BigInteger skipStep(String number1, String number2) {
  107.         BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
  108.         BigInteger firstStep = num1.add(num2);
  109.         return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
  110.     }
  111.  
  112.     public static BigInteger calc(int term) {
  113.         BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
  114.         for (int x = 3; x < term; x++) {
  115.             num1 = num1.add(num2);
  116.             x++;
  117.             if (x == term) {
  118.                 return num1;
  119.             }
  120.             num2 = num2.add(num1);
  121.         }
  122.         return num2;
  123.     }
  124.  
  125.     public static String calcLine(int term) {
  126.         return "F(" + term + ") =\n\n" + calc(term);
  127.     }
  128.    
  129.     public static String calcFormatted(int term){
  130.         StringBuilder sb = new StringBuilder();
  131.         sb.append("F(");
  132.         sb.append(term);
  133.         sb.append(") =\n\n");
  134.         char[] chars = calc(term).toString().toCharArray();
  135.         for (int x = 0; x < chars.length; x++){
  136.             sb.append(chars[x]);
  137.             if ((x + 1) % 8 == 0){
  138.                 sb.append(' ');
  139.             }
  140.         }
  141.         return sb.toString();
  142.     }
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement