(记数-进制转换-java)用fibonacci数列为基做进制转换

来源:互联网 发布:阿里云邮箱 ssl端口 编辑:程序博客网 时间:2024/06/02 05:11

如同上一篇解释http://blog.csdn.net/fzxy002763/article/details/7959555的一些基本概念,这里我们采用fibonacci做为基

根据zeckendorf定理,这里如果j>>(意思为远大于,至少大两个)k  ->  j>k+2,那么任何一个正整数都有唯一表示,不过在程序里面没有用到这个。

注意fibonacci作为基这没有重复项,基初始几个为0 1 2 3 5 8 13。。。。这样,不存在1 1的重复,需要注意,代码还是java的

import javax.print.DocFlavor.STRING;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.IOException;import java.io.StringWriter;import java.math.*;public class transform {/** * make by Edward.xu    * 9.8.2012 0:24 * small version 0.3 */// the O(finbonacciNumber.length()*n)static JTextField deciamlField;static JTextField finbonacciField; static long decimal;static long finbonacci;static long decimalPrint; static String decimalNumber;static String finbonacciNumber;// debug use// it's a loop flagstatic int i;public static void main(String[] args) {// TODO Auto-generated method stub// print function,long time no to use java// to write a kitfinbonacciNumber = new String();decimalNumber = new String();System.out.println("徐方鑫特制-超级小型机制转换器");// create a frame which is the main panelJFrame frame = new JFrame("徐方鑫设计-must");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// the main panelJPanel panel = new JPanel();frame.add(panel);// i like use the box.expecially the vertical boxBox mainBox = Box.createVerticalBox();panel.add(mainBox);// to prove my write/*JLabel verionLabel = new JLabel("Version 0.1");JLabel schoolLabel = new JLabel("Master of must 2012");JLabel nameLabel = new JLabel("Made by Edward.xu");mainBox.add(verionLabel);mainBox.add(schoolLabel);mainBox.add(nameLabel);*/// the first box which tip this is decimal numberBox deciamlBox = Box.createHorizontalBox();mainBox.add(deciamlBox);JLabel deciamlLabel = new JLabel("Deciaml:");deciamlField = new JTextField();deciamlBox.add(deciamlLabel);deciamlBox.add(deciamlField);// the second box which tip this is finbonacci numberBox finbonacciBox = Box.createHorizontalBox();mainBox.add(finbonacciBox);JLabel finbonacciLabel = new JLabel("finbonacci:");finbonacciField = new JTextField();finbonacciBox.add(finbonacciLabel);finbonacciBox.add(finbonacciField);// the button start transformBox buttonBox = Box.createHorizontalBox();mainBox.add(buttonBox);JButton decToBinButton = new JButton("10 to Fibonacci");JButton binToDecButton = new JButton("Fibonacci to 10");buttonBox.add(decToBinButton);buttonBox.add(binToDecButton);    // the main function of the calculatedecToBinButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e1){finbonacciNumber = "";try{decimalNumber = deciamlField.getText();decimal = Long.parseLong(decimalNumber);if(decimal > 1000) throw new Exception();// main transform// calculate the top bitfor(i=0 ; calFibonacci(i) < decimal ; i++);if(calFibonacci(i) == decimal) i = i;else if(calFibonacci(i) > decimal) i = i-1;//System.out.print("i "+i);// end calculate the tip bit// that's notice i use the function// like i can choice what bit is 1// that change bit data to the number// is too 2,but funny// to use the function like change to the decimalfinbonacci = (long)Math.pow(10, i-1);finbonacciNumber = String.valueOf(finbonacci);finbonacciField.setText(finbonacciNumber);// update the new decimal which had minus the top bitdecimal = decimal - calFibonacci(i);//System.out.println("   the decimal least:"+decimal);// warning because of the long limit// add my lazy,it's time to go to bed// so i won't to change it to the string// that cause it's only can change the number // below 1000// even it can be solve , but i won't to do while(decimal !=0){// use the same function calculate the bit secondfor(i=0 ; calFibonacci(i) < decimal ; i++);if(calFibonacci(i) == decimal) i = i;else if(calFibonacci(i) > decimal) i = i-1;//System.out.print("i2 "+i);decimal = decimal - calFibonacci(i);//System.out.println("   the decimal least2:"+decimal);finbonacci = finbonacci + (long)Math.pow(10, i-1);finbonacciNumber = String.valueOf(finbonacci);finbonacciField.setText(finbonacciNumber);}}catch(Exception ex1){JOptionPane.showMessageDialog(null, "你是在填数字吗?(注意不要大于1000),这可以改良,但是我懒得改了,注意,转换为显示的时候为乘10,改良就直接按照字符11这样打印,不要用Long类型做保存,用string,这样添加就行了。。。挺晚了,想睡了,不改了");}}});binToDecButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e2){try{finbonacci = 0;decimal = 0;decimalPrint = 0;finbonacciNumber = finbonacciField.getText();finbonacci = Long.parseLong(finbonacciNumber);// notice that's is decimalPrintdecimalPrint = finbonacci % 10;for (int i = finbonacciNumber.length() - 1; i>0 ; i--){// the calculate this bitdecimal = finbonacci / (long) (Math.pow(10, i)) ;finbonacci = finbonacci - (finbonacci / (long) (Math.pow(10, i)))*(long) Math.pow(10, i);// calculate// different between -2 basement// the 10 is act as calFibonacci(2),not calFinbonacci(1)// it's must be warningdecimalPrint = decimalPrint + decimal * calFibonacci(i + 1);//System.out.println("decimal:"+decimal+" i:"+i);}System.out.println("decimalPrint:"+decimalPrint);//System.out.println(decimal);decimalNumber = String.valueOf(decimalPrint);deciamlField.setText(decimalNumber);}catch(Exception ex1){JOptionPane.showMessageDialog(null, "你是在填数字吗?");}}});// start the frame and panel,make it visibleframe.setSize(300,140);frame.setVisible(true);}//that's to calculate the Fibonacci serialspublic static long calFibonacci(int n){long fibonacci_one,fibonacci_two;fibonacci_one = 0;fibonacci_two = 1;for(int i = 0 ; i < n ; i++){long temp;// to simple is// temp = b// b = b + a// a = temptemp = fibonacci_two;fibonacci_two = fibonacci_two + fibonacci_one ;fibonacci_one = temp;//System.out.println(" "+fibonacci_two+" ");}if(n == 0) return 0;else if(n == 1) return 1;else if(n == 2) return 2;else return fibonacci_two;}}