转一个二进制和十进制之间的转换程序

package sxpi.demo;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BintoDec {

	public static void main(String[] args) {

		SimpleFrame frame = new SimpleFrame();
		frame.setLocation(200, 250);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setTitle("2to10&10to2");

		// BtoD();

	}

}

class SimpleFrame extends JFrame {
	public SimpleFrame() {
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		JPanel totalpanel = new JPanel();
		totalpanel.setLayout(new GridLayout(4, 1));
		JPanel textpanel1 = new JPanel();
		JPanel textpanel2 = new JPanel();
		JPanel buttonpanel = new JPanel();
		JPanel settingpanel = new JPanel();
		settingpanel.setLayout(new GridLayout(1, 1));
		textpanel1.setLayout(new GridLayout(2, 1));
		textpanel2.setLayout(new GridLayout(2, 1));
		buttonpanel.setLayout(new FlowLayout());
		// 按钮
		JButton jb1 = new JButton("2to10");
		JButton jb2 = new JButton("10to2");

		// 标签
		JLabel jl1 = new JLabel("输入:");
		JLabel jl2 = new JLabel("输出:");
		JLabel jl3 = new JLabel("设置输出小数位数: ");

		// 文本框
		final JTextField jtf1 = new JTextField();
		final JTextField jtf3 = new JTextField();
		jtf1.setColumns(25);
		jtf1.setColumns(3);
		settingpanel.add(jl3);
		settingpanel.add(jtf3);
		textpanel1.add(jl1);
		textpanel1.add(jtf1);
		final JTextField jtf2 = new JTextField();
		jtf2.setColumns(25);

		// 二进制转为十进制
		jb1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = jtf1.getText();
				Double d = BtoD(s);
				jtf2.setText(d.toString());
			}

			private double BtoD(String s) {
				String b = new String(s);
				char[] bm;
				int e = 0; // 整数长度
				int d = 0; // 小数长度
				bm = b.toCharArray();
				double c = 0; // 小数
				double f = 0;// 整数
				int j = 0;
				while (j < bm.length && bm[j] != '.') {
					j++;
					e++;
				}
				d = bm.length - 1 - e;

				// 整数
				for (int i = 0; i < e; i++)

				{
					// System.out.println(bm[i]);
					f = f + (bm[i] - 48) * Math.pow(2, e - i - 1);
				}
				// 小数
				for (int i = e + 1; i < b.length(); i++) {
					c = c + (bm[i] - 48) * Math.pow(2, -(i - e));
				}

				return c + f;
				// TODO 自动生成方法存根

			}
		});

		// 十进制转为二进制
		jb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = jtf1.getText();
				// DtoB(s);
				String sss = DtoB(s);
				jtf2.setText(sss.toString());
			}

			private String DtoB(String s) {

				String b = new String(s);
				char[] bm;
				bm = b.toCharArray();
				int set = Integer.parseInt(jtf3.getText());
				char[] cm = new char[set];
				int a[] = new int[10];
				int c = 0; // 整数长度
				int d = 0; // 小数长度
				int i = 0;
				Integer cc = 0; // 整数部分
				Double dd = 0.0; // 小数部分
				double total = Double.parseDouble(s);

				String strzhengshu;
				int decc;
				while (i < bm.length && bm[i] != '.') {
					i++;
					c++;
				}
				d = bm.length - 1 - c;
				// 整数部分的转化
				for (int j = 0; j < c; j++) {
					cc = (int) (cc + (bm[j] - 48) * 1 * Math.pow(10, c - j - 1));

				}
				String str = cc.toString();
				decc = Integer.parseInt(str, 10);
				strzhengshu = Integer.toBinaryString(decc);

				// 小数部分的转化
				dd = total - cc;
				System.out.print(set);
				for (int k = 0; k < set; k++) {

					if (dd * 2 >= 1) {
						cm[k] = '1';
						dd = dd * 2 - 1;
					} else {
						cm[k] = '0';
						dd = dd * 2;

					}
				}

				String strxiaoshu = "";
				for (int l = 0; l < set; l++) {

					strxiaoshu += cm[l];

				}
				String str5 = "";
				str5 = strzhengshu + "." + strxiaoshu;
				return str5;

			}

		});
		textpanel2.add(jl2);
		textpanel2.add(jtf2);

		buttonpanel.add(jb1);
		buttonpanel.add(jb2);
		totalpanel.add(settingpanel);
		totalpanel.add(textpanel1);
		totalpanel.add(textpanel2);
		totalpanel.add(buttonpanel);
		Container ct = getContentPane();
		ct.add(totalpanel);

	}

	public static final int DEFAULT_WIDTH = 300;
	public static final int DEFAULT_HEIGHT = 200;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值