主题:求各位大神帮忙解答一下fortran的问题
我需要写一program去输出**的并集和交集。
题目是这样的
In set theory, the
union of two sets is the list of all elements that appear in either (or both) of
the sets., and the intersection of the two sets is the list of all elements that
appear in both sets only .
A consists of the elements
A {1, 3,7, 6, 2
,5 }
B consist of the elements
B { -1, 2, 0, 5, 8, 9}
A union B
{-1, 0, 1, 2, 3, 5, 6, 7, 8, 9
A intersection B { 2, 5 }
Write a
program that reads in two arrays of integers representing the elements of two
sets from two different user-specified input files and calculates both the union
and the intersection of the two sets. Use arrays to contain the input sets and
build both the union and intersection. Note that the input sets may not be
sorted in order, so your algorithm must work regardless of the order in which
set elements are entered.
Test your program on two
files
inputA. dat 0, 1, -3, 5, -11, 6, 8, 11,17,15,7,12
inputB.
dat 0, -1, 3, 7, -6, 16, 5, 12, 21
我写的是这样的:
PROGRAM set
!
!
Purpose:
! To read in two arrays of intrgers representing the
! elements
of two sets from two different user-specified
! input files, and calculate
both the union and the
! intersection of the two sets.
!
! Record of
revisions:
! Date Programmer Description of change
! ======== ============
=======================
! 11/14/12 Huang Wenjun Original
code
!
IMPLICIT NONE ! Data dictionary: declare variable types &
definitions
CHARACTER::filenameA
CHARACTER::filenameB
INTEGER,DIMENSION(15)::a
! Data array of set A
INTEGER,DIMENSION(15)::b ! Data array of set
B
INTEGER,DIMENSION(15)::union ! The union of set A and set
B
INTEGER,DIMENSION(15)::intersection ! The intersection of set A &
B
INTEGER::i ! Loop index
INTEGER::j ! Loop index
INTEGER::n=0 ! Number
of data values to
sort
INTEGER::m=0
INTEGER::u=0
INTEGER::c=0
INTEGER::status1 ! I/0
status: 0 for success
INTEGER::status2
INTEGER::temp ! Temporary variable
for swapping ! Get the name of file containing the input
data
WRITE(*,1000)
1000 FORMAT(1X,'Enter the file name which only contain
&
integers for set A')
READ(*,*)filenameA
WRITE(*,1010)
1010
FORMAT(1X,'Enter the file name which only contain &
intergers for set
B')
READ(*,*)filenameB ! Open input data file. Status is OLD because the
input data must
! already exist.
OPEN ( UNIT=2, FILE=filenameA,
STATUS='OLD', ACTION='READ', &
IOSTAT=status1 ) ! Was the OPEN
successful?
fileopen1: IF (status1==0) THEN ! OPEN successful ! First read in
data
openfile1: DO
READ (2,*,IOSTAT=status1)temp ! get
value
IF(status1/=0) EXIT ! Exit on end of data
n=n+1 ! Bump
count
a(n)=temp
END DO openfile1 ! Open input data file. Status is OLD
because the input data must
! already exist.
OPEN ( UNIT=3,
FILE=filenameB, STATUS='OLD', ACTION='READ', &
IOSTAT=status2 ) ! Was the
OPEN successful?
fileopen2: IF (status2==0) THEN ! OPEN successful ! First
read in data
openfile2: DO
READ (3,*,IOSTAT=status2)temp ! get
value
IF(status2/=0) EXIT ! Exit on end of data
m=m+1 ! Bump
count
b(m)=temp
END DO openfile2 ! Now, sort the data
outer: DO
i=1,n
inner: DO j= 1,m
IF (a(i)==b(j))
THEN
c=c+1
intersection(c)=a(i)
u=u+1
union(u)=a(i)
ELSE
u=u+1
union(u)=a(i)
u=u+1
union(u)=b(j)
END IF
END DO
inner
END DO outer WRITE(*,*)'The union of the two
sets:'
WRITE(*,'(4X,F10.4)')(union(u),u=1,u)
WRITE(*,*)'The intersection
of the two sets:'
WRITE(*,'(4X,F10.4)')(intersection(c),c=1,c) ELSE fileopen2
WRITE(*,1040) status2
1040 FORMAT(' ','Error opening fileB:
IOSTAT=',I6)
END IF fileopen2
ELSE fileopen1
WRITE(*,1050)
status1
1050 FORMAT(' ','Error opening fileA: IOSTAT=',I6)
END IF
fileopen1
CLOSE(UNIT=2)
CLOSE(UNIT=3) END PROGRAM
set
待我已输入两个文件,run的结果就是Error opening fileA. IOSTAT=
2
真心不知道要怎么办,求助求助
题目是这样的
In set theory, the
union of two sets is the list of all elements that appear in either (or both) of
the sets., and the intersection of the two sets is the list of all elements that
appear in both sets only .
A consists of the elements
A {1, 3,7, 6, 2
,5 }
B consist of the elements
B { -1, 2, 0, 5, 8, 9}
A union B
{-1, 0, 1, 2, 3, 5, 6, 7, 8, 9
A intersection B { 2, 5 }
Write a
program that reads in two arrays of integers representing the elements of two
sets from two different user-specified input files and calculates both the union
and the intersection of the two sets. Use arrays to contain the input sets and
build both the union and intersection. Note that the input sets may not be
sorted in order, so your algorithm must work regardless of the order in which
set elements are entered.
Test your program on two
files
inputA. dat 0, 1, -3, 5, -11, 6, 8, 11,17,15,7,12
inputB.
dat 0, -1, 3, 7, -6, 16, 5, 12, 21
我写的是这样的:
PROGRAM set
!
!
Purpose:
! To read in two arrays of intrgers representing the
! elements
of two sets from two different user-specified
! input files, and calculate
both the union and the
! intersection of the two sets.
!
! Record of
revisions:
! Date Programmer Description of change
! ======== ============
=======================
! 11/14/12 Huang Wenjun Original
code
!
IMPLICIT NONE ! Data dictionary: declare variable types &
definitions
CHARACTER::filenameA
CHARACTER::filenameB
INTEGER,DIMENSION(15)::a
! Data array of set A
INTEGER,DIMENSION(15)::b ! Data array of set
B
INTEGER,DIMENSION(15)::union ! The union of set A and set
B
INTEGER,DIMENSION(15)::intersection ! The intersection of set A &
B
INTEGER::i ! Loop index
INTEGER::j ! Loop index
INTEGER::n=0 ! Number
of data values to
sort
INTEGER::m=0
INTEGER::u=0
INTEGER::c=0
INTEGER::status1 ! I/0
status: 0 for success
INTEGER::status2
INTEGER::temp ! Temporary variable
for swapping ! Get the name of file containing the input
data
WRITE(*,1000)
1000 FORMAT(1X,'Enter the file name which only contain
&
integers for set A')
READ(*,*)filenameA
WRITE(*,1010)
1010
FORMAT(1X,'Enter the file name which only contain &
intergers for set
B')
READ(*,*)filenameB ! Open input data file. Status is OLD because the
input data must
! already exist.
OPEN ( UNIT=2, FILE=filenameA,
STATUS='OLD', ACTION='READ', &
IOSTAT=status1 ) ! Was the OPEN
successful?
fileopen1: IF (status1==0) THEN ! OPEN successful ! First read in
data
openfile1: DO
READ (2,*,IOSTAT=status1)temp ! get
value
IF(status1/=0) EXIT ! Exit on end of data
n=n+1 ! Bump
count
a(n)=temp
END DO openfile1 ! Open input data file. Status is OLD
because the input data must
! already exist.
OPEN ( UNIT=3,
FILE=filenameB, STATUS='OLD', ACTION='READ', &
IOSTAT=status2 ) ! Was the
OPEN successful?
fileopen2: IF (status2==0) THEN ! OPEN successful ! First
read in data
openfile2: DO
READ (3,*,IOSTAT=status2)temp ! get
value
IF(status2/=0) EXIT ! Exit on end of data
m=m+1 ! Bump
count
b(m)=temp
END DO openfile2 ! Now, sort the data
outer: DO
i=1,n
inner: DO j= 1,m
IF (a(i)==b(j))
THEN
c=c+1
intersection(c)=a(i)
u=u+1
union(u)=a(i)
ELSE
u=u+1
union(u)=a(i)
u=u+1
union(u)=b(j)
END IF
END DO
inner
END DO outer WRITE(*,*)'The union of the two
sets:'
WRITE(*,'(4X,F10.4)')(union(u),u=1,u)
WRITE(*,*)'The intersection
of the two sets:'
WRITE(*,'(4X,F10.4)')(intersection(c),c=1,c) ELSE fileopen2
WRITE(*,1040) status2
1040 FORMAT(' ','Error opening fileB:
IOSTAT=',I6)
END IF fileopen2
ELSE fileopen1
WRITE(*,1050)
status1
1050 FORMAT(' ','Error opening fileA: IOSTAT=',I6)
END IF
fileopen1
CLOSE(UNIT=2)
CLOSE(UNIT=3) END PROGRAM
set
待我已输入两个文件,run的结果就是Error opening fileA. IOSTAT=
2
真心不知道要怎么办,求助求助