Pascal splitting line into real and strings

ⅰ亾dé卋堺 提交于 2020-01-05 05:43:07

问题


I know that this language have died a couple of years ago, but still required in most of schools in our country -.- I got file with data, which looks like:

  • Number of lines
  • Name Surname (real type digit) (another real type digit)

For e.g.

  • 2
  • Brat Sunbather 5.66 55.4
  • Bart Simpson 55.7 45.4

And I need to create result file, which looks like this:

  • Name Surname (Previously given real type digits multiplied)
  • Total

For e.g.

  • Brat Sunbather 313.56
  • Bart Simpson 2528.78
  • Total: 2842.34

I'm stuck in trying to split the line into string and real, even in the book I've given in the examples all data is on separate line:

  • String
  • Digit
  • String
  • Digit

I can't find anything on the net and hope you could help me. Thank you in advance.


回答1:


This should get you started - I got as far as reading the file, splitting the line, and converting the strings to reals:

Program Test;

var
    fileVar: Text;
    l: string[81];
    inputFilename: string[14];
    lCount: Integer;
    i: Integer;
    code: Integer;

    spacePos: Integer;

    firstName: string[100];
    secondName: string[100];

    num1: real;
    num2: real;
    product: real;

    s: string[100];

begin
    inputFilename := 'input.txt';
    Assign(fileVar, inputFilename);
    Reset(fileVar);

    Readln(fileVar, l);
    Val(l, lCount);

    Writeln('l count=', lCount);

    for i := 1 to lCount do
    begin
        Readln(fileVar, l);
        spacePos := Pos(' ', l);
        firstName := Copy(l, 0, spacePos);
        Delete(l, 1, spacePos);

        spacePos := Pos(' ', l);
        secondName := Copy(l, 0, spacePos);
        Delete(l, 1, spacePos);

        spacePos := Pos(' ', l);
        s := Copy(l, 0, spacePos - 1);
        Val(s, num1, code);
        Delete(l, 1, spacePos);

        Val(l, num2, code);

        WriteLn(firstName);
        Writeln(secondName);
        Writeln(num1);
        Writeln(num2);
    end;

    Close(fileVar);
end.




来源:https://stackoverflow.com/questions/8244797/pascal-splitting-line-into-real-and-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!