Fortran:IF语句不起作用(尽管变量具有正确的值)

时间:2015-03-06 20:18:17

标签: if-statement fortran

    read(100,5) temp, a, charac    
  5 format(A11,i1,A7)

    read(100,*) b
    read(100,*) c
    read(100,*) d

    if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
         .or. d .ne. 0) .and. trim(charac) .ne. 'spline') then

      print *, 'Scenario A'

    else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
              .or. d .ne. 0) .and. trim(charac) .eq. 'spline') then

      print *, 'Scenario B'

    else

      print *, 'Scenario C'

    end if

输入文件:     * ------------------------------------------------- --------------

My input file from which i am reading these looks as below:
      1      spline
      0
      0
      0
*----------------------------------------------------------------

我想要什么

scenario A is triggered if any of the integers a,b,c,d have a value of "1" and  input file not containing the word "spline"

scenario B is triggered if any of the integers a,b,c,d have a value of "1" and  input file contains the word "spline"

scenario C is triggered if all of the integers a,b,c,d have a value of "0" and  input file not containing the word "spline"

* ---------------------------------------------- -----------------------

我最初在场景A的样条线上放置了一些其他词来触发,但如果没有任何内容写入空格,我要求场景A和C触发。

这就是我在read中使用格式化的原因,否则当read语句在该特定行中找不到任何字符时,它将转到下一行。

我已尝试打印变量,变量显示正确的值以触发方案B,但仍会触发A

1 个答案:

答案 0 :(得分:0)

 read(100,*,end=5)a,charac    
    5 go to 6

6   read(100,*)b
    read(100,*)c
    read(100,*)d

    if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
         .or. d .ne. 0) .and. trim(charac) .ne. 'spline') then

      print*, 'Scenario A'

    else if ((a .ne. 0 .or. b .ne. 0 .or. c .ne. 0 &
              .or. d .ne. 0) .and. trim(charac) .eq. 'spline') then

      print*, 'Scenario B'

    else

      print*, 'Scenario C'

    endif

说明: 我面临的问题是我希望代码工作,以便它读取整数的值" a"和角色" charac"来自同一条线。如果" charac"我想让代码经历不同的场景。在输入文件中有特定值,如果没有输入" charac"即输入行仅包含整数且不包含字符。 出现问题的是,如果输入文件在该特定行中没有字符,则read语句继续读入下一行。

为了避免这种情况,我在读取一定数量的字符后使用格式化来停止读取行,这就是我的问题所示。这样做if语句不能正常工作,可能是因为格式化而无法正确读取输入。

我是通过在read命令中使用end语句并使用go to命令来实现的。所以现在read语句在那一行结束,如果没有找到charac的任何输入,我会要求代码转到下一个read语句。

这是我正在使用的确切代码:

read(100,*)curv,spanwise_spline
if (trim(spanwise_spline).eq.'spline')then
 go to 6
elseif (trim(spanwise_spline).ne.'spline')then
go to 7
endif

6 read(100,*)
7 read(100,*)thick_distr
read(100,*)
read(100,*)thick
read(100,*)
read(100,*)LE