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

MrBone MrBone 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
自己写不出来了,从文件读取数据的时候赋值搞不清楚……

要求:从两个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;
}
}
...全文

相关推荐

VC++ 矩阵运算源码

多年前用VC6.0写的矩阵运算的源码,具备矩阵的加法、减法、求逆、倒置等,好久没看源码,也没编程了,具体的功能只能想起个大概

fx510

基于C++语言的矩阵库设计源码

该项目是基于C++语言开发的矩阵库,源码共包含24个文件,涵盖9个头文件、3个JSON配置文件、2个JPG图片文件、1个Git忽略文件、1个LICENSE文件、1个README文件、1个.cpp源文件以及1个matrix库文件。该库提供了矩阵的基本运算功能,适用于需要进行矩阵运算的软件开发项目。

wlm2024r

Win+VScode安装Eigen库[源码]

本文详细介绍了在Windows系统下使用VScode安装Eigen库的简洁方法。Eigen是一个用于线性运算的C++模板库,支持矩阵和矢量运算。文章首先介绍了Eigen库的基本信息,并指出该方法适用于将Eigen库放在项目文件之外的情况。接着,文章分步骤详细说明了安装过程,包括前置环境准备、Eigen库下载、VScode配置、测试代码编写以及运行效果展示。特别提醒了在配置tasks.json和c_cpp_properties.json文件时需要注意的细节,避免常见的错误。最后,通过测试代码验证了安装的正确性,并展示了运行效果。

sss66

复数矩阵QR分解求逆矩阵

一个vc开发的复数矩阵用qr分解方法求解逆矩阵的V刹动态库源码,以在vc6平台调试通过,原始矩阵是4维的,可扩展

sxhheb

lsqlin_C++_lsqlin_最小二乘法.zip

lsqlin_C++_lsqlin_最小二乘法.zip

mYlEaVeiSmVp
评论 17
收藏 已收藏
分享
返回顶部
MrBone
C++ 语言 65,214 成员 250,514 内容