School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,17 @@
#include <iostream>
#include "Matrix.h"
#include "ExtendedTest.h"
#include "ShortTest.h"
using namespace std;
int main() {
testAll();
testAllExtended();
cout << "Test End" << endl;
system("pause");
}
@@ -0,0 +1,166 @@
#include <assert.h>
#include "Matrix.h"
#include "ExtendedTest.h"
#include <iostream>
#include <exception>
using namespace std;
void testCreate() {
cout << "Test create" << endl;
Matrix m(10, 10);
assert(m.nrLines() == 10);
assert(m.nrColumns() == 10);
for (int i = 0; i < m.nrLines(); i++)
for (int j = 0; j < m.nrColumns(); j++)
assert(m.element(i, j) == NULL_TELEM);
}
void testModify() {
cout << "Test modify" << endl;
Matrix m(10, 10);
for (int j = 0; j < m.nrColumns(); j++)
m.modify(4, j, 3);
for (int i = 0; i < m.nrLines(); i++)
for (int j = 0; j < m.nrColumns(); j++)
if (i == 4)
assert(m.element(i, j) == 3);
else
assert(m.element(i, j) == NULL_TELEM);
}
void testQuantity() {
cout << "Test quantity" << endl;
Matrix m(200, 300);
for (int i = m.nrLines() / 2; i < m.nrLines(); i++) {
for (int j = 0; j <= m.nrColumns() / 2; j++)
{
int v1 = j;
int v2 = m.nrColumns() - v1 - 1;
if (i % 2 == 0 && v1 % 2 == 0)
m.modify(i, v1, i*v1);
else
if (v1 % 3 == 0)
m.modify(i, v1, i + v1);
if (i % 2 == 0 && v2 % 2 == 0)
m.modify(i, v2, i*v2);
else
if (v2 % 3 == 0)
m.modify(i, v2, i + v2);
}
}
for (int i = 0; i <= m.nrLines() / 2; i++) {
for (int j = 0; j <= m.nrColumns() / 2; j++)
{
int v1 = j;
int v2 = m.nrColumns() - v1 - 1;
if (i % 2 == 0 && v1 % 2 == 0)
m.modify(i, v1, i*v1);
else
if (v1 % 3 == 0)
m.modify(i, v1, i + v1);
if (i % 2 == 0 && v2 % 2 == 0)
m.modify(i, v2, i*v2);
else
if (v2 % 3 == 0)
m.modify(i, v2, i + v2);
}
}
for (int i = 0; i < m.nrLines(); i++)
for (int j = 0; j < m.nrColumns(); j++)
if (i % 2 == 0 && j % 2 == 0)
assert(m.element(i, j) == i * j);
else
if (j % 3 == 0)
assert(m.element(i, j) == i + j);
else assert(m.element(i, j) == NULL_TELEM);
}
void testExceptions() {
cout << "Test exceptions" << endl;
Matrix m(10, 10);
try {
m.element(-10, 0);
assert(false);
}
catch (exception&) {
assert(true);
}
try {
m.modify(12, 0, 1);
assert(false);
}
catch (exception&) {
assert(true);
}
try {
assert(m.nrLines());
}
catch (exception&) {
assert(false);
}
}
void testMix() {
cout << "Test mix" << endl;
int size = 2000;
Matrix m(size/2, size);
for (int i = 0; i < size/2; i++) {
for (int j = 0; j < size; j++) {
if (i == j) {
m.modify(i, j, 11);
}
else if (i == 100 * j) {
m.modify(i, j, 111);
}
else if ((i + j) % 1111 == 0) {
m.modify(i, j, 1111);
}
}
}
for (int i = 0; i < size/2; i++) {
for (int j = 0; j < size; j++) {
if (i == j) {
TElem old = m.modify(i, j, NULL_TELEM);
assert(old == 11);
}
else if (i == 100 * j) {
TElem old = m.modify(i, j, -111);
assert(old == 111);
}
else if ((i + j) % 1111 == 3) {
m.modify(i, j, 1);
}
}
}
for (int i = 0; i < size/2; i++) {
for (int j = 0; j < size; j++) {
TElem e = m.element(i, j);
if (i == j) {
assert(e == NULL_TELEM);
}
else if (i == 100 * j) {
assert(e == -111);
}
else if ((i + j) % 1111 == 3) {
assert(e == 1);
}
else if ((i + j) % 1111 == 0) {
assert(e == 1111);
}
else {
assert(e == NULL_TELEM);
}
}
}
}
void testAllExtended() {
testCreate();
testModify();
testQuantity();
testMix();
testExceptions();
}
@@ -0,0 +1,4 @@
#pragma once
#include "ExtendedTest.h"
void testAllExtended();
@@ -0,0 +1,81 @@
#include "Matrix.h"
#include <exception>
using namespace std;
Matrix::Matrix(int nrLines, int nrColumns) {
// Theta(1)
if (nrLines <= 0 || nrColumns <= 0) {
throw exception();
}
this->numberOfLines = nrLines;
this->numberOfColumns = nrColumns;
this->values = new TElem[nrLines * nrColumns];
this->lines = new int[nrLines * nrColumns];
this->columns = new int[nrColumns+1];
for (int i = 0; i < nrColumns+1; i++) {
this->columns[i] = 0;
}
}
int Matrix::nrLines() const {
// Theta(1)
return this->numberOfLines;
}
int Matrix::nrColumns() const {
// Theta(1)
return this->numberOfColumns;
}
TElem Matrix::element(int i, int j) const {
// Best: theta(1) | Worst: theta( nr of non-zero elements in the column) | Total: theta( nr of non-zero elements in the column)
if(i>=this->numberOfLines || j>=this->numberOfColumns || i<0 || j<0)
throw exception();
for(int k=0;k<this->columns[j+1]-this->columns[j];k++){
if(this->lines[k+this->columns[j]]==i)
return this->values[k+this->columns[j]];
}
return NULL_TELEM;
}
TElem Matrix::modify(int i, int j, TElem e) {
// Best: theta(1) | Worst: theta( nr of rows + nr of non zero values + nr of non zero values in the column) | Total: theta( nr of rows + nr of non zero values + nr of non zero values in the column)
if(i>=this->numberOfLines || j>=this->numberOfColumns || i<0 || j<0)
throw exception();
for(int k=0;k<this->columns[j+1]-this->columns[j];k++){
if(this->lines[k+this->columns[j]]==i){
swap(this->values[k+this->columns[j]],e);
return e;
}
}
for(int k=this->columns[this->numberOfColumns];k>this->columns[j];k--){
swap(this->lines[k],this->lines[k-1]);
swap(this->values[k],this->values[k-1]);
}
this->lines[this->columns[j]]=i;
this->values[this->columns[j]]=e;
for(int k=j+1;k<=this->numberOfColumns;k++)
this->columns[k]++;
return NULL_TELEM;
}
void Matrix::setMainDiagonal(TElem e) {
// Theta(min(nr of rows,nr of cols))
int length = this->numberOfLines;
if (this->numberOfColumns < this->numberOfLines) {
length = this->numberOfColumns;
}
for (int i = 0; i < length; i++) {
this->modify(i, i, e);
}
}
@@ -0,0 +1,39 @@
#pragma once
//DO NOT CHANGE THIS PART
typedef int TElem;
#define NULL_TELEM 0
class Matrix {
private:
//TODO - Representation
TElem* values;
int* lines;
int* columns;
int numberOfLines;
int numberOfColumns;
public:
//constructor
Matrix(int nrLines, int nrCols);
//returns the number of lines
int nrLines() const;
//returns the number of columns
int nrColumns() const;
//returns the element from line i and column j (indexing starts from 0)
//throws exception if (i,j) is not a valid position in the Matrix
TElem element(int i, int j) const;
//modifies the value from line i and column j
//returns the previous value from the position
//throws exception if (i,j) is not a valid position in the Matrix
TElem modify(int i, int j, TElem e);
void setMainDiagonal(TElem e);
};
@@ -0,0 +1,15 @@
#include <assert.h>
#include "Matrix.h"
using namespace std;
void testAll() {
Matrix m(4, 4);
assert(m.nrLines() == 4);
assert(m.nrColumns() == 4);
m.modify(1, 1, 5);
assert(m.element(1, 1) == 5);
TElem old = m.modify(1, 1, 6);
assert(m.element(1, 2) == NULL_TELEM);
assert(old == 5);
}
@@ -0,0 +1,3 @@
#pragma once
void testAll();