Commit db65fe2f authored by Mario Bajer's avatar Mario Bajer
Browse files

added: list, delete and multi functionality

removed: show as this is incorporated in list now
parent a791ab2e
...@@ -4,9 +4,12 @@ project(ese_zeitlhofer_bajer) ...@@ -4,9 +4,12 @@ project(ese_zeitlhofer_bajer)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
include_directories(E:/IT/Tools/) include_directories(E:/IT/Tools/)
#nicht cmake benutzen!
add_executable(matrix add_executable(matrix
show.cpp show.cpp
list.cpp
delete.cpp
multi.cpp
generate.cpp generate.cpp
Matrix.cpp Matrix.cpp
Matrix.hpp) Matrix.hpp)
\ No newline at end of file
//
// Created by Mario on 05/05/2020.
//
#include "Matrix.hpp" #include "Matrix.hpp"
bool exists(string filename){ bool exists(string filename){
...@@ -9,39 +5,89 @@ bool exists(string filename){ ...@@ -9,39 +5,89 @@ bool exists(string filename){
return f.good(); return f.good();
} }
void Matrix::printtocnsl(){ void htmlHeader(string title){
// cout << "<p>"; cout << "Content-type:text/html\r\n\r\n";
for (int i = 0; i < this->dim; i++) { 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++){
cout << "<p>"; cout << "<p>";
for (int j = 0; j < this->dim; j++) for(int j = 0; j < this->dim; j++){
cout << (*this)(i,j) << " "; cout << (*this)(i,j) << " ";
}
cout << "</p>"; cout << "</p>";
} }
// cout << "</p>";
} }
void Matrix::generate(int dims) { int Matrix::generateMatrix(int dimension){
srand(time(NULL)); json j;
// Matrix matrix;
this->dim = dims;
this->data = new int[dims * dims];
for (int i = 0; i < (dims * dims); i++) this->dim = dimension;
this->data = new int[this->dim * this->dim];
srand(time(NULL));
for (int i = 0; i < this->dim * this->dim; i++)
{
this->data[i] = rand() % 10; this->data[i] = rand() % 10;
}
this->ID = rand() % 100; this->ID = rand() % 100;
while(exists("Json/" + to_string(this->ID)+".json")){ string folderpath = "Json/";
string testpath = folderpath + to_string(this->ID);
while(exists(testpath)){
cout << "<p>" << "File mit ID: " << this->ID << " existiert bereits!" << "</p>";
this->ID = rand() % 100; this->ID = rand() % 100;
testpath = folderpath + to_string(this->ID);
} }
folderpath += to_string(this->ID) + ".json";
json j;
j["id"] = this->ID; j["id"] = this->ID;
j["dim"] = this->dim; j["dimension"] = this->dim;
vector<int> vector1(this->data, this->data + (this->dim * this->dim)); int size = this->dim * this->dim;
j["data"] = vector1; vector<int> v(this->data, this->data + size);
ofstream outFileGen; j["matrix"] = v;
outFileGen.open(to_string(this->ID)+".json"); ofstream o;
outFileGen << j << endl; o.open(folderpath);
outFileGen.close(); o << j << endl;
cout << "File generiert\n"; o.close();
cout << "<p>" << "File wurde mit ID: " << this->ID << " in Ordner " << folderpath << " erstellt!" << "</p>";
return this->ID;
} }
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;
}
\ No newline at end of file
// #ifndef MATRIX_HPP
// Created by Mario on 05/05/2020. #define MATRIX_HPP
//
#ifndef ESE_ZEITLHOFER_BAJER_MATRIX_H
#define ESE_ZEITLHOFER_BAJER_MATRIX_H
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <stdio.h> #include <cstdio>
#include <stdlib.h> #include <cstdlib>
#include <sys/stat.h>
#include <nlohmann/json.hpp>
#include <cgicc/CgiDefs.h> #include <cgicc/CgiDefs.h>
#include <cgicc/Cgicc.h> #include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h> #include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h> #include <cgicc/HTMLClasses.h>
#include <json.hpp>
using namespace std; using namespace std;
using namespace cgicc; using namespace cgicc;
using json = nlohmann::json; using json = nlohmann::json;
class Matrix { class Matrix{
public: public:
Matrix(int dim);
int ID; int ID;
int dim; int dim;
int *data; int *data;
void generate(int dims); int generateMatrix(int dimension);
void printtocnsl(); void print();
inline int& operator()(unsigned row, unsigned col) { inline int& operator()(unsigned row, unsigned col) {
return data[dim * row + col]; return data[dim*row + col];
} }
inline int operator()(unsigned row, unsigned col) const { inline int operator()(unsigned row, unsigned col) const {
return data[dim * row + col]; return data[dim*row + col];
} }
Matrix operator+(Matrix b);
Matrix operator-(Matrix b);
private: private:
}; };
void htmlHeader(string title);
#endif //ESE_ZEITLHOFER_BAJER_MATRIX_H #endif
\ No newline at end of file
#include "Matrix.hpp"
int main(){
htmlHeader("delete.cgi");
Cgicc formData;
string folderpath = "";
form_iterator fi = formData.getElement("deleteBtn");
if( !fi->isEmpty() && fi != (*formData).end()) {
folderpath = **fi;
}
else {
cout << "Error!" << endl;
}
const char * deletepath = folderpath.c_str();
if(remove(deletepath) != 0){
cout << "<p>Fehler bei Löschvorgang!</p>" << endl;
}
else{
cout << "<p>File " << folderpath << " wurde gelöscht!</p>" << endl;
}
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "</div>" << endl;
cout << "</body>\n";
cout << "</html>\n";
}
\ No newline at end of file
//
// Created by Mario on 08/05/2020.
//
#include "Matrix.hpp" #include "Matrix.hpp"
int main() { int main(){
Matrix matrix; Matrix matrix(2);
Cgicc formData; Cgicc formData;
cout << "Content-type:text/html\r\n\r\n"; htmlHeader("generate.cgi");
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>generate.cgi</title>" << endl;
cout << "</head>" << endl;
matrix.generate(10);
cout << "<body>\n";
cout << "<div class=\"container\">" << endl;
form_iterator fi = formData.getElement("dimension"); form_iterator fi = formData.getElement("dimension");
if( !fi->isEmpty() && fi != (*formData).end()) { if( !fi->isEmpty() && fi != (*formData).end()) {
matrix.generate(stoi(**fi)); //nur vielfache von 2 erlaubt
}else { int input = stoi(**fi);
cout << "Wrong input" << endl; int dim2 = 2;
while (dim2 < input)
dim2 *= 2;
matrix.generateMatrix(dim2);
}
else
{
cout << "No Dimension entered" << endl;
} }
cout << "<a href=\"http://localhost:80/index.html\">Back to Start</a>"; cout << "</div>";
cout << "</div>" << endl; cout << "<br/>\n";
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "</body>\n"; cout << "</body>\n";
cout << "</html>\n"; cout << "</html>\n";
return 0;
} }
\ No newline at end of file
#include "Matrix.hpp"
namespace fs = filesystem;
int main(){
htmlHeader("list.cgi");
cout << "<body>" << endl;
cout << "<br>" << endl;
cout << "<div class=\"container\">" << endl;
cout << "<table class=\"table\">" << endl;
cout << "<thead class=\"thead-dark\">" << endl;
cout << "<tr>" << endl;
cout << "<th>ID</th>" << endl;
cout << "<th>Dimension</th>" << endl;
cout << "<th>File Location</th>" << endl;
cout << "<th></th>" << endl;
cout << "</tr>" << endl;
cout << "</thead>" << endl;
cout << "<tbody>" << endl;
string path = "Json";
for(const auto & entry : fs::directory_iterator(path)){
json existingjson;
ifstream in(entry.path());
in >> existingjson;
cout << "<tr>" << endl;
cout << "<td>" << existingjson["id"] << "</td>" << endl;
cout << "<td>" << existingjson["dimension"] << "</td>" << endl;
cout << "<td>" << entry.path() << "</td>" << endl;
cout << "<form action=\"/cgi-bin/show.cgi\" method=\"GET\">" << endl;
cout << "<td>" << endl;
cout << "<button name=\"showBtn\" class=\"btn btn-primary\" type=submit value=" << entry.path() << ">" << endl;
cout << "Show" << endl;
cout << "</button>" << endl;
cout << "</form>" << endl;
cout << "<form action=\"/cgi-bin/delete.cgi\" method=\"GET\">" << endl;
cout << "<td>" << endl;
cout << "<button name=\"deleteBtn\" class=\"btn btn-danger\" type=submit value=" << entry.path() << ">" << endl;
cout << "Delete" << endl;
cout << "</button>" << endl;
cout << "</form>" << endl;
cout << "</td>" << endl;
cout << "</tr>" << endl;
}
cout << "</tbody>" << endl;
cout << "</table>" << endl;
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "</div>" << endl;
cout << "</body>\n";
cout << "</html>\n";
}
\ No newline at end of file
...@@ -2,12 +2,17 @@ CC=g++ ...@@ -2,12 +2,17 @@ CC=g++
CFLAGS=-Wall -std=c++17 CFLAGS=-Wall -std=c++17
LDLIBS=-lcgicc LDLIBS=-lcgicc
all: show generate all: show generate list delete multi
show: show:
$(CC) $(CFLAGS) show.cpp Matrix.cpp $(LDLIBS) -o /mnt/e/IT/Tools/xampp/cgi-bin/show.cgi $(CC) $(CFLAGS) show.cpp Matrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/show.cgi
generate: generate:
$(CC) $(CFLAGS) generate.cpp Matrix.cpp $(LDLIBS) -o /mnt/e/IT/Tools/xampp/cgi-bin/generate.cgi $(CC) $(CFLAGS) generate.cpp Matrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/generate.cgi
list:
$(CC) $(CFLAGS) list.cpp Matrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/list.cgi
delete:
$(CC) $(CFLAGS) delete.cpp Matrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/delete.cgi
multi:
$(CC) $(CFLAGS) multi.cpp Matrix.cpp $(LDLIBS) -o /usr/lib/cgi-bin/multi.cgi
clean: clean:
rm -f /mnt/e/IT/Tools/xampp/cgi-bin/*.cgi rm -f /usr/lib/cgi-bin/*.cgi
#include "Matrix.hpp"
int cut = 64;
Matrix mult_std(Matrix a, Matrix b) {
Matrix c(a.dim);
for (int i = 0; i < a.dim; i++)
for (int k = 0; k < a.dim; k++)
for (int j = 0; j < a.dim; j++)
c(i,j) += a(i,k) * b(k,j);
return c;
}
Matrix get_part(int pi, int pj, Matrix m) {
Matrix p(m.dim / 2);
pi = pi * p.dim;
pj = pj * p.dim;
for (int i = 0; i < p.dim; i++)
for (int j = 0; j < p.dim; j++)
p(i,j) = m(i + pi,j + pj);
return p;
}
void set_part(int pi, int pj, Matrix* m, Matrix p) {
pi = pi * p.dim;
pj = pj * p.dim;
for (int i = 0; i < p.dim; i++)
for (int j = 0; j < p.dim; j++)
(*m)(i + pi,j + pj) = p(i,j);
}
Matrix mult_strassen(Matrix a, Matrix b) {
if (a.dim <= cut)
return mult_std(a, b);
Matrix a11 = get_part(0, 0, a);
Matrix a12 = get_part(0, 1, a);
Matrix a21 = get_part(1, 0, a);
Matrix a22 = get_part(1, 1, a);
Matrix b11 = get_part(0, 0, b);
Matrix b12 = get_part(0, 1, b);
Matrix b21 = get_part(1, 0, b);
Matrix b22 = get_part(1, 1, b);
Matrix m1 = mult_strassen(a11 + a22, b11 + b22);
Matrix m2 = mult_strassen(a21 + a22, b11);
Matrix m3 = mult_strassen(a11, b12 - b22);
Matrix m4 = mult_strassen(a22, b21 - b11);
Matrix m5 = mult_strassen(a11 + a12, b22);
Matrix m6 = mult_strassen(a21 - a11, b11 + b12);
Matrix m7 = mult_strassen(a12 - a22, b21 + b22);
Matrix c(a.dim);
set_part(0, 0, &c, m1 + m4 - m5 + m7);
set_part(0, 1, &c, m3 + m5);
set_part(1, 0, &c, m2 + m4);
set_part(1, 1, &c, m1 - m2 + m3 + m6);
return c;
}
Matrix run(Matrix (*f)(Matrix, Matrix), Matrix a, Matrix b) {
Matrix c(a.dim);
if(a.dim != b.dim) {
cout << "Dimensions of Matrices do not match!" << endl;
return c;
}
c = f(a, b);
return c;
}
int main(){
Cgicc formData;
htmlHeader("multi.cgi");
cout << "<body>\n";
// cout << "<br>" << endl;
cout << "<div class=\"container\">" << endl;
string matrixs1 = "";
string matrixs2 = "";
form_iterator fi1 = formData.getElement("multi1");
if( !fi1->isEmpty() && fi1 != (*formData).end()) {
matrixs1 = **fi1;
cout << matrixs1 << endl;
}
else {
cout << "Error on Matrix 1!" << endl;
}
form_iterator fi2 = formData.getElement("multi2");
if( !fi2->isEmpty() && fi2 != (*formData).end()) {
matrixs2 = **fi2;
cout << matrixs2 << endl;
}
else {
cout << "Error on Matrix 2!" << endl;
}
string debug = "debug 0";
cout << debug << endl;
json json1;
json json2;
ifstream in1(matrixs1);
ifstream in2(matrixs2);
in1 >> json1;
in2 >> json2;
cout << "debug 1" << endl;
// cout << "<p style=\"word-wrap: break-word\">" << json1 << "</p>" << endl;
// cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
// cout << "<p style=\"word-wrap: break-word\">" << json1 << "</p>" << endl;
Matrix matrix1(2);
vector<int> v1 = json1["matrix"];
matrix1.ID = json1["id"];
matrix1.dim = json1["dimension"];
matrix1.data = &v1[0];
cout << to_string(matrix1.ID) << endl;
// matrix.print();
Matrix matrix2(2);
vector<int> v2 = json1["matrix"];
matrix2.ID = json1["id"];
matrix2.dim = json1["dimension"];
matrix2.data = &v1[0];
cout << to_string(matrix2.ID) << endl;
cout << "debug 2" << endl;
Matrix result(matrix1.dim);
cout << "dim for result" << to_string(result.dim) << endl;
cout << "debug 3" << endl;
result = run(mult_strassen, matrix1, matrix2);
cout << "debug 4";
result.generateMatrix(result.dim);
cout << "Resulting Matrix saved at ID: " + to_string(result.ID) << endl;
cout << "</div>" << endl;
cout << "</body>\n";
cout << "</html>\n";
return 0;
}
\ No newline at end of file
#include "Matrix.hpp" #include "Matrix.hpp"
int main(){ int main(){
Matrix matrix; Cgicc formData;
Cgicc formContent;
// cout << "Content-type:text/html\r\n\r\n"; htmlHeader("show.cgi");
// cout << "<html>\n"; cout << "<body>\n";
// cout << "<head>\n"; cout << "<br>" << endl;
// cout << "<title>show.cgi</title>\n";
// cout << "</head>\n";
// cout << "<body>\n";
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>show.cgi</title>" << endl;
cout << "</head>" << endl;
matrix.generate(10);
cout << "after generate\n";
string folderpath = ""; string folderpath = "";
form_iterator fi = formContent.getElement("ID"); form_iterator fi = formData.getElement("showBtn");
if( !fi->isEmpty() && fi != (*formContent).end()) { if( !fi->isEmpty() && fi != (*formData).end()) {
folderpath = **fi; folderpath = **fi;
} }
else { else {
...@@ -33,16 +17,18 @@ int main(){ ...@@ -33,16 +17,18 @@ int main(){
} }
json j; json j;
ifstream inFile("48.json"); ifstream in(folderpath);
inFile >> j; in >> j;
matrix.ID = j["id"]; cout << "<div class=\"container\">" << endl;