1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
//
// project4.cpp
// CS 202
//
// Created by Shane Kurr on 7/2/12.
// Copyright (c) 2012 UNLV. All rights reserved.
//
#include <iostream>
#include <iomanip>
using namespace std;
class matrixType{
friend ostream& operator<<(ostream&, const matrixType &);//
friend istream& operator>>(istream&, matrixType &);//
public:
const matrixType& operator=(const matrixType &right);//
void setMatrix();//
matrixType(int rowSize=10, int columnSize=10);//
matrixType(const matrixType ©);//
matrixType operator+(matrixType right);
matrixType operator-(matrixType right);
matrixType operator*(matrixType right);
private:
int **matrix;
int rowSize;
int columnSize;
};
matrixType matrixType::operator+(matrixType right){
if (rowSize!=right.rowSize || columnSize!=right.columnSize) {
cout << "First matrix does not match dimensions of right matrix, cannot be completed" << endl;
return *this;}
for (int j=0; j<columnSize; j++) {
for (int i=0; i<rowSize; i++) {
matrix[j][i]=matrix[j][i]+right.matrix[j][i];
}
}
return *this;
}
matrixType matrixType::operator-(matrixType right){
if (rowSize!=right.rowSize || columnSize!=right.columnSize) {
cout << "First matrix does not match dimensions of right matrix, cannot be completed" << endl;
return *this;}
for (int j=0; j<columnSize; j++) {
for (int i=0; i<rowSize; i++) {
matrix[j][i]=matrix[j][i]-right.matrix[j][i];
}
}
return *this;
}
istream& operator >>(istream& ob1, matrixType& m){
char ch;
for (int j=0; j<m.columnSize; j++) {
for (int i=0; i<m.rowSize; i++){
ob1 >> m.matrix[j][i];
}
ob1.get(ch);
}
return ob1;
}
ostream& operator <<(ostream& ob1,const matrixType& m){
for (int j=0; j<m.columnSize; j++) {
for (int i=0; i<m.rowSize; i++) {
ob1 << right << setw(4) << m.matrix[j][i];
}
ob1 << endl;
}
}
void matrixType::setMatrix(){
char ch;
cin >> columnSize >> rowSize;
cin.get(ch);
matrix = new int*[columnSize];
for (int i=0; i<columnSize; i++) {
matrix[i] = new int[rowSize];
}
}
const matrixType& matrixType::operator=(const matrixType &right){
if (rowSize!=right.rowSize || columnSize!=right.columnSize) {
cout << "First matrix does not match dimensions of right matrix, cannot be completed" << endl;
return *this;}
for (int j=0; j<columnSize; j++) {
for (int i=0; i<rowSize; i++) {
matrix[j][i]=right.matrix[j][i];
}
}
return *this;
}
matrixType::matrixType(const matrixType ©){
if (rowSize!=copy.rowSize || columnSize!=copy.columnSize) {
cout << "First matrix does not match dimensions of right matrix, cannot be completed" << endl;
}
for (int j=0; j<columnSize; j++) {
for (int i=0; i<rowSize; i++) {
matrix[j][i]=copy.matrix[j][i];
}
}
}
//clientcode
int main(){
matrixType m1,m2,m3,m4,m5;
m1.setMatrix();
m2.setMatrix();
m3.setMatrix();
m4.setMatrix();
m5.setMatrix();
cin >> m1;
cin >> m2;
cin >> m4;
m3= m1+m2;
cout << m1;
cout << m2;
cout << m3;
cout << m4;
return 0;
}
|