I finally got this exercise working and i just need someone to tell me im awesome.
Or just some feedback or tips on what i can improve(probably alot:)).
Write a program which performs addition, subtraction, multiplication of matrices. The dimensions of both the matrices would be specified by the user (dynamic memory allocation required). Use of structure or a class to define the matrix would be a good idea. (Expert)
main.cpp
#include "stdafx.h"
#include "MM.hpp"
using namespace std;
int main()
{
Matrix i;
i.read();
cout << "Enter operator" << endl;
char ch; cin >> ch;
while(ch != 'x' && ch != 'X' && ch != '*' && ch != '+' && ch != '-') {
cout << "invalid operator" << endl;
cin >> ch;
}
i.calculate(ch);
i.print(ch);
system("pause"); // cin.get() doesnt allways work :s
return 0;
}
MM.hpp
#ifndef GUARD_MM_info
#define GUARD_MM_info
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
#include <utility>
#include <algorithm>
class Matrix {
public:
typedef std::vector<std::vector<int> > matrix;
int calculate(const char);
void print(const char&);
void read();
private:
void Menu1(); void Menu2();
void Menu3(); void Menu4();
void Menu5(); void Menu6();
int space(const int&);
void readLeft();
void readRight();
void multiply();
void add();
void substract();
std::pair<int, int> setSize(std::string);
void setValues(const char&, matrix&, const std::pair<int, int>);
matrix ML;
matrix MR;
matrix R;
int spaces, spaceout;
};
void Matrix::multiply() {
std::vector<int> v(MR[0].size(), 0);
R = std::vector<std::vector<int> >(ML.size(), v);
for(int i = 0; i != R.size(); i++)
for(int j = 0; j != R[0].size(); j++)
if(ML.size() < MR[0].size())
for(int k = 0; k != MR.size(); k++)
R[i][j] += ( ML[i][k]*MR[k][i] );
else
for(int k = 0; k != MR.size(); k++)
R[i][j] += ( ML[i][k]*MR[k][j] );
}
void Matrix::add() {
std::vector<int> v(std::max(ML[0].size(), MR[0].size()), 0);
R = std::vector<std::vector<int> >(std::max(ML.size(), MR.size()), v);
for(int i = 0; i != ML.size(); i++)
for(int j = 0; j != ML[0].size(); j++)
R[i][j]+=ML[i][j];
for(int i = 0; i != MR.size(); i++)
for(int j = 0; j != MR[0].size(); j++)
R[i][j]+=MR[i][j];
}
void Matrix::substract() {
std::vector<int> v(std::max(ML[0].size(), MR[0].size()), 0);
R = std::vector<std::vector<int> >(std::max(ML.size(), MR.size()), v);
for(int i = 0; i != ML.size(); i++)
for(int j = 0; j != ML[0].size(); j++)
R[i][j]+=ML[i][j];
for(int i = 0; i != MR.size(); i++)
for(int j = 0; j != MR[0].size(); j++)
R[i][j]-=MR[i][j];
}
int Matrix::calculate(const char c) {
if (c == '*' || c == 'x' || c == 'X') {
if(ML[0].size() == MR.size()) { multiply(); return 0; }
else { Menu1(); Menu5(); Menu6();
char ch; std::cin >> ch;
while(ch != '1' && ch != '2') { Menu3(); std::cin >> ch; }
if(ch == '1'){ readLeft(); calculate('*'); }
else if(ch == '2'){ readRight(); calculate('*'); }
}}
if(c == '+') {
if(ML.size() == MR.size() && ML[0].size() == MR[0].size()) { add(); return 0; }
else { Menu2(); Menu5(); Menu6();
char ch; std::cin >> ch;
while(ch != '1' && ch != '2') { Menu3(); std::cin >> ch; }
if(ch == '1'){ readLeft(); calculate('+'); }
else if(ch == '2'){ readRight(); calculate('+'); }
}}
if(c == '-') {
if(ML.size() == MR.size() && ML[0].size() == MR[0].size()) { substract(); return 0; }
else { Menu2(); Menu5(); Menu6();
char ch; std::cin >> ch;
while(ch != '1' && ch != '2') { Menu3(); std::cin >> ch; }
if(ch == '1'){ readLeft(); calculate('-'); }
else if(ch == '2'){ readRight(); calculate('-'); }
}}
Menu3();
char ch; std::cin >> ch; calculate(ch);
}
void Matrix::print(const char& c = ' ') {
std::cout << std::endl;
int sp1 = 3, sp2 = 3;
for(std::vector<int>::size_type t = 0; t != std::max(ML.size(),MR.size()); t++) {
if(ML.size() > t) {
for(int j = 0; j != ML[0].size(); j++) {
std::cout << "["<< std::string((3-space(ML[t][j])), ' ')
<< ML[t][j] << "]"; if(t == 0)sp1+=5; }
if(t == 0) std::cout << " " << c << " "; else std::cout << " ";
}else std::cout << std::string(sp1, ' ');
if(MR.size() > t) {
for(int j = 0; j != MR[0].size(); j++) {
std::cout << "["<< std::string((3-space(MR[t][j])), ' ')
<< MR[t][j] << "]"; if(t == 0) sp2+=5; }
if(t == 0) std::cout << " = "; else std::cout << " ";
}else std::cout << std::string(sp2, ' ');
if(R.size() > t) {
for(int j = 0; j != R[0].size(); j++)
std::cout << "["<< std::string((3-space(R[t][j])), ' ')
<< R[t][j] << "]";
}
std::cout << std::endl;
}
}
void Matrix::Menu1() { std::cout << "Can not multiply, columns in left matrix must equal rows in right matrix." << std::endl; }
void Matrix::Menu2() { std::cout << "Both matrixes have to be of the same size." << std::endl; }
void Matrix::Menu3() { std::cout << "You have not entered a valid operator(*, +, -), please try again." << std::endl; }
void Matrix::Menu4() { std::cout << "[3]Exit" << std::endl; }
void Matrix::Menu5() { std::cout << "[1]Change left matrix" << std::endl; }
void Matrix::Menu6() { std::cout << "[2]Change right matrix" << std::endl; }
int Matrix::space(const int& N) {
if(N == 0) return 1;
int n = N, s = 0;
if(N<0){ n = N-(N*2); s++; }
while(n>=1) {
n = n/10;
s++;
}
return s;
}
void Matrix::read() {
std::pair<int, int> Left(setSize("Left"));
std::pair<int, int> Right(setSize("Right"));
setValues('L', ML, Left);
setValues('R', MR, Right);
}
void Matrix::readRight() {
std::pair<int, int> Right(setSize("Right"));
setValues('R', MR, Right);
}
void Matrix::readLeft() {
std::pair<int, int> Left(setSize("Left"));
setValues('L', ML, Left);
}
std::pair<int, int> Matrix::setSize(std::string side) {
std::string s = side;
std::pair<int, int> p;
std::cout << s << " matrix rows: "; std::cin >> p.first;
std::cout << s << " matrix columns: "; std::cin >> p.second;
return p;
}
void Matrix::setValues(const char& c, matrix& M, const std::pair<int, int> p) {
// p.first = Rows; p.second = Columns;
int value = 0;
std::vector<int> v(p.second, 0);
M = std::vector<std::vector<int> >(p.first, v);
for(int i = 0; i != p.first; i++) {
for(int j = 0; j != p.second; j++) {
std::cout << c <<"[" << i << "][" << j << "]:";
std::cin >> value; M[i][j] = value; std::cout;
spaces = std::max(1,space(M[i][j]));
}
}
}
#endif