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)
set(CMAKE_CXX_STANDARD 20)
include_directories(E:/IT/Tools/)
#nicht cmake benutzen!
add_executable(matrix
show.cpp
list.cpp
delete.cpp
multi.cpp
generate.cpp
Matrix.cpp
Matrix.hpp)
\ No newline at end of file
//
// Created by Mario on 05/05/2020.
//
#include "Matrix.hpp"
bool exists(string filename){
......@@ -9,39 +5,89 @@ bool exists(string filename){
return f.good();
}
void Matrix::printtocnsl(){
// cout << "<p>";
for (int i = 0; i < this->dim; i++) {
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++){
cout << "<p>";
for (int j = 0; j < this->dim; j++)
for(int j = 0; j < this->dim; j++){
cout << (*this)(i,j) << " ";
}
cout << "</p>";
}
// cout << "</p>";
}
void Matrix::generate(int dims) {
srand(time(NULL));
// Matrix matrix;
this->dim = dims;
this->data = new int[dims * dims];
int Matrix::generateMatrix(int dimension){
json j;
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->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;
testpath = folderpath + to_string(this->ID);
}
json j;
folderpath += to_string(this->ID) + ".json";
j["id"] = this->ID;
j["dim"] = this->dim;
vector<int> vector1(this->data, this->data + (this->dim * this->dim));
j["data"] = vector1;
ofstream outFileGen;
outFileGen.open(to_string(this->ID)+".json");
outFileGen << j << endl;
outFileGen.close();
cout << "File generiert\n";
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;
}
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
//
// Created by Mario on 05/05/2020.
//
#ifndef ESE_ZEITLHOFER_BAJER_MATRIX_H
#define ESE_ZEITLHOFER_BAJER_MATRIX_H
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <cstdio>
#include <cstdlib>
#include <nlohmann/json.hpp>
#include <cgicc/CgiDefs.h>
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>
#include <json.hpp>
using namespace std;
using namespace cgicc;
using json = nlohmann::json;
class Matrix {
class Matrix{
public:
Matrix(int dim);
int ID;
int dim;
int *data;
void generate(int dims);
void printtocnsl();
int generateMatrix(int dimension);
void print();
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 {
return data[dim * row + col];
return data[dim*row + col];
}
Matrix operator+(Matrix b);
Matrix operator-(Matrix b);
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"
int main() {
Matrix matrix;
int main(){
Matrix matrix(2);
Cgicc formData;
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>generate.cgi</title>" << endl;
cout << "</head>" << endl;
matrix.generate(10);
htmlHeader("generate.cgi");
cout << "<body>\n";
cout << "<div class=\"container\">" << endl;
form_iterator fi = formData.getElement("dimension");
if( !fi->isEmpty() && fi != (*formData).end()) {
matrix.generate(stoi(**fi));
}else {
cout << "Wrong input" << endl;
//nur vielfache von 2 erlaubt
int input = stoi(**fi);
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>" << endl;
cout << "</div>";
cout << "<br/>\n";
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "</body>\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++
CFLAGS=-Wall -std=c++17
LDLIBS=-lcgicc
all: show generate
all: show generate list delete multi
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:
$(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:
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"
int main(){
Matrix matrix;
Cgicc formContent;
Cgicc formData;
// cout << "Content-type:text/html\r\n\r\n";
// cout << "<html>\n";
// cout << "<head>\n";
// 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";
htmlHeader("show.cgi");
cout << "<body>\n";
cout << "<br>" << endl;
string folderpath = "";
form_iterator fi = formContent.getElement("ID");
if( !fi->isEmpty() && fi != (*formContent).end()) {
form_iterator fi = formData.getElement("showBtn");
if( !fi->isEmpty() && fi != (*formData).end()) {
folderpath = **fi;
}
else {
......@@ -33,16 +17,18 @@ int main(){
}
json j;
ifstream inFile("48.json");
inFile >> j;
matrix.ID = j["id"];
matrix.dim = j["dim"];
vector<int> vector2 = j["data"];
matrix.data = &vector2[0];
matrix.printtocnsl();
ifstream in(folderpath);
in >> j;
cout << "<div class=\"container\">" << endl;
cout << "<a href=\"http://localhost:80/index.html\">Back to Index</a>";
cout << "<p style=\"word-wrap: break-word\">" << j << "</p>" << endl;
// Matrix matrix;
// vector<int> v = j["matrix"];
// matrix.ID = j["id"];
// matrix.dim = j["dimension"];
// matrix.data = &v[0];
// matrix.print();
cout << "<a href=\"http://localhost:80/index.html\">Back to Start</a>";
cout << "</div>" << endl;
cout << "</body>\n";
cout << "</html>\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment