Matrix.cpp 2.55 KB
Newer Older
Mario Bajer's avatar
cleanup  
Mario Bajer committed
1
2
#include "Matrix.hpp"

Mario Bajer's avatar
Mario Bajer committed
3
bool existsm(string filename){
Mario Bajer's avatar
cleanup    
Mario Bajer committed
4
5
    ifstream f(filename);
    return f.good();
Mario Bajer's avatar
cleanup  
Mario Bajer committed
6
7
}

8
9
10
11
12
13
14
15
16
17
18
19
20
void htmlHeader(string title){
    cout << "Content-type:text/html\r\n\r\n";
    cout << "<html>" << endl;
    cout << "<head>" << endl;
    cout << "<meta charset=\"utf-8\">" << endl;
    cout << "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">" << endl;
    cout << "<link rel=\"stylesheet\" href=\"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css\" integrity=\"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh\" crossorigin=\"anonymous\">";
    cout << "<title>" << title << "</title>" << endl;
    cout << "</head>" << endl;
}

void Matrix::print(){
    for(int i = 0; i < this->dim; i++){
Mario Bajer's avatar
cleanup  
Mario Bajer committed
21
        cout << "<p>";
22
23
24
        for(int j = 0; j < this->dim; j++){
            cout  << (*this)(i,j) << " ";
        }
Mario Bajer's avatar
cleanup  
Mario Bajer committed
25
26
27
28
        cout << "</p>";
    }
}

29
30
int Matrix::generateMatrix(int dimension){
    json j;
Mario Bajer's avatar
cleanup  
Mario Bajer committed
31

32
33
34
35
36
    this->dim = dimension;
    this->data = new int[this->dim * this->dim];
    srand(time(NULL));
    for (int i = 0; i < this->dim * this->dim; i++)
    {
Mario Bajer's avatar
cleanup    
Mario Bajer committed
37
        this->data[i] = rand() % 10;
38
    }
Mario Bajer's avatar
cleanup  
Mario Bajer committed
39

Mario Bajer's avatar
cleanup    
Mario Bajer committed
40
    this->ID = rand() % 100;
41
42
    string folderpath = "Json/";
    string testpath = folderpath + to_string(this->ID);
Mario Bajer's avatar
Mario Bajer committed
43
    while(existsm(testpath)){
44
        cout << "<p>" << "File mit ID: " << this->ID << " existiert bereits!" << "</p>";
Mario Bajer's avatar
cleanup    
Mario Bajer committed
45
        this->ID = rand() % 100;
46
        testpath = folderpath + to_string(this->ID);
Mario Bajer's avatar
cleanup  
Mario Bajer committed
47
    }
48
    folderpath += to_string(this->ID) + ".json";
Mario Bajer's avatar
cleanup    
Mario Bajer committed
49
    j["id"] = this->ID;
50
51
52
53
54
55
56
57
58
59
    j["dimension"] = this->dim;
    int size = this->dim * this->dim;
    vector<int> v(this->data, this->data + size);
    j["matrix"] = v;
    ofstream o;
    o.open(folderpath);
    o << j << endl;
    o.close();
    cout << "<p>" << "File wurde mit ID: " << this->ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
    return this->ID;
Mario Bajer's avatar
cleanup  
Mario Bajer committed
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

Matrix::Matrix(int dim) {
    int dim2 = 2;
    while (dim2 < dim)
        dim2 *= 2;
    this->dim = dim2;
    this->ID = 101;

    this->data = new int[dim * dim];

    for (int i = 0; i < dim * dim; i++)
        this->data[i] = 0;
}


Matrix Matrix::operator+(Matrix b) {
    Matrix c(b.dim);
    for (int i = 0; i < b.dim; i++)
        for (int j = 0; j < b.dim; j++)
            c(i,j) = (*this)(i,j) + b(i,j);

    return c;
}

Matrix Matrix::operator-(Matrix b) {
    Matrix c(b.dim);
//    this->dim = b.dim;
    for (int i = 0; i < this->dim; i++)
        for (int j = 0; j < this->dim; j++)
            c(i,j) = (*this)(i,j) - b(i,j);

    return c;
}