矩阵n次方运算
时间:2010-06-21 来源:ht2411
说明:矩阵在data.txt文件中,用空格分开;width 是矩阵的宽度,根据情况改变。
//版本2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define width 6
void cal(int ** arr1, int ** arr2, int ** ret);
void print(int ** ret);
void main()
{
ifstream fin("data.txt");
string s;
int i,j,k;
//申请资源
int ***ret = new int**[width];
for(i=0;i<width;i++)
{
ret[i] = new int*[width];
for(j=0;j<width;j++)
ret[i][j] = new int[width];
}
i = 0;
j = 0;
while(fin>>s)
{
ret[0][i][j] = atoi(s.c_str()); //读取文件数据装入数组
j++;
if(j == width)
{
i++;
j = 0;
}
}
//计算两个矩阵的行列式
for(i=0;i<width-1;i++)
cal(ret[0],ret[i],ret[i+1]);
//计算距离矩阵(从i到j需要的最短跳数)
int **dis = new int*[width];
for(i=0;i<width;i++)
dis[i] = new int[width];
for(i=0;i<width;i++)
{
for(j=0;j<width;j++)
{
if(i==j)
{
dis[i][j] = 0;
}
else
{
for(k=0;k<width;k++)
{
if(ret[k][i][j]!=0)
{
dis[i][j] = k+1;
break;
}
}
if(k==width)
dis[i][j] = -1;
}
}
}
//打印各阶矩阵值
for(i=0;i<width;i++)
{
cout<<i+1<<endl;
print(ret[i]);
cout<<endl;
}
//打印距离矩阵
cout<<"Distance:"<<endl;
print(dis);
cout<<endl;
//释放资源
for(i=0;i<width;i++)
{
for(j=0;j<width;j++)
delete [] ret[i][j];
delete [] ret[i];
}
delete [] ret;
delete []dis;
}
//计算两个矩阵的行列式
void cal(int ** arr1, int ** arr2, int ** ret)
{
int temp;
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
{
temp = 0;
for(int k = 0;k<width;k++)
temp +=arr1[i][k]*arr2[k][j];
ret[i][j] = temp;
}
}
//打印矩阵的值
void print(int ** ret)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
{
cout<<ret[i][j]<<" ";
}
cout<<endl;
}
}
//--------------------------------------
//版本1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define width 6
void cal(int ** arr1, int ** arr2, int ** ret);
void print(int ** ret);
void main()
{
#if 0
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while(fin.getline(str,LINE_LENGTH))
{
cout<<"read from file:"<<str<<endl;
}
#endif
#if 0
ifstream fin("data.txt");
string s;
while(getline(fin,s))
{
cout<<"read from file:" <<s<<endl;
}
#endif
#if 1
ifstream fin("data.txt");
string s;
int i,j;
int **arr = new int*[width];
for(i=0;i<width;i++)
arr[i] = new int[width];
i = 0;
j = 0;
while(fin>>s)
{
// cout<<s;
arr[i][j] = atoi(s.c_str()); //读取文件数据装入数组
j++;
if(j == width)
{
i++;
j = 0;
}
}
int **ret2 = new int*[width];
int **ret3 = new int*[width];
int **ret4 = new int*[width];
int **ret5 = new int*[width];
int **ret6 = new int*[width];
for(i=0;i<width;i++)
{
ret2[i] = new int[width];
ret3[i] = new int[width];
ret4[i] = new int[width];
ret5[i] = new int[width];
ret6[i] = new int[width];
}
cal(arr,arr,ret2);
cal(arr,ret2,ret3);
cal(ret2,ret2,ret4);
cal(ret2,ret3,ret5);
cal(ret3,ret3,ret6);
cout<<"1:"<<endl;
print(arr);
cout<<"\n2:"<<endl;
print(ret2);
cout<<"\n3:"<<endl;
print(ret3);
cout<<"\n4:"<<endl;
print(ret4);
cout<<"\n5:"<<endl;
print(ret5);
cout<<"\n6:"<<endl;
print(ret6);
for(i = 0;i<width;i++)
{
delete [] arr[i];
delete [] ret2[i];
delete [] ret3[i];
delete [] ret4[i];
delete [] ret5[i];
delete [] ret6[i];
}
delete []arr;
delete []ret2;
delete []ret3;
delete []ret4;
delete []ret5;
delete []ret6;
#endif
}
//----------
void cal(int ** arr1, int ** arr2, int ** ret)
{
int temp;
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
{
temp = 0;
for(int k = 0;k<width;k++)
temp +=arr1[i][k]*arr2[k][j];
ret[i][j] = temp;
}
}
void print(int ** ret)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
{
cout<<ret[i][j]<<" ";
}
cout<<endl;
}
}
//版本2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define width 6
void cal(int ** arr1, int ** arr2, int ** ret);
void print(int ** ret);
void main()
{
ifstream fin("data.txt");
string s;
int i,j,k;
//申请资源
int ***ret = new int**[width];
for(i=0;i<width;i++)
{
ret[i] = new int*[width];
for(j=0;j<width;j++)
ret[i][j] = new int[width];
}
i = 0;
j = 0;
while(fin>>s)
{
ret[0][i][j] = atoi(s.c_str()); //读取文件数据装入数组
j++;
if(j == width)
{
i++;
j = 0;
}
}
//计算两个矩阵的行列式
for(i=0;i<width-1;i++)
cal(ret[0],ret[i],ret[i+1]);
//计算距离矩阵(从i到j需要的最短跳数)
int **dis = new int*[width];
for(i=0;i<width;i++)
dis[i] = new int[width];
for(i=0;i<width;i++)
{
for(j=0;j<width;j++)
{
if(i==j)
{
dis[i][j] = 0;
}
else
{
for(k=0;k<width;k++)
{
if(ret[k][i][j]!=0)
{
dis[i][j] = k+1;
break;
}
}
if(k==width)
dis[i][j] = -1;
}
}
}
//打印各阶矩阵值
for(i=0;i<width;i++)
{
cout<<i+1<<endl;
print(ret[i]);
cout<<endl;
}
//打印距离矩阵
cout<<"Distance:"<<endl;
print(dis);
cout<<endl;
//释放资源
for(i=0;i<width;i++)
{
for(j=0;j<width;j++)
delete [] ret[i][j];
delete [] ret[i];
}
delete [] ret;
delete []dis;
}
//计算两个矩阵的行列式
void cal(int ** arr1, int ** arr2, int ** ret)
{
int temp;
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
{
temp = 0;
for(int k = 0;k<width;k++)
temp +=arr1[i][k]*arr2[k][j];
ret[i][j] = temp;
}
}
//打印矩阵的值
void print(int ** ret)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
{
cout<<ret[i][j]<<" ";
}
cout<<endl;
}
}
//--------------------------------------
//版本1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define width 6
void cal(int ** arr1, int ** arr2, int ** ret);
void print(int ** ret);
void main()
{
#if 0
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while(fin.getline(str,LINE_LENGTH))
{
cout<<"read from file:"<<str<<endl;
}
#endif
#if 0
ifstream fin("data.txt");
string s;
while(getline(fin,s))
{
cout<<"read from file:" <<s<<endl;
}
#endif
#if 1
ifstream fin("data.txt");
string s;
int i,j;
int **arr = new int*[width];
for(i=0;i<width;i++)
arr[i] = new int[width];
i = 0;
j = 0;
while(fin>>s)
{
// cout<<s;
arr[i][j] = atoi(s.c_str()); //读取文件数据装入数组
j++;
if(j == width)
{
i++;
j = 0;
}
}
int **ret2 = new int*[width];
int **ret3 = new int*[width];
int **ret4 = new int*[width];
int **ret5 = new int*[width];
int **ret6 = new int*[width];
for(i=0;i<width;i++)
{
ret2[i] = new int[width];
ret3[i] = new int[width];
ret4[i] = new int[width];
ret5[i] = new int[width];
ret6[i] = new int[width];
}
cal(arr,arr,ret2);
cal(arr,ret2,ret3);
cal(ret2,ret2,ret4);
cal(ret2,ret3,ret5);
cal(ret3,ret3,ret6);
cout<<"1:"<<endl;
print(arr);
cout<<"\n2:"<<endl;
print(ret2);
cout<<"\n3:"<<endl;
print(ret3);
cout<<"\n4:"<<endl;
print(ret4);
cout<<"\n5:"<<endl;
print(ret5);
cout<<"\n6:"<<endl;
print(ret6);
for(i = 0;i<width;i++)
{
delete [] arr[i];
delete [] ret2[i];
delete [] ret3[i];
delete [] ret4[i];
delete [] ret5[i];
delete [] ret6[i];
}
delete []arr;
delete []ret2;
delete []ret3;
delete []ret4;
delete []ret5;
delete []ret6;
#endif
}
//----------
void cal(int ** arr1, int ** arr2, int ** ret)
{
int temp;
for(int i=0;i<width;i++)
for(int j=0;j<width;j++)
{
temp = 0;
for(int k = 0;k<width;k++)
temp +=arr1[i][k]*arr2[k][j];
ret[i][j] = temp;
}
}
void print(int ** ret)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<width;j++)
{
cout<<ret[i][j]<<" ";
}
cout<<endl;
}
}
相关阅读 更多 +