矩阵转置程序
来源:互联网 发布:技术支持 恒久网络 编辑:程序博客网 时间:2024/06/04 18:52
#include <iostream>
using namespace std;
#define RANK 3 // N 阶矩阵
#define DEBUG //调试
////////////////////////////////////////////////////////////////////////
//判断数组内是否全为数字型
bool IsNum(char* num)
{
int flag = 0;
unsigned long Len;
Len = strlen(num);
for (int i = 0;i < Len;i ++)
{
if (num[i] < '0' || num[i] > '9') //非数字
{
if (num[i] == '.') //是小数点
{
if (flag) //多个小数点
{
return false;
}
flag = 1;
}
else //非小数点
{
return false;
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
int main(void)
{
int i,j;
double *p; //指向一个一维数组
p = new double[RANK * RANK]; //动态分配
#ifdef DEBUG
//测试用(自动填充)
for (int k=0; k<RANK * RANK; k++)
*(p+k) = 1+k;
#else
//用户输入
char Input[50];
cout<<"请输入"<<RANK<<"阶矩阵的元素:"<<endl<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
error:
cout<<"请输入第"<<i+1<<"行,第"<<j+1<<"个元素为:";
cin>>Input;
if (strlen(Input) >= 50) //输入过长
{
cout<<"你输入的值过长,请重新输入!"<<endl;
goto error;
}
else if (! IsNum(Input)) //检测合法性
{
//不合法
cout<<"你输入的值不合法,请重新输入!"<<endl;
goto error;
}
else
{
*(p + i * RANK + j) = atof( Input );
}
}
}
#endif
//显示用户输入
cout<<"用户输入为"<<RANK<<"阶矩阵:"<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout<<*(p + i * RANK + j)<<"/t";
}
cout<<endl<<endl;
}
//开始转置
for (i = 0; i < RANK; i ++)
{
for (j = i; j < RANK; j ++)
{
if ( i != j ) //不在对角线上
{
//两数交换
*(p + i * RANK + j) += *(p + j * RANK + i); //a=a+b
*(p + j * RANK + i) = *(p + i * RANK + j) - *(p + j * RANK + i); //b=a-b
*(p + i * RANK + j) -= *(p + j * RANK + i); //a=a-b
//异或好像不能对double型进行操作,所以,用加减来代替
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
// *(p + j * RANK + i) ^= *(p + i * RANK + j); //b=a^b
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
}
}
}
//显示转置结果
cout<<"转置后的结果为:"<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout<<*(p + i * RANK + j)<<"/t";
}
cout<<endl<<endl;
}
return 1;
}
using namespace std;
#define RANK 3 // N 阶矩阵
#define DEBUG //调试
////////////////////////////////////////////////////////////////////////
//判断数组内是否全为数字型
bool IsNum(char* num)
{
int flag = 0;
unsigned long Len;
Len = strlen(num);
for (int i = 0;i < Len;i ++)
{
if (num[i] < '0' || num[i] > '9') //非数字
{
if (num[i] == '.') //是小数点
{
if (flag) //多个小数点
{
return false;
}
flag = 1;
}
else //非小数点
{
return false;
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////
int main(void)
{
int i,j;
double *p; //指向一个一维数组
p = new double[RANK * RANK]; //动态分配
#ifdef DEBUG
//测试用(自动填充)
for (int k=0; k<RANK * RANK; k++)
*(p+k) = 1+k;
#else
//用户输入
char Input[50];
cout<<"请输入"<<RANK<<"阶矩阵的元素:"<<endl<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
error:
cout<<"请输入第"<<i+1<<"行,第"<<j+1<<"个元素为:";
cin>>Input;
if (strlen(Input) >= 50) //输入过长
{
cout<<"你输入的值过长,请重新输入!"<<endl;
goto error;
}
else if (! IsNum(Input)) //检测合法性
{
//不合法
cout<<"你输入的值不合法,请重新输入!"<<endl;
goto error;
}
else
{
*(p + i * RANK + j) = atof( Input );
}
}
}
#endif
//显示用户输入
cout<<"用户输入为"<<RANK<<"阶矩阵:"<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout<<*(p + i * RANK + j)<<"/t";
}
cout<<endl<<endl;
}
//开始转置
for (i = 0; i < RANK; i ++)
{
for (j = i; j < RANK; j ++)
{
if ( i != j ) //不在对角线上
{
//两数交换
*(p + i * RANK + j) += *(p + j * RANK + i); //a=a+b
*(p + j * RANK + i) = *(p + i * RANK + j) - *(p + j * RANK + i); //b=a-b
*(p + i * RANK + j) -= *(p + j * RANK + i); //a=a-b
//异或好像不能对double型进行操作,所以,用加减来代替
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
// *(p + j * RANK + i) ^= *(p + i * RANK + j); //b=a^b
// *(p + i * RANK + j) ^= *(p + j * RANK + i); //a=a^b
}
}
}
//显示转置结果
cout<<"转置后的结果为:"<<endl;
for (i = 0; i < RANK; i ++)
{
for (j = 0; j < RANK; j ++)
{
cout<<*(p + i * RANK + j)<<"/t";
}
cout<<endl<<endl;
}
return 1;
}
- 矩阵转置程序
- 设有3*3矩阵,编写程序求解矩阵B=A+A(T),矩阵B为矩阵A及其转置矩阵A(T)之和
- CUDA之矩阵转置程序优化实例
- 编写C语言程序实现矩阵的转置
- 复习矩阵,程序中的矩阵
- 转置矩阵,逆矩阵和倒转置矩阵
- 求矩阵的转置矩阵和两个矩阵相乘
- 2.3 单位矩阵和转置矩阵
- 矩阵转置与矩阵相乘
- C++矩阵操作+,-,*,=,矩阵转置
- 矩阵乘法的程序
- 矩阵键盘扫描程序
- 矩阵键盘扫描程序
- MTL 矩阵转置
- 矩阵转置
- 矩阵转置(C)
- 稀疏矩阵转置
- 矩阵转置
- Jan Goyvaerts正则表达式(二)
- bootchart--检测linux启动性能的软件
- 钱
- 音视频编码技术与格式大全
- 软件过程与度量Software Process
- 矩阵转置程序
- ASP.NET验证码源代码
- 金山词霸2006常规窗口不见了!只能看到最大化的的原因
- 视频格式与质量
- C#实现UDP打洞
- How to use plugin audio codecs in OpenH323 and OPAL
- 博客的原理
- PWLib入门
- TrueCrypt--自由的软件加密工具