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

📄 unit1.~pas

📁 crc任意位生成多项式 任意位运算 自适应算法 循环冗余校验码(CRC
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jpeg, ExtCtrls, ImgList, IdBaseComponent, IdComponent,
  IdIPWatch;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    Memo2: TMemo;
    Edit3: TEdit;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2, Unit3;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
 data=array[0..60] of Integer;
 data2=array[0..60,0..60] of Integer;
var
i,j,n,m,jinwei,l1,l2,zhongjian,l3:integer;
str,str2:string;
Gx:data2;
Mx,xx:data;
begin
    l1:=length(form1.Edit1.Text);
    l2:=length(form1.Edit2.Text);
    l3:=l1+l2;
    for i:=1 to l3-1 do
    begin     //       提取
        //x:=form1.edit1.TBDockHeight;
        //str:='whild'+'100';
        //str;=string(form1.Edit1);
        //showmessage('hennl');
        //form1.listbox1.
        //form1.listbox1.Items.Add(str);
        if i<=length(form1.Edit1.Text) then          //Gx付值
          begin
            if form1.Edit1.Text<>'' then
            begin
            str:=copy(form1.Edit1.Text,i,1);
              if (str='0')or (str='1') then
                begin
                  Gx[1,i]:=strtoint(copy(form1.Edit1.Text,i,1));
                  //str2:=str2+inttostr(Gx[1,i]);
                  //Str2:=str2+#13; {一行处理完成后,加上回车控制符}
                end
              else
                begin
                      showmessage('请输入  传输码元Gx');
                      exit;
                      Application.ProcessMessages;
                      //MessageBox('This   should   be   on   top.',   'Look',   mb_OK);
                      //break;

                end
            end
          end
        else
          begin
            Gx[1,i]:=0;
          end;
        if i<=length(form1.Edit2.Text) then           //Mx复制
          begin
              str:=copy(form1.Edit2.Text,i,1);
              if (copy(form1.Edit2.Text,1,1)='1') and (str='0')or (str='1')  then
                begin
                 Mx[i]:= strtoint (copy(form1.Edit2.Text,i,1));
                end
              else
                begin
                      showmessage('请输入  生成多项式Mx');
                      exit;
                      Application.ProcessMessages;
                      //MessageBox('This   should   be   on   top.',   'Look',   mb_OK);
                      //break;
                end
          end;
        //form1.listbox1.Items.Add(inttostr(Gx[i]));          {打印}
        //Memo1.lines.text:=str2;
    end;

//计算//////////////////////////

for i:=1 to l2  do
    Gx[2,i]:=Gx[1,i];    //g(i+l1)=0;      %补齐了


i:=1;jinwei:=0;
while jinwei<=l1  do
begin
    j:=1;
    n:=1;
    xx[1]:=0;
    

  while j<l1+l2 do                          //%异或运算g(i+1,jinwei+j)=xor( g(i,jinwei+j),m(j));
   begin
    if j<=l2 then
        begin
        Gx[i+1,jinwei+j]:=Mx[j];               //%m负值
        //Gx[i+2,jinwei+j]:=xor(Gx[i,jinwei+j],Mx[j]);  //%异或
        if Gx[i,jinwei+j]<>Mx[j] then
          begin
            Gx[i+2,jinwei+j]:=1;
            xx[n]:=j;
            n:=n+1;
          end
        else
          begin
            Gx[i+2,jinwei+j]:=0;
          end
        end
    else
		if j+jinwei<=l1  then
        begin
          Gx[i+2,jinwei+j]:=Gx[1,jinwei+j];             //%g负值
        end;
     j:=j+1;
  end;



  j:=1;
 if xx[1]=0  then
   begin
     xx[1]:=l2+1;
     while   Gx[i+2,jinwei+j]=0 do
       begin
         jinwei:=jinwei+1;
       end;
   end
 else
 begin
     jinwei:=jinwei+xx[1]-1;
 end;

 i:=i+2;
end;




///////////////////////////////


///////////打印

for i:=1 to l1+l2+1 do {利用双循环组织数组ary2的输出串}
  Begin
      j:=1; n:=1;xx[1]:=0;
   {   for j:=1 to l1+l2-1 do
        begin
          if Gx[i,j]=1 then
            begin
              xx[n]:=j;
              n:=n+1;
            end;
          if xx[1]>l1-l2  then
            begin
              //break;   //跳出for循环
              //continue;//跳出单次循环
              exit;   //跳出整个函数
            end;
        end;}

    For j:=1 to l1+l2-1 do {内循环完成一行上数据的输出串处理}
      begin
      str2:=str2+inttostr(Gx[i,j]);
          if Gx[i,j]=1 then
            begin
              xx[n]:=j;
              n:=n+1;
            end;
          if xx[1]>l1  then
            begin
            //break;
            if l1>l2 then
              //break;   //跳出for循环
              //continue;//跳出单次循环
              //exit;   //跳出整个函数
            end;
    end;
    form1.listbox1.Items.Add(inttostr(xx[1]));
    
    //if (xx[1]=0)  then
    if (xx[1]=0) or (xx[1]>l1) then
      break;
    str2:=str2+#13; {一行处理完成后,加上回车控制符}
    End;
  str2:=str2+'    此行后'+inttostr(l2-1)+'即为校验位';
  Memo1.lines.text:=str2; {在memo1显示ary2数组的内容}
  Showmessage(str2);
    {if xx[1]>l1-l2  then
        begin
          break;   //跳出for循环
        end;}
End;

procedure TForm1.Button2Click(Sender: TObject);
type
 data=array[0..40] of Integer;
 data2=array[0..40,0..40] of Integer;
var
i,j,n,m,jinwei,l1,l2,zhongjian,l3:integer;
str,str2:string;
Gx:data2;
Mx,xx:data;
begin
    l1:=length(form1.Edit1.Text);
    l2:=length(form1.Edit2.Text);
    l3:=l1+l2;
    for i:=1 to l3 do
    begin     //       提取
        //x:=form1.edit1.TBDockHeight;
        //str:='whild'+'100';
        //str;=string(form1.Edit1);
        //showmessage('hennl');
        //form1.listbox1.
        //form1.listbox1.Items.Add(str);
        if  (i<=length(form1.Edit3.Text)) then          //Gx付值
          begin
            if form1.Edit1.Text<>'' then
             begin
            str:=copy(form1.Edit3.Text,i,1);
              if (length(form1.Edit3.Text)=l3-1) and ((str='0') or (str='1')) then
                begin
                  Gx[1,i]:=strtoint(copy(form1.Edit3.Text,i,1));
                  //str2:=str2+inttostr(Gx[1,i]);
                  //Str2:=str2+#13; {一行处理完成后,加上回车控制符}
                end
              else
                begin
                      showmessage('位数不对 请输入实收数据');
                      exit;
                      Application.ProcessMessages;
                      //MessageBox('This   should   be   on   top.',   'Look',   mb_OK);
                      //break;

                end;
            end;
          end
        else
          begin

            //showmessage(copy(form1.Edit3.Text,i,1));
 {           str:=copy(form1.Edit3.Text,i,1);
              if (length(form1.Edit3.Text)=length(form1.Edit2.Text)-1) and not(str='0') or (str='1') then
                begin
                  Gx[1,i]:=strtoint(copy(form1.Edit3.Text,i-l1,1));
                  //str2:=str2+inttostr(Gx[1,i]);
                  //Str2:=str2+#13; {一行处理完成后,加上回车控制符}
 {               end
              else
                begin
                      showmessage('校验马有误');
                      exit;
                      //Application.ProcessMessages;
                      //MessageBox('This   should   be   on   top.',   'Look',   mb_OK);
                      //break;
                end    }
          end;
        if i<=length(form1.Edit2.Text) then           //Mx复制
          begin
              str:=copy(form1.Edit2.Text,i,1);
              if (copy(form1.Edit2.Text,1,1)='1') and (str='0')or (str='1')  then
                begin
                 Mx[i]:= strtoint (copy(form1.Edit2.Text,i,1));
                end
              else
                begin
                      showmessage('请输入  生成多项式Mx');
                      exit;
                      Application.ProcessMessages;
                      //MessageBox('This   should   be   on   top.',   'Look',   mb_OK);
                      //break;
                end
          end;
        //form1.listbox1.Items.Add(inttostr(Gx[i]));          {打印}
        //Memo1.lines.text:=str2;
    end;

//计算//////////////////////////

for i:=1 to l2  do
    Gx[2,i]:=Gx[1,i];    //g(i+l1)=0;      %补齐了


i:=1;jinwei:=0;
while jinwei<=l1  do
begin
    j:=1;
    n:=1;
    xx[1]:=0;
    

  while j<l1+l2 do                          //%异或运算g(i+1,jinwei+j)=xor( g(i,jinwei+j),m(j));
   begin
    if j<=l2 then
        begin
        Gx[i+1,jinwei+j]:=Mx[j];               //%m负值
        //Gx[i+2,jinwei+j]:=xor(Gx[i,jinwei+j],Mx[j]);  //%异或
        if Gx[i,jinwei+j]<>Mx[j] then
          begin
            Gx[i+2,jinwei+j]:=1;
            xx[n]:=j;
            n:=n+1;
          end
        else
          begin
            Gx[i+2,jinwei+j]:=0;
          end
        end
    else
		  if j+jinwei<=l3  then
        begin
          Gx[i+2,jinwei+j]:=Gx[1,jinwei+j];             //%g负值
        end;
     j:=j+1;
  end;



  j:=1;
 if xx[1]=0  then
   begin
     xx[1]:=l2+1;
     while   Gx[i+2,jinwei+j]=0 do
       begin
         jinwei:=jinwei+1;
       end;
   end
 else
 begin
     jinwei:=jinwei+xx[1]-1;
 end;

 i:=i+2;
end;




///////////////////////////////


///////////打印

for i:=1 to l1+l2+2 do {利用双循环组织数组ary2的输出串}
  Begin
      j:=1;xx[1]:=0;n:=1;
   {   for j:=1 to l1+l2-1 do
        begin
          if Gx[i,j]=1 then
            begin
              xx[n]:=j;
              n:=n+1;
            end;
          if xx[1]>l1-l2  then
            begin
              //break;   //跳出for循环
              //continue;//跳出单次循环
              exit;   //跳出整个函数
            end;
        end;}

    For j:=1 to l1+l2-1 do {内循环完成一行上数据的输出串处理}
     begin
      str2:=str2+inttostr(Gx[i,j]);
          if Gx[i-1,j]<>Gx[i-2,j] then
            begin
              xx[n]:=j;
              n:=n+1;
              //break;
            end
          else
            xx[l1+l2]:=xx[l1+l2]+1;

          if xx[1]>l1  then
            begin
              //break;
            if l1>l2 then
              //break;   //跳出for循环
              //continue;//跳出单次循环
              //exit;   //跳出整个函数
            end;

    end;
    form1.listbox1.Items.Add(inttostr(xx[l1+l2]));

    //if (xx[1]=0)  then
    if  xx[l1+l2]=l1+l2-1 then
      break;
    if  (xx[1]>l1) then
      break;
    ;str2:=str2+#13; {一行处理完成后,加上回车控制符}
    xx[l1+l2]:=0;
    End;
  str2:=str2+'    此行若为0,则校验成功,数据传输没出错' ;
  Memo2.lines.text:=str2; {在memo1显示ary2数组的内容}
  Showmessage(str2);
    {if xx[1]>l1-l2  then
        begin
          break;   //跳出for循环
        end;}
End;

procedure TForm1.Button3Click(Sender: TObject);
begin
   Form2.Show;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
      Form1.close;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
         Form3.Show;
end;

end.

⌨️ 快捷键说明

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