C++多文件
我的三个程序文件,1.CPP,2.CPP,3.CPP可为什莫编译第二个时总出错:unexpected end of file found,程序如下://1.CPP
#include<iostream.h>
#include<math.h>
class matrix
{public:matrix(int dims=2);
~matrix();
void setmatrix(double*rmatr);
void printm();
protected:
int index;
double *matrixa;
};
class linequ:public matrix
{
public:
linequ(int dims);
~linequ();
void setlinequ(double*a,double*b);
void printl();
int solve();
void showx();
private:
double *sums;
double *solu;
};
//2.cpp
#include "1.CPP"
void matrix::setmatrix(double *rmatr)//设置矩阵
{
for(int i=0;i<index*index;i++)//矩阵成员赋初值
*(matrixa+i)=rmatr[i];
}
matrix::matrix(int dims)//矩阵matrix类的构造函数
{index=dims;
matrixa=new double[index*index];
}
matrix::~matrix()//矩阵matrix类的析构函数
{delete[]matrixa;}
void matrix::printm()//显示矩阵的元素
{cout<<"the matrix is:"<<endl;
for(int i=0;i<index;i++)
{for(int j=0;j<index;j++)
cout<<*(matrixa+i*index+j)<<" ";
cout<<endl;
}
}
linequ::linequ(int dims):matrix(dims)//派生类linequ的构造函数
{sums=new double[dims];
solu=new double[dims];
}
linequ::~linequ()//派生类linequ的析构函数
{delete[]sums;
delete[]solu;
}
void linequ::setlinequ(double*a,double*b)//设置线性方程组
{setmatrix(a); //调用基类函数
for(int i=0;i<index;i++)
sums[i]=b[i];
}
void linequ::printl()//显示线性方程组
{cout<<"the line equation is:"<<endl;
for(int i=0;i<index;i++)
{for( int j=0;j<index;j++)
cout<<*(matrixa+i*index+j)<<" ";
cout<<" "<<sums[i]<<endl;
}
}
void linequ::showx()//输出方程的解
{cout<< "the result is:"<<endl;
for(int i=0;i<index;i++)
{ cout<<"x["<<i<<"]="<<solu[i]<<endl;
}
}
int linequ::solve()//全选主元高斯消去法解方程
{
int*js,l,k,i,j,is,p,q;
double d,t;
js=new int[index];
l=1;
for(k=0;k<index;k++)//消去过程
{d=0.0;
for(i=k;i<=index-1;i++)
for(j=0;j<=index-1;j++)
{t=fabs(matrixa[i*index+j]);
if(t>d)
{d=t;js[k]=j;is=i;}
}
if (d+l==0)
l=0;
else
{if(js[k]!=k)
for(i=0;i<=index;i++)
{p=i*index+k;q=i*index+js[k];
t=matrixa[p];matrixa[p]=matrixa[q];matrixa[q]=t;
}
if (is!=k)
{for(j=k;j<=index-1;j++)
{p=k*index+j; q=is*index+j;
t=matrixa[p];matrixa[q]=matrixa[q];matrixa[q]=t;
}
t=sums[k];sums[k]=sums[is];sums[is]=t;
}
}
if(l==0)
{delete[]js;cout<<"fail"<<endl;
return(0);}
d=matrixa[k*index+k];
for(j=k+1;j<=index-1;j++)
{p=k*index+j;matrixa[p]=matrixa[p]/d;
sums[k]=sums[k]/d;
for(i=k+1;i<=index-1;i++)
{for(j=k+1;j<=index-1;j++)
{p=i*index+j;
matrixa[p]=matrixa[p]-matrixa[i*index+k]*matrixa[k*index+j];}
sums[i]=sums[i]-matrixa[i*index+k]*sums[k];
}
}
d=matrixa[(index-1)*index+index-1];
if (fabs(d)+1.0==1.0)
{delete[]js;cout<<"fail"<<endl;
return(0);}
solu[index-1]=sums[index-1]/d;//回代过程
for(i=index-2;i>=0;i--)
{t=0.0;
for(j=i+1;j<=index-1;j++)
t=t+matrixa[i*index+j]*solu[j];
solu[i]=solu[i]-t;}
js[index-1]=index-1;
for(k=index-1;k>=0;k--)
if(js[k]!=k)
{t=solu[k];solu[k]=solu[js[k]];solu[js[k]]=t;}
delete[]js;
return(1);
};
//3.cpp
#include"1.CPP"
void main()
{double a[]={0.2368,0.2471,0.2568,1.2671,
0.1968,0.2071,1.2168,0.2271,
0.1581,1.1675,0.1768,0.1871,
1.1161,0.1254,0.1397,0.1490};
double b[4]={1.8471,1.7471,1.6471,1.5471};
linequ equl(4);
equl.setlinequ(a,b);
equl.printl();
if(equl.solve())
equl.showx();
else
cout<<"fail"<<endl;
};
请高手指教,谢谢,急!