首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

(记数-进制变换-java)用-2为基做进制转换

2012-09-14 
(记数-进制转换-java)用-2为基做进制转换这学期有门课叫做工程数学基础,第一章节我们学习一些关于记数的知

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

这学期有门课叫做工程数学基础,第一章节我们学习一些关于记数的知识。

这里大致有几个概念要知道,第一为基,如10进制,10就是基,记做a,其次为数字符号集合s,这个又常常被称为a的完全剩余类,在s和a上存在一些约束关系,例如(未证明),s中最大元素的值小于|a|,s中元素的个数要等于|a|,在正整数领域,而s中的元素未必是一成不变的,如同3进制,我们可以使用0,1,2,同时也可以使用0,1,-1,如此,但是如果不满足以上两个条件,可能会导致,不能够表示整个数系中所有的数,这里需要注意。

同时我们不仅能够用正整数做基,还可以用负数,虚数,无理数,以及fibonacci数列作为基,如下程序(采用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.7.2012 0:30 * small version 0.2 */static JTextField deciamlField;static JTextField binaryField; static long decimal;static long binary;static String decimalNumber;static String binaryNumber;public static void main(String[] args) {// TODO Auto-generated method stub// print function,long time no to use java// to write a kitbinaryNumber = 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 Binary numberBox binaryBox = Box.createHorizontalBox();mainBox.add(binaryBox);JLabel binaryLabel = new JLabel("Binary:");binaryField = new JTextField();binaryBox.add(binaryLabel);binaryBox.add(binaryField);// the button start transformBox buttonBox = Box.createHorizontalBox();mainBox.add(buttonBox);JButton decToBinButton = new JButton("10 to -2");JButton binToDecButton = new JButton("-2 to 10");buttonBox.add(decToBinButton);buttonBox.add(binToDecButton);    // the main function of the calculatedecToBinButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e1){binaryNumber = "";try{decimalNumber = deciamlField.getText();decimal = Long.parseLong(decimalNumber);// main transformwhile(decimal != 1){if((decimal%(-2))==-1) {   //rewrite %,it's very necessary   //System.out.print(decimal - ((-2)*(decimal / (-2) + 1)));   binaryNumber = binaryNumber + String.valueOf(decimal - ((-2)*(decimal / (-2) + 1)));   decimal = decimal / (-2) + 1;}else{   //System.out.print(decimal - ((-2)*(decimal / (-2))));   binaryNumber = binaryNumber + String.valueOf(decimal - ((-2)*(decimal / (-2))));   decimal = decimal / (-2);}}//System.out.print("1");binaryNumber = binaryNumber + "1";// end main transformStringBuilder reverseBinaryNumber = new StringBuilder(binaryNumber);binaryNumber = reverseBinaryNumber.reverse().toString();binaryField.setText(binaryNumber);}catch(Exception ex1){JOptionPane.showMessageDialog(null, "你是在填数字吗?");}}});binToDecButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e2){try{binary = 0;decimal = 0;// x/power(10,n) * power((-2),n)binaryNumber = binaryField.getText();binary = Long.parseLong(binaryNumber);decimal = binary % 10 + decimal;for (int i = binaryNumber.length() - 1; i>0 ; i--){//System.out.println(i);decimal = decimal + ((binary / (long) (Math.pow(10, i)) )* (long)Math.pow(-2, i));binary = binary - (binary / (long) (Math.pow(10, i)))*(long) Math.pow(10, i);//System.out.println(binary);}//System.out.println(decimal);decimalNumber = String.valueOf(decimal);deciamlField.setText(decimalNumber);}catch(Exception ex1){JOptionPane.showMessageDialog(null, "你是在填数字吗?");}}});// start the frame and panel,make it visibleframe.setSize(300,140);frame.setVisible(true);}}


热点排行