Fortran Coder

查看: 7867|回复: 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] 纯文本查看 复制代码
01program add_gamit_station_info
02implicit none
03 
04integer :: count,stat,err,err2,i,stat2,stat3,k,stat4
05character :: example_line*300,filename*30,line*300,site*4
06character (len=300),allocatable :: example_line_array(:)
07character (len=4),allocatable :: example_site_array(:)
08 
09open (11,file='list.txt')
10close (11,status='delete')
11call system ('dir /b *.info > list.txt')
12open (11,file='list.txt')
13open (22,file='station_cwb.info.db')
14open (33,file='station_expt.info.db')
15!---------------------------------------------> to check array size
16do k=1,5,1
17  read (22,*,iostat=stat4) example_line
18end do
19count=0
20stat=0
21do while (stat==0)
22  read (22,'(a300)',iostat=stat) example_line
23  if (stat/=0) exit
24  count=count+1
25end do
26!---------------------------------------------> to check array size
27rewind (22)
28!---------------------------------------------> to allocate the example_line_array & example_site_array
29allocate (example_line_array(count),stat=err)
30 
31if (err==0) then
32  write (*,*) 'The example_line_array allocated sucessfully.'
33else if (err/=0) then
34  write (*,*) 'The example_line_array failed to allocate.'
35end if
36 
37allocate (example_site_array(count),stat=err2)
38 
39if (err2==0) then
40  write (*,*) 'The example_site_array allocated sucessfully.'
41else if (err/=0) then
42  write (*,*) 'The example_site_array failed to allocate.'
43end if
44!---------------------------------------------> to allocate the example_line_array & example_site_array
45 
46!---------------------------------------------> to assign array value from station_cwb.info into example_line_array
47do k=1,5,1
48  read (22,*,iostat=stat4) example_line
49end do
50 
51do i=1,count,1
52  read (22,'(a300)') example_line_array(i)
53end do
54!---------------------------------------------> to assign array value from file into example_line_array
55 
56!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
57!where (example_site_array/=' ') example_site_array=example_line_array(2:5)
58 
59 
60forall (i=1:count:1) example_site_array(i)=example_line_array(i)(2:5)
61!---------------------------------------------> to assign array value from example_line_array(2:5) into example_site_array
62do i=1,count,1
63  write (*,*) example_site_array(i)
64end do
65rewind (22)
66!---------------------------------------------> to check whether is new site.
67!---------------------------------------------> if it is a new site , we write down the name of site.
68!---------------------------------------------> if it isn't a new site , we skip site.                              
69stat2=0
70do while (stat2==0)
71  read (11,*,iostat=stat2) filename
72  if (stat2/=0) exit
73  open (44,file=trim(filename))
74   
75  do k=1,5,1
76      read (44,*,iostat=stat4) line
77  end do
78   
79  stat3=0
80  do while (stat3==0)
81    read (44,'(a300)',iostat=stat3) line
82    if (stat3/=0) exit
83    site=line(2:5)
84    if (any(example_site_array==site)) cycle
85    write (33,'(a4)') site                 
86  end do
87end do
88!---------------------------------------------> to check whether is new site.
89!---------------------------------------------> if it is a new site , we write down the name of site.
90!---------------------------------------------> if it isn't a new site , we skip site.
91close (11)
92close (22)
93close (33)
94close (44)
95end program






Desktop.rar

4.14 KB, 下载次数: 0

相關資料

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

2038

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1676 元
贡献
715 点

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

沙发
发表于 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, 2025-5-6 05:46

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

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