⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code.java

📁 通信原理编码器设计
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class code {
	public static void main(String[] args) 
	{
		codeFrame frame=new codeFrame();
		frame.setTitle("二进制编码器");
		frame.setBounds(200, 100,400,300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

class codeFrame extends JFrame
{
	public codeFrame()
	{
		codePanel panel=new codePanel();
		Container content=getContentPane();
		content.add(panel);
		
	}
}

class codePanel extends JPanel implements ActionListener
{
	String str1=new String("双向码:");
	String str2=new String("密勒码:");
	String str3=new String(" CMI码:");
	String str4=new String("AMI码:");
	String str5=new String("HDB3码");
	String str6=new String("差分码:");
	JLabel label=new JLabel("请输入:");
	JLabel codeLabel1=new JLabel(str1);
	JLabel codeLabel2=new JLabel(str2);
	JLabel codeLabel3=new JLabel(str3);
	JLabel codeLabel4=new JLabel(str4);
	JLabel codeLabel5=new JLabel(str5);
	JLabel codeLabel6=new JLabel(str6);
	TextField number=new TextField(33);
	TextField toWrite1=new TextField(34);
	TextField toWrite2=new TextField(34);
	TextField toWrite3=new TextField(34);
	TextField toWrite4=new TextField(34);
	TextField toWrite5=new TextField(33);
	TextField toWrite6=new TextField(33);
	JButton button=new JButton("ALL");
	JButton button1=new JButton("转换");
	JButton button2=new JButton("转换");	
	JButton button3=new JButton("转换");	
	JButton button4=new JButton("转换");	
	JButton button5=new JButton("转换");	
	JButton button6=new JButton("转换");	
	
	private static String codeInput="";
	public codePanel()
	{
		setLayout(new FlowLayout());
		add(label);
		add(number);
		add(button);button.addActionListener(this);
		add(codeLabel1);
		add(toWrite1);toWrite1.setEditable(false);
		add(button1);button1.addActionListener(this);
		add(codeLabel2);
		add(toWrite2);toWrite2.setEditable(false);
		add(button2);button2.addActionListener(this);
		add(codeLabel3);
		add(toWrite3);toWrite3.setEditable(false);
		add(button3);button3.addActionListener(this);
		add(codeLabel4);
		add(toWrite4);toWrite4.setEditable(false);
		add(button4);button4.addActionListener(this);
		add(codeLabel5);
		add(toWrite5);toWrite5.setEditable(false);
		add(button5);button5.addActionListener(this);
		add(codeLabel6);
		add(toWrite6);toWrite6.setEditable(false);
		add(button6);button6.addActionListener(this);
		add(new Label("声明:版权所有,侵权必究!————举报电话:13969651126"));
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==button)
		{
			String code=getManchester();
			toWrite1.setText(code);
			code=getMiller();
			toWrite2.setText(code);
			code=getCMI();
			toWrite3.setText(code);
			code=getAMI();
			toWrite4.setText(code);
			code=getHDB3();
			toWrite5.setText(code);
			code=getDivide();
			toWrite6.setText(code);
		}
		if(e.getSource()==button1)
		{
			String code=getManchester();
			toWrite1.setText(code);
		}
		if(e.getSource()==button2)
		{
			String code=getMiller();
			toWrite2.setText(code);
		}
		if(e.getSource()==button3)
		{
			String code=getCMI();
			toWrite3.setText(code);
		}
		if(e.getSource()==button4)
		{
			String code=getAMI();
			toWrite4.setText(code);
		}
		if(e.getSource()==button5)
		{
			String code=getHDB3();
			toWrite5.setText(code);
		}
		if(e.getSource()==button6)
		{
			String code=getDivide();
			toWrite6.setText(code);
		}
	}
	public String getManchester()
	{
		String codeManchester="";
		String toAppend="";
		codeInput=number.getText();
		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
				toAppend=" ̄_";
			else if(str==0)
				toAppend="_ ̄";
			else 
				{
				codeManchester="请输入二进制代码!!!";
				break;
				}
			
			
			codeManchester+=toAppend;
		}
		return codeManchester;
		
	}
	public String getMiller()
	{
		String codeMiller="";
		String toAppend="";
		codeInput=number.getText();
		boolean blowCheck=false;
		boolean singleCheck=false;
		
		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
			{
				if(blowCheck==false)
				{
					toAppend=" ̄_";
					blowCheck=true;
				}
				else
				{
					toAppend="_ ̄";
					blowCheck=false;
				}
				singleCheck=true;
			}
			else if(str==0)
			{
				if(singleCheck==true)
				{
					if(blowCheck==true)
						toAppend="__";
					else 
						toAppend=" ̄ ̄";
					singleCheck=false;
				}
				else
				{
					if(blowCheck==true)
					{
						toAppend=" ̄ ̄";
						blowCheck=false;
					}
					else
					{
						toAppend="__";
						blowCheck=true;
					}
				}
			}
			else 
			{
			codeMiller="请输入二进制代码!!!";
			break;
			}
			
			codeMiller+=toAppend;
		}
		return codeMiller;
	}
	public String getCMI()
	{
		String codeCMI="";
		String toAppend="";
		codeInput=number.getText();
		boolean oneCheck=false;
		
		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
			{
				if(oneCheck==false)
				{
					toAppend=" ̄ ̄";
					oneCheck=true;
				}
				else
				{
					toAppend="__";
					oneCheck=false;
				}
			}
			else if(str==0)
				toAppend="_ ̄";
			else 
			{
			codeCMI="请输入二进制代码!!!";
			break;
			}
			codeCMI+=toAppend;	
		}
		
		return codeCMI;
	}
	public String getAMI()
	{
		String codeAMI="";
		String toAppend="";
		codeInput=number.getText();
		boolean oneCheck=false;
		
		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
			{
				if(oneCheck==false)
				{
					toAppend="_";
					oneCheck=true;
				}
				else
				{
					toAppend=" ̄";
					oneCheck=false;
				}
			}
			else if(str==0)
			{
				toAppend="―";
			}
			else
			{
				codeAMI="请输入二进制代码!!!";
				break;
			}
			codeAMI+=toAppend;
		}
		return codeAMI;
	}
	public String getHDB3()
	{
		String codeHDB3="";
		codeInput=number.getText();
		boolean oneCheck=true;
		boolean vCheck=true;
		boolean zeroMultiCheck=false;
		int bCheck=0;
		int zeroCount=0;

		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
			{
				if(oneCheck==true)
				{
					codeHDB3+="_";
					oneCheck=false;
				}
				else
				{
					codeHDB3+=" ̄";
					oneCheck=true;
				}
				zeroMultiCheck=false;
				zeroCount=0;
			}
			else if(str==0)
			{
				if(zeroCount!=3)
				{
					codeHDB3+="―";
					zeroCount+=1;
				}
				else
				{
					codeHDB3=codeHDB3.substring(0,codeHDB3.length()-3);
					if(zeroMultiCheck==false)
					{
						if(oneCheck==false&&vCheck==false)
						{
							codeHDB3+=" ̄―― ̄";
							bCheck=1;vCheck=true;
						}
						else if(oneCheck==false&&vCheck==true)
						{
							codeHDB3+="―――_";
							bCheck=0;vCheck=false;
						}
						else if(oneCheck==true&&vCheck==true)
						{
							codeHDB3+="_――_";
							bCheck=-1;vCheck=false;
						}
						else
						{
							codeHDB3+="――― ̄";
							bCheck=0;vCheck=true;
						}	
						zeroMultiCheck=true;
					}
					else 
					{
						if((bCheck==1&&vCheck==true)|(bCheck==0&&vCheck==true))
						{
							codeHDB3+="_――_";
							bCheck=-1;vCheck=false;
						}
						if((bCheck==0&&vCheck==false)|(bCheck==-1&&vCheck==false))
						{
							codeHDB3+=" ̄―― ̄";
							bCheck=1;vCheck=true;
						}
					}
					zeroCount=0;
				}
			}
			else 
				{
					codeHDB3="请输入二进制代码!!!";
					break;
				}
		}
		return codeHDB3;
	}
	public String getDivide()
	{
		String codeDivide="";
		String toAppend="";
		boolean blowCheck=false;//默认协议初始码“_ ̄”
		
		codeInput=number.getText();
		byte [] toDeal=codeInput.getBytes();
		for(int i=0;i<toDeal.length;i++)
		{
			int str=Integer.parseInt(Byte.toString(toDeal[i]))-48;
			if(str==1)
			{
				if(blowCheck==true)
					toAppend=" ̄_";
				else
					toAppend="_ ̄";
			}
			else if(str==0)
			{
				if(blowCheck==true)
				{
					toAppend="_ ̄";
					blowCheck=false;
				}
				else
				{
					toAppend=" ̄_";
					blowCheck=true;
				}
			}
			else
			{
				codeDivide="请输入二进制代码!!!";
				break;
			}
			codeDivide+=toAppend;
		 }
		return codeDivide;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -