字符串一:1001*1003+(1005-5002)
  其中1001对应数值:23.6
      1003对应数值:15
      1005对应数值:65
      5002对应数值:652
我要结果字符串二:23.6*15+(65-652)
依次把数值替换回去。

解决方案 »

  1.   

    type
      TToken=record
        strName:string;
        strValue:string;
      end;const
        TokenCnt            =       4;
        strToken:array[0..TokenCnt-1] of TToken=(
                                            (strName:1001;strValue:'23.6'),
                                            (strName:1003;strValue:'15'),
                                            (strName:1005;strValue:'65'),
                                            (strName:5002;strValue:'652')
                                            );
        strOper:set of char=['+','-','*','/','(',')'];function AStringReplace(strOld:string):string;
    var
        i:integer;
        strValue:string;
    begin
        strValue:=strOld;
        for i:=0 to TokenCnt-1 do
            if strOld=strToken[i].strName then
                strValue:=strToken[i].strValue;
        Result:=strValue;
    end;function MyReplace(strTemp:string):string;
    var
        strVar:string;
        strValue:string;
        i:integer;
    begin
        strVar:='';
        strValue:='';
        for i:=1 to Length(strTemp) do
        begin
            if (strTemp[i] in strOper) then
            begin
                strVar:=AStringReplace(strVar);
                strValue:=strVar+strTemp[i]+MyReplace(Copy(strTemp,i+1,length(strTemp)));
                break;
            end else
                strVar:=strVar+strTemp[i];
        end;
        if strValue='' then
            strValue:=AStringReplace(strTemp);
        Result:=strValue;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
        i:integer;
        strTemp:string;
    begin
        strTemp:='1001*1003+(1005-5002)'
        strTemp:=MyReplace(strTemp);
        Label1.Caption:=strTemp;
    end;