24小时热门版块排行榜    

查看: 3566  |  回复: 23
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

zyj8119

木虫 (著名写手)


[交流] 【求助】程序运行已经无错误,但是输入文件有问题,求教。

CODE:
#include
#include
#include
#include
using namespace std;
class splin1
{
private:
        int n,m;
        double *x,*y,*dy,*ddy,*t,*z,*dz,*ddz,integ;
        double *s;
public:
        splin1 (int nn,int mm)
        {
                n=nn;m=mm;
                x=new double[n];
                y=new double[n];
                dy=new double[n];
                ddy=new double[n];
                s=new double[n];
                t=new double[n];
                z=new double[m];
                dz=new double[m];
                ddz=new double[m];
        }
        void input();
        void interp();
        void output();
        ~splin1()
        {delete[] x,y,dy,ddy,t,z,dz,ddz,s;}
};

void splin1::input()

{
        int k;
        char str1[20];
        cout<<"\n输入文件名:";
        cin>>str1;
        ifstream fin(str1);
        if(!fin)
        {cout<<"\n不能打开这个文件"<         for(k=0;k         {fin>>x[k];fin>>y[k];}
        fin>>dy[0]>>dy[n-1];
        for(k=0;k>t[k];
        fin.close();
}

void splin1::interp()
{
        int i,j;
        double h0,h1,alpha,beta;
        s[0]=dy[0];dy[0]=0.0;
        h0=x[1]-x[0];
        for(j=1;j<=n-2;j++)
        {
                h1=x[j+1]-x[j];
                alpha=h0/(h0+h1);
                beta=(1.0-alpha)*(y[j]-y[j-1])/h0;
                beta=3.0*(beta+alpha*(y[j+1]-y[j])/h1);
                dy[j]=-alpha/(2.0+(1.0-alpha)*dy[j-1]);
                s[j]=(beta-(1.0-alpha)*s[j-1]);
                s[j]=s[j]/(2.0+(1.0-alpha)*dy[j-1]);
                h0=h1;
        }
        for(j=n-2;j>=0;j--)
                dy[j]=dy[j]*dy[j+1]+s[j];
        for(j=0;j<=n-2;j++)s[j]=x[j+1]-x[j];
        for(j=0;j<=n-2;j++)
        {
                h1=s[j]*s[j];
                ddy[j]=6.0*(y[j+1]-y[j])/h1-2.0*(2.0*dy[j]+dy[j+1])/s[j];
        }
        h1=s[n-2]*s[n-2];
        ddy[n-1]=6.*(y[n-2]-y[n-1])/h1+2.*(2.*dy[n-1]+dy[n-1])/s[n-2];
        integ=0.0;
        for(i=0;i<=n-2;i++)
        {
                h1=0.5*s[i]*(y[i]+y[i+1]);
                h1=h1-s[i]*s[i]*s[i]*(ddy[i]+ddy[i+1])/24.0;
                integ=integ+h1;
        }
        for(j=0;j<=m-1;j++)
        {
                if(t[j]>=x[n-1])i=n-2;
                else
                {
                        i=0;
                        while(t[j]>x[j+1])i=i+1;
                }
                h1=(x[j+1]-t[j])/s[i];
                h0=h1*h1;
                z[j]=(3.0*h0-2.0*h0*h1)*y[i];
                z[j]=z[j]+s[i]*(h0-h0*h1)*dy[i];
                dz[j]=6.0*(h0-h1)*y[i]/s[i];
                dz[j]=dz[j]+(3.0*h0-2.0*h1)*dy[i];
                ddz[j]=(6.0-12.0*h1)*y[i]/(s[i]*s[i]);
                ddz[j]=ddz[j]+(2.0-6.0*h1)*dy[i]/s[i];
                h1=(t[j]-x[i])/s[i];
                h0=h1*h1;
                z[j]=z[j]+(3.0*h0-2.0*h0*h1)*y[i+1];
                z[j]=z[j]-s[i]*(h0-h0*h1)*dy[i+1];
                dz[j]=dz[j]-6.0*(h0-h1)*y[i+1]/s[i];
                dz[j]=dz[j]+(3.0*h0-2.0*h1)*dy[i+1];
                ddz[j]=ddz[j]+(6.0-12.0*h1)*y[i+1]/(s[i]*s[i]);
                ddz[j]=ddz[j]-(2.0-6.0*h1)*dy[i+1]/s[i];
        }
}

void splin1::output()
{
        int i;
        char str2[20];
        cout<<"\n输出文件名:";
        cin>>str2;
        ofstream fout(str2);
        if(!fout)
        {cout<<"\n不能打开这个文件"<         cout<             <         for(i=0;i         {
                fout<                     <                 cout<                         <         }
        fout<         cout<         <         for(i=0;i         {
                fout<                 <                 cout<                 <         }
        fout<         fout<         fout.close();
}

int main()
{
        splin1 solution(12,8);
        solution.input();
        solution.interp();
        solution.output();
}
               

       

输入文件为:
0.52  5.28794
8.0   13.8400
17.95 20.2000
28.65 24.9000
50.65 31.1000
104.6 36.5000
156.6 36.6000
260.7 31.0000
364.4 20.9000
468.0 7.80000
507.0 1.50000
520.0 0.200000

1.86548 -0.046115

4.0  14.0  30.0  60.0  130.0  230.0 450.0  515.0
结果出现这个问题:[Session started at 2011-04-06 17:46:04 +0800.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
Loading program into debugger…
Program loaded.
run
[Switching to process 1601]
Running…

输入文件名:splin1.txt

不能打开这个文件splin1.txt

Debugger stopped.
Program exited with status value:1.(gdb)
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zyj8119

木虫 (著名写手)


引用回帖:
Originally posted by smart0193 at 2011-04-07 16:01:45:
我这里很正常啊。你单独跟踪一下,看看那里出问题了。附图:

是不是得耗时很长?
6楼2011-04-07 16:58:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 24 个回答

zyj8119

木虫 (著名写手)


引用回帖:
Originally posted by zyj8119 at 2011-04-06 20:39:34:
[code]#include<iostream>
#include<iomanip>
#include<fstream>
#include<cmath>
using namespace std;
class splin1
{
private:
        int n,m;
        double *x,*y,*dy,*ddy,*t,*z,*dz ...

我已经把程序与数据文件放在一个文件夹下面。。。
2楼2011-04-06 20:44:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

smart0193

木虫 (小有名气)


★ ★ ★
zyj8119(金币+1):谢谢参与
微尘、梦想(金币+2): 谢谢参与应助…… 2011-04-07 17:26:38
我用VC6运行了你的程序,采用的也是你给出的例子文件,没有问题,可以输出结果。如下:
            0.52         5.28794   -6.27744e+066    2.84681e+066
               8           13.84    1.90782e+066   -6.58235e+065
           17.95            20.2   -5.40918e+065    1.66027e+065
           28.65            24.9    1.93591e+065   -2.87356e+064
           50.65            31.1   -7.10913e+064     4.6735e+063
           104.6            36.5    1.61149e+064   -1.44065e+063
           156.6            36.6    5.22698e+063    1.02188e+063
           260.7              31   -6.36429e+064   -2.34503e+063
           364.4            20.9    2.48875e+065    8.37238e+063
             468             7.8    -9.3144e+065   -3.11584e+064
             507             1.5    2.47047e+066    2.05615e+065
             520             0.2   -6.27744e+066   -2.89728e+066

   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132
   -6.27744e+066    1.93191e+265   -9.23265e+198    2.94154e+132

6.20039e+068

你的问题是不是由于你的输入文件还在打开着?在执行程序的时候,要关闭它
3楼2011-04-07 09:37:51
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zyj8119

木虫 (著名写手)


引用回帖:
Originally posted by smart0193 at 2011-04-07 09:37:51:
我用VC6运行了你的程序,采用的也是你给出的例子文件,没有问题,可以输出结果。如下:
            0.52         5.28794   -6.27744e+066    2.84681e+066
               8           13.84    1.90782e+066 ...

我原来用的是 xcode,现在用VC6.0编译了,但是还是有问题,运行到这一步就停止了:
4楼2011-04-07 13:37:23
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 龙 兔 虎 猫 高级回复 (可上传附件)
信息提示
请填处理意见