Advertisement
Guest User

Untitled

a guest
Jun 10th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module list
  2.    use environment
  3.  
  4.    implicit none
  5.  
  6.    integer, parameter                        :: name_len = 15
  7.  
  8.    type file
  9.       character(name_len, kind = CH_)        :: name = ""
  10.       type(file), pointer                    :: next => Null()
  11.       type(file), pointer                    :: previous => Null()
  12.    end type
  13.  
  14.    type files
  15.       integer                 :: count = 0
  16.       type(file), pointer     :: start => null()
  17.       type(file), pointer     :: end   => null()
  18.       type(file), pointer     :: curr  => null()
  19.    end type
  20.  
  21.    contains
  22.  
  23.    pure subroutine add_file(list, f)
  24.       type(files), intent(inout)             :: list
  25.       type(file), intent(inout), target      :: f
  26.       type(file), pointer                    :: tmp
  27.  
  28.  
  29.       if(associated(list%end)) then
  30.          call add_with_sort(list, list%start, f)
  31.       else
  32.          list%start => f
  33.          list%end   => f
  34.          list%count = list%count + 1
  35.       end if
  36.    end subroutine
  37.  
  38.    pure subroutine relate(f1, f2)
  39.       type(file), intent(inout), target     :: f1, f2
  40.  
  41.       f1%next => f2
  42.       f2%previous => f1
  43.  
  44.  
  45.    end subroutine
  46.    !СУПЕР-ПУПЕР-ГИПЕР ФУНКЦИЯ СОРТИРОВКИ ЧЕРЕЗ РЕКУРСИЮ И ЕСЛИ ОНА НЕ ЧИСТАЯ, Я ПОВЕШУСЬ, НУ ЧИСТАЯ ЖЕ НУ!!!!
  47.    pure recursive subroutine add_with_sort(list, f1, f2)
  48.       type(files), intent(inout)               :: list
  49.       type(file), intent(inout), target        :: f1, f2
  50.       type(file), pointer                      :: tmp1, tmp2
  51.  
  52.       if(.not. associated(f1%previous)) then
  53.          if(f1%name > f2%name) then
  54.             tmp1 => f1
  55.             list%start => f2
  56.             call relate(f2, f1)
  57.          else
  58.             if(.not. associated(f1%next)) then
  59.             call relate(f1, f2)
  60.             return
  61.          else
  62.             call add_with_sort(list, f1%next, f2)
  63.          end if
  64.       end if
  65.       else
  66.          if(f1%name > f2%name) then
  67.             tmp1 => f1%previous
  68.             call relate(tmp1, f2)
  69.             call relate(f2, f1)
  70.          else
  71.             if(.not. associated(f1%next)) then
  72.                call relate(f1, f2)
  73.                return
  74.             else
  75.                call add_with_sort(list, f1%next, f2)
  76.             end if
  77.          end if
  78.       end if
  79.  
  80.  
  81.    end subroutine
  82.  
  83.    function next(List) result(curr)
  84.       type(files), intent(inout)    :: List
  85.       type(file), pointer           :: curr
  86.  
  87.       if(associated(List%curr)) then
  88.          curr => List%curr%next
  89.          List%curr => curr%next
  90.       else
  91.          curr => List%start
  92.          List%curr => curr%next
  93.       end if
  94.  
  95.    end function
  96.  
  97.  
  98.    function Read_file_list(input_file) result(list)
  99.       character(*), intent(in)         :: input_file
  100.       type(files), pointer             :: list
  101.  
  102.       allocate(list)
  103.  
  104.       open(newunit = in, file = input_file, encoding = E_)
  105.          call Read_file(in, list)
  106.       close(in)
  107.    end function
  108.  
  109.    recursive subroutine Read_file(in, list)
  110.       integer, intent(in)           :: in
  111.       type(files), intent(inout)    :: list
  112.       type(file), pointer           :: f
  113.       character(:), allocatable     :: format
  114.       character(name_len, kind = CH_)     :: str
  115.       integer io, pos
  116.      
  117.       format = "(a)"
  118.       allocate(f)
  119.  
  120.       read(in, format, iostat = io) str
  121.  
  122.       f%name = str
  123.  
  124.       if(io == 0) then
  125.          call add_file(list, f)
  126.          call Read_file(in, list)
  127.       else
  128.          deallocate(f)
  129.          nullify(f)
  130.       end if
  131.          
  132.  
  133.    end subroutine
  134.  
  135.    subroutine Write_list(output_file, list, pos)
  136.       character(*), intent(in)         :: output_file
  137.       type(files), intent(inout)       :: list
  138.       character(*), intent(in)         :: pos
  139.       type(file), pointer              :: curr
  140.  
  141.       open(newunit = out, file = output_file, encoding = E_, position = pos)
  142.          write(out,*)
  143.          curr => list%start
  144.          call write_item(out, curr)
  145.       close(out)
  146.  
  147.    end subroutine
  148.  
  149.    recursive subroutine write_item(out, f)
  150.       integer, intent(in)     :: out
  151.       type(file), intent(in)  :: f
  152.  
  153.       type(file), pointer     :: tmp
  154.       character(:), allocatable  :: format
  155.       format = "(2(a, 1x))"
  156.       tmp => f%next
  157.  
  158.       write(out, format) f%name
  159.  
  160.       if(associated(tmp)) &
  161.          call write_item(out, tmp)
  162.  
  163.  
  164.    end subroutine
  165.  
  166.  
  167.    end module
  168.  
  169. PROGRAM Lab2_1
  170.       use list
  171.       use environment
  172.       implicit none
  173.  
  174.       character(*), parameter                            :: data_file = "../data/product.dat"
  175.  
  176.       character(*), parameter                            :: upd_file = "../data/updates"
  177.       type(files), pointer                               :: linked_list => Null()
  178.       type(files), pointer                               :: linked_list2 => Null()
  179.  
  180.       linked_list => Read_file_list(upd_file)
  181.  
  182.       call Write_list(output, linked_list, "rewind")
  183. end program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement