📄 shannon.java
字号:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
class FrameTry extends JFrame implements ActionListener
{
JTextField tf;
Label lbout,lbbianma;
JLabel lbsum;
JButton btadd,btbianma,btclear;
double x[]=new double[100];
double sum=0;
int count=0;
JTextField tfym;
JButton btyima;
Label yima;
String str[]=new String[100];
public FrameTry()
{
Container c=getContentPane();
c.setLayout(null);
Label lb1=new Label("输入信源");
lb1.setSize(60,20);
lb1.setLocation(10,10);
c.add(lb1);
tf=new JTextField("");
tf.setSize(60,20);
tf.setLocation(70,10);
c.add(tf);
btadd=new JButton("添加");
btadd.setSize(60,20);
btadd.setLocation(150,10);
btadd.addActionListener(this);
c.add(btadd);
JLabel lb2=new JLabel("信源和:");
lb2.setSize(60,20);
lb2.setLocation(10,40);
c.add(lb2);
lbsum=new JLabel("0");
lbsum.setSize(80,20);
lbsum.setLocation(60,40);
c.add(lbsum);
btbianma=new JButton("编码");
btbianma.setSize(60,20);
btbianma.setLocation(150,40);
btbianma.addActionListener(this);
c.add(btbianma);
btclear=new JButton("清空");
btclear.setSize(60,20);
btclear.setLocation(220,40);
btclear.addActionListener(this);
c.add(btclear);
Label lb3=new Label("所输入信源如下:");
lb3.setSize(300,20);
lb3.setLocation(10,70);
c.add(lb3);
lbout=new Label("");
lbout.setSize(200,20);
lbout.setLocation(10,100);
c.add(lbout);
Label lb4=new Label("对应编码如下:");
lb4.setSize(300,20);
lb4.setLocation(10,130);
c.add(lb4);
lbbianma=new Label();
lbbianma.setSize(200,20);
lbbianma.setLocation(10,160);
c.add(lbbianma);
Label lalyima=new Label("输入译码");
lalyima.setSize(60,20);
lalyima.setLocation(10,200);
c.add(lalyima);
tfym=new JTextField();
tfym.setSize(60,20);
tfym.setLocation(80,200);
c.add(tfym);
btyima=new JButton("译码");
btyima.setSize(60,20);
btyima.setLocation(160,200);
btyima.addActionListener(this);
c.add(btyima);
JLabel labl=new JLabel("信源为:");
labl.setSize(60,20);
labl.setLocation(10,240);
c.add(labl);
yima=new Label();
yima.setSize(100,20);
yima.setLocation(80,240);
c.add(yima);
setSize(400,400);
setTitle("香农编码");
c.setBackground(Color.white);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btadd)
{
try
{
String str=tf.getText();
String strsub;
if((str.substring(1,2)).equals(".") && str.substring(0,1).equals("0"))
{
strsub=str.substring(2,str.length());
double i=Integer.parseInt(strsub);
for(int k=0;k<strsub.length();k++)
i=i/10;
sum=sum+i;
if(sum<=1)
{
lbsum.setText(sum+"");
x[count]=i;
count++;
tf.setText("");
String xinyuan="";
}
else
{
sum=sum-i;
JOptionPane.showMessageDialog(null,"信源和不能大于1 !");
tf.setText("");
}
}
else
{
JOptionPane.showMessageDialog(null,"信源为小数!");
tf.setText("");
}
}
catch(Exception e2)
{
JOptionPane.showMessageDialog(null,"信源为小数!");
tf.setText("");
}
}
if(e.getSource()==btbianma)
{
if(sum<1)
{
JOptionPane.showMessageDialog(null,"信源必须归一!");
}
else
{
for(int i=0;i<count;i++)
{
double t=x[i];
for(int k=i+1;k<count;k++)
{
if(t<x[k])
{
t=x[k];
x[k]=x[i];
x[i]=t;
}
}
}
int len[]=new int[count];
double gailv[]=new double[count];
gailv[0]=0;
for(int i=0;i<count;i++)
{
double n=1/x[i];
int c=0;
int t=1;
while(t<n)
{
t=t*2;
c++;
}
len[i]=c;
}
for(int i=0;i<count-1;i++)
{
gailv[i+1]=x[i]+gailv[i];
}
String bm="";
String gl="";
for(int i=0;i<count;i++)
{
int c=0;
String st="";
double a=gailv[i]*2;
while(c<len[i])
{
if(a<1)
{
a=a*2;
st=st+"0";
}
else
{
a=(a-1)*2;
st=st+"1";
}
c++;
}
str[i]=st;
bm=bm+st+" ";
gl=gl+x[i]+" ";
}
lbout.setText(gl);
lbbianma.setText(bm);
}
}
if(e.getSource()==btclear)
{
lbsum.setText("0");lbout.setText("");lbbianma.setText("");
double x[]=new double[100];
sum=0;
count=0;
tfym.setText("");
yima.setText("");
for(int i=0;i<100;i++)
x[i]=0;
}
if(e.getSource()==btyima)
{
String strb=tfym.getText();
int k=0;
for(int i=0;i<count;i++)
{
if(strb.equals(str[i]))
{
k=i;
break;
}
}
yima.setText(x[k]+"");
}
}
}
class Shannon
{
public static void main(String args[])
{
new FrameTry();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -