如何在Fortran中查找文本文件的最大值和最小值

时间:2016-11-18 00:25:44

标签: fortran max min do-loops

PROGRAM maxmin

IMPLICIT NONE


INTEGER :: openStatus, ioStatus     
INTEGER :: counter, counter2, counter3                  
INTEGER :: numberOfInts             
INTEGER :: int_val, max_int, min_int                    

OPEN (UNIT=12, FILE="INT.DAT", STATUS="OLD", ACTION="READWRITE", IOSTAT=openStatus)

IF (openStatus > 0) THEN
  WRITE (*, '(1X, A)') "Can't open file"
  STOP
END IF

WRITE (*, '(1X, A)') "How many integers do you want to read?"
READ (*, '(I6)') numberOfInts

DO counter = 1, numberOfInts

  READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val

  IF (ioStatus < 0) THEN
    WRITE (*, '(1X, A)')  "End of file"
    EXIT
  END IF

END DO

DO counter2 = 1, numberOfInts, 1

   If (counter2 == 1) Then
      max_int = int_val
   END IF
   If (int_val > max_int) Then
      max_int = int_val
   END IF

END DO

DO counter3 = 1, numberOfInts, 1

   If (counter3 == 1) Then
      min_int = int_val
   END IF
   If (int_val < min_int) Then
      min_int = int_val
   END IF

END DO

WRITE (*, '(1X, A)', ADVANCE = "NO") "The max is:"
WRITE (*, '(1X, I4)') max_int

WRITE (*, '(1X, A)', ADVANCE = "NO") "The min is:"
WRITE (*, '(11X, I4)') min_int

CLOSE (12)

END PROGRAM maxmin

Text File

从上图中,当我打印最大值和最小值时,它们总是打印该文本文件中的最后一个数字,即7.我不明白如何解决问题。

我知道整个代码是正确的,但代码的最小部分绝对不是。我很困惑。

2 个答案:

答案 0 :(得分:1)

这是什么

DO counter = 1, numberOfInts

  READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val

  IF (ioStatus < 0) THEN
    WRITE (*, '(1X, A)')  "End of file"
    EXIT
  END IF

END DO

确实是它重复读取一个数字,然后它读取另一个数字并覆盖先前的值,依此类推。最后int_val包含最新的读取值,因为之前的所有值都被后来的值覆盖。

所以当你来到这里时

DO counter2 = 1, numberOfInts, 1

   If (counter2 == 1) Then
      max_int = int_val
   END IF
   If (int_val > max_int) Then
      max_int = int_val
   END IF

END DO

int_val只是读取的最后一个数字,它将是最小值和最大值。

您应该将值存储在数组中,或者在一个循环中执行所有操作:

max_int = -(huge(max_int)-1)      !make initial max_int very small
min_int = huge(max_int)           !make initial min_int very big

DO counter = 1, numberOfInts

  READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val

  IF (ioStatus < 0) THEN
    WRITE (*, '(1X, A)')  "End of file"
    EXIT
  END IF

  If (int_val > max_int) Then
      max_int = int_val
  END IF

  If (int_val < min_int) Then
      min_int = int_val
  END IF
END DO

答案 1 :(得分:0)

另一种方法是使用内部函数(MAXVAL,MAXLOC,MINVAL,MINLOC),并将所有值放入数组或向量中。

PROGRAM maxmin
IMPLICIT NONE

INTEGER                   :: openStatus, ioStatus     
INTEGER                   :: counter!, counter2, counter3                  
INTEGER, PARAMETER        :: MaxIn = 1000000             
INTEGER, DIMENSION(MaxIn) :: int_val                   
INTEGER                   :: numberOfInts             
INTEGER                   :: max_int, min_int                    

OPEN (UNIT=12, FILE="INT.DAT", STATUS="OLD", ACTION="READWRITE", IOSTAT=openStatus)

IF (openStatus > 0) THEN
  WRITE (*, '(1X, A)') "Can't open file"
  STOP
END IF

WRITE (*, '(1X, A)') "How many integers do you want to read?"
READ (*, '(I6)') numberOfInts

!Test array size....
IF(numberOfInts > MaxIn) THEN
  WRITE (*, '(1X,A,I9,A,I7)') "Requested " , numberOfInts," However Array size=",MaxIn
  STOP
ENDIF

DO counter = 1, numberOfInts

  READ (12, '(1X, I5)', IOSTAT = ioStatus) int_val(Counter)

  IF (ioStatus < 0) THEN
    WRITE (*, '(1X, A)')  "End of file"
    EXIT
  END IF

END DO

min_int = MINVAL(int_val(1:numberOfInts))
max_int = MAXVAL(int_val(1:numberOfInts))

WRITE (*, '(1X, A)', ADVANCE = "NO") "The max is:"
WRITE (*, '(1X, I4)') max_int

WRITE (*, '(1X, A)', ADVANCE = "NO") "The min is:"
WRITE (*, '(11X, I4)') min_int

CLOSE (12)

END PROGRAM maxmin