C/C++实现矩阵相乘(一)
2014-03-29 21:58
393 查看
说明:
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,否则返回 0
return 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:
![](https://img-blog.csdn.net/20140426162524687?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2ltcGxlYWN0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
MatrixTwo.txt:
![](https://img-blog.csdn.net/20140426162630484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2ltcGxlYWN0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
MatrixResult.txt
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,否则返回 0
return 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
相关文章推荐
- 关于指针的一些事情
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C与C++之间相互调用实例方法讲解
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- C++ Primer 第一部分基本语言
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解
- 浅谈C++中的string 类型占几个字节
- C/C++ 宏详细解析
- 深入分析C++中两个大数相乘结果不正确的问题
- 探讨C++中数组名与指针的用法比较分析
- 深入解析C++中的引用类型
- C++可变参数的实现方法