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

📄 crcframe.java

📁 网路原理实验中的循环冗余校验算法
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.JButton;



public class CrcFrame extends Frame{
	
	int xor(int x,int y)
	{
	        if(x==y)
	                return 0;
	        else
	                return 1;
	}
	int mcount,ma=0;
	int num;
	int pcount=4;
	int qcount=0;
	int rcount=0;
	int[] zero=new int[20];
	int[] M=new int[20];
	int[] P=new int[5];
	int[] Q=new int[20];
	int[] R=new int[20];
	int[] GetM=new int [20];
	JButton Bweishu = new JButton("传输位数(6~10)");
	JButton b1 = new JButton("提交");
	JButton b2 = new JButton("生成码字");
	JButton b3 = new JButton("send(无噪声)");
	JButton b4 = new JButton("send(含噪声)");
	JButton b5 = new JButton("随机生成");
	TextField Tweishu = new TextField();
	TextField mazi = new TextField();
	TextField info = new TextField("info");
	TextField T1 = new TextField();
	TextField T2 = new TextField();
	TextField T3 = new TextField();
	TextField T4 = new TextField();
	TextField T5 = new TextField();
	TextField T6 = new TextField();
	TextField T7 = new TextField();
	TextField T8 = new TextField();
	TextField T9 = new TextField();
	TextField T10 = new TextField();
	ArrayList<TextField> Texts=new ArrayList<TextField>();
	
	public static void main(String[] args) {
		new CrcFrame().View();
		// new ChatClient().reView();
		// new ChatClient().information();
	}
		
		void View() {
			P[0]=1;
			P[1]=0;
			P[2]=0;
			P[3]=1;
			P[4]=1;
			for(int i=0;i<20;i++){
				zero[i]=0;
			}
			int[] zero=new int[20];
			this.setSize(600,450);
			this.setLocation(100, 100);
			this.setVisible(true);
			this.setLayout(null);
			Bweishu.setBounds(80,100,130,20);
			Bweishu.setEnabled(false);
			b1.setBounds(300,100,70,20);
			b2.setBounds(150,200,150,25);
			b3.setBounds(150,300,150,25);
			b4.setBounds(150,250,150,25);
			b5.setBounds(90,150,100,25);
			b5.setVisible(false);
			info.setBounds(310, 300, 200, 25);
			info.setEnabled(false);
			Tweishu.setBounds(220,100,70,20);
			mazi.setBounds(310,200,100,25);
			mazi.setEnabled(false);
			T1.setBounds(200,150,30,20);
			T2.setBounds(240,150,30,20);
			T3.setBounds(280,150,30,20);
			T4.setBounds(320,150,30,20);
			T5.setBounds(360,150,30,20);
			T6.setBounds(400,150,30,20);
			T7.setBounds(440,150,30,20);
			T8.setBounds(480,150,30,20);
			T9.setBounds(520,150,30,20);
			T10.setBounds(560,150,30,20);
			T1.setVisible(false);
			T2.setVisible(false);
			T3.setVisible(false);
			T4.setVisible(false);
			T5.setVisible(false);
			T6.setVisible(false);
			T7.setVisible(false);
			T8.setVisible(false);
			T9.setVisible(false);
			T10.setVisible(false);
			Texts.add(T1);
			Texts.add(T2);
			Texts.add(T3);
			Texts.add(T4);
			Texts.add(T5);
			Texts.add(T6);
			Texts.add(T7);
			Texts.add(T8);
			Texts.add(T9);
			Texts.add(T10);
			
			this.add(Bweishu);
			this.add(Tweishu);
			this.add(mazi);
			this.add(info);
			this.add(b1);
			this.add(b2);
			this.add(b3);
			this.add(b4);
			this.add(b5);
			this.add(T1);
			this.add(T2);
			this.add(T3);
			this.add(T4);
			this.add(T5);
			this.add(T6);
			this.add(T7);
			this.add(T8);
			this.add(T9);
			this.add(T10);
			
			this.setBackground(Color.lightGray);
			b1Lis c1= new b1Lis();
			b2Lis c2= new b2Lis();
			b3Lis c3= new b3Lis();
			b4Lis c4= new b4Lis();
			b5Lis c5= new b5Lis();
			b1.addActionListener(c1);
			b2.addActionListener(c2);
			b3.addActionListener(c3);
			b4.addActionListener(c4);
			b5.addActionListener(c5);
			//this.setResizable(false);
			
			this.setTitle("计科061319 肖亮");
			this.addWindowListener(new MyWindowMonitor());
			
			

		}
		
		class MyWindowMonitor extends WindowAdapter {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				// notconnect();
				System.exit(0);
			}
		}
		
		class b1Lis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				b5.setVisible(false);
				for(int j=0;j<10;j++){
					Texts.get(j).setVisible(false);
				}
				try {
					mcount = Integer.parseInt(Tweishu.getText());
					num = mcount;
					if(mcount<=10&&mcount>=6){
						b5.setVisible(true);
						for(int j=1;j<=mcount;j++){
							Texts.get(j-1).setVisible(true);
						}
					}
				} catch (NumberFormatException e1) {
					
				}
				
			}
		}
		
     class b5Lis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				
				for(int j=0;j<num;j++){
					double x=Math.random();
					int t;
					if(x>0.5)
						t=1;
					else 
						t=0;
					Texts.get(j).setText(""+t);
					if(j==0){
						Texts.get(j).setText("1");
					}
				}
				
				
			}
		}
		
		class b2Lis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				mcount = num;
				ma=0;
				qcount=0;
				for(int i=0;i<mcount;i++){
					int j=Integer.parseInt(Texts.get(i).getText());
					System.out.println(j);
					if(j>1&&j<0){
						ma=0;
						return;
					}
					M[i]=j;
					ma = ma+j*(int)(Math.pow(2,(mcount-1-i)));
				}
				System.out.println(ma);
				
				for(int i=1;i<5;i++){        //补零2nM
		               M[mcount]=0;
		               mcount++;
		        }
				
				for(int i=0;i<=mcount-1-pcount;i++){
	                if(i==0){
	                        for(int k=0;k<=pcount;k++)
	                                R[k]=M[k];        //初次运算
	                }
	           if(R[0]==1){
	                        Q[qcount++]=1;
	                        for(int j=0;j<=pcount;j++){        //位数次异或运算
	                                R[j]=xor(R[j],P[j]);        //异或运算        
	                        }
	                        if(i!=mcount-pcount){
	                                for(int j=0;j<=pcount;j++){        //余数移位及获取m的对


	                                        if(j==pcount)
	                                                R[pcount]=M[i+pcount+1];
	                                        else
	                                                R[j]=R[j+1];
	                                }
	                        }
	                }
	                else{
	                     Q[qcount++]=0;
	                     if(i!=mcount-pcount){
	                     for(int j=0;j<=pcount;j++){        //余数移位及获取m的对

	                    if(j==pcount)
	                       R[pcount]=M[i+pcount+1];
	                    else
	                        R[j]=R[j+1];
	                                }
	                        }
	                }
				}
				mcount = mcount - 4;
				for(int i=0;i<4;i++){
					M[mcount]= R[i];
					mcount++;	
				}
				for(int i=0;i<mcount;i++){
					System.out.print(M[i]);
					
				}
				System.out.println("");
				int num=0;
				String s="";
				for(int i=0;i<mcount;i++){
					s = s+M[i];
				}
				
				mazi.setText(s);
			}	
		}
		
		class b3Lis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<mcount;i++){
					R[i]=zero[i];
				}
				for(int i=0;i<mcount;i++){
					Q[i]=zero[i];
				}
				     //数组清零
				for(int i=0;i<mcount;i++){
					GetM[i]=M[i];
				}
				qcount = 0;
				for(int i=0;i<=mcount-1-pcount;i++){
	                if(i==0){
	                        for(int k=0;k<=pcount;k++)
	                                R[k]=GetM[k];        //初次运算
	                }
	           if(R[0]==1){
	                        Q[qcount++]=1;
	                        for(int j=0;j<=pcount;j++){        //位数次异或运算
	                                R[j]=xor(R[j],P[j]);        //异或运算        
	                        }
	                        if(i!=mcount-pcount){
	                                for(int j=0;j<=pcount;j++){        //余数移位及获取m的对


	                                        if(j==pcount)
	                                                R[pcount]=GetM[i+pcount+1];
	                                        else
	                                                R[j]=R[j+1];
	                                }
	                        }
	                }
	                else{
	                     Q[qcount++]=0;
	                     if(i!=mcount-pcount){
	                     for(int j=0;j<=pcount;j++){        //余数移位及获取m的对

	                    if(j==pcount)
	                       R[pcount]=GetM[i+pcount+1];
	                    else
	                        R[j]=R[j+1];
	                                }
	                    }
	                }
				}
				int count=0;
				for(int i=0;i<4;i++){
					if(R[i]==1){
						count++;
						info.setText("检测到传输数据错误,请求重发");
					}
				}
				if(count==0){
					info.setText("传输正确,发送下一帧数据");
				}
			}	
		}
		
		class b4Lis implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				for(int i=0;i<mcount;i++){
					GetM[i]=M[i];
				}
				qcount = 0;
				int x=(int)(Math.random()*mcount);
				if(GetM[x]==1){
					GetM[x]=0;
				}
				else{
					GetM[x]=1;
				}
				
				for(int i=0;i<mcount;i++){
					System.out.print(GetM[i]);
				}
				for(int i=0;i<mcount;i++){
					R[i]=zero[i];
				}
				for(int i=0;i<mcount;i++){
					Q[i]=zero[i];
				}
				  //数组清零
				for(int i=0;i<=mcount-1-pcount;i++){
	                if(i==0){
	                        for(int k=0;k<=pcount;k++)
	                                R[k]=GetM[k];        //初次运算
	                }
	                if(R[0]==1){
	                        Q[qcount++]=1;
	                        for(int j=0;j<=pcount;j++){        //位数次异或运算
	                                R[j]=xor(R[j],P[j]);        //异或运算        
	                        }
	                        if(i!=mcount-pcount){
	                                for(int j=0;j<=pcount;j++){        //余数移位及获取m的对


	                                        if(j==pcount)
	                                                R[pcount]=GetM[i+pcount+1];
	                                        else
	                                                R[j]=R[j+1];
	                                }
	                        }
	                }
	                else{
	                     Q[qcount++]=0;
	                     if(i!=mcount-pcount){
	                     for(int j=0;j<=pcount;j++){        //余数移位及获取m的对

	                    if(j==pcount)
	                       R[pcount]=GetM[i+pcount+1];
	                    else
	                        R[j]=R[j+1];
	                                }
	                    }
	                }
				}
				int count=0;
				for(int i=0;i<4;i++){
					if(R[i]==1){
						count++;
						info.setText("检测到传输数据错误,请求重发");
					}
				}
				if(count==0){
					info.setText("传输正确,发送下一帧数据");
				}
			}
		}
		
}

⌨️ 快捷键说明

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