herrDeng網內搜尋

自訂搜尋

Ads

2016年12月9日 星期五

Ex11 簡易Matrix Class


14 則留言:

B10433011 提到...

#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
#define row 4
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
term[i][j] = a[i][j];
}
}
double&operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "--------------------\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix M;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
M.term[i][j] = r*A(i, j);
}
return M;
}
};
int main()
{
double a[4][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12}};
double b[4][3] = { {0,2,3},{0,1,1},{0,1,2},{2,3,5}};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
Matrix M = 2.0*A;
M.print();
system("Pause");
return 0;
}

B10133186 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

B10433080 提到...

public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
term[i][j] = a[i][j];
}
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "====================\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}


friend Matrix&operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C(i, j) = A(i, j) + B(i, j);
}
return C;

}
friend Matrix&operator*(double r, Matrix A) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C.term[i][j] = r*A(i,j);
}
return C;
}

};
int main()
{
double a[3][3] = { { 1,2,3 },{ 4,5,6 },{ 7,8,9 } };
double b[3][3] = { { 4,5,6 },{ 7,8,9 },{ 1,2,3 } };
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
C = 3.0*A;
C.print();



system("Pause");
return 0;



}

A10433018 提到...

#include
#include
#include
using namespace std;
#define row 3
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
};

Matrix&operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
C.term[i][j] = A(i, j)*2 + B(i, j)*2;
}
return C;
}

int main() {
double a[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
double b[3][3] = { {4,5,6 }, { 7,8,9 }, { 1,2,3 }};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
system("Pause");
return 0;
};

A10433036 提到...

#include
#include
#include
using namespace std;
#define row 3
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
};

Matrix&operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
C.term[i][j] = A(i, j)*2 + B(i, j)*2;
}
return C;
}

int main() {
double a[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
double b[3][3] = { {4,5,6 }, { 7,8,9 }, { 1,2,3 }};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
system("Pause");
return 0;
};



A10433024 提到...

#include
#include
#include
using namespace std;
#define row 3
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
};

Matrix&operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row; i++) {
for (int j = 0;j < col; j++)
C.term[i][j] = A(i, j)*2 + B(i, j)*2;
}
return C;
}

int main() {
double a[3][3] = { {1,2,3},{4,5,6},{7,8,9} };
double b[3][3] = { {4,5,6 }, { 7,8,9 }, { 1,2,3 }};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
system("Pause");
return 0;
};



a10433015 提到...

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

#define row 3
#define col 3
class Matrix
{
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
term[i][j] = a[i][j];
}

}
double& operator()(int i, int j) {
return term[i][j];


}
void print() {
cout << "=================\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
{
cout << term[i][j] << "\t";
}
cout << endl;
}

}
void R(int q) {
cout << "=================\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
{
cout << term[i][j] * q << "\t";
}
cout << endl;
}
}
friend Matrix&operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;

}
friend Matrix&operator*(Matrix A, Matrix B)
{
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
C(i, j) = 0;
for (int k = 0;k < 3;k++)
C(i, j) += A(i, k)*B(k, j);


}

}
return C;
}
};


int main() {

double a[3][3] = { {1,2,3},{4,5,6},{7,8,9 }
};
double b[3][3] = { {4,5,6 }, { 7,8,9 }, { 1,2,3 }
};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
A.R(2);
C = A*B;
C.print();
system("Pause");
return 0;




}

b10233127 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

B10333054 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

B10333054 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

B10333016 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

B10333049 提到...

class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
term[i][j] = a[i][j];
}
}
double& operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "----------------\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}

}

friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix C;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
C.term[i][j] = r*A(i, j) ;
}
return C;
}

};

b10233072黃宥婕 提到...


#include
#include
using namespace std;
#define row 4
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
term[i][j] = a[i][j];
}
}
double&operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "--------------------\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix M;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
M.term[i][j] = r*A(i, j);
}
return M;
}
};
int main()
{
double a[4][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12}};
double b[4][3] = { {0,2,3},{0,1,1},{0,1,2},{2,3,5}};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
Matrix M = 2.0*A;
M.print();
system("Pause");
return 0;
}

b10233074 提到...

#include
#include
#include
using namespace std;
#define row 4
#define col 3
class Matrix {
public:
double term[row][col];
Matrix() { memset(term, 0, sizeof(term)); }
Matrix(double a[row][col]) {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
term[i][j] = a[i][j];
}
}
double&operator()(int i, int j) {
return term[i][j];
}
void print() {
cout << "--------------------\n";
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
cout << term[i][j] << "\t";
}
cout << endl;
}
}
friend Matrix& operator+(Matrix A, Matrix B) {
Matrix C;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
C.term[i][j] = A(i, j) + B(i, j);
}
return C;
}
friend Matrix& operator*(double r, Matrix A) {
Matrix M;
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++)
M.term[i][j] = r*A(i, j);
}
return M;
}
};
int main()
{
double a[4][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12}};
double b[4][3] = { {0,2,3},{0,1,1},{0,1,2},{2,3,5}};
Matrix A(a), B(b);
A.print();
B.print();
Matrix C = A + B;
C.print();
Matrix M = 2.0*A;
M.print();
system("Pause");
return 0;
}

Related Posts Plugin for WordPress, Blogger...

熱門文章