Fortran Coder

标题: Fortran判断是否为闰年 [打印本页]

作者: 15235312522    时间: 2020-7-2 09:15
标题: Fortran判断是否为闰年
Fortran判断是否为闰年,求教老师看一下哪里出错
[Fortran] 纯文本查看 复制代码
program test0504
implicit none
integer year
write(*,*) "Please input the yaer:"
read(*,*) year
!if((mod(yaer,4.0)==0).or.(mod(yaer,100.0)==0.and.mod(yaer,400.0)==0)) then
if((yaer%4.0==0.0).or.(yaer%100.0==0.0.and.yaer%400.0==0.0)) then
write(*,*) "This year is a leap yaer."
else
write(*,*) "This year is not a leap yaer."
end if
stop
end

错误:
错误        1         error #5082: Syntax error, found REAL_CONSTANT '4.0' when expecting one of: <IDENTIFIER> [              
错误        2         error #5082: Syntax error, found REAL_CONSTANT '100.0' when expecting one of: <IDENTIFIER> [        
错误        3         error #5082: Syntax error, found REAL_CONSTANT '400.0' when expecting one of: <IDENTIFIER> [               
错误        4         error #5082: Syntax error, found ')' when expecting one of: , (/ : ]        
错误        5        Compilation Aborted (code 1)        




作者: fcode    时间: 2020-7-2 10:54
[Fortran] 纯文本查看 复制代码
program test0504
  implicit none
  integer year
  write(*,*) "Please input the yaer:"
  read(*,*) year
  if((mod(year,4)==0).or.(mod(year,100)==0.and.mod(year,400)==0)) then
    write(*,*) "This year is a leap yaer."
  else
    write(*,*) "This year is not a leap yaer."
  end if
end program test0504

作者: li913    时间: 2020-7-2 22:29
(mod(year,400)==0) .or. (mod(year,100)/=0.and.mod(year,4)==0)
作者: liudy02    时间: 2020-7-3 00:21
li913 发表于 2020-7-2 22:29
(mod(year,400)==0) .or. (mod(year,100)/=0.and.mod(year,4)==0)

这个写法不符合尽量减少计算量的原则,虽然没啥大影响……
最节省计算量的写法应该是三个表达式的次序完全倒过来
作者: liudy02    时间: 2020-7-3 00:23
fcode 发表于 2020-7-2 10:54
[mw_shl_code=fortran,true]program test0504
  implicit none
  integer year

版主,判断表达式似乎不太对劲……
作者: 15235312522    时间: 2020-7-3 09:09
谢谢各位老师,改好的程序如下,可以正常运行了
[Fortran] 纯文本查看 复制代码
program test0504
  implicit none
  integer year
  write(*,*) "Please input the yaer:"
  read(*,*) year
  if((mod(year,400)==0).or.(mod(year,100)/=0.and.mod(year,4)==0)) then
   write(*,*) "This year is a leap yaer."
  else
   write(*,*) "This year is not a leap yaer."
  end if
  stop
end program test0504

作者: 15235312522    时间: 2020-7-3 09:25
liudy02 发表于 2020-7-3 00:21
这个写法不符合尽量减少计算量的原则,虽然没啥大影响……
最节省计算量的写法应该是三个表达式的次序完 ...

谢谢老师的指教,问题处理好了
作者: fcode    时间: 2020-7-3 09:57
liudy02 发表于 2020-7-3 00:23
版主,判断表达式似乎不太对劲……

的确不对,光想着改语法问题了,没考虑逻辑错误。
作者: liudy02    时间: 2020-7-7 09:41
本帖最后由 liudy02 于 2020-7-7 09:44 编辑
15235312522 发表于 2020-7-3 09:25
谢谢老师的指教,问题处理好了

最合理的判断语句应该是:
(mod(year,4)==0 .and. (mod(year,100)/=0 .or. mod(year,400)==0))
这样才是最节省计算量的办法
作者: 15235312522    时间: 2020-7-14 10:45
收到,谢谢老师




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2