2014-09-24 66 views
0

嗨我傳遞字符串從命令行//2,3,4,5,6和作爲參數在ip1。字符串數組在帕斯卡爾

當我運行此代碼時,它給出錯誤「錯誤:預期的類型標識符」和「致命:語法錯誤」;「預期但是」ARRAY「found」。

請讓我知道是什麼問題.....

program main; 
    uses SysUtils; 
    var 
    output : Array of integer; 
    var 
    ip1 : Array of integer; 

    function add(input1:Array of integer) : Array of integer; 
     begin 
      add := input1; 
     end; 
    type 
    TIntegerArray = Array of Integer; 

    function IntArray(var input:string) : TIntegerArray; 
    var 
     p: integer; 
    begin 
     p := Pos(',', input); 
     if p = 0 then 
      p := MaxInt - 1; 
     result[0] := Copy(input, 1, p - 1); 
     result[1] := Copy(input, p + 1); 
    end; 

    begin 
     ip1 := IntArray(ParamStr(1)); 
     output := add(ip1); 
     write('output ',output,'time',0.0); 
    end. 

回答

3

你必須在你發佈的代碼這麼多的問題,這是很難知道從哪裏開始...

  1. 您需要靠攏的類型聲明TIntegerArray達開始你的程序,所以它可以用作addIntArray的返回類型,以及add的參數。 array of IntegerTIntegerArray是Pascal中的兩種不同類型,不能互換。

  2. 在盲目使用它們之前,您不檢查是否收到任何參數。如果它們不存在,你的代碼根本不起作用。您需要檢查以確保您已收到參數,並在沒有找到它們的情況下生成一條帶有說明的有用消息。

  3. 您從不爲IntArray返回值分配任何空間。在使用動態數組時,您需要使用SetLength來聲明數組中適當數量的元素,然後才能爲其分配任何內容。 (請參閱下面的#4。)

  4. 您的IntArray只是假設input中只有兩個項目,其中您的示例命令行顯示更多。你需要使用一個循環。 (

  5. IntArray嘗試使用ParamStr(1)作爲var參數。ParamStr(1)是一個常數,而不能作爲var事情過去了。

  6. 您不能直接傳遞一個數組writewriteln 。你必須遍歷數組中的元素並單獨輸出每個元素

  7. (不是一個問題,只是信息)add什麼都不會「添加」任何東西,所以它的名字真的很差。那個行爲簡單地描述它在做什麼,以便你的代碼更易於閱讀和理解。 (我不知道你打算與add的事,但你有什麼,現在沒有任何用處。

  8. (另有「不是一個真正的問題」,而是信息。)您不處理的情況下,任何異常無法將參數轉換爲整數。提供給StrToInt的無效值將引發異常。您應該使用ValStrToIntDef,或至少在轉換周圍使用try..except塊來處理無效參數。

  9. (另一個「不是真正的問題」。)你不會做任何事情來暫停程序,所以你可以看到write語句的輸出,這使得很難測試或調試你的程序來自IDE。

這是您的代碼的工作(測試)版本。

program main; 

uses 
    System.SysUtils; 

type 
    TIntegerArray = Array of Integer; 

var 
    ip1, output: TIntegerArray; 

function add(input1: TIntegerArray) : TIntegerArray; 
begin 
    Result := input1; 
end; 

function IntArray(input:string) : TIntegerArray; 
var 
    p: Integer; 
    i: Integer;    // Tracks current index into Result array 
begin 
    i := 0; 
    p := Pos(',', input); 
    while P > 0 do 
    begin 
    Inc(i);    // Increment index 
    SetLength(Result, i); // Allocate element in array 
    Result[i] := StrToInt(Copy(input, 1, P - 1)); // Assign value 
    System.Delete(input, 1, P); // Remove portion we just read 
    P := Pos(',', input);   // See if there's another comma 
    end; 

    // Now get the part after last ',' and add to array also 
    i := Length(Result); 
    if (i > 0) and (input <> '') then 
    begin 
    SetLength(Result, i + 1); 
    Result[i + 1] := StrToInt(input); 
    Input := ''; 
    end; 
end; 

var 
    Ctr: Integer; 

begin 
    if ParamCount > 0 then 
    begin 
    ip1 := IntArray(ParamStr(1)); 
    output := add(ip1); 
    Write('Output: '); 
    for Ctr := Low(output) to High(output) do 
     Write(output[Ctr], ' '); 
    // Don't know what this is supposed to do, but... 
    WriteLn('time', 0.0); 
    end 
    else 
    begin 
    WriteLn('ParamCount: ', ParamCount); 
    WriteLn('Syntax: ', ExtractFileName(ParamStr(0)) + ' <arg,arg[,arg...]>'); 
    end; 
    ReadLn; 
end. 
+0

謝謝它真的很有幫助! – 2014-09-25 05:48:03

0

你需要()也使用tintegerarray作爲返回類型爲加,就像你已經爲intarray做。

之後,你會發現Pascal是強類型的,並且不允許將字符串分配給參數。

ip1:= intarray(paramstr(1));類型轉換看起來非常狡猾btw。也許再次查找paramstr和paramcount的幫助。