freedom 发表于 2016-4-6 22:46:15

还是找不出错误,这是用秦九韶算法求多项式解简单程序

program mytp16
implicit none
integer n
double precision x,poly
double precision,allocatable::cf(:),npoly(:)
write(6,*)'input the nombre n'
read(5,*)n
write(6,*)'input the x'
read(5,*)x
allocate(cf(0:n),npoly(n))
cf=poly(n,npoly,cf,x)
npoly=poly(n,npoly,cf,x)
write(6,'("npoly(",i3,")=",d15.8)')n,npoly(n)
end program

double precision function poly(n,npoly,cf,x)
implicit none
integer i,n
double precision cf(0:n),npoly(0:n),x
open(15,file='the coefficient of npoly.txt')
do i=0,n
cf(i)=2.d0*i+1
write(15,*) cf(i)
end do
close(15)
open(15,file='the coefficient of npoly.txt')
do i=0,n
read(15,*) cf(i)
write(6,'("cf(",i3,")=",d15.8)')i,cf(i)
end do
close(15)
npoly(0)=cf(n)
npoly(1)=cf(n)*x+cf(n-1)
do i=2,n
npoly(i)=npoly(i-1)*x+cf(n-i)
end do
poly=npoly(n)
end function poly
!--------------------------------------------------
!Silverfrost FTN95 for Microsoft Visual Studio
!Free Format FTN95 Source File
!--------------------------------------------------


fcode 发表于 2016-4-7 08:32:13

1.请描述你遇到的问题
2.屏幕输入输出,请直接用 * ,而不要用 5 和 6!!!语法并没有规定 5 和 6 的具体含义。
3.你为什么要写入文件再读进来?

freedom 发表于 2016-4-8 01:55:50

我要用秦九韶算法计算多项式,然后在没有用函数时,运行的是正常的,但是把代码专程函数后总是不出结果,我的作业老师要求这样的,先读入再用

fcode 发表于 2016-4-8 09:12:59

我让你描述你的问题,而不是描述你的需求。我已经知道你要应用秦九昭算法。
我需要知道的是,你遇到的什么问题?是编译错误?运行错误?还是结果不对?如果遇到错误,请给出错误的现象描述或错误提示。这些都是求助的基本信息。

关于你的代码,文件读写为什么要放在函数里呢?多次调用就会多次写入文件,再读文件。你不觉得浪费吗?还有为什么 poly 要调用两次?
poly 已经是单变量的结果了,就不需要再用一个数组来接收了。

以下是我的修改版,简化了很多东西。

Program mytp16
implicit none
integer n , i
Real(Kind=8) :: x , y
Real(Kind=8) , allocatable::cf(:)
write(*,*)'input the nombre n'
read(*,*)n
write(*,*)'input the x'
read(*,*)x
allocate( cf(0:n) )
open(15,file='the coefficient of npoly.txt')
Do i=0,n
    cf(i) = 2.d0 * i + 1.d0
    write(15,*) cf(i)
End Do
close(15)   
y = poly ( cf , x )
write(*,'("y=",es13.6)') y

contains

Real(Kind=8) Function poly( cf , x ) result( y )
    integer i , n
    Real(Kind=8) :: cf(0:) , x
    n = size( cf ) - 1
    y = cf(n) * x + cf( n - 1 )
    Do i = 2 , n
      y = y * x + cf(n-i)
    End Do
End Function poly

End Program mytp16

freedom 发表于 2016-4-8 18:20:14

版主太好了,太感谢了!:'(
页: [1]
查看完整版本: 还是找不出错误,这是用秦九韶算法求多项式解简单程序