-1

I have an input file in the format:

(a n), (a n-1), ... (a 0)

How can I form a list like below in Pascal

type
  tt = ^t;
  t = record
       a: Integer;
       n: Integer;
       next: tt
  end;

For example:
(5 10), (5 9), (5 8), (5 7), (5 6), (5 5), (5 4), (5 3), (5 2), (5 1), (5 0)
Should be like on the image:
enter image description here

New code (works as expected):

program ex4_19;

type
  tt = ^t;
  t = record
       a: Integer;
       n: Integer;
       next: tt
  end;

var
  ukzv, ukrs: tt;
  inp: text;
  raDone: boolean;
  i: integer;
  str: string;
begin
  assign(inp, 'f1.txt'); reset(inp);
  assign(output, 'out.txt'); rewrite(output);

  new(ukzv);
  ukrs:=ukzv; 

  read(inp, str);
  writeln(str);

  for i:=1 to length(str) do
  begin
    case str[i] of
      '(':
      begin
         raDone:=false;
         new(ukzv^.next);
         ukzv:=ukzv^.next;
         ukzv^.a:=0;
         ukzv^.n:=0;
      end;
      '0' .. '9':
      begin
         if raDone = false then
            ukzv^.a:=ukzv^.a * 10 + (ord(str[i]) - ord('0'))
         else
            ukzv^.n:=ukzv^.n * 10 + (ord(str[i]) - ord('0'));
      end;
      ' ':
      begin
         if raDone = false then
         begin
            raDone:=true;
         end;
      end;
      ')':
      begin
         ukzv^.next:=nil;
      end;
    end;
  end;

   ukzv:=ukrs;

   while ukzv^.next <> nil do
   begin
     writeln(ukzv^.next^.a, ' ', ukzv^.next^.n);
     ukzv:=ukzv^.next;
   end;
end.

I have the error "Invalid numeric format" because after the second number we have ')'. I don't know how to read number only until ')' because numbers can be different (1-1000).

  • What have you tried, and how has it failed? [so] is not a code-writing service; you should provide a [mcve] of your specific problem, and describe both your efforts to solve it and how they hsve been insufficent. See [ask] a good question. – Jeff Zeitlin Nov 23 '17 at 09:00
  • @JeffZeitlin Sorry, updated. I don't know how to implement that task. I don't think that my code above will help someone because it's just a bit. – Alexey Ryabov Nov 23 '17 at 09:14
  • Starting hint: You will need to read your input as strings, purely, and then parse it out to get your numbers. – Jeff Zeitlin Nov 23 '17 at 09:24
  • @JeffZeitlin I've tried so and I've deleted all ')', '(', ',' but I don't know how to read this string. I have a string "5 10 5 9 5 8 ...5 0 ". What should I do? I want it to be worked with different numbers, e.g "15 399 15 398 ... 15 0". – Alexey Ryabov Nov 23 '17 at 09:37

1 Answers1

2

There are different ways, one is the following. Start with reading the file into a string variable.

'(5 10), (5 9), (5 8), (5 7), (5 6), (5 5), (5 4), (5 3), (5 2), (5 1), (5 0)'

Then use a loop (for..do, repeat..until or while..do) to step through the characters one at a time. Use a case statement to decide on the actions.

Here's the processing per character spelled out, should be straight forward to implement as a case statement.

You need a boolean (e.g. raDone: boolean) to indicate whether new digits go to a or n in the records.

Get next char, '(', you know it's time to link in a new record (`r` in the following).
Get next char, '5', it's a digit and `not raDone`, so you accumulate `r.a` with it. See below!
Get next char, ' ', it's a space and `not raDone`, you know that entry for `r.a` has ended, set `raDone` to indicate next digits belong to `r.n`.
Get next char, '1', it's a digit and `raDone`, so you accumulate `r.n` with it.
Get next char, '0', it's a digit and `raDone`, so you accumulate `r.n` with it.
Get next char, ')', you know the entry for the current record is ready. 
Get next char, comma, nothing to do, just skip it
Get next char, ' ', space, nothing to do, just skip it

To accumulate a binary value (say r.n) with a decimal digit (converted from a character):

r.n := r.n * 10 + (ord(decimal character) - ord('0');

You probably want to add error checking for erroneous content in the input string.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54