给你一个思路: 建立一个堆栈,从文本开头一个一个字符读,如果是0..9 or . 那么压入栈中 当读到一个非 0..9 or . ,看栈中有没有数据,如果有,就把它转化为实数,然后加2后写回文本,之后清空栈,继续读
//例子: //实际应用中只需要把Memo1.Lines[j]等改为ReadLn,WriteLn等。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Memo2: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var s1,s2,s:string; i,j:integer; v:integer; bPoint:Bool; begin For j:=0 To Memo1.Lines.Count-1 Do Begin s1:=Memo1.Lines[j]; s:=''; s2:=''; bPoint:=False; For i:=1 to length(s1) do Begin If s1[i] in ['0'..'9'] then Begin If Not bPoint Then s:=s+s1[i] Else Begin s:=''; s2:=s2+s1[i]; End; End Else Begin If s1[i]='.' Then Begin bPoint:=True; End Else bPoint:=False; If Length(s)<>0 Then Begin s:=IntToStr(StrToInt(s)+2); s2:=s2+s; End; s2:=s2+s1[i]; s:=''; End; End; If i=Length(s1)+1 Then If Length(s)<>0 Then Begin s:=IntToStr(StrToInt(s)+2); s2:=s2+s; End; Memo2.Lines[j]:=s2; End; end;end.
当读到一个非 0..9 or . ,看栈中有没有数据,如果有,就把它转化为实数,然后加2后写回文本,之后清空栈,继续读
//实际应用中只需要把Memo1.Lines[j]等改为ReadLn,WriteLn等。unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
s1,s2,s:string;
i,j:integer;
v:integer;
bPoint:Bool;
begin
For j:=0 To Memo1.Lines.Count-1 Do
Begin
s1:=Memo1.Lines[j];
s:='';
s2:='';
bPoint:=False;
For i:=1 to length(s1) do
Begin
If s1[i] in ['0'..'9'] then
Begin
If Not bPoint Then
s:=s+s1[i]
Else
Begin
s:='';
s2:=s2+s1[i];
End;
End
Else
Begin
If s1[i]='.' Then
Begin
bPoint:=True;
End
Else
bPoint:=False;
If Length(s)<>0 Then
Begin
s:=IntToStr(StrToInt(s)+2);
s2:=s2+s;
End;
s2:=s2+s1[i];
s:='';
End;
End;
If i=Length(s1)+1 Then
If Length(s)<>0 Then
Begin
s:=IntToStr(StrToInt(s)+2);
s2:=s2+s;
End;
Memo2.Lines[j]:=s2;
End;
end;end.