По разделам

 

Решение задачи 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 Просмотров: 4599
File 2 Просмотров: 3881
File 3 Просмотров: 1822
File 4 Просмотров: 2277
File 5 Просмотров: 1763
File 6 Просмотров: 1568
File 7 Просмотров: 1373
File 8 Просмотров: 1833
File 9 Просмотров: 1087
File 10 Просмотров: 1602
File 11 Просмотров: 1111
File 12 Просмотров: 1254
File 13 Просмотров: 1097
File 14 Просмотров: 976
File 15 Просмотров: 882
File 16 Просмотров: 1130
File 17 Просмотров: 1311
File 18 Просмотров: 906
File 19 Просмотров: 776
File 20 Просмотров: 958
File 21 Просмотров: 815
File 22 Просмотров: 978
File 23 Просмотров: 839
File 24 Просмотров: 835
File 25 Просмотров: 893
File 26 Просмотров: 870
File 27 Просмотров: 738
File 28 Просмотров: 992
File 29 Просмотров: 756
File 30 Просмотров: 838
File 31 Просмотров: 739
File 32 Просмотров: 778
File 33 Просмотров: 850
File 34 Просмотров: 857
File 35 Просмотров: 650
File 36 Просмотров: 745
File 37 Просмотров: 629
File 38 Просмотров: 700
File 39 Просмотров: 718
File 40 Просмотров: 630
File 41 Просмотров: 682
File 42 Просмотров: 841
File 43 Просмотров: 624
File 44 Просмотров: 630
File 45 Просмотров: 693
File 46 Просмотров: 604
File 47 Просмотров: 728
File 48 Просмотров: 851
File 49 Просмотров: 744
File 50 Просмотров: 777
File 51 Просмотров: 756
File 52 Просмотров: 772
File 53 Просмотров: 609
File 54 Просмотров: 586
File 55 Просмотров: 662
File 56 Просмотров: 727
File 57 Просмотров: 560
File 58 Просмотров: 708
File 59 Просмотров: 664
File 60 Просмотров: 1167

Комментарии

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



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