По разделам

 

Решение задачи File 50


Даны два файла вещественных чисел с именами S1 и S2, элементы которыхупорядоченыповозрастанию.Объединитьэтифайлывновыйфайл с именем S3 так, чтобы его элементы также оказались упорядоченными по возрастанию.

Код (Python)

# -*- coding: utf-8 -*-
import random
import sys

def GenerateNumbers(fname):
    N = random.randrange(1,5)
    numbers = random.sample(range(1, 100), N)
    numbers.sort()
    print(numbers)
    try:
        f = open(fname, "w")
        for x in numbers:
            line = str(x)+"\n"
            f.write(line)
        f.close()
    except:
        print("Error: ", sys.exc_info()[0])

try:
    file1 = "file50_1.txt"
    file2 = "file50_2.txt"
    f_output = "file50_all.txt"

    GenerateNumbers(file1)
    GenerateNumbers(file2)

    with open(file1, 'r') as f1, \
         open(file2, 'r') as f2, \
         open(f_output, 'w') as f_out:

        line1 = f1.readline()
        x1 = int(line1)
        line2 = f2.readline()
        x2 = int(line2)

        print("while start")
        while True:
            if x1 < x2:
                print("x1 < x2")
                f_out.write(line1)
                line1 = f1.readline()
                if line1:
                    x1 = int(line1)
                else:
                    f_out.write(line2)
                    break
            else:
                print("x1 >= x2")
                f_out.write(line2)
                line2 = f2.readline()
                if line2:
                    x2 = int(line2)
                else:
                    f_out.write(line1)
                    break
        print("while end")

        if line1:
            while line1:
                line1 = f1.readline()
                if line1:
                    f_out.write(line1)
                    
        if line2:
            while line2:
                line2 = f2.readline()
                if line2:
                    f_out.write(line2)

except Exception as e:
    print("Error: ", sys.exc_info())
    exc_type, exc_obj, exc_tb = sys.exc_info()
    print(exc_type, exc_tb.tb_lineno)
									

Код (Pascal)

program File50;
var
 S1,S2,S3: String;
 F1,F2,F3: File of Real;
 El1,El2:Real;
begin
  Write('S1: ');
  Readln(S1);
  Write('S2: ');
  Readln(S2);
  Write('S3: ');
  Readln(S3);
  Assign(F1,S1);
  Assign(F2,S2);
  Assign(F3,S3);
  Reset(F1);
  Reset(F2);
  ReWrite(F3);
  Read(F1,El1);
  Read(F2,El2);
  while true do
   begin
    if El1<El2 then
     begin
      Write(F3,El1);
      if not (eof(F1)) then Read(F1,El1)
      else
       begin
        Write(F3,El2);
        break;
       end;
     end
    else
     begin
      Write(F3,El2);
      if not(eof(F2)) then Read(F2,El2)
      else
       begin
        Write(F3,El1);
        break;
       end;
     end;
   end;
  if not (Eof(F2)) then
   begin
    while not(eof(F2)) do
     begin
      Read(F2,El2);
      Write(F3,El2);
     end;
   end;
  if not (Eof(F1)) then
   begin
    while not(eof(F1)) do
     begin
      Read(F1,El1);
      Write(F3,El1);
     end;
   end;
  Close(F1);
  Close(F2);
  Close(F3);
  Readln;
end.
									




File. Абрамян
File 1 Просмотров: 4808
File 2 Просмотров: 4084
File 3 Просмотров: 2023
File 4 Просмотров: 2555
File 5 Просмотров: 1940
File 6 Просмотров: 1696
File 7 Просмотров: 1518
File 8 Просмотров: 2019
File 9 Просмотров: 1196
File 10 Просмотров: 1788
File 11 Просмотров: 1242
File 12 Просмотров: 1396
File 13 Просмотров: 1226
File 14 Просмотров: 1133
File 15 Просмотров: 1015
File 16 Просмотров: 1271
File 17 Просмотров: 1466
File 18 Просмотров: 1025
File 19 Просмотров: 916
File 20 Просмотров: 1089
File 21 Просмотров: 927
File 22 Просмотров: 1154
File 23 Просмотров: 952
File 24 Просмотров: 952
File 25 Просмотров: 1017
File 26 Просмотров: 1004
File 27 Просмотров: 844
File 28 Просмотров: 1119
File 29 Просмотров: 874
File 30 Просмотров: 961
File 31 Просмотров: 897
File 32 Просмотров: 906
File 33 Просмотров: 963
File 34 Просмотров: 983
File 35 Просмотров: 806
File 36 Просмотров: 859
File 37 Просмотров: 745
File 38 Просмотров: 817
File 39 Просмотров: 831
File 40 Просмотров: 788
File 41 Просмотров: 795
File 42 Просмотров: 996
File 43 Просмотров: 739
File 44 Просмотров: 753
File 45 Просмотров: 812
File 46 Просмотров: 713
File 47 Просмотров: 858
File 48 Просмотров: 966
File 49 Просмотров: 854
File 50 Просмотров: 888
File 51 Просмотров: 862
File 52 Просмотров: 888
File 53 Просмотров: 745
File 54 Просмотров: 737
File 55 Просмотров: 788
File 56 Просмотров: 868
File 57 Просмотров: 706
File 58 Просмотров: 825
File 59 Просмотров: 789
File 60 Просмотров: 1276

Комментарии

Чтобы написать комментарии вам нужно войти в систему или зарегистрироваться



Заявка на расчет