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的帮助。