unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var StandardText:TMemo; i,j,WrongNum:integer; inputLine,StandardLine:String; begin WrongNum := 0; StandardText := TMemo.Create(self); StandardText.ParentWindow := self.Handle; StandardText.Visible := false; StandardText.Lines.LoadFromFile('C:\Standerd.txt'); if Memo1.Lines.Count = StandardText.Lines.Count then begin for i := 0 to Memo1.Lines.Count - 1 do begin inputLine := trim(Memo1.Lines.Strings[i]); StandardLine := trim(StandardText.Lines.Strings[i]); if length(inputLine) = length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; end else if length(inputLine) > length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (Length(inputLine) - length(StandardLine)); end else begin for j := 0 to Length(inputLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (length(StandardLine) - Length(inputLine)); end; end; end else if Memo1.Lines.Count > StandardText.Lines.Count then begin for i := 0 to StandardText.Lines.Count - 1 do begin inputLine := trim(Memo1.Lines.Strings[i]); StandardLine := trim(StandardText.Lines.Strings[i]); if length(inputLine) = length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; end else if length(inputLine) > length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (Length(inputLine) - length(StandardLine)); end else begin for j := 0 to Length(inputLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (Length(StandardLine) - Length(inputLine)); end; end; for j := i to Memo1.Lines.Count -1 do WrongNum := WrongNum + Length(trim(Memo1.Lines.Strings[j])); end else begin for i := 0 to Memo1.Lines.Count - 1 do begin inputLine := trim(Memo1.Lines.Strings[i]); StandardLine := trim(StandardText.Lines.Strings[i]); if length(inputLine) = length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; end else if length(inputLine) > length(StandardLine) then begin for j := 0 to length(StandardLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (Length(inputLine) - Length(StandardLine)); end else begin for j := 0 to Length(inputLine) do begin if inputLine[j] <> StandardLine[j] then Inc(WrongNum); end; WrongNum := WrongNum + (Length(StandardLine) - Length(inputLine)); end; end; for j := i to StandardText.Lines.Count - 1 do WrongNum := WrongNum + Length(trim(StandardText.Lines.Strings[j])) end; ShowMessage(IntToStr(WrongNum)); StandardText.Free; end;procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Lines.SaveToFile('C:\Standerd.txt'); end;end.
str1, str2相比较就可以了,当然中文的子串可能还要麻烦些
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var StandardText:TMemo;
i,j,WrongNum:integer;
inputLine,StandardLine:String;
begin
WrongNum := 0;
StandardText := TMemo.Create(self);
StandardText.ParentWindow := self.Handle;
StandardText.Visible := false;
StandardText.Lines.LoadFromFile('C:\Standerd.txt'); if Memo1.Lines.Count = StandardText.Lines.Count then
begin
for i := 0 to Memo1.Lines.Count - 1 do
begin
inputLine := trim(Memo1.Lines.Strings[i]);
StandardLine := trim(StandardText.Lines.Strings[i]);
if length(inputLine) = length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
end
else if length(inputLine) > length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (Length(inputLine) - length(StandardLine));
end
else
begin
for j := 0 to Length(inputLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (length(StandardLine) - Length(inputLine));
end;
end;
end
else if Memo1.Lines.Count > StandardText.Lines.Count then
begin
for i := 0 to StandardText.Lines.Count - 1 do
begin
inputLine := trim(Memo1.Lines.Strings[i]);
StandardLine := trim(StandardText.Lines.Strings[i]);
if length(inputLine) = length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
end
else if length(inputLine) > length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (Length(inputLine) - length(StandardLine));
end
else
begin
for j := 0 to Length(inputLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (Length(StandardLine) - Length(inputLine));
end;
end;
for j := i to Memo1.Lines.Count -1 do
WrongNum := WrongNum + Length(trim(Memo1.Lines.Strings[j]));
end
else
begin
for i := 0 to Memo1.Lines.Count - 1 do
begin
inputLine := trim(Memo1.Lines.Strings[i]);
StandardLine := trim(StandardText.Lines.Strings[i]);
if length(inputLine) = length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
end
else if length(inputLine) > length(StandardLine) then
begin
for j := 0 to length(StandardLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (Length(inputLine) - Length(StandardLine));
end
else
begin
for j := 0 to Length(inputLine) do
begin
if inputLine[j] <> StandardLine[j] then
Inc(WrongNum);
end;
WrongNum := WrongNum + (Length(StandardLine) - Length(inputLine));
end;
end;
for j := i to StandardText.Lines.Count - 1 do
WrongNum := WrongNum + Length(trim(StandardText.Lines.Strings[j]))
end;
ShowMessage(IntToStr(WrongNum));
StandardText.Free;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Lines.SaveToFile('C:\Standerd.txt');
end;end.