Fortran不循环计算输入的变量
当我仅为摄氏度运行代码时,我得到代码下面的结果:Fortran不循环计算输入的变量,fortran,do-loops,Fortran,Do Loops,当我仅为摄氏度运行代码时,我得到代码下面的结果: program temperature ! F C temperature conversion implicit none real :: celcius=0.0, fahrenheit=0.0 integer:: t,n print *,'enter the number of lines' read*, n do n=1,n print*,'enter the value of t: one per line',n read*,
program temperature
! F C temperature conversion
implicit none
real :: celcius=0.0, fahrenheit=0.0
integer:: t,n
print *,'enter the number of lines'
read*, n
do n=1,n
print*,'enter the value of t: one per line',n
read*, t
celcius=5/9*(t-32)
enddo
do n=1,n
print*, t, celcius
enddo
end program
结果
enter the number of lines
3
enter the value of t: one per line 1
50
enter the value of t: one per line 2
20
enter the value of t: one per line 3
10
10 0.00000000E+00
10 0.00000000E+00
10 0.00000000E+00
10 0.00000000E+00
很明显,编译器在计算中没有选择
t
的值。您至少有三个问题:
5/9*(t-32)
从左到右求值,因此5/9
部分是一个整数(截断)除法,总是产生0。零与任何有限的乘积都是零。有几种方法可以解决这个问题,但其中一种更简单的方法是将表达式重写为5*(t-32)/9
t
和celcius
是标量。他们每次只持有一个号码。在第一个循环中,按顺序为每个循环分配多个值。稍后执行第二个循环以打印结果时,将只能访问分配给每个变量的最后一个值。如果您必须将输出延迟到读取所有输入之后,那么处理它的一种方法是使t
和celcius
数组具有足够的大小,并将您的值存储在不同的元素中。(另请注意:英语单词的正确拼写为“celsius”。)do
循环完成后,迭代变量的值是它在循环的下一次迭代中应该具有的值(如果存在)。因此,通过同时使用变量n
作为迭代变量和上限,可以使每次循环后的值与之前不同。有几种方法可以解决这个问题,但我敦促您避免将n
重用为迭代变量。避免一个有目的的迭代变量是没有效率的您至少有三个问题:
5/9*(t-32)
从左到右求值,因此5/9
部分是一个整数(截断)除法,总是产生0。零与任何有限的乘积都是零。有几种方法可以解决这个问题,但其中一种更简单的方法是将表达式重写为5*(t-32)/9
t
和celcius
是标量。他们每次只持有一个号码。在第一个循环中,按顺序为每个循环分配多个值。稍后执行第二个循环以打印结果时,将只能访问分配给每个变量的最后一个值。如果您必须将输出延迟到读取所有输入之后,那么处理它的一种方法是使t
和celcius
数组具有足够的大小,并将您的值存储在不同的元素中。(另请注意:英语单词的正确拼写为“celsius”。)do
循环完成后,迭代变量的值是它在循环的下一次迭代中应该具有的值(如果存在)。因此,通过同时使用变量n
作为迭代变量和上限,可以使每次循环后的值与之前不同。有几种方法可以解决这个问题,但我敦促您避免将n
重用为迭代变量。避免一个有目的的迭代变量是没有效率的你有几个选择
program temperature
! F C temperature conversion
implicit none
real, allocatable :: fahrenheit(:)
integer:: n
print *,'enter the number of lines'
read*, n
allocate(fahrenheit(n))
print*,'enter the value of t: all items on one line'
read*, fahrenheit
print *, 'F: ', fahrenheit
print *, 'C:', fahrenheit_to_celcius(fahrenheit)
contains
pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
real, intent(in) :: t_f
real :: t_c
t_c = 5.*(t_f-32.)/9.
end function fahrenheit_to_celcius
end program
program temperature
! F C temperature conversion
implicit none
real :: fahrenheit
integer:: i, n
print *,'enter the number of lines'
read*, n
do i = 1, n
print*,'enter the value of t: one per line',n
read*, fahrenheit
print *, 'F: ', fahrenheit, 'C:', fahrenheit_to_celcius(fahrenheit)
enddo
contains
pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
real, intent(in) :: t_f
real :: t_c
t_c = 5.*(t_f-32.)/9.
end function fahrenheit_to_celcius
end program
fahrenheit_to_celcius
在这两种情况下都是相同的
我修复了
5/9
(返回0)和混合变量。您有几个选项
program temperature
! F C temperature conversion
implicit none
real, allocatable :: fahrenheit(:)
integer:: n
print *,'enter the number of lines'
read*, n
allocate(fahrenheit(n))
print*,'enter the value of t: all items on one line'
read*, fahrenheit
print *, 'F: ', fahrenheit
print *, 'C:', fahrenheit_to_celcius(fahrenheit)
contains
pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
real, intent(in) :: t_f
real :: t_c
t_c = 5.*(t_f-32.)/9.
end function fahrenheit_to_celcius
end program
program temperature
! F C temperature conversion
implicit none
real :: fahrenheit
integer:: i, n
print *,'enter the number of lines'
read*, n
do i = 1, n
print*,'enter the value of t: one per line',n
read*, fahrenheit
print *, 'F: ', fahrenheit, 'C:', fahrenheit_to_celcius(fahrenheit)
enddo
contains
pure elemental function fahrenheit_to_celcius(t_f) result(t_c)
real, intent(in) :: t_f
real :: t_c
t_c = 5.*(t_f-32.)/9.
end function fahrenheit_to_celcius
end program
fahrenheit_to_celcius
在这两种情况下都是相同的
我修复了
5/9
(返回0)和混合变量。只有在第一个循环中输入的最后一个值提供了循环外t
的值。如果要使用多个值,则应使用一个数组,或围绕第一个循环重新构造其余值。只有在第一个循环中输入的最后一个值才为循环外的t
提供值。如果您想使用多个值,您应该使用一个数组,或者围绕第一个循环重新构造其余的值?很抱歉,我知道这是一个愚蠢的问题,但我正在试图理解数组和循环是如何工作的。我认为还有一个更严重的问题:don=1,n
,即重新定义n
的值这是一个有趣的观察结果,@albert,主要是因为它错得出奇。在符合要求的Fortran实现中,索引的do
循环的迭代次数是在进入循环之前计算的(好像)。因此,如果上界由一个变量给出,并且该变量在循环执行期间被修改,包括如果它被用作迭代变量,则迭代次数不受影响。事实上,您可以在OP报告的输出中看到这一点的影响。您尤其可以看到,在第一个循环中,initialn
的值为3,在第二个循环中,initialn
的值为4(不是100%确定,但我认为循环末尾的循环计数器的值实际上取决于实现)。是的,@albert,我