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);

       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
原创粉丝点击