用Delphi写一个小程序,从一个文本文件中把所有数字累加起来,然后显示出结果(就是提取里面的数字)。文本文件内容如下所示:华伦巴菲特-世界首富的选股法则:
1. 股东权益报酬率(1季及2年平均)均大于3.75%
2. 现金流量成长率(1季及2年平均)均大于5%
3. 价格/净值比小于2
4. 毛利率(1季及2年平均)均大于15%
5. 本益比小于20结果=1+2+3+75+1+2+5+2+1+2+15+20

解决方案 »

  1.   

    function Calc(const AText: String): Cardinal;
    var
      FItem: Char;
      FData: String;
    begin
      Result := 0;
      for FItem in AText do
        if FItem in ['0'..'9'] then
        begin
          FData := FData + FItem;
        end else
        begin
          if (Length(FData) > 0) and (FItem <> #163) then
            Result := Result + StrToInt(FData);      FData := '';
        end;
    end;
      

  2.   

    我与定了一个,function GetTextSum(Const fileName: String): Int64;
    var
      fs: TFileStream;
      buf: Char;
      value: Byte;
      CurrNumber, Pos, Len: Integer;
    begin
      Result := 0;
      Pos := 0;
      Len := 0;
      CurrNumber := 0;  if Not fileExists(fileName) then exit;
      fs := TFileStream.Create(fileName, fmOpenRead);
      if fs.Size<=0 then exit;
      fs.Position:=0;  while (Pos<=fs.Size) do
      begin
        fs.Read(buf, SizeOf(buf));
        pos := pos + 1;
        value := ord(buf);
        if (value>=48) and (value<=57) then
        begin
          CurrNumber := CurrNumber * 10 + StrToInt(buf);
        end else
        begin
          Result := Result + CurrNumber;
          CurrNumber := 0;
        end;
        if pos=fs.Size then
          Result := Result + CurrNumber;
      end;end;
      

  3.   

    遍历一遍 用 trystrtoint 也可以 同样判断一下下一个是否为数字
    而且“-”的话特殊处理一下
      

  4.   

    编译有错误:31  function TForm1.Calc(const AText:string):Cardinal;
    32  var
          FItem:Char;
          FData:string;
        begin
          Result:=0;
    37    for FItem in AText do
            if FItem in ['0'..'9'] then
            begin
              FData:=FData+FItem;
            end
            else
            begin
              if (Length(FData)>0)and(FItem<>#163) then
                Result:=Result+StrToInt(FData);
              FData:='';
    47      end;
    48  end;
    [Error] Unit1.pas(37): Operator not applicable to this operand type
    [Error] Unit1.pas(47): Incompatible types: 'Boolean' and 'procedure, untyped pointer or untyped parameter'
    [Error] Unit1.pas(48): Expression expected but 'END' found