Fortran: Чистая подпрограмма перемещения текста группы строк с Первой строки на Последнюю после K-й строки - PullRequest
0 голосов
/ 28 мая 2020

Задача выполняется в виде программного проекта из двух модулей, в котором необходимо заранее использовать однонаправленные списки неизвестной длины.

Задача: Разработать чистую процедуру перемещения по тексту группа строк от Первой до последней строки после K-й строки. Значения First, Last, K устанавливаются во втором входном файле. Правильные данные - это ситуация, когда First≤Last, а K не принадлежит интервалу [First, Last]. Индикация Элемент списка - это строка.

Я ввел, вывел и получил элемент из списка. Но я не могу понять, как переставить предложения в тексте в соответствии с условиями задачи.

Код на github: введите здесь описание ссылки

source_process.f90

module Source_Process
   use Environment
   use Source_IO

   implicit none

contains
   recursive function get_element(current_element, number) result(element)
      type(SourceLine), pointer, intent(in)  :: current_element
      type(SourceLine), pointer :: element
      integer, intent(in) :: number

      element => null()
      if (associated(current_element)) then
         if (number == 0) then
            element => current_element
         else
            element => get_element(current_element%next, number - 1)
         end if
      end if
   end function get_element
end module Source_process

Заранее благодарим за ответ.

1 Ответ

0 голосов
/ 01 июня 2020
module Source_Process
   use Environment
   use Source_IO

   implicit none

contains
   subroutine Move_Strings(NB, KB, K, InitialStrings)    
      type(SourceLine), pointer, intent(inout)  :: InitialStrings
      integer, intent (in)                      :: NB, KB, K

      type(SourceLine), pointer  :: CurrentInitial, CurrentTmp
      type(SourceLine), pointer  :: CurrentK, CurrentNB, PrevNB, CurrentKB

      integer                    ::  number_string = 1

      CurrentInitial => InitialStrings

      CurrentK  => null()
      CurrentNB => null()
      CurrentKB => null()

      do while (Associated(CurrentInitial))
         if (number_string == K) then
            CurrentK => CurrentInitial
         else if (number_string == NB - 1) then
            PrevNB => CurrentInitial
         else if (number_string == NB) then
            CurrentNB => CurrentInitial
         else if (number_string == KB) then
            CurrentKB => CurrentInitial
         end if
         CurrentInitial => CurrentInitial%Next
         number_string = number_string + 1
      end do

      if (NB == 1) then
         InitialStrings => CurrentKB%next
      else
         PrevNB%next => CurrentKB%next
      end if

      CurrentKB%next => CurrentK%next
      CurrentK%next => CurrentNB

   end subroutine Move_Strings
end module Source_process

Проблема решена.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...