逻辑运算符“ .or”的操作数。在(!)处为Logical(4)/ CHARACTER(1)

时间:2018-08-07 12:47:31

标签: fortran gfortran

我有以下代码:

print*,"type s for a square and type t for a triangle"
read*,fig
if(fig =='t' .or. 'T') then
print*,"Enter the sides of the triangle"
read*,a,b,c
area=tri(a,b,c)
print*,"The area of the triangle is",area
else if (fig=='s' .or.'S') then 
print*,"Enter the side of the square"
read*,s
area=sq(s)

它给我以下错误:

intro_fun.f:9:24:

  if(fig == 't' .or. 'T') then
                        1
Error: Operands of logical operator '.or.' at (1) are LOGICAL(4)/CHARACTER(1)
intro_fun.f:14:27:

  else if(fig=='s' .or. 'S') then
                           1
Error: Operands of logical operator '.or.' at (1) are LOGICAL(4)/CHARACTER(1)

2 个答案:

答案 0 :(得分:5)

您正在使用以下语句:

>>> list_1 = ['AA' ,'BB'] >>> list_1 = [i for i in list_1 for _ in range(4)] >>> list_1 ['AA', 'AA', 'AA', 'AA', 'BB', 'BB', 'BB', 'BB'] 应该是:

if(fig == 't' .or. 'T')

答案 1 :(得分:1)

Fortran有很多方法可以做到这一点。我的第一个测试使用内在比较的功能来对整个值数组广播比较,然后使用ANY转换内在函数将数组结果简化为我们要提取的信息。这是一次与多个值进行比较的好方法,在我看来,当要比较两个以上的值时,它更具可读性。

第二个测试使用SCAN内在函数,这是Fortran可以找出输入字符串中是否有任何字符与给定字符串中的任何字符匹配的方法。

第三项测试使用VERIFY,有点像SCAN,但逻辑为负。

第四项测试使用C interoperabilitytoupper库访问函数C,以执行不区分大小写的比较。不过,必须小心,因为该函数使用整数输入和输出,因此我们必须通过IACHARACHAR进行转换。

program verify_test
   implicit none
   character fig
   integer i
! verify.txt must exist in the current directory
! and have one character on each of the first two lines.
   open(10,file='verify.txt',status='old')
   do i = 1, 2
      read(10,'(a)') fig
      write(*,'(*(g0))') "ANY(fig == ['t','T']) = ",ANY(fig == ['t','T'])
      write(*,'(*(g0))') "SCAN(fig,'tT') /= 0 = ",SCAN(fig,'tT') /= 0
      write(*,'(*(g0))') "VERIFY(fig,'tT') == 0 = ",VERIFY(fig,'tT') == 0
BLOCK
   interface
      function toupper(c) bind(C,name='toupper')
         use ISO_C_BINDING
         implicit none
         integer(C_INT) toupper
         integer(C_INT), value :: c
      end function toupper
   end interface
   write(*,'(*(g0))') "achar(toupper(iachar(fig))) == 'T' = ",achar(toupper(iachar(fig))) == 'T'
END BLOCK
   end do
end program verify_test