求个矩阵乘法的源码(C++的)

MrBone 2009-05-25 10:46:11
自己写不出来了,从文件读取数据的时候赋值搞不清楚……

要求:从两个TXT文件中各读取一个超级大的矩阵,将读取出的两矩阵相乘,将所得的第三个矩阵写到第三个TXT文件中,读取的矩阵和输出的矩阵中,每行的两个相邻数用逗号隔开,计算运算时间,并将时间写入第三个文件中。

另附已经写好的JAVA源码如下:

import java.util.*;
import java.io.*;

public class Matrix {
public static void main(String[] args) {

Date date = new Date();
long previousTime = date.getTime();
long lastTime = 0;

//设置1个参数,如果等于1,则计算并把结果写入文件,且计算时间,如果等于2,计算并计算时间,不写文件,但计算结果,在 屏幕输出 如果等于3,only计算,不在屏幕输出结果,只显示计算时间~
int choose = 0;

try {
choose = Integer.parseInt(args[0]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Please input a number to choose the way that you want to display!");
System.exit(-1);
} catch(Exception e) {
System.out.println("Please input a correct number!");
System.exit(-1);
}


//将结果输出到文件中
File f = new File("f://1.txt");
String strEncode = "UTF-8";

BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), strEncode));
} catch(IOException e) {
e.printStackTrace();
}

StringBuilder sb = new StringBuilder();

int Cut = 0;


int[][] b = ReadFile.read("F://3.txt");
int[][] c = ReadFile.read("F://4.txt");

int[][] a = null;

for(int i=0; i<b.length; i++) {
for(int j=0; j<b[i].length; j++) {
a = new int[b.length][b[i].length];
a[i][j] = b[i][j] * c[i][j];
Cut += 1;
sb.append(a[i][j] + " ");
}
sb.append("\r\n");
}

date = new Date();
lastTime = date.getTime();

long useTime = lastTime - previousTime;

if(choose == 1) {
try {
bw.write(sb.toString());
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch(IOException e) {
e.printStackTrace();
}
}

System.out.println("运行结果输出到F://1.txt文件中");
}

if(choose == 2) {
System.out.println("-共进行计算" + Cut + "次!");
System.out.println(sb.toString());
}

if(choose == 3) {
System.out.println("一共运行" + useTime + "毫秒");
}
}
}


class ReadFile {
public static int[][] read(String filename) {
int x = 0;
int y = 0;

Map m = new HashMap();

File f = new File(filename);

BufferedReader br = null;
String str = new String();

String[] s = null;

try {
br = new BufferedReader(new FileReader(f));
} catch(FileNotFoundException e) {
System.out.println("File not found!");
System.exit(-1);
}

try {
while((str = br.readLine()) != null) {
s = trim(str).split(",");
y = s.length;
x += 1;
m.put(x, s);
}
} catch(IOException e) {
e.printStackTrace();
}

int[][] array = new int[x][y];

for(int i=0; i<x; i++) {
for(int j=0; j<y; j++) {
String[] strs = (String[])m.get(i+1);
array[i][j] = Integer.parseInt(strs[j]);
}
}

return array;
}

public static String trim(String str) {
str = str.replaceAll(" ", "");
return str;
}
}
...全文
746 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
光宇广贞 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hairetz 的回复:]
http://download.csdn.net/source/724544


两个四维矩阵乘法C++
[/Quote]

四维矩阵是个什么东西……
zhan1094 2009-05-26
  • 打赏
  • 举报
回复
大学的线性代数就有这个啦~~~
MrBone 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lzh9955 的回复:]
有很多的!
[/Quote]

能帮忙找找么?麻烦啦~
lzh9955 2009-05-26
  • 打赏
  • 举报
回复
有很多的!
stephenxu111 2009-05-26
  • 打赏
  • 举报
回复
使用MATLAB?
pathuang68 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 MrBone 的回复:]
引用 6 楼 hikaliv 的回复:
引用 4 楼 hairetz 的回复:
http://download.csdn.net/source/724544
两个四维矩阵乘法C++

四维矩阵是个什么东西……

同问……
[/Quote]
同问......
MrBone 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hikaliv 的回复:]
引用 4 楼 hairetz 的回复:
http://download.csdn.net/source/724544


两个四维矩阵乘法C++


四维矩阵是个什么东西……
[/Quote]

同问……
winingsky 2009-05-26
  • 打赏
  • 举报
回复
up,lZ加油了,我也想做做!!
ofcourseqi 2009-05-26
  • 打赏
  • 举报
回复
文件读到程序中的是一个字符串buffer,只要对这个字符串buffer进行操作就行了啊,挑出你想要的数据,舍去不要的东西。
MrBone 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ofcourseqi 的回复:]
数据读取有什么问题?说说看...
[/Quote]

怎么把TXT文件中的矩阵正确赋值到二维数组中?TXT文件中的矩阵中行的数据是用逗号隔开的。
ofcourseqi 2009-05-26
  • 打赏
  • 举报
回复
数据读取有什么问题?说说看...
MrBone 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ofcourseqi 的回复:]
//===我去年写的,希望对你有用:

/*===================稀疏矩阵运算器========================//
*文件名:matrixOperation.cpp
*创建时间:2008.12.1
[/Quote]

十分感谢~~~~
比我需要的多了很多功能,但我比较棘手的数据读取问题木有解决……
ofcourseqi 2009-05-26
  • 打赏
  • 举报
回复
//===我去年写的,希望对你有用:

/*===================稀疏矩阵运算器========================//
*文件名:matrixOperation.cpp
*创建时间:2008.12.1
===========================================================*/

#include <iostream>
using namespace std;

//=======================================十字链表============================//
typedef double Data;
struct Node
{
Data data; //数据
int i; //行
int j; //列
Node * right; //指向向右连接的节点
Node * down; //指向向下连接的节点
};

Node* Create(int m,int n)
{
Node *lie=new Node[n+1]; //列的头节点
for(int j(0);j<n+1;j++)
{
(lie+j)->down=NULL;
(lie+j)->right=NULL;
}
Node *hang=new Node[m+1]; //行的头节点
for(int i(0);i<m+1;i++)
{
(hang+i)->down=NULL;
(hang+i)->right=NULL;
}
Node *head=new Node; //矩阵头指针
head->right=lie;
head->down=hang;
head->i=m;
head->j=n;

return head;
}
void InsertN(Node *head,int i,int j,Data data)
{
//---------创建新的节点--------//
Node *newNodeP=new Node;
newNodeP->data=data;
newNodeP->i=i;
newNodeP->j=j;
newNodeP->right=NULL;
newNodeP->down=NULL;
//---------插入操作------------//
//==行链的插入(末尾插入)
Node *insertP = head->down+i;
while (insertP->right != NULL) //到达行链的末端
insertP=insertP->right;
insertP->right=newNodeP;
//==列链的插入(末尾插入)
insertP=(head->right+j);
while(insertP->down != NULL) //到达列链的末端
insertP=insertP->down;
insertP->down=newNodeP;
}

void DeleteN(Node *head,int i,int j)
{
Node *deletePre; //指向要删除节点的前一节点
Node *deleteP; //指向要删除节点
//---------删除操作---------------//
//==将要删除的节点剔除行链
deleteP=(head->down+i);
while(deleteP->j != j)
{
deletePre=deleteP;
deleteP=deleteP->right;
}
deletePre->right=deleteP->right;
//将要删除的节点剔除列链
deleteP=(head->right+j);
while(deleteP->i !=i)
{
deletePre=deleteP;
deleteP=deleteP->down;
}
deletePre->down=deleteP->down;
//删除节点
delete deleteP;
}

Data ChangeData(Node *head,int i,int j,Data data)
{
Node *dataP=(head->down+i);
while(dataP->j != j) //找到节点
{
dataP=dataP->right;
if(dataP==NULL)
{
cout<<"节点未找到!"<<endl;
break;
}
}

dataP->data=data;
return dataP->data;
}
Data GetData(Node *head,int i,int j)
{
Node *dataP=(head->down+i);
while(dataP->j != j)
{
dataP=dataP->right;
if(dataP==NULL)
{
return 0;
}
}

return dataP->data;
}

void Destroy(Node *head)
{
for(int i(1);i<=head->i;i++)
{
while( (head->down+i)->right !=NULL )
DeleteN(head,i,(head->down+i)->right->j);
}
delete[] head->down;
delete[] head->right;
delete head;
}
//==============================================================================//

Node *CreateMatrix(int i,int j); //创建一个矩阵
Node *Add(Node *M1P,Node *M2P); //加法
Node *Sub(Node *M1P,Node *M2P); //减法
Node *Mul(Node *M1P,Node *M2P); //Multiplication 乘法
void PrintOut(Node *M); //打印输出矩阵
void Operation(); //一次运算
int main()
{
while(1)
{
Operation();
cout<<'\n'<<endl;
cout<<"是否还要进行其他的计算?(Y/N)\t";
char ch;
fflush(stdin);
cin>>ch;
if(ch != 'y' && ch != 'Y') break;
}
return 0;
}

Node* CreateMatrix(int i,int j)
{
Node *Matrix=Create(i,j);
cout<<"请输入矩阵的元素,输入顺序为 行,列,数据(用空格隔开),元素与元素之间用逗号隔开:"<<endl;
fflush(stdin); //清空输入流
cin.peek();
char ch;
do
{
int lie,hang;
double data;
ch=cin.peek();
while( (ch<'0' || ch>'9') && ch!='-')
{
cin.ignore();
ch=cin.peek();
}
cin>>hang;

ch=cin.peek();
while((ch<'0' || ch>'9') && ch!='-')
{
if(ch==',' || ch==10)
return 0;
cin.ignore();
ch=cin.peek();
}
cin>>lie;

ch=cin.peek();
while((ch<'0' || ch>'9') && ch!='-')
{
if(ch==',' || ch==10)
return 0;
cin.ignore();
ch=cin.peek();
}
cin>>data;

InsertN(Matrix,hang,lie,data);
ch=cin.peek();
}while(ch != 10);

return Matrix;
}

Node *Add(Node *M1P,Node *M2P)
{
if(M1P->i != M2P->i && M1P->j != M2P->j) //只有两个矩阵的行列相等才能进行加法
{
cout<<"两个矩阵行列数不同,不能相加!"<<endl;
return 0;
}
Node *sum=Create(M1P->i,M1P->j); //创建一个矩阵用于储存结果
double num1,num2; //分别表示从第一个矩阵和第二个矩阵取得的相应行列的数据
for(int i(1);i<=M1P->i;i++)
{
for(int j(1);j<=M2P->j;j++)
{
num1=GetData(M1P,i,j);
num2=GetData(M2P,i,j);
double temp=num1+num2;
if(temp!=0)
InsertN(sum,i,j,temp);
}
}

return sum;
}

Node *Sub(Node *M1P,Node *M2P)
{
if(M1P->i != M2P->i && M1P->j != M2P->j)
{
cout<<"两个矩阵行列数不同,不能相减!"<<endl;
return 0;
}
Node *sum=Create(M1P->i,M1P->j);
double num1,num2;
for(int i(1);i<=M1P->i;i++)
{
for(int j(1);j<=M2P->j;j++)
{
num1=GetData(M1P,i,j);
num2=GetData(M2P,i,j);
double temp=num1-num2;
if(temp!=0)
InsertN(sum,i,j,temp);
}
}

return sum;
}

Node *Mul(Node *M1P,Node *M2P)
{
/*只有两个第一个矩阵的行等于第二个矩阵的列,
第二个矩阵的列等于第一个矩阵的行才能进行乘法*/
if(M1P->i != M2P->j && M1P->j != M2P->i)
{
cout<<"两个矩阵行列数不能满足乘法!"<<endl;
return 0;
}
Node *sum=Create(M1P->i,M2P->j);
double num1,num2;
for(int i(1);i<=M1P->i;i++)
{
for(int j(1);j<=M2P->j;j++)
{
double s(0);//保存sum的第i行,第j列 的值
/*用M1P的第i行的转置乘以M2P的第j列
计算sum的第i行,第j列 的值*/
for(int n(1);n<=M2P->i;n++)
{
num1=GetData(M1P,i,n);
num2=GetData(M2P,n,j);
double temp=num1*num2;
s += temp;
}
if(s != 0)
InsertN(sum,i,j,s);
}
}

return sum;
}

void PrintOut(Node *M)
{
for(int i(1);i<=M->i;i++)
{
for(int j(1);j<=M->j;j++)
{
double temp=GetData(M,i,j);
cout<<temp<<'\t';
}
cout<<endl;
}
}

void Operation()
{
Node *M1,*M2; //创建两个矩阵
cout<<"请输入第一个矩阵的行数和列数:"<<endl;
int m,n; //分别表示行和列
fflush(stdin); //清空输入流
cin>>m>>n;
while(1)
{
M1=CreateMatrix(m,n);
if(M1!=0) break; //如果创建成功则退出循环
cout<<"格式错误,请重新输入:"<<endl;
}

cout<<"请输入第二个矩阵的行数和列数:"<<endl;
fflush(stdin);
cin>>m>>n;
while(1)
{
M2=CreateMatrix(m,n);
if(M2!=0) break;
cout<<"格式错误,请重新输入:"<<endl;
}

Node *result; //保存运算结果的矩阵
cout<<"请选择运算方法:1、相加 2、相减 3、相乘"<<endl;
fflush(stdin);
cin>>m;
switch(m)
{
case 1:
result=Add(M1,M2); //加法
break;
case 2:
result=Sub(M1,M2); //减法
break;
case 3:
result=Mul(M1,M2); //乘法
break;
}
cout<<"运算结果是:"<<endl;
PrintOut(M1);
cout<<endl;
switch(m)
{
case 1:
cout<<"+"<<endl;
break;
case 2:
cout<<"-"<<endl;
break;
case 3:
cout<<"*"<<endl;
break;
}
cout<<endl;
PrintOut(M2);
cout<<endl;
cout<<"||"<<endl;
cout<<endl;
PrintOut(result); //输出结果
//释放内存
Destroy(M1);
Destroy(M2);
Destroy(result);
}
  • 打赏
  • 举报
回复
http://download.csdn.net/source/724544


两个四维矩阵乘法C++
光宇广贞 2009-05-25
  • 打赏
  • 举报
回复
加油干。
pathuang68 2009-05-25
  • 打赏
  • 举报
回复
ltc_mouse 2009-05-25
  • 打赏
  • 举报
回复
自己写不出来了,从文件读取数据的时候赋值搞不清楚……
-------------------------
读数据不就是把距阵元素一个一个读出来吗? for循环就可以了啊... lz应该先试着自己做~

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧