Skip to content

Commit

Permalink
seminar 4
Browse files Browse the repository at this point in the history
  • Loading branch information
dimiturtrz committed Mar 13, 2020
1 parent 45f5700 commit b0af88f
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 0 deletions.
29 changes: 29 additions & 0 deletions exercises/4/seminar/4/Matrix/Matrix.cbp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Matrix" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Matrix" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
18 changes: 18 additions & 0 deletions exercises/4/seminar/4/Matrix/Matrix.depend
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# depslib dependency file v1.0
1584117413 source:c:\users\dtrz\desktop\sem3\new folder\matrix\rational.cpp
"ration.hpp"

1584117865 c:\users\dtrz\desktop\sem3\new folder\matrix\ration.hpp

1584117687 source:c:\users\dtrz\desktop\sem3\new folder\matrix\main.cpp
<iostream>
"ration.hpp"
"matrix.hpp"

1584117920 c:\users\dtrz\desktop\sem3\new folder\matrix\matrix.hpp
"ration.hpp"

1584117754 source:c:\users\dtrz\desktop\sem3\new folder\matrix\matrix.cpp
<iostream>
"matrix.hpp"

30 changes: 30 additions & 0 deletions exercises/4/seminar/4/Matrix/Matrix.layout
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_layout_file>
<FileVersion major="1" minor="0" />
<ActiveTarget name="Debug" />
<File name="main.cpp" open="1" top="1" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="276" topLine="0" />
</Cursor>
</File>
<File name="matrix.hpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="189" topLine="0" />
</Cursor>
</File>
<File name="matrix.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="486" topLine="17" />
</Cursor>
</File>
<File name="ration.hpp" open="1" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="0" topLine="0" />
</Cursor>
</File>
<File name="rational.cpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
<Cursor>
<Cursor1 position="21" topLine="0" />
</Cursor>
</File>
</CodeBlocks_layout_file>
Binary file added exercises/4/seminar/4/Matrix/bin/Debug/Matrix.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions exercises/4/seminar/4/Matrix/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include "ration.hpp"
#include "matrix.hpp"
using namespace std;

int main () {
Rational r1(5, 2), r2(1, 1);
Rational r3, r4;

Rational data[2][2];
data[0][0] = r1;
data[0][1] = r2;
data[1][0] = r3;
data[1][1] = r4;

Matrix m1(data, 2, 2);

cout<< r3.getA()<< " "<< r3.getB()<< endl;

return 0;
}
60 changes: 60 additions & 0 deletions exercises/4/seminar/4/Matrix/matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include "matrix.hpp"


void Matrix::clearData() {
for(int i = 0; i< N; ++i) {
delete [] data[i];
}
delete [] data;
}

void Matrix::copyData(const Matrix& m) {
data = new Rational*[N];
for(int i = 0; i< m.N; ++i) {
data[i] = new Rational[m.M];
}

for(int i = 0; i< m.N; ++i) {
for(int j = 0; j< m.M; ++j) {
data[i][j] = m.data[i][j];
}
}
}

Matrix::Matrix(Rational** data, int N, int M): data(NULL), N(N), M(M) {
if(data == NULL) {
return;
}

this->data = new Rational*[N];
for(int i = 0; i< N; ++i) {
this->data[i] = new Rational[M];
}

for(int i = 0; i< N; ++i) {
for(int j = 0; j< M; ++j) {
this->data[i][j] = data[i][j];
}
}
}

Matrix::Matrix(const Matrix& m) {
copyData();
}

Matrix::Matrix& operator=(const Matrix& m) {
if(this == &m) {
return *this;
}
clearData();
copyData(m);
return *this;
}

Matrix::~Matrix() {
for(int i = 0; i< N; ++i) {
delete [] data[i];
}
delete [] data;
}
20 changes: 20 additions & 0 deletions exercises/4/seminar/4/Matrix/matrix.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include "ration.hpp"
#ifndef MATRIX
#define MATRIX

class Matrix {
int N, M;
Rational** data;

void clearData();
void copyData(const Matrix& m);

public:
Matrix(Rational** data = NULL, int N = 0, int M = 0);
Matrix(const Matrix& m);
Matrix& operator=(const Matrix& m);
~Matrix();
};

#endif
Binary file added exercises/4/seminar/4/Matrix/obj/Debug/main.o
Binary file not shown.
Binary file added exercises/4/seminar/4/Matrix/obj/Debug/matrix.o
Binary file not shown.
Binary file added exercises/4/seminar/4/Matrix/obj/Debug/rational.o
Binary file not shown.
16 changes: 16 additions & 0 deletions exercises/4/seminar/4/Matrix/ration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef RATIONAL
#define RATIONAL

class Rational {
int a;
int b;

public:

int getA() const;
int getB() const;

Rational(int c = 0, int d = 1);
};

#endif
18 changes: 18 additions & 0 deletions exercises/4/seminar/4/Matrix/rational.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "ration.hpp"

int Rational::getA() const {
return a;
}

int Rational::getB() const {
return b;
}

Rational::Rational(int c, int d) {
a = c;
if(b == 0) {
b = 1;
} else {
b = d;
}
}
105 changes: 105 additions & 0 deletions exercises/4/seminar/4/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include <iostream>
#include <cassert>
using namespace std;

int strlen(const char* str) {
int i = 0;
for(; str[i] != '\0'; ++i);
return i;
}

void strcpy(char* destination, const char* source) {
int i = 0;
for(; source[i] != '\0'; ++i) {
destination[i] = source[i];
}
destination[i] = '\0';
}

// strlen, strcpy, strcat
class String {

private:
char* data;
int length;

void copyData(const String& str) {
length = str.length;
this->data = new char[length + 1];
strcpy(this->data, str.data);
}

void clearData() {
delete [] data;
}

public:
// resource aquisition is initialization
String(const char* data = ""): data(NULL), length(0) {
length = strlen(data);
this->data = new char[length + 1];
strcpy(this->data, data);
}

String(const String& str) {
copyData(str);
}

String& operator=(const String& str) {
if(this == &str) {
return *this;
}
clearData();
copyData(str);

return *this;
}

~String() {
clearData();
}

void strcat(const String& str) {
char* newData = new char[length + str.length + 1];
for(int i = 0; data[i] != '\0'; ++i) {
newData[i] = data[i];
}
cout<< length<< endl;
for(int i = 0; str.data[i] != '\0'; ++i) {
newData[i + length] = str.data[i];
}
newData[length + str.length] = '\0';

delete [] data;

data = newData;
length = length + str.length;
}

static String strcat(const String& s1, const String& s2) {
String result;
result.strcat(s1);
result.strcat(s2);
return result;
}

int getLength() const {
return length;
}

const char* getData() const {
return data;
}
};

int main () {
String s1("str1"), s2("str2");

s1.strcat(s2);
cout<< s1.getData()<< endl;

String s3 = String::strcat(s1, s2);
cout<< s3.getData()<< endl;

return 0;
}
36 changes: 36 additions & 0 deletions exercises/4/seminar/4/stringBuild.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
using namespace std;

int strlen(const char* str) {
int i = 0;
for(; str[i] != '\0'; ++i);
return i;
}

void strcpy(char* destination, const char* source) {
int i = 0;
for(; source[i] != '\0'; ++i) {
destination[i] = source[i];
}
destination[i] = '\0';
}

class String {
char* data;
int length;

public:
String(const char* data) {}
String(const String& str) = delete;
String& operator=(const String& str) = delete;
~String() {}
};

int main () {

String s1("str1"), s2("str2");

cout<< s1.getData()<< " "<< s2.getData()<< endl;

return 0;
}
18 changes: 18 additions & 0 deletions exercises/4/seminar/4/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;

int main() {
char* arr = new char[10];
for(int i = 0; i< 9; ++i) {
arr[i] = 'a' + i;
}
arr[9] = '\0';
cout<< arr<< endl;
if(true) {
char* arr2 = arr;
delete [] arr2;
}
cout<< arr<< endl;

return 0;
}
Loading

0 comments on commit b0af88f

Please sign in to comment.