【问题标题】:string to array in pascal字符串到帕斯卡数组
【发布时间】:2014-11-18 15:34:39
【问题描述】:

您好,我正在从命令行传递字符串 // - 2,3,4,5,6 并作为 ip1 中的参数。

当我运行此代码时,它会给出错误“错误:预期类型标识符”和“致命:语法错误,”;“预期但发现”ARRAY”。

请告诉我是什么问题.....

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.

【问题讨论】:

    标签: arrays command-line-arguments pascal


    【解决方案1】:

    你发布的代码有很多问题,很难知道从哪里开始......

      1234563到addarray of IntegerTIntegerArray 在 Pascal 中是两种不同的类型,不能互换。
    1. 在盲目使用参数之前,您不会检查是否收到任何参数。如果它们不存在,则您的代码根本不起作用。您需要检查以确保您已收到参数,如果未找到参数,请生成带有说明的有用消息。

    2. 您永远不会为IntArray 返回值分配任何空间。在使用动态数组时,您需要使用SetLength 声明数组中正确数量的元素,然后才能为它们分配任何内容。 (见下文#4。)

    3. 您的 IntArray 只是假设 input 中只有两个项目,您的示例命令行显示更多。您需要使用循环。 (

    4. 您的IntArray 尝试使用ParamStr(1) 作为var 参数。 ParamStr(1) 是一个常量,不能作为var 传递任何东西。

    5. 您不能将数组直接传递给writewriteln。您必须遍历数组中的元素并单独输出。

    6. (不是真正的问题,只是信息)add 没有“添加”任何东西,所以它的名字真的很糟糕。您应该选择实际描述它正在做什么的名称,以便您的代码更易于阅读和理解。 (我不确定你打算用 add 做什么,但你现在所拥有的并没有什么用处。

    7. (另一个“不是真正的问题”,而是信息。)如果参数无法转换为整数,您不会处理任何异常。提供给StrToInt 的无效值将引发异常。您应该使用ValStrToIntDef,或者至少在转换周围使用try..except 块来处理无效参数。

    8. (另一个“不是真正的问题”。)您无需在最后暂停程序,因此您可以看到 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.
    

    【讨论】:

      【解决方案2】:

      您也需要使用 tintegerarray 作为 add() 的返回类型,就像您已经为 intarray 所做的那样。

      之后你会发现 Pascal 是强类型的,并且不允许将字符串分配给参数。

      ip1:=intarray(paramstr(1));顺便说一句,typecast 看起来非常狡猾。也许再次查找 paramstr 和 paramcount 的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多