(记数-进制转换-java)用-2为基做进制转换
2012-09-09 09:10
281 查看
这学期有门课叫做工程数学基础,第一章节我们学习一些关于记数的知识。
这里大致有几个概念要知道,第一为基,如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 kit
binaryNumber = new String();
decimalNumber = new String();
System.out.println("徐方鑫特制-超级小型机制转换器");
// create a frame which is the main panel
JFrame frame = new JFrame("徐方鑫设计-must");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// the main panel
JPanel panel = new JPanel();
frame.add(panel);
// i like use the box.expecially the vertical box
Box 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 number
Box 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 number
Box binaryBox = Box.createHorizontalBox();
mainBox.add(binaryBox);
JLabel binaryLabel = new JLabel("Binary:");
binaryField = new JTextField();
binaryBox.add(binaryLabel);
binaryBox.add(binaryField);
// the button start transform
Box 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 calculate
decToBinButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e1)
{
binaryNumber = "";
try{
decimalNumber = deciamlField.getText();
decimal = Long.parseLong(decimalNumber);
// main transform
while(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 transform
StringBuilder 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 visible
frame.setSize(300,140);
frame.setVisible(true);
}
}
这里大致有几个概念要知道,第一为基,如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 kit
binaryNumber = new String();
decimalNumber = new String();
System.out.println("徐方鑫特制-超级小型机制转换器");
// create a frame which is the main panel
JFrame frame = new JFrame("徐方鑫设计-must");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// the main panel
JPanel panel = new JPanel();
frame.add(panel);
// i like use the box.expecially the vertical box
Box 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 number
Box 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 number
Box binaryBox = Box.createHorizontalBox();
mainBox.add(binaryBox);
JLabel binaryLabel = new JLabel("Binary:");
binaryField = new JTextField();
binaryBox.add(binaryLabel);
binaryBox.add(binaryField);
// the button start transform
Box 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 calculate
decToBinButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e1)
{
binaryNumber = "";
try{
decimalNumber = deciamlField.getText();
decimal = Long.parseLong(decimalNumber);
// main transform
while(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 transform
StringBuilder 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 visible
frame.setSize(300,140);
frame.setVisible(true);
}
}
相关文章推荐
- (记数-进制转换-java)用fibonacci数列为基做进制转换
- Java 进制转换的方法
- java常见进制数之间的转换
- Java基础知识强化106:Java中 int 的各进制之间的转换
- POJ 1220 (两种任意进制之间的转换(大数),java的BigInteger)
- java中进制的转换,Byte与16进制的转换
- 11、十进制和N进制的转换 -- JAVA自带函数
- YYMMDD转换成4位字符压缩表示算法java实现——应用各位不等进制的思想
- Java语言基础(关键字、标识符、注释、常量与变量、进制、数据类型、类型转换、代码示例)
- Java算法大全,java进制装换,java日期转换
- Java 各进制间的转换
- 最全面的Java字节byte操作,处理Java基本数据的转换及进制转换操作工具
- java 数据类型-进制,unicode强制转换与隐式转换(1)
- Java常用进制及其转换
- java入门学习笔记,不同进制之间的转换!
- 任意进制转换的总结 (c和java)
- javascript 进制转换(2进制、8进制、10进制、16进制之间的转换)
- 单向链栈实现进制转换(十进制转换成N进制数【正数】)
- [置顶] JAVA概述(6)常量,关键字,进制转换
- JavaSE教程-03Java中分支语句与四种进制转换-思维导图