Pascal将行拆分为实数和字符串 [英] Pascal splitting line into real and strings

查看:108
本文介绍了Pascal将行拆分为实数和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这种语言已经在几年前消失了,但是在我们国家的大多数学校中仍然需要-.- 我得到了带有数据的文件,看起来像:

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:

  • 行数
  • 姓名(真实类型数字)(另一个真实类型数字)

例如

  • 2
  • Brat Sunbather 5.66 55.4
  • 巴特·辛普森55.7 45.4

我需要创建结果文件,如下所示:

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

  • 姓名(以前用实型数字加倍)
  • 总计

例如

  • 布拉特·日光浴者313.56
  • 巴特·辛普森2528.78
  • 总计: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:

  • 字符串
  • 数字
  • 字符串
  • 数字

我在网上找不到任何东西,希望您能帮助我.预先谢谢你.

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

推荐答案

这应该让您入门-我已经读完文件,拆分行并将字符串转换为实数了:

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.


这篇关于Pascal将行拆分为实数和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆