原代码如下:
#include<stdio.h>
#include<iostream.h>
const double density=7800.0;
const double To=20.0;
const double Te=1300.0;
const double h=250.0;
const double dx=0.005;
const double dt=10;
const int N=27;
double t[]={20.0,100.0,200.0,300.0,400.0,500.0,600.0,700.0,800.0,900.0};
double GetCp(double T)
{double Cp;
double c[]={485.0,502.0,518.0,535.0,560.0,594.0,600.0,629.0,686.0,698.0};
for(int i=0;i<10;i++)
{ if(T>0.0&&T<=t[0]) return Cp=c[0]-(t[0]-T)/t[0]*c[0];
else if(T>t[i]&&T<=t[i+1])
return Cp=c[i]+(T-t[i])*(c[i+1]-c[i])/(t[i+1]-t[i]);
}
}
double GetK(double T)
{double K;
double k[]={60.0,55.0,51.0,46.0,41.0,37.0,34.0,30.0,28.0,27.0};
for(int i=0;i<10;i++)
{ if(T>0.0&&T<=t[0]) return K=k[0];
else if(T>t[i]&&T<=t[i+1])
return K=k[i]-(T-t[i])*(k[i]-k[i+1])/(t[i+1]-t[i]);
}
}
void main()
{ int i;
double t0[N]={To},a1=0,b1=0,c1=0;
double r[N]={0.0},y[N]={0.0},x[N]={0.0};
double other1[N]={To},d[N]={To};
for( i=1;;i++)
{
double T1=t0[i];
double K=GetK(T1),Cp=GetCp(T1);
a1=-dt*K/(density*Cp*dx*dx);
b1=1+2*dt*K/(density*Cp*dx*dx);
c1=-dt*K/(density*Cp*dx*dx);
double a[N]={a1}; a[0]=a[1]=0.0;a[N-1]=2*a1;
double b[N]={b1}; b[0]=0.0;b[1]=1-a1*dx*h/K-a1;
double c[N]={c1}; c[0]=0.0;c[N-1]=0.0;
double other2[N]={0.0};other2[1]=dt*h*Te/(density*dx*Cp);
for(int i=0;i<N;i++)
d[i]=other1[i]+other2[i];
int k;
r[1]=c[1]/b[1];y[1]=d[1]/b[1];
for( k=2;k<N-1;k++)
{
r[k]=c[k]/(b[k]-r[k-1]*a[k]);
y[k]=(d[k]-y[k-1]*a[k])/(b[k]-r[k-1]*a[k]);
}
y[N-1]=(d[N-1]-y[N-2]*a[N-1])/(b[N-1]-r[N-2]*a[N-1]);
x[N-1]=y[N-1];
t0[N-1]=x[N-1];
for(k=N-2;k>=1;k--)
{x[k]=y[k]-r[k]*x[k+1];other1[k]=x[k];t0[1]=x[1];}
if(t0[1]>=1200) goto exit;
}
exit:
cout<<"the temperature of the surface reaching the 1200 need "<<i<<"seconds"<<endl;
}
编译结果:
Compiling...
heattransfer.cpp
E:\我的文件\c++源文件\练习\heattransfer\heattransfer.cpp(19) : warning C4715: 'GetCp' : not all control paths return a value
E:\我的文件\c++源文件\练习\heattransfer\heattransfer.cpp(28) : warning C4715: 'GetK' : not all control paths return a value
Linking...
heattransfer.exe - 0 error(s), 0 warning(s)
而且总是出现这个对话框: