my 9th-grader friend received a homework assignment in computer science, namely to write a program in Pascal ABC.
The condition goes like this:
A street seller of newspapers receives a ruble from the sale of each of the 50 newspapers. From the sale of each of the subsequent newspapers, he receives 20% more. Develop a program that calculates a salesperson’s earnings if he sells 200 newspapers per day. Use the input.txt and output.txt files for input and output.
I made a program that simply calculates the seller’s earnings and displays the received data in the console, but I do not know how to work with data input and output.
The program looks like this:
Var
A: real;
k: integer;
Begin
Write ('Cost of one newspaper:'); ReadLn (A);
Write ('Number of newspapers:'); ReadLn (k);
if k & lt; = 50 then WriteLn ('Seller's earnings:', k * A, 'rub.')
else WriteLn ('Seller's earnings:', (A * ((k-50) * 1.2 + 50)): 0: 2, 'rub.')
End.
Answer 1
To work with text files at the initial level, you need a bunch of:
– variable of type TextFile
(hereinafter f
)
– AssignFile (f, name)
– Reset / Rewrite
(for input and output, respectively)
– Readln / Writeln
with the first argument f
– CloseFile
In this case, the format is assumed to be rigid – for example, the price in the first line, quantity in the second. In general, reading can go to the end of the file with a check like while not EOF (f)