大家好:
  现在两个字符串s1='111111111100',s2='00011111011'.请问如何用delphi 写程式,使s1和s2
进行或运算得到字符串s3:='111111111111',s1和s2进行与运算得到字符串s4:='00011111000'
thanks

解决方案 »

  1.   


    function StrOr(const S1, S2: string): string;
    var
      I: Integer;
      P1, P2: Integer;
    begin
      Result := '';
      if S1 = '' then Result := S2
      else if S2 = '' then Result := S1
      else begin
        P1 := Length(S1);
        P2 := Length(S2);
        while (P1 > 0) or (P2 > 0) do
        begin
          if P1 <= 0 then Result := S2[P2] + Result
          else if P2 <= 0 then Result := S1[P1] + Result
          else begin
            if (S1[P1] = '1') or (S2[P2] = '1') then Result := '1' + Result
            else Result := '0' + Result;
          end;
          Dec(P1);
          Dec(P2);
        end;
      end;
    end;function StrAnd(const S1, S2: string): string;
    var
      I: Integer;
      P1, P2: Integer;
    begin
      Result := '';
      if (S1 = '') or (S2 = '') then Result := '' //其中一个为空,返回空?
      else begin
        P1 := Length(S1);
        P2 := Length(S2);
        while (P1 > 0) and (P2 > 0) do
        begin
          if (S1[P1] = '1') and (S2[P2] = '1') then Result := '1' + Result
          else Result := '0' + Result;
          Dec(P1);
          Dec(P2);
        end;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(StrOr('111111111100', '00011111011'));
      ShowMessage(StrAnd('111111111100', '00011111011'));
    end;