Fortran Coder

查看: 7218|回复: 2
打印 上一主题 下一主题

[讨论] 關於forall和where在程序裡使用的問題

[复制链接]

35

帖子

12

主题

0

精华

熟手

F 币
173 元
贡献
117 点
跳转到指定楼层
楼主
发表于 2015-8-24 21:01:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 max533 于 2015-8-24 21:14 编辑

下面是我的一個程序,但我不清楚為什麼forall和where在這個案例上的應用。為什麼forall可以,但where不行。
分別在line57和line60,如果使用forall的話,在螢幕上就只能顯示4個字的字串。如果使用where的話,在螢幕上就只能顯示3個字的字串。
程式的相關附件在壓縮檔中。

先在這邊感謝各位高手。


[Fortran] 纯文本查看 复制代码
program add_gamit_station_info
implicit none

integer :: count,stat,err,err2,i,stat2,stat3,k,stat4
character :: example_line*300,filename*30,line*300,site*4
character (len=300),allocatable :: example_line_array(:)
character (len=4),allocatable :: example_site_array(:) 

open (11,file='list.txt')
close (11,status='delete')
call system ('dir /b *.info > list.txt')
open (11,file='list.txt')
open (22,file='station_cwb.info.db')
open (33,file='station_expt.info.db')
!---------------------------------------------> to check array size
do k=1,5,1
  read (22,*,iostat=stat4) example_line
end do
count=0
stat=0
do while (stat==0)
  read (22,'(a300)',iostat=stat) example_line
  if (stat/=0) exit
  count=count+1
end do
!---------------------------------------------> to check array size
rewind (22)
!---------------------------------------------> to allocate the example_line_array & example_site_array
allocate (example_line_array(count),stat=err)

if (err==0) then 
  write (*,*) 'The example_line_array allocated sucessfully.'
else if (err/=0) then 
  write (*,*) 'The example_line_array failed to allocate.'
end if

allocate (example_site_array(count),stat=err2)

if (err2==0) then
  write (*,*) 'The example_site_array allocated sucessfully.'
else if (err/=0) then 
  write (*,*) 'The example_site_array failed to allocate.'
end if 
!---------------------------------------------> to allocate the example_line_array & example_site_array

!---------------------------------------------> to assign array value from station_cwb.info into example_line_array
do k=1,5,1
  read (22,*,iostat=stat4) example_line
end do

do i=1,count,1
  read (22,'(a300)') example_line_array(i)
end do
!---------------------------------------------> to assign array value from file into example_line_array

!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
!where (example_site_array/=' ') example_site_array=example_line_array(2:5)


forall (i=1:count:1) example_site_array(i)=example_line_array(i)(2:5)
!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
do i=1,count,1
  write (*,*) example_site_array(i)
end do
rewind (22)
!---------------------------------------------> to check whether is new site.
!---------------------------------------------> if it is a new site , we write down the name of site.
!---------------------------------------------> if it isn't a new site , we skip site.                               
stat2=0
do while (stat2==0)
  read (11,*,iostat=stat2) filename
  if (stat2/=0) exit
  open (44,file=trim(filename))
  
  do k=1,5,1
      read (44,*,iostat=stat4) line
  end do
  
  stat3=0
  do while (stat3==0)
    read (44,'(a300)',iostat=stat3) line 
    if (stat3/=0) exit
    site=line(2:5)
    if (any(example_site_array==site)) cycle
    write (33,'(a4)') site                  
  end do
end do 
!---------------------------------------------> to check whether is new site.
!---------------------------------------------> if it is a new site , we write down the name of site.
!---------------------------------------------> if it isn't a new site , we skip site.
close (11)
close (22)
close (33)
close (44)
end program






Desktop.rar

4.14 KB, 下载次数: 0

相關資料

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1958

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1341 元
贡献
565 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2015-8-24 21:21:28 | 只看该作者
改为
where (example_site_array/=' ') example_site_array(:)=example_line_array(:)(2:5)
你那样写的话,编译器会认为 (2:5) 是数组片段,而不是子字符串

35

帖子

12

主题

0

精华

熟手

F 币
173 元
贡献
117 点
板凳
 楼主| 发表于 2015-8-24 21:27:02 | 只看该作者
fcode 发表于 2015-8-24 21:21
改为
where (example_site_array/=' ') example_site_array(:)=example_line_array(:)(2:5)
你那样写的话, ...

感謝fcode的說明,居然犯了這種錯誤,感謝fcode提醒。
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-4-25 00:49

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表