回 帖 发 新 帖 刷新版面

主题:新手上路,有个error始终解决不了

部分代码:
write(*,*)"请输入数据个数nn=:"
read(*,*)nn
write(*,*)"请输入水的粘度Uw=:"
read(*,*)Uw
write(*,*)"请输入含盐量Csep=:"
read(*,*)Csep
write(*,*)"请输入Sp=:"
read(*,*)Sp
write(*,*)"请输入聚合物的浓度:"
read(*,*) (Cp(i),i=1,nn)
write(*,*)"请输入聚合物的粘度:"
read(*,*) (Up(i),i=1,nn)
sumC1=0;sumC2=0;sumC3=0;sumC4=0
sumU1=0;sumU2=0;sumU3=0
do i=1,nn
  sumC1=sumC1+Cp(i)
  sumC2=sumC2+Cp(i)**2
  sumC3=sumC3+Cp(i)**3
  sumC4=sumC4+Cp(i)**4
  sumU1=sumU1+(Up(i)/Uw-1)/(Csep**Sp*Cp(i))
  sumU2=sumU2+(Up(i)/Uw-1)/(Csep**Sp)
  sumU3=sumU3+(Up(i)/Uw-1)/(Csep**Sp)*Cp(i)     
end do
real::A(3,3)=(/nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/)
real::S(3)=(/sumU1,sumU2,sumU3/)
提示错误:
Error: A specification statement cannot appear in the executable section.
大概意思是声明在执行语句之前吧,但是我的A,S数组里的变量都是通过上面的计算得到的,不能放在计算之前啊!这种情况怎么办呢?请高手多多指教!不胜感激!

回复列表 (共11个回复)

沙发

非得声明的时候赋值啊?先声明,算完再赋值!!

板凳

我在计算之前声明了啊怎么也不行呢?

3 楼


贴出来

4 楼

主程序如下:
program main
use LinearAlgebra
implicit none
integer,parameter::N=3
integer::i,nn
real::A(N,N)
real::S(N)
real::ANS(N)
real::Cp(20),Up(20)
real::Uw,Csep,Sp,sumC1,sumC2,sumC3,sumC4,sumU1,sumU2,sumU3
write(*,*)"请输入数据个数nn=:"
read(*,*)nn
write(*,*)"请输入水的粘度Uw=:"
read(*,*)Uw
write(*,*)"请输入含盐量Csep=:"
read(*,*)Csep
write(*,*)"请输入Sp=:"
read(*,*)Sp
write(*,*)"请输入聚合物的浓度:"
read(*,*) (Cp(i),i=1,nn)
write(*,*)"请输入聚合物的粘度:"
read(*,*) (Up(i),i=1,nn)
sumC1=0;sumC2=0;sumC3=0;sumC4=0
sumU1=0;sumU2=0;sumU3=0
do i=1,nn
  sumC1=sumC1+Cp(i)
  sumC2=sumC2+Cp(i)**2
  sumC3=sumC3+Cp(i)**3
  sumC4=sumC4+Cp(i)**4
  sumU1=sumU1+(Up(i)/Uw-1)/(Csep**Sp*Cp(i))
  sumU2=sumU2+(Up(i)/Uw-1)/(Csep**Sp)
  sumU3=sumU3+(Up(i)/Uw-1)/(Csep**Sp)*Cp(i)     
end do
real::A(N,N)=(/nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/)
real::S(N)=(/sumU1,sumU2,sumU3/)call Gauss_Jordan(A,S,ANS)
!调用子程序用Gauss_Jordan消去法求解方程
call Gauss_Jordan(A,S,ANS)
write(*,*)'ANS:'
do i=1,N
  write(*,"(1x,a1,'=',F8.4)")char(64+i),ANS(i)
end do
stop
end program

5 楼


不能real::A(N,N)=(/nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/)
real::S(N)=(/sumU1,sumU2,sumU3/)call Gauss_Jordan(A,S,ANS)
这样赋值啊,晕。

6 楼

program main
use LinearAlgebra
implicit none
integer,parameter::N=3
integer::i
real::A(N,N)
real::S(N)
real::ANS(N)
real::Cp(20),Up(20)
real::Uw,Csep,Sp,sumC1,sumC2,sumC3,sumC4,sumU1,sumU2,sumU3,nn
write(*,*)"请输入数据个数nn=:"
read(*,*)nn
write(*,*)"请输入水的粘度Uw=:"
read(*,*)Uw
write(*,*)"请输入含盐量Csep=:"
read(*,*)Csep
write(*,*)"请输入Sp=:"
read(*,*)Sp
write(*,*)"请输入聚合物的浓度:"
read(*,*) (Cp(i),i=1,nn)
write(*,*)"请输入聚合物的粘度:"
read(*,*) (Up(i),i=1,nn)
sumC1=0;sumC2=0;sumC3=0;sumC4=0
sumU1=0;sumU2=0;sumU3=0
do i=1,nn
  sumC1=sumC1+Cp(i)
  sumC2=sumC2+Cp(i)**2
  sumC3=sumC3+Cp(i)**3
  sumC4=sumC4+Cp(i)**4
  sumU1=sumU1+(Up(i)/Uw-1)/(Csep**Sp*Cp(i))
  sumU2=sumU2+(Up(i)/Uw-1)/(Csep**Sp)
  sumU3=sumU3+(Up(i)/Uw-1)/(Csep**Sp)*Cp(i)     
end do
A(N,N)=(/nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/)
S(N)=(/sumU1,sumU2,sumU3/)
call Gauss_Jordan(A,S,ANS)
write(*,*)'ANS:'
do i=1,N
  write(*,"(1x,a1,'=',F8.4)")char(64+i),ANS(i)
end do
stop
end program

新手上路,请您多多指点哦!这样改过来又出现新问题了呢,我把nn都变成实型变量了啊,还这样提示呢,越来越懵了呢

Error: The shapes of the array expressions do not conform.   [A]
A(N,N)=(/nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/)
^
Error: The shapes of the array expressions do not conform.   [S]
S(N)=(/sumU1,sumU2,sumU3/)

7 楼


怎么给数组赋值不知道吗?
data A /nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/ 
试试。

8 楼

module LinearAlgebra
implicit none
contains
!Gauss_Jordan消去法的子程序
subroutine Gauss_Jordan(A,S,ANS)
implicit none
real::A(:,:)
real::S(:)
real::ANS(:)
real,allocatable::B(:,:)
integer::i,N
N=size(A,1)
allocate(B(N,N))
B=A
ANS=S
call Upper(B,ANS,N)
call Lower(B,ANS,N)
forall(i=1:N)
  ANS(i)=ANS(i)/B(i,i)
end forall
return
end subroutine Gauss_Jordan
!求解上三角矩阵的子程序
subroutine Upper(M,S,N)
implicit none
integer::N
real::M(N,N)
real::S(N)
integer::I,J
real::E
do I=1,N-1
  do J=I+1,N
    E=M(J,I)/M(I,I)
    M(J,I:N)=M(J,I:N)-M(I,I:N)*E
    S(J)=S(J)-S(I)*E
  end do
end do
return
end subroutine Upper
!求解下三角矩阵的子程序
subroutine Lower(M,S,N)
implicit none
integer::N
real::M(N,N)
real::S(N)
integer::I,J
real::E
do I=N,2,-1
  do J=I-1,1,-1
    E=M(J,I)/M(I,I)
    M(J,1:N)=M(J,1:N)-M(I,1:N)*E
    S(J)=S(J)-S(I)*E
  end do
end do
return
end subroutine Lower
end module
program main
use LinearAlgebra
implicit none
integer,parameter::N=3
integer::i,j
real::A(N,N)
real::S(N)
real::ANS(N)
real::Cp(20),Up(20)
real::Uw,Csep,Sp,sumC1,sumC2,sumC3,sumC4,sumU1,sumU2,sumU3,nn
write(*,*)"请输入数据个数nn=:"
read(*,*)nn
write(*,*)"请输入水的粘度Uw=:"
read(*,*)Uw
write(*,*)"请输入含盐量Csep=:"
read(*,*)Csep
write(*,*)"请输入Sp=:"
read(*,*)Sp
write(*,*)"请输入聚合物的浓度:"
read(*,*) (Cp(i),i=1,nn)
write(*,*)"请输入聚合物的粘度:"
read(*,*) (Up(i),i=1,nn)
sumC1=0;sumC2=0;sumC3=0;sumC4=0
sumU1=0;sumU2=0;sumU3=0
do i=1,nn
  sumC1=sumC1+Cp(i)
  sumC2=sumC2+Cp(i)**2
  sumC3=sumC3+Cp(i)**3
  sumC4=sumC4+Cp(i)**4
  sumU1=sumU1+(Up(i)/Uw-1)/(Csep**Sp*Cp(i))
  sumU2=sumU2+(Up(i)/Uw-1)/(Csep**Sp)
  sumU3=sumU3+(Up(i)/Uw-1)/(Csep**Sp)*Cp(i)     
end do
data A /nn,sumC1,sumC2,sumC1,sumC2,sumC3,sumC2,sumC3,sumC4/
data S /sumU1,sumU2,sumU3/
call Gauss_Jordan(A,S,ANS)
write(*,*)'ANS:'
do i=1,N
  write(*,"(1x,a1,'=',F8.4)")char(64+i),ANS(i)
end do
stop
end program

Error: A symbol must be a defined parameter in this context.
这个是整个的程序,麻烦您帮我看看好吗,我把数组按照您说的改了,可是还是提示错误,12个错误都是有关A,B数组变量的。麻烦了啊

9 楼


实在不行,不会这样么?
A(1,1) = nn
A(2,1) = sumC1
A(3,1) = sumC2
A(1,2) = sumC1
A(2,2) = sumC2
A(3,2) = sumC3
A(1,3) = sumC2
A(2,3) = sumC3
A(3,3) = sumC4
S(1) = sumU1
S(2) = sumU2
S(3) = sumU3

10 楼

真是太感谢您啦!终于调通了,我都没想到这样赋值呢。

我来回复

您尚未登录,请登录后再回复。点此登录或注册