0% found this document useful (0 votes)
121 views7 pages

C++ Matric Calculator Backup

This C++ code defines a Matrix class to represent matrices and perform operations on them such as input, output, addition, subtraction, etc. Matrices can be input from the console or from files. The code defines constructor, destructor, and member functions for input, output, arithmetic operations, and determining matrix dimensions. It also includes a main function to test the class by creating a matrix, inputting values, and storing it in an array of matrices.

Uploaded by

maxmueller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views7 pages

C++ Matric Calculator Backup

This C++ code defines a Matrix class to represent matrices and perform operations on them such as input, output, addition, subtraction, etc. Matrices can be input from the console or from files. The code defines constructor, destructor, and member functions for input, output, arithmetic operations, and determining matrix dimensions. It also includes a main function to test the class by creating a matrix, inputting values, and storing it in an array of matrices.

Uploaded by

maxmueller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

// Matrix-calculator with input and output from and to console and file

#include <iostream>
#include <string>
#include <fstream>

class Matrix {

public:

typedef int type;

// default constructor
Matrix() {}

// constructor
Matrix (unsigned int row, unsigned int column): row_count(row),
column_count(column), matrix_pointer(0) {
matrix_struct_pointer = new Matrix_struct(row, column);
matrix_pointer = matrix_struct_pointer->matr;
}

// copy-constructor
Matrix (const Matrix& other): row_count(other.row_count),
column_count(other.column_count), matrix_pointer(0) {

matrix_struct_pointer = new Matrix_struct(row_count, column_count);


matrix_pointer = matrix_struct_pointer->matr;

copy(other);
}

// assign-constructor
void operator= (const Matrix& other) {

if (*matrix_pointer != *other.matrix_pointer) {

(*this).~Matrix();

matrix_struct_pointer = new Matrix_struct(row_count, column_count);


matrix_pointer = matrix_struct_pointer->matr;

copy(other);
}
}

// copies the Matrix_struct from other to this


void copy (const Matrix& other) {

unsigned int r_counter = 0;


unsigned int c_counter = 0;

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row) {


for (type* column = *row; column < *row+column_count; ++column) {
*column = *( *(other.matrix_pointer+r_counter) + c_counter);
++c_counter;
}
++r_counter;
c_counter = 0;
}
}

// destructor
~Matrix () {

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row)


delete[] *row;

delete[] matrix_pointer;

delete this;
}

// input from console


void console_input () {

type next_input;

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row) {


for (type* column = *row; column < (*row)+column_count; ++column) {
std::cin >> next_input;
*column = next_input;
}
}
std::cout << "input successful \n";
}

// input from file


void file_input (std::string filepath) {

type next_input;

std::ifstream in;
in.open(filepath);

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row) {


for (type* column = *row; column < (*row)+column_count; ++column) {
in >> next_input;
*column = next_input;
}
}

in.close();
std::cout << "input successful \n";
}

// prints the matrix row by row to console


void console_output() {

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row) {

std::cout << "| ";

for (type* column = *row; column < *row+column_count; ++column)


std::cout << *column << " | ";
std::cout << std::endl;
}

std::cout << "Output finished.";


}

// prints the matrix row by row to a file


void file_output(std::string filepath) {

std::ofstream out;
out.open(filepath);

for (type** row = matrix_pointer; row < matrix_pointer+row_count; ++row) {

out << "| ";

for (type* column = *row; column < *row+column_count; ++column)


out << *column << " | ";

out << std::endl;


}

out.close();
std::cout << "Output finished.";
}

// adds a value of type to a single element


void add_value_element (type value, int row, int column) {

if (row > 0 && column > 0 && row <= row_count && column <= column_count)
*(*((matrix_pointer+row)-1) + (column-1)) += value;

else
std::cout << "Row and column have to be positive and within the
dimensions of the matrix. \n";
}

// adds a value of type to a single column


void add_value_column (type value, int column) {

if (column > 0 && column <= column_count) {


for (int row = 1; row <= row_count; ++row)
add_value_element (value, row, column);
}

else
std::cout << "Column has to be positive and within the dimensions of
the matrix. \n";
}

// adds a value of type to a single row


void add_value_row (type value, int row) {

if (row > 0 && row <= row_count) {


for (int column = 1; column <= column_count; ++column)
add_value_element (value, row, column);
}

else
std::cout << "Row has to be positive and within the dimensions of the
matrix. \n";
}

// adds a value of type to the whole matrix


void add_value_matrix (type value) {

for (int row = 1; row <= row_count; ++row)


add_value_row (value, row);
}

// subtracts a value of type from a single element


void subtract_value_element (type value, int row, int column) {

if (row > 0 && column > 0)


*(*((matrix_pointer+row)-1) + (column-1)) -= value;

else
std::cout << "Row and column have to be positive and within the
dimensions of the matrix. \n";
}

// subtracts a value of type from a single column


void subtract_value_column (type value, int column) {

if (column > 0 && column <= column_count) {


for (int row = 1; row <= row_count; ++row)
subtract_value_element (value, row, column);
}

else
std::cout << "Column has to be positive and within the dimensions of
the matrix. \n";
}

// subtracts a value of type from a single row


void subtract_value_row (type value, int row) {

if (row > 0 && row <= row_count) {


for (int column = 1; column <= column_count; ++column)
subtract_value_element (value, row, column);
}

else
std::cout << "Row has to be positive and within the dimensions of the
matrix. \n";
}

// subtracts a value of type from the whole matrix


void subtract_value_matrix (type value) {

for (int row = 1; row <= row_count; ++row)


subtract_value_row (value, row);
}

// subtraction (column) --> subtraction (single element)

// subtraction (row) --> subtraction (single element)


// subtraction (matrix) --> subtraction (row)

// multiplication (single element)

// multiplication (column) --> multiplication (single element)

// multiplication (row) --> multiplication (single element)

// multiplication (matrix) --> multiplication (row)

// division (single element)

// division (column) --> division (single element)

// division (row) --> division (single element)

// division (matrix) --> division (row)

// determinant (parameters: reference to matrix, row_count, column_count)


// 2x2-determinant, delete all elements (--> still have backup)
// else: determinant without last row/column

// prints the dimensions (row*column) to the console


void dimensions() {
std::cout << "The dimensions are " << row_count << "x" << column_count <<
". \n";
}

private:

// holds the Matrix as a 2D-array and a pointer to it


struct Matrix_struct {

Matrix_struct (unsigned int row, unsigned int column) {


matr = new type*[row];
for (type** pointer = matr; pointer < matr+row; ++pointer)
*pointer = new type[column];
}

type** matr;
};

unsigned int row_count;


unsigned int column_count;
Matrix_struct* matrix_struct_pointer; // points to Matrix_struct
type** matrix_pointer; // == matrix_struct_pointer->matr
};

int main() {

const int Max_Matrix_Count = 100; // max number of matrices that the programm
can work on at the same time
Matrix* matrices[Max_Matrix_Count];

for (int c = 0; c < Max_Matrix_Count; ++c)


matrices[c] = 0;

unsigned int row_count;


unsigned int column_count;
int input_mode;
std::string filepath;

//-------- enter dimensions and check the (changeable) limits --------//

std::cout << "What are the dimensions of the matrix (row, column)? \n";
std::cin >> row_count;
std::cin >> column_count;

while (row_count == 0 || column_count == 0) {

if (row_count = 0) {
std::cout << "Invalid number of rows (has to be > 0).";
std::cin >> row_count;
}

if (column_count == 0) {
std::cout << "Invalid number of columns (has to be > 0).";
std::cin >> column_count;
}
}

if (row_count * column_count > 100000) {


std::cout << "The size of the matrix exceeds the limit of 100'000 entries.
Please enter smaller dimensions (row, column).";
std::cin >> row_count;
std::cin >> column_count;
}

//------------------------- input of matrix -------------------------//

std::cout << "How would you like to input your matrix? \n";
std::cout << "For console input, please press 1. \n";
std::cout << "For file input, please press 2. \n";
std::cin >> input_mode;

// console input
if (input_mode == 1) {

std::cout << "Please enter the Matrix. \n";

for (int slot = 0; slot < Max_Matrix_Count; ++slot) {

if (matrices[slot] == 0) {

matrices[slot] = new Matrix(row_count, column_count);


matrices[slot]->console_input();
break;
}
}

// file input
else if (input_mode == 2) {
std::cout << "Please enter the filepath. \n";
std::cin >> filepath;

for (int slot = 0; slot < Max_Matrix_Count; ++slot) {

if (matrices[slot] == 0) {

matrices[slot] = new Matrix(row_count, column_count);


matrices[slot]->file_input(filepath);
break;
}
}
}

//------------------ different options are listed ------------------//

bool end; // if end == true, the programm will end

do { // ask for more matrices or operations to be done

// SAVE OLD VERSION OF MATRIX!

// input another matrix


// ---> check if slot is empty!

// matrix-multiplication

// determinant

// output

// end (output and termination)

} while (end != true);

return 0;
}

You might also like