VC++6.0下运行的。
#include<iostream>
#include<math.h>
using namespace std;
const double m=1e-5;
const int Max=11; //定义最大阶
const double RI[10]={};
class A
{
private:
unsigned x,y,n;
double B[Max][Max];
double v[Max],w[Max],u[Max],f[Max],h[Max];
public:
void creat();
void strum();
void sum();
void open();
};
void A::creat()
{
cout<<"请输入矩阵的阶数: ";
cin>>n;
x=n;
y=n;
for(unsigned i=0;i<x;i++)
{
cout<<"输入第"<<i+1<<"行 ";
for(unsigned j=0;j<y;j++)
cin>>B[i][j];
}
}
void A::strum()
{
double s,e=0,t=0,b,r;
unsigned i,j,c;
for(c=0;c<n-1;c++)
u[c]=0;
u[n-1]=1;
do{
b=0;
r=0;
for(i=0;i<=n-1;i++)
{
v[i]=0;
w[i]=u[i];
for(j=0;j<=n-1;j++)
v[i]+=B[i][j]*u[j];
if(fabs(v[i])>b)
b=fabs(v[i]);
}
for(i=0;i<=n-1;i++)
{
u[i]=v[i]/b;
if(fabs(u[i]-w[i])>r)
r=fabs(u[i]-w[i]);
}
}
while(r>=m);
cout<<"主特征根 T="<<b<<"\n";
for(i=0;i<=x-1;i++)
{ cout<<"特征向量"<<i+1<<"为: ";
cout<<u[i]<<" ";
cout<<"\n";}
s=(b-n)/(n-1);
cout<<"\n"<<"CI="<<s<<"\n";
cout<<"CR="<<s/RI[n-1]<<"\n";
for(i=0;i<=n-1;i++)
{for(j=0;j<=n-1;j++)
t+=B[i][j] ;}
for(i=0;i<=n-1;i++)
{ h[i]=0;
f[i]=0;
for(j=0;j<=n-1;j++)
h[i]+=B[i][j];
f[i]=h[i]/t;
e+=f[i];}
for(i=0;i<=n-1;i++)
cout<<"\n"<<"权重向量"<<i+1<<"为: "<<f[i]/e;
cout<<"\n";
}
void A::sum()
{ unsigned i,j;
cout<<"\n请输入所要乘的一维数组: ";
for(i=0;i<y;i++)
cin>>v[i];
for(i=0;i<x;i++)
{
u[i]=0;
for(j=0;j<y;j++)
u[i]+=B[i][j]*v[j];
cout<<"\n总排序向量"<<i+1<<"为: "<<u[i];
}
cout<<"\n";
}
void A::open()
{
cout<<"\n输入矩阵的行数: ";
cin>>x;
cout<<"输入矩阵的列数: ";
cin>>y;
for(unsigned i=0;i<x;i++)
{
cout<<"输入第"<<i+1<<"行:";
for(unsigned j=0;j<y;j++)
cin>>B[i][j];
}
}
int main()
{
int k;
A a1;
cout<<"求主特征根等,请按1回车 ";
cout<<"\n求总排序向量,请按2回车 ";
cin>>k;
if(k==1)
{a1.creat();
a1.strum();}
else if(k==2)
{a1.open();
a1.sum();}
else
cout<<"\n\n\n\n抱歉,您的输入有误!\n";
return 0;
}
追问辛苦你了朋友,看来这个矩阵不对,那我的改改!!谢谢啦