如何为特定变量创建代码?

时间:2017-03-23 05:09:55

标签: fortran nested-loops fortran95 do-loops

我想为Fortran 95嵌入一个代码。

例如:我读过整数变量

   read *, x

例如x = 4。我的源创建了四个循环,它有四个循环变量

   loop1:do a=1,16
   loop2:do b=1,16
   loop3:do c=1,16
   loop4:do d=1,16
   ........smt......
   end do loop4
   end do loop3
   end do loop2
   end do loop1

我正在研究一个试图找到魔方的代码。我可以通过使用奇数平方矩阵的算法找到魔法代码。可能,我也可以生成一个偶数和双偶数的魔方。但是,我正在尝试通过编写一个逐个元素来寻找魔方的程序来提高我的编码技能。

implicit integer (a-z)
  

counte =可能性计数器,magcon =魔方生成计数器   上帝和魔鬼是逻辑变量。但是我把它们用作整数。

    integer GG(3,3),COUNTE,magcon
    integer god,devil
   open(55,file='mymagics')
   COUNTE=0
    magcon=0


 loop1:do a=9,1,-1
    loop2:do b=9,1,-1
    loop3:do c=9,1,-1
    loop4:do d=9,1,-1
    loop5:do e=9,1,-1
    loop6:do f=9,1,-1
    loop7:do g=9,1,-1
    loop8:do h=9,1,-1
    loop9:do i=9,1,-1
  

这些循环用于评估元素

    GG(1,1)=a
    GG(1,2)=b
    GG(1,3)=c
    GG(2,1)=d
    GG(2,2)=e
    GG(2,3)=f
    GG(3,1)=g
    GG(3,2)=h
    GG(3,3)=i
    call elementcontrol(gg,devil)
    if(devil.eq.1)then
    call magiccontrol(GG,god)

    else if(devil.eq.0) then

        cycle



    endif
    COUNTE=COUNTE+1


if(allah.eq.1) then
magcon=magcon+1
write(55,66)
   write(55,*) counte ,"possibility is tried"
   write(55,*)"**************************************"
     write(55,*)"**************************************"
       write(55,*)"**************************************"
   write(55,*)"--------------------------------------"
write(55,*)GG(1,1),GG(1,2),GG(1,3)
write(55,*)GG(2,1),GG(2,2),GG(2,3)
write(55,*)GG(3,1),GG(3,2),GG(3,3)
write(55,*)"--------------------------------------"
  write(55,*)"**************************************"
    write(55,*)"**************************************"
      write(55,*)"**************************************"

write(55,66)
66 format(//)




else

    print *, counte ,"possibility is unvalid"

    end if
enddo loop9
enddo loop8
enddo loop7
enddo loop6
enddo loop5
enddo loop4
enddo loop3
enddo loop2
enddo loop1
print *, "finally done!"
print *, magcon,"magic square is found"
stop

end


subroutine magiccontrol(magic,logic)







integer logic,z
integer magic(3,3),sumrow(3),sumcol(3),sumdia(2)
  

这些是行,列和对角线求和器

 do z=1,3
    sumrow(z)=0
    sumcol(z)=0
    sumdia(z)=0
    end do
do 31 k=1,3
do 31 l=1,3
    sumrow(k)=sumrow(k)+(magic(k,l))
    31 continue



 do 52 m=1,3
    do 52 n=1,3
        sumcol(m)=sumcol(m)+(magic(n,m))
        52 continue


        do 69 i=1,3

        sumdia(1)=sumdia(1)+magic(i,i)
        sumdia(2)=sumdia(2)+magic((4-i),i)



    69 continue

    loop1:do y=1,3

    loop2:do f=1,3

     loop3:do x=1,2

    if(sumrow(y).eq.15) then
       if(sumcol(f).eq.15)then
        if(sumdia(x).eq.15)then
    logic=1
    else
        logic=0
        exit loop1
  end if
        else
            logic=0
            exit loop1
    end if
       else
        logic=0

        exit loop1
    end if
end do loop3
end do loop2
end do loop1
  

15是魔术常数。循环用于评估aquare是否是魔术。

end
subroutine elementcontrol(elecon,logic2)

integer elecon(3,3),a1,a2,a3,a4,a5,a6,coun(9)
do a4=1,9
    coun(a4)=0
end do
logic2=0
do a1=1,9
    do a2=1,3
        do a3=1,3
            if(a1.eq.elecon(a2,a3))then
                coun(a1)=coun(a1)+1
            end if
        end do
    end do
end do
do a5=1,9
 do a6=1,9
      if(a5.ne.a6) then
    if(coun(a5).eq.coun(a6)) then
        logic2=1
        else
            logic2=0
            exit
    end if
    else
        cycle
        end if
end do
end do
  

有循环来评估每个元素是否彼此不同。

现在的问题是,如果我倾向于增加魔方的行数和列数,我必须重写元素说明符循环。但我不愿意这样做。所以我想声明一个变量,读取它,并且能够使程序创建do循环作为读取。 我希望我清楚地知道我想知道什么。

1 个答案:

答案 0 :(得分:0)

测试看起来像这样:

LOGICAL FUNCTION IsMagical(dim_o_square, SquareData)
IMPLICIT NONE
INTEGER                                    , INTENT(IN   ) :: Dim_o_Square 
REAL, DIMENSION(Dim_o_Square, Dim_o_Square), INTENT(IN   ) :: SquareData

REAL, DIMENSION(Dim_o_Square)                              :: Row_Sum, Col_Sum
REAL                                                       :: Diag_Sum

IsMagical = .FALSE.
INTEGER                                                    :: I 

IF(Dim_o_Square < 2) THEN
  WRITE(*,*) '[SubMagic?:line10]  DIMENSION of square is hosed'
  RETURN
ENDIF

!   Fill the data to determine PFM'ness
DIAG = 0
DO I = 1, Dim_o_Square
  COL_Sum(I) = SUM(SquareData(:,I))
  ROW_Sum(I) = SUM(SquareData(I,:))
  DIAG_Sum   = Diag + SquareData(I,I)
ENDDO

!   Test for PFM'ness
DO I = 2, Dim_o_Square
  IF( COL(I) /= Diag .OR. ROW(I) /= Diag ) THEN
    RETURN
  ENDIF
ENDDO

!Must be magical at this point...
IsMagical = .TRUE.
WRITE(*,*) '[SubMagic?:line40]  Magical and sum value (Row/Col/Diag)=', DIAG_Sum

RETURN
END FUNCTION IsMagical

也许在生产广场的概念中有一些延续?