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

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

这里大致有几个概念要知道,第一为基,如10进制,10就是基,记做a,其次为数字符号集合s,这个又常常被称为a的完全剩余类,在sa上存在一些约束关系,例如(未证明),s中最大元素的值小于|a|,s中元素的个数要等于|a|,在正整数领域,而s中的元素未必是一成不变的,如同3进制,我们可以使用012,同时也可以使用01,-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);

	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值