C/C++实现矩阵相乘(一)
来源:互联网 发布:阿里云服务器登录失败 编辑:程序博客网 时间:2024/04/30 11:59
说明:
1、用法:代码从MatrixOne.txt和MatrixTwo.txt中各读取一个矩阵数据进行乘运算,将计算所得矩阵输出到 MatrixResult.txt文件中;
2、MatrixOne.txt为乘式,MatrixTwo.txt为被乘式,前者矩阵的列数等于后者矩阵的行数;
3、所处理数据均为自然数,负数,浮点数,字母等情况未做处理。
4、代码中字符串类型PathOne,PathTwo,PathOut分别为MatrixOne.txt,MatrixOne.txt,MatrixOut.txt路径;
5、代码中主要函数
int GetkMatrixCol(string str);
1、用法:代码从MatrixOne.txt和MatrixTwo.txt中各读取一个矩阵数据进行乘运算,将计算所得矩阵输出到 MatrixResult.txt文件中;
2、MatrixOne.txt为乘式,MatrixTwo.txt为被乘式,前者矩阵的列数等于后者矩阵的行数;
3、所处理数据均为自然数,负数,浮点数,字母等情况未做处理。
4、代码中字符串类型PathOne,PathTwo,PathOut分别为MatrixOne.txt,MatrixOne.txt,MatrixOut.txt路径;
5、代码中主要函数
int GetkMatrixCol(string str);
int GetMatrixRow(string str);
vector<int> GetNumber(string str);
void MatrixCalculate(vector<int>, vector<int>, int, int, int, int,string);
6、另有一种实现方法,相对简单 C/C++实现矩阵相乘(二)
#include<iostream>#include<fstream>#include<vector>#include<list>#include<string>using namespace std;int GetkMatrixCol(string str); //返回值为 0 表示矩阵排列错误,否则返回矩阵列数int GetMatrixRow(string str); //返回值为 0 表示获取行数出错,否则返回矩阵行数vector<int> GetNumber(string str);//以一维数组的形式存放矩阵中所有的元素void MatrixCalculate(vector<int>, vector<int>, int, int, int, int, string);//进行矩阵运算,并将得到的矩阵写入到txt文件int main(){vector<int> MatrixOne, MatrixTwo;int ColOne = 0, ColTwo = 0, RowOne = 0, RowTwo = 0;string PathOne = "d:\MatrixOne.txt";string PathTwo = "d:\MatrixTwo.txt";string PathOut = "d:\MatrixResult.txt";ColOne = GetkMatrixCol(PathOne); ColTwo = GetkMatrixCol(PathTwo);RowOne = GetMatrixRow(PathOne);RowTwo = GetMatrixRow(PathTwo);//判断获取文件中矩阵信息时是否出错if (ColOne == 0 || ColTwo == 0 || RowOne == 0 || RowTwo == 0 || ColOne != RowTwo){cout << "矩阵排列错误,或者两矩阵行列不匹配,或文件不存在" << endl;return 0;}MatrixCalculate(GetNumber(PathOne), GetNumber(PathTwo), ColOne, RowOne, ColTwo, RowTwo, PathOut);cout <<"矩阵计算已经完成 , "<<"请查看 "<<PathOut<<" 文件 !"<< endl;return 0;}int GetkMatrixCol(string str){char ch;int standard = 0;int Col = 0;bool isnum = false;ifstream fin;fin.open(str);if (!fin.is_open()){ //打开文件成功is_open()返回 1,否则返回 0return 0;}while ((ch = fin.get()) != EOF){if (ch == '\n'){if (standard == 0) standard = Col; //把第一行元素个数作为标准,其他行元素个数都与其比较if (standard != Col){Col = 0;break;}isnum = false;Col = 0;continue;}//isnum相当于开关,在计算数字个数中有关键作用if (!(ch == ' ') && !isnum){Col++;isnum = true;}if ((ch == ' ') && isnum){isnum = false;}}if (ch == EOF&&Col != standard) Col = 0; //避免读到文件末尾直接退出循环,最后一行元素个数没有与标准比较的情况fin.close();return Col;}vector<int> GetNumber(string str){int temp = 0, i = 0;list<int> num;list<int>::iterator it;vector<int> matrix;char ch = NULL;ifstream fin;fin.open(str);if (!fin.is_open()){return matrix;}while ((ch = fin.get()) != EOF){if (ch != ' '&&ch != '\n'){num.push_front((ch - '0')); //ch必须得减去'0'或48,才能得到0~~9数字}else{for (it = num.begin(), i = 0; it != num.end(); it++, i++){temp += (*it)*pow(10, i); //将读入的字符转换为数字,例如文本中“ 37 28”转换成十进制数37和28} //利用list可以在表首加入元素的特性matrix.push_back(temp);temp = 0;num.clear(); //清除list,以便处理下一个数字}}fin.close();for (it = num.begin(), i = 0; it != num.end(); it++, i++){temp += (*it)*pow(10, i);}matrix.push_back(temp); //读取到文件末尾时,最后一个数字没有处理return matrix;//返回包含矩阵元素的vector}int GetMatrixRow(string str){int Row = 1;bool open;char ch;ifstream fin;fin.open(str);open = fin.is_open();if (!open){Row = 0;return Row;}while ((ch = fin.get()) != EOF){if (ch == '\n') Row++;}return Row;}void MatrixCalculate(vector<int> MatrixOne, vector<int> MatrixTwo, int ColOne, int RowOne, int ColTwo, int RowTwo, string PathOut){int num = 0;ofstream fout;fout.open(PathOut);vector<int> MatrixResult; //将计算结果存储在vector容器中for (int rowone = 0; rowone < RowOne; rowone++)for (int coltwo = 0; coltwo < ColTwo; coltwo++){for (int colone = 0, rowtwo = 0; colone < ColOne, rowtwo < ColOne; colone++, rowtwo++){num += MatrixOne[rowone*ColOne + colone] * MatrixTwo[coltwo + rowtwo*ColTwo];}MatrixResult.push_back(num);num = 0;}//将计算结果输出到路径为PathOut的文件中 for (int m = 0; m < MatrixResult.size(); m++){fout << MatrixResult[m] << " ";if ((m + 1) % ColTwo == 0){fout << '\n';}}}
MatrixOne.txt:
MatrixTwo.txt:
MatrixResult.txt
0 0
- C/C++实现矩阵相乘(一)
- C语言实现矩阵相乘
- C语言实现矩阵相乘
- C语言实现矩阵相乘
- 矩阵相乘 C语言实现
- 任意维矩阵相乘-C实现
- C语言 矩阵的相乘实现
- C/C++ 实现矩阵相乘(二)
- 实现稀疏矩阵相乘C/C++
- 矩阵相乘的C代码实现
- C++:矩阵相乘
- C编程:矩阵相乘
- c语言矩阵相乘
- C语言矩阵相乘
- 矩阵相乘C语言
- c 动态规划 矩阵相乘
- CUDA C 任意矩阵相乘
- 矩阵相乘(C案例)
- cocos2dx-新建工程时避免文件和文件夹的拷贝
- 9.菜鸟启程,向大牛进发
- zoj 2839 Find the Sequences(数学题)
- c函数之【文件及目录函数】
- 高效程序员的 7 个共同特征
- C/C++实现矩阵相乘(一)
- DCMTK-Configure the logger from a program
- 简单使用jsoup
- 关于共享文件夹
- [2012校赛新生组]狡兔三窟
- 写在开博前的话
- 最全的常用正则表达式
- CURL 请求错误代码
- c函数之【信号函数】