Skip to content

Instantly share code, notes, and snippets.

@jacyzon
Created December 2, 2012 17:14
Show Gist options
  • Save jacyzon/4189944 to your computer and use it in GitHub Desktop.
Save jacyzon/4189944 to your computer and use it in GitHub Desktop.
Matrix operator overloading
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class matrix {
private:
vector<vector<double>> matrix_double;
public:
friend istream& operator>>( istream& inStream, matrix &b ) {
//使用者輸入
string tmp_string;
while (getline(inStream, tmp_string)) {
//按空白分割字串
vector<string> results_string;
stringstream s(tmp_string);
while(!s.eof()) {
string tmp;
s >> tmp;
if(tmp != "") results_string.push_back(tmp);
}
//轉為double
vector<double> results_double;
for(vector<string>::iterator it = results_string.begin(); it != results_string.end(); it++) {
istringstream strStream(*it);
double num;
strStream >> num;
results_double.push_back(num);
}
if(!results_double.empty()) b.matrix_double.push_back(results_double);
//除錯
if(b.matrix_double.size() > 1)
if( b.matrix_double[b.matrix_double.size()-1].size() != b.matrix_double[b.matrix_double.size()-2].size()) {
b.matrix_double.pop_back();
cerr << "錯誤!!矩陣元素個數不一樣。" << endl;
cerr << "請重新輸入:" << endl;
}
}
inStream.clear();
return inStream;
}
friend ostream& operator<<( ostream& outStream, matrix &b ) {
for(unsigned int i = 0; i < b.matrix_double.size(); i++, outStream << endl)
for(vector<double>::iterator it = b.matrix_double[i].begin(); it != b.matrix_double[i].end(); it++)
outStream << *it << ' ';
outStream.clear();
return outStream;
}
friend matrix operator+(matrix &a, matrix &b) {
matrix c;
if( a.matrix_double.size() != a.matrix_double[0].size() || b.matrix_double.size() != b.matrix_double[0].size())
cerr << "不能相加!!不為方陣。" << endl;
else if(a.matrix_double.size() != b.matrix_double[0].size())
cerr << "不能相加!!兩矩陣大小不一樣。" << endl;
else {
for(unsigned int i = 0; i < a.matrix_double.size(); i++) {
vector<double> results_double;
for(unsigned int j = 0; j < a.matrix_double.size(); j++) {
results_double.push_back(a.matrix_double[i][j] + b.matrix_double[i][j]);
}
c.matrix_double.push_back(results_double);
}
}
return c;
}
friend matrix operator-(matrix &a, matrix &b) {
matrix c;
if( a.matrix_double.size() != a.matrix_double[0].size() || b.matrix_double.size() != b.matrix_double[0].size())
cerr << "不能相減!!不為方陣。" << endl;
else if(a.matrix_double.size() != b.matrix_double[0].size())
cerr << "不能相減!!兩矩陣大小不一樣。" << endl;
else {
for(unsigned int i = 0; i < a.matrix_double.size(); i++) {
vector<double> results_double;
for(unsigned int j = 0; j < a.matrix_double.size(); j++) {
results_double.push_back(a.matrix_double[i][j] - b.matrix_double[i][j]);
}
c.matrix_double.push_back(results_double);
}
}
return c;
}
friend matrix operator*(matrix &a, matrix &b) {
matrix c;
if(a.matrix_double[0].size() != b.matrix_double.size())
cerr << "不能相乘!!" << endl;
else {
//c.matrix_double.resize(a.matrix_double.size());
for(unsigned int i = 0; i < a.matrix_double.size(); i++) {
vector<double> results_double(b.matrix_double[0].size());
for(unsigned int j = 0; j < a.matrix_double[0].size(); j++)
for(unsigned int k = 0; k < b.matrix_double[0].size(); k++)
results_double[k] += (a.matrix_double[i][j] * b.matrix_double[j][k]);
c.matrix_double.push_back(results_double);
}
}
return c;
}
};
int main() {
matrix a, b, c; //宣告矩陣 a, b, c
cin >> a >> b; //輸入矩陣 a, b
c = a + b; cout << c; //輸出 a + b
matrix d = a * b; cout << d; //輸出 a * b
matrix k = a - b; cout << k; //輸出 a - b
return system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment