我正在编写一个 Fortran90 程序,该程序将获取一个包含要绘制的点的输入文件,对这些点进行排序,计算最小和最大 x 和 y 值,将所有这些打印到另一个文件,然后创建一个图形并打印出来。
到目前为止,我正在计算最小值和最大值,并且我已经将它们全部打印到一个文件中。
程序的结构是这样的:
Please enter an input file:
GRAPH3.txt
Please enter an output file:
output.txt
然后它会从 GRAPH3.txt 中提取数据,做一些事情,然后打印出来。
GRAPH3.txt 看起来像这样:
11
8.3 8.3
12.0 12.0
2.0 2.0
4.0 4.0
1.0 1.0
4.5 4.5
12.1 12.1
4.6 4.6
3.0 3.0
7.2 7.2
9.0 9.0
第一个数字表示有多少点。其余的只是数字对。
当我运行我的程序时,这是我的输出:
presort
8.30 8.30
12.00 12.00
2.00 2.00
4.00 4.00
1.00 1.00
4.50 4.50
12.10 12.10
4.60 4.60
3.00 3.00
7.20 7.20
9.00 9.00
postsort
1.00 1.00
2.00 2.00
3.00 3.00
4.00 4.00
4.00 4.00
4.00 4.00
7.20 7.20
8.00 8.00
9.00 9.00
12.00 12.00
12.00 12.00
Xmin: 1.00
Xmax: 12.00
Ymin: 0.00
ymax: 12.00
显然,ymin
应该是1.00
,但它显示在0.00
。你知道它为什么会这样做吗?
这是我的程序:
PROGRAM G6P4
implicit none
character(len=30) :: infile,outfile
logical inexist, outexist,more,quit
integer i,j,n,overwrite
real x(100),y(100),xmax,xmin,ymax,ymin
more=.true.
inexist = .false.
outexist = .false.
overwrite=2
do while (.not.inexist)
print *, 'Please enter an input filename'
read *, infile
INQUIRE(FILE=infile, EXIST=inexist)
if (.not.inexist) then
print *, 'Invalid Input File'
end if
end do
do while (more.or.infile.eq.outfile)
print *, 'Please enter an output filename'
read *, outfile
INQUIRE(FILE=outfile, EXIST=outexist)
if (infile.eq.outfile) then
print *, 'Outfile cannot equal infile.'
else
if (outexist) then
print *, 'File already exists'
print *, '1 to enter overwrite, 2 to enter new file, 0 to quit'
read *,overwrite
select case (overwrite)
case (1)
more =.false.
case (2)
more = .true.
case (0)
more = .false.
quit = .true.
case default
print *,'NOPE'
more = .true.
end select
else
more=.false.
end if
end if
end do
if (quit.eqv..false.) then
OPEN(1, FILE=infile)
OPEN(2, FILE=outfile)
READ(1,*)n
if (n.le.100.and.n.gt.0) then
do i = 1, n
read (1,*) x(i),y(i)
end do
write (2,*) 'presort'
do i = 1, n
write (2,'(2(F6.2,X))') x(i),y(i)
end do
call sort(x,y,n)
write (2,*) 'postsort'
do i = 1, n
write (2,'(2(F6.2,X))') x(i),y(i)
end do
xmin = x(1)
xmax = x(n)
ymax = y(1)
ymax = y(n)
do i = 2, n
if (ymin.GT.y(i)) then
ymin=y(i)
end if
if (ymax.lt.y(i)) then
ymax=y(i)
end if
enddo
write (2,'(A,X,F6.2)') 'Xmin: ',xmin
write (2,'(A,X,F6.2)') 'Xmax: ',xmax
write (2,'(A,X,F6.2)') 'Ymin: ',ymin
write (2,'(A,X,F6.2)') 'ymax: ',ymax
else
print *,'File has invalid number of data points'
end if
end if
end PROGRAM
subroutine sort(x,y,n)
real, intent(out),dimension(100) :: x,y
integer :: n
integer :: end,j,t
logical :: more
end = n-1
do while (more)
more = .false.
do j = 1, end
if (x(j).gt.x(j+1)) then
write (*,'(A,2(F6.1,X))')'Pre: ',x(j),x(j+1)
t = x(j)
x(j)=x(j+1)
x(j+1)=t
t=y(j)
y(j) = y(j+1)
y(j+1)=t
more = .true.
write (*,'(A,2(F6.1,X))')'Post: ',x(j),x(j+1)
end if
enddo
end = end-1
enddo
end subroutine
我发现的另一个问题是,如果我从 sort 子例程中删除 write 语句,由于某种原因,程序根本不会排序。任何线索为什么会这样做?