Fortran 的优势就是矩阵运算。你的程序可以非常简单。
program main
real a(3,3),b(3,3),c(3,3),cmax
integer n,m
print*,'输入m'
read*,m
print*,'输入n'
read*,n
print*,'输入矩阵a'
read*,a
print*,'输入b'
read*,b
call smv(a,b,c,m,m,n,cmax)
print*,'matrice c',c,'cmax',cmax
end
subroutine smv(a,b,c,ld,m,n,cmax)
real a(ld,m),b(ld,m),c(ld,m)
integer n,m
c = a + b
cmax = maxval(c)
end subroutine
这两句代码就可以了。
我建议你看一下这个文章,关于定义数组和变量的一些常识:fcode.cn/guide-43-1.html
不保证全对 你在检查一下吧program BIN_SUM
implicit none
integer:: x,y,z,counter
integer::array_x(8),array_y(8),array_sum(8),array_carry(8)
!!initialize all the arrays to zero
do counter=1,8
array_x(counter)=0
array_y(counter)=0
array_sum(counter)=0
array_carry(counter)=0
end do
write(*,*) "please enter two positive number X and Y which are smaller than 256"
read(*,*) x
read(*,*) y
z=x+y
if (z>256) then
write(*,*) "the sum of two number must be smaller than 256"
end if
if(x>=0 .and. x<=256 .and. y>=0 .and. y<=256) then
!divide x by 2, and put the remainder into each colunm of the array
do counter=8,1,-1
array_x(counter)=mod(x,2)
x=x/2
end do
write(*,*)"your X number in binary form is:"
write(*,*) (array_x(counter),counter=1,8)
do counter=8,1,-1
array_y(counter)=mod(y,2)
y=y/2
end do
write(*,*)"your Y number in binary form is :"
write(*,*) (array_y(counter),counter=1,8)
else
write(*,*) "wrong walue! please type your number between 0 and 256"
end if
!!use do loop to perform the calculation, if array_x + array _y = 2 put 1 in the carry array and put zero in the sum array
do counter=8,1,-1
if ((array_x(counter)+array_y(counter)+array_carry(counter))==1)then
array_sum(counter)=1
array_carry(counter-1)=0
end if
if ((array_x(counter)+array_y(counter)+array_carry(counter))==2) then
array_sum(counter)=0
array_carry(counter-1)=1
end if
if ((array_x(counter)+array_y(counter)+array_carry(counter))==3) then
array_sum(counter)=1
array_carry(counter-1)=1
end if
end do
write(*,*)"the sum of two numbers is :",z
write(*,*)"the sum of two number in binary form is :"
write(*,*) (array_sum(counter),counter=1,8)
end program BIN_SUM
“出现则输出相应的第二列第三列,进行加减运算”这句里不清楚是否需要输出第一列的数据,我就只输出了第二、第三列。
需要第一列数据的话,改一下write21。
还有加减运算不清楚具体需要对哪些数据进行加法还是减法运算,
我就只对第二列和第三列数据进行了求和。
需要其他加减结果的话,改一下write22。
!
implicit none
integer a(10,3),b(5),i,j
! 读取文件A
open(11,file='A.txt')
do i=1,10
read(11,*) (a(i,j),j=1,3)
end do
close(11)
! 读取文件B
open(12,file='B.txt')
do i=1,5
read(12,*) b(i)
end do
close(12)
! 比较A和B,并输出结果
open(21,file='result.txt')
open(22,file='sum.txt')
do i=1,5
do j=1,10
if(a(j,1).eq.b(i)) then
write(21,*) a(j,2),a(j,3)
write(22,*) a(j,2)+a(j,3)
end if
end do
end do
close(21)
close(22)
stop
end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)