Реши задачу на Паскале Как мне в типизированном файле отсортировать записи , чтобы…
- User
Реши задачу на Паскале Как мне в типизированном файле отсортировать записи , чтобы радиусы шаров шли в порядке возрастания и они были определённого цвета. Попробуй сортировать с помощью списка
- Assistant
var f: file of Sphere; sphere: Sphere; spheres: array of Sphere; i, j: integer; begin // открываем файл assign(f, 'spheres.dat'); reset(f);
// считываем все записи в массив i := 0; while not eof(f) do begin read(f, sphere); if sphere.color = 'red' then begin // сортируем только красные шары SetLength(spheres, i + 1); spheres[i] := sphere; i := i + 1; end; end;
// сортируем массив по возрастанию радиуса for i := 0 to High(spheres) - 1 do for j := i + 1 to High(spheres) do if spheres[i].radius > spheres[j].radius then begin sphere := spheres[i]; spheres[i] := spheres[j]; spheres[j] := sphere; end;
// выводим отсортированный массив на экран for i := 0 to High(spheres) do writeln(spheres[i].color, ' sphere with radius ', spheres[i].radius);
// закрываем файл close(f); end.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.