从文件中读取数据并在C 中执行矩阵乘法
Reading data from file and performing matrix multiplication in c++
我是C 的新手,并且正在尝试执行从文件中获取数据的矩阵乘法。但是我无法获得乘法部分。有人请帮助我解决这个问题。
在尝试进行2个矩阵的乘法时,但无法获得输出。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string line;
int d[3][3],e[3][3],f[3][3];
int i=0;
int x=0;
int j=0;
string a[20];
string b[20];
string c[20];
ifstream myfile;
myfile.open("numeric.txt");
while(getline (myfile,line))
{
if(line!=" ")
{
a[x]=line;
b[x]=line;
cout<<a[x]<<endl;
x++;
}
}
cout<<"first Matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<=4;j++)
{
cout<<a[i][j]<<"";
}
cout<<endl;
}
cout<<"Second Matrix"<<endl;
for(i=1;i<4;i++)
{
for(j=0;j<6;j++)
{
cout<<b[i][j]<<"";
}
cout<<endl;
}
cout<<"Multiplication"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<=3;j++)
{
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
cout<<"Multiplication Result"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
这是您写入(固定大小)矩阵的完整示例,以及如何从该文件构建它们:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main() {
const size_t matrixSize = 3;
//
ofstream matrixOutput("matrix.txt");
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
matrixOutput << j*i << ' ';
}
matrixOutput << 'n';
}
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
matrixOutput << j*i * 2 << ' ';
}
matrixOutput << 'n';
}
matrixOutput.close();
//
ifstream matrixData("matrix.txt");
size_t matrixInput[matrixSize][matrixSize];
size_t matrixInput2[matrixSize][matrixSize];
size_t position = 0;
size_t number = 0;
while (matrixData >> number) {
const size_t matrixNumber = size_t(floor(position / (matrixSize*matrixSize)));
const size_t row = size_t(floor(position / matrixSize)) % matrixSize;
switch (matrixNumber) {
case 0:
matrixInput[row][position % 3] = number; break;
case 1:
matrixInput2[row][position % 3] = number; break;
}
position++;
}
matrixData.close();
cout << "Matrices: " << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput[i][j] << ' ';
}
cout << endl;
}
cout << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput2[i][j] << ' ';
}
cout << endl;
}
cout << endl;
cout << "Matrices multiplication: " << endl;
for (size_t i = 0; i < matrixSize; i++) {
for (size_t j = 0; j < matrixSize; j++) {
cout << matrixInput[i][j] * matrixInput2[i][j] << ' ';
}
cout << endl;
}
// ...
}
注意:如果愿意,也可以将矩阵大小写入文件,以便以后可以检索并构建自定义大小矩阵。
相关文章:
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 如何使字符串出现在编译的二进制可执行文件的开头?
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何更改使用 CPack 安装的可执行文件的名称?
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- CMake:我们可以为一组不形成可执行文件或库的特定文件指定包含目录吗?
- 如何将图像传入C++可执行文件并将输出图像存储在新目录中?
- 共享库 (.so) 没有扩展名的 Linux 可执行文件之间的区别?
- 如何在 linux 可执行文件中使用静态库
- 错误:当我从"WinDbg"打开可执行文件时,找不到符号文件。默认导出 ntdll 的符号.dll
- 编译的C可执行文件被Windows defender检测为病毒
- Linux 可执行文件通过 dlopen 在emplace_back崩溃打开共享库
- 是否可以在命令行中将输入参数传递给可执行文件
- 如何在可执行文件中存储常量数组?
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- 可执行文件C++包括.obj,.lib和.dll吗?
- 无法在 Linux 上运行C++ ZMQ 项目的可执行文件
- 从 C 可执行文件加载动态库时收到错误C++"undefined symbol"