Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fib;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.math.BigInteger;
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int term = 0;
- boolean formPrev = false, barePrev = false, copyPrev = true, printPrev = false, hexPrev = false;
- while (true) {
- try {
- String s = sc.nextLine().trim().toLowerCase();
- boolean form = false, bare = false, copy = true, print = false, hex = false;
- if (s.startsWith("f ")) {
- form = true;
- s = s.substring(s.indexOf(" ") + 1);
- } else if (s.startsWith("b ")) {
- bare = true;
- s = s.substring(s.indexOf(" ") + 1);
- } else if (s.startsWith("x ")) {
- hex = true;
- s = s.substring(s.indexOf(" ") + 1);
- }
- if (s.startsWith("p ")) {
- print = true;
- copy = false;
- s = s.substring(s.indexOf(" ") + 1);
- } else if (s.startsWith("h ")) {
- print = true;
- s = s.substring(s.indexOf(" ") + 1);
- }
- if (s.equals("") || s.equals("n")) {
- form = formPrev;
- bare = barePrev;
- copy = copyPrev;
- print = printPrev;
- hex = hexPrev;
- term++;
- } else if (s.equals("r")) {
- } else if (s.equals("s")) {
- term += 2;
- } else if (s.equals("p")) {
- term--;
- } else if (s.startsWith("c ")) {
- String[] parts = s.split(" ");
- try {
- clip(addBig(parts[1], parts[2]).toString());
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println("You must put two numbers to add!");
- }
- continue;
- } else if (s.equals("m")) {
- System.out.println(term);
- continue;
- } else if (s.equals("help")) {
- System.out.println("Number: Clip the nth term.");
- System.out.println("'n' or nothing: Calculate the next term.");
- System.out.println("'r': recalculate the current term.");
- System.out.println("'p': calculate the previous term.");
- System.out.println("'s': calculate the one after the next term.");
- System.out.println("'c [NUMBER] [NUMBER]': add two big numbers.");
- System.out.println("'b [COMMAND]': calculate the nth term, but do not format it.");
- System.out.println("'f [COMMAND]': format the result with spaces.");
- System.out.println("'x [COMMAND]': calculate nth term in hexadecimal.");
- System.out.println("'b' and 'f' can preceed any command that calculates fibonacci numbers.");
- System.out.println("'p': print out the result.");
- System.out.println("'h': copy and print out the result.");
- System.out.println("Options 'p' and 'h' must come after 'b', 'f' or 'x' if they are used.");
- System.out.println("'m': print out the current term.");
- continue;
- } else {
- try {
- term = Integer.parseInt(s);
- } catch (NumberFormatException e) {
- System.out.println("Invalid input! Enter 'help' for valid inputs!");
- }
- }
- if (bare) {
- proc(calc(term).toString(), print, copy);
- } else if (form){
- proc(calcFormatted(term), print, copy);
- } else if (hex){
- proc(convertToHex(calc(term)), print, copy);
- }else {
- proc(calcLine(term), print, copy);
- }
- //Carry the settings over to the next command so that pressing 'n'
- //keeps the settings previously indicated
- formPrev = form;
- barePrev = bare;
- copyPrev = copy;
- printPrev = print;
- hexPrev = hex;
- } catch (NoSuchElementException e) {
- }
- }
- }
- private static void proc(String result, boolean print, boolean copy) {
- if (print) {
- System.out.println(result);
- }
- if (copy) {
- clip(result);
- }
- }
- private static Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
- private static void clip(String s) {
- StringSelection ss = new StringSelection(s);
- c.setContents(ss, null);
- }
- public static BigInteger addBig(String number1, String number2) {
- BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
- return num1.add(num2);
- }
- public static BigInteger skipStep(String number1, String number2) {
- BigInteger num1 = new BigInteger(number1), num2 = new BigInteger(number2);
- BigInteger firstStep = num1.add(num2);
- return firstStep.add(num1.max(num2)); //This skips a step in the Fibonacci sequence, by adding the bigger number twice.
- }
- public static BigInteger calc(int term) {
- BigInteger num1 = new BigInteger("1"), num2 = new BigInteger("2");
- for (int x = 3; x < term; x++) {
- num1 = num1.add(num2);
- x++;
- if (x == term) {
- return num1;
- }
- num2 = num2.add(num1);
- }
- return num2;
- }
- public static String calcLine(int term) {
- return "F(" + term + ") =\n\n" + calc(term);
- }
- public static String calcFormatted(int term){
- StringBuilder sb = new StringBuilder();
- sb.append("F(");
- sb.append(term);
- sb.append(") =\n\n");
- char[] chars = calc(term).toString().toCharArray();
- for (int x = 0; x < chars.length; x++){
- sb.append(chars[x]);
- if ((x + 1) % 8 == 0){
- sb.append(' ');
- }
- }
- return sb.toString();
- }
- //Hexadecimal converter added by Chickendodo
- public static String convertToHex(BigInteger num1){
- BigInteger quotient = new BigInteger("0"), remainder = new BigInteger("0");
- BigInteger[] numAndRemainder = num1.divideAndRemainder(new BigInteger("16"));
- StringBuffer numInHex = new StringBuffer();
- do{
- quotient = numAndRemainder[0];
- remainder = numAndRemainder[1];
- numInHex = toHex(remainder).append(numInHex);
- numAndRemainder = quotient.divideAndRemainder(new BigInteger("16"));
- } while (quotient.compareTo(new BigInteger("16")) != -1);
- numInHex = new StringBuffer(quotient.toString()).append(numInHex);
- return numInHex.toString();
- }
- public static StringBuffer toHex(BigInteger num1){
- StringBuffer num1Hex = new StringBuffer("");
- for (int i = 0; i < 10; i++){
- if (num1.intValue() == i){
- num1Hex.append(i);
- }
- }
- if (num1.intValue() == 10){
- num1Hex.append("A");
- }
- else if (num1.intValue() == 11){
- num1Hex.append("B");
- }
- else if (num1.intValue() == 12){
- num1Hex.append("C");
- }
- else if (num1.intValue() == 13){
- num1Hex.append("D");
- }
- else if (num1.intValue() == 14){
- num1Hex.append("E");
- }
- else if (num1.intValue() == 15){
- num1Hex.append("F");
- }
- return num1Hex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement