2014-09-27 47 views
-4

我有這些數據:拉扎勒斯顯示數字從備忘錄爲exampel lisbox

CMD210 STA_ 99.0 US溫度22.1Ç

CMD210 STAB 99.9 US溫度22℃

CMD210 STAB爲0.1mS溫度22.1Ç

CMD210 STA_ 99.5 US溫度22.1ç

CMD210 STAB 99.4 US溫度22℃

CMD210 ST__ 99.0 US溫度22.2Ç

CMD210 STAB爲0.1mS溫度22℃

CMD210 STAB 99.3 US溫度22.2Ç

我想具有從備忘錄顯示溫度爲exampel的程序在一個列表框中。

我知道我必須得到循環和一些與2字符與「p」和「C」,因爲數字是那些字母之間....

procedure TForm1.Button4Click(Sender: TObject); 
 
    var 
 
    midlet,midler:char; 
 
    resultat,x:integer; 
 
    linecount,index:integer; 
 
    found: boolean; 
 
begin 
 
midlet:= 'p'; 
 
    midler:='C'; 
 
    index:=0; 
 
    resultat:=midlet+x+midler 
 
    found := false; 
 
    linecount := Memo1.lines.count; 
 
while index<= linecount - 1 do 
 
begin 
 
    if x = memo1.lines[index] then 
 
    found := true; 
 
    index :=index + 1; 
 
    end 
 
if found = true then 
 
    ListBox1.text:= floattostrF(x,ffFixed,15,2); 
 
end;

+2

請張貼的你嘗試過什麼到目前爲止的代碼片段。 我強烈建議在發佈任何問題之前做一些研究。 – ararog 2014-09-27 09:31:41

+0

好吧,我會嘗試 – 2014-09-27 09:39:23

+0

我只是不知道如何定義'resultat' – 2014-09-27 10:00:34

回答

0

有幾個問題在你的例子中,所以這個答案將被限制爲「如何從一條線提取和轉換溫度」。你基本上有兩種方法來完成這項任務:

  • 使用regular expressions
  • 編寫自定義分析器。

定製解析器是很容易寫:

  • 積累非空白字符的標識符。
  • 如果標識符等於Temp那麼定義一個標誌。
  • 如果定義了標誌並且已經累計了標誌,則將標識符轉換爲double。

例如:

program Project1; 

uses 
    sysutils; 

const 
    line1 = 'CMD210 STAB 99.3 uS Temp 22.2 C'; 
    line2 = 'CMD210 STAB 0.1 mS Temp 22 C'; 
    line3 = 'it is quite hot over there Temp 55.123456 C'; 
    line4 = 'bla bla bla bla 12.564 C'; 
    line5 = ''; 

function getTemperature(aLine: string): double; 
var 
    reader: PChar; 
    identifier: string; 
    AccumulateTemp: boolean; 
const 
    _Nan = 0/0; 
begin 
    // initialize local variables. 
    identifier := ''; 
    AccumulateTemp := false; 
    // success can be tested with isNan() 
    result := _Nan; 
    // add a distinct terminal char: 
    aLine := aLine + #0; 
    reader := @aLine[1]; 

    while(true) do begin 
    if reader^= #0 then 
     exit; 
    // blank: test the identifier 
    if reader^ in [#9, ' '] then 
    begin 
     if AccumulateTemp then 
     begin 
     if not TryStrToFloat(identifier, result) then 
      result := _Nan; 
     AccumulateTemp := false; 
     exit; 
     end; 
     if identifier = 'Temp' then 
     AccumulateTemp := true; 
     identifier := ''; 
    end else 
    // accumulate 
     identifier := identifier + reader^; 
    Inc(reader); 
    end; 
end; 

begin 
    DecimalSeparator := '.'; 
    writeln(format('%.7f', [getTemperature(line1)]) ); 
    writeln(format('%.7f', [getTemperature(line2)]) ); 
    writeln(format('%.7f', [getTemperature(line3)]) ); 
    writeln(format('%.7f', [getTemperature(line4)]) ); 
    writeln(format('%.7f', [getTemperature(line5)]) ); 
    readln; 
end. 

其輸出

22.2000000 
22.0000000 
55.1234560 
Nan 
Nan