diff --git a/Hazard/Hazard/CellCollection.cpp b/Hazard/Hazard/CellCollection.cpp new file mode 100644 index 0000000..8f38a1e --- /dev/null +++ b/Hazard/Hazard/CellCollection.cpp @@ -0,0 +1,89 @@ +#include "stdafx.h" +#include +#include +#include "Cell.h" +#include "CellCollection.h" + +using namespace std; + +extern uint dimension; +extern uint numElements; + +void CellCollection::add(Cell* &cell) +{ + this->cells.push_back(cell); +} + +void CellCollection::init(PrimImplikantCollection* &globalPIC) +{ + this->globalPIC = globalPIC; + this->cells.resize(numElements); + + for (uint i = 0; i < numElements; i++) + this->cells[i] = new Cell(i, globalPIC); +} + +void CellCollection::findHazards() +{ + for (uint i = 0; i < numElements; i += 2) // we only need to check every 2nd as long as it's gray + { + uint I = i ^ (i/2); // transform to gray code --> Schachbrettmuster + Cell* currentCell = cells[I]; // this is the cell we are currently checking + + if (currentCell->value == false) // no hazard can occur + continue; + + cout << " Checking cell " << I << endl; + vector* hazardousNeighbors = currentCell->getHazards(this->cells); + + if (hazardousNeighbors->size() == 0) // no hazard found + { + delete hazardousNeighbors; + continue; + } + + for (vector::iterator nc = hazardousNeighbors->begin(); nc < hazardousNeighbors->end(); nc++) + { + printf("Hazard found! Cell %d <--> Cell %d\n", I, (*nc)->index); + this->globalPIC->add(I, (*nc)->index); // add PI that solves hazard. Not quite smart... + (*nc)->refresh(this->globalPIC); // refresh the foreign PIC (a PI was added) + } + currentCell->refresh(this->globalPIC); // refresh the current PIC (a PI was added) + delete hazardousNeighbors; + } +} + + + +void CellCollection::Dispose() +{ + for (uint i = 0; i < this->cells.size(); i++) + delete cells[i]; +} + + + +uint CellCollection::size() +{ + return cells.size(); +} + +Cell* CellCollection::back() +{ + return cells.back(); +} + +Cell* CellCollection::front() +{ + return cells.front(); +} + +Cell* CellCollection::at(uint index) +{ + return cells.at(index); +} + +Cell * CellCollection::operator[](uint &index) +{ + return cells.at(index); +} \ No newline at end of file diff --git a/Hazard/Hazard/CellCollection.h b/Hazard/Hazard/CellCollection.h new file mode 100644 index 0000000..26f5259 --- /dev/null +++ b/Hazard/Hazard/CellCollection.h @@ -0,0 +1,37 @@ +#pragma once + +#include "stdafx.h" +#include "Cell.h" +#include "CellCollection.h" + +using namespace std; + +#ifndef _CELLCOLLEC_H +#define _CELLCOLLEC_H + + +class CellCollection { +public: + void add(Cell* &cell); + void init(PrimImplikantCollection* &globalPIC); + void findHazards(); + + void Dispose(); + + uint size(); + Cell* back(); + Cell* front(); + Cell* at(uint index); + Cell* operator[](uint &index); + + CellCollection(PrimImplikantCollection* &globalPIC) + { + this->init(globalPIC); + }; + + + vector cells; + PrimImplikantCollection* globalPIC; +}; + +#endif \ No newline at end of file diff --git a/Hazard/Hazard/Hazard.cpp b/Hazard/Hazard/Hazard.cpp index 1383546..8d6eed3 100644 --- a/Hazard/Hazard/Hazard.cpp +++ b/Hazard/Hazard/Hazard.cpp @@ -1,6 +1,3 @@ -// Hazard.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung. -// - #include "stdafx.h" #include #include @@ -9,6 +6,7 @@ #include "PrimImplikant.h" #include "PrimImplikantCollection.h" #include "Cell.h" +#include "CellCollection.h" #include "Wertetabelle.h" #include "KV.h" @@ -23,124 +21,89 @@ void user_main(void) FILE * input; FILE * error; FILE * list; - fopen_s(&input, "res\\input.txt", "r"); - if (input == 0) + if (open_files(input, error, list) != 0) { - cout << "Fehler Inputdatei"; - system("pause"); - return; - } - fopen_s(&error, "res\\errorParser.txt", "a"); - if (error == 0) - { - cout << "Fehler Fehlerdatei"; - system("pause"); - return; - } - fopen_s(&list, "res\\listParser.txt", "w"); - if (list == 0) - { - cout << "Fehler Listdatei"; - system("pause"); + pause(); return; } PrimImplikantCollection* globalPIC = new PrimImplikantCollection(); vector* variables = new vector(); - CParser parser; - parser.IP_init_token_table(); - parser.InitParse(input, error, list); - if (parser.yyparse(globalPIC, variables) != 0) - { - system("pause"); - return; - } + CParser* parser = new CParser(input, error, list); + bool parseFailure = parser->yyparse(globalPIC, variables) != 0; + fclose(input); fclose(error); fclose(list); - //system("pause"); - - /*pic.add(7); - pic.add("0x1"); - pic.add("100"); - pic.add("00x"); - pic.add(4); + delete parser; - PrimImplikant prim7(7); - PrimImplikant prim13("0x1"); - PrimImplikant prim4("100"); - PrimImplikant prim4567("1xx"); - pic.add(prim4567);*/ - - /*for (int p = 0; p < numElements; p++) + if (parseFailure) { - //printf("Pos %d: prim7=%d, prim13=%d, prim4=%d, prim4567=%d, pic=%d\n", p, prim7.valueAt(p), prim13.valueAt(p), prim4.valueAt(p), prim4567.valueAt(p), pic.valueAt(p)); - printf("Pos %d: Matching collections: ", p); - PrimImplikantCollection matchingPIs = globalPIC->primImplikantenAt(p); - for (int i = 0; i < matchingPIs.size(); i++) - //cout << i->name < ", "; - printf("%s, ", matchingPIs[i]->name.c_str()); - cout << endl; - }*/ - + pause(); + return; + } // initialize Cells - vector cells; - cells.resize(numElements); - uint numOnes = 0; - for (uint i = 0; i < numElements; i++) - { - cells[i] = new Cell(i, globalPIC); - } + CellCollection* allCells = new CellCollection(globalPIC); - Wertetabelle* wt = new Wertetabelle(&cells, variables); + // print Wertetabelle and KV of imported data + Wertetabelle* wt = new Wertetabelle(allCells, variables); wt->Print(); - delete wt; - KV* kv = new KV(globalPIC, cells, 30); + KV* kv = new KV(globalPIC, allCells, 30); kv->Print(30); //system("pause"); - // find hazards - for (uint i = 0; i < numElements; i += 2) - { - uint grayI = i ^ (i/2); // transform to gray code --> Schachbrettmuster - Cell* currentCell = cells[grayI]; // this is the cell we are currently checking - - if (currentCell->value == false) // no hazard can occur - continue; - - cout << " Checking cell " << grayI << endl; - vector* hazardousNeighbors = currentCell->getHazards(cells); - - if (hazardousNeighbors->size() == 0) // no hazard found - { - delete hazardousNeighbors; - continue; - } - - for (vector::iterator c = hazardousNeighbors->begin(); c < hazardousNeighbors->end(); c++) - { - printf("Hazard found! Cell %d <--> Cell %d\n", grayI, (*c)->index); - globalPIC->add(grayI, (*c)->index); // add PI that solves hazard. Not quite smart... - (*c)->refresh(globalPIC); // refresh the local PIC (a PI was added) - } - currentCell->refresh(globalPIC); - delete hazardousNeighbors; - } + // find and solve Hazards + allCells->findHazards(); //system("pause"); - wt = new Wertetabelle(&cells, variables); + + // print Wertetabelle and KV of corrected data wt->Print(); delete wt; - kv->Print(30*2 + kv->width(), 30); + kv->Print(30 + kv->width() + 30, 30); // Diagramm neben dem vorherigen delete kv; globalPIC->Dispose(); - //system("pause"); + allCells->Dispose(); + delete globalPIC; + delete allCells; + + pause(); return; +} + + +int open_files(FILE * &input, FILE * &error, FILE * &list) +{ + fopen_s(&input, "res\\input.txt", "r"); + if (input == 0) + { + cout << "Fehler Inputdatei"; + return 1; + } + fopen_s(&error, "res\\errorParser.txt", "a"); + if (error == 0) + { + cout << "Fehler Fehlerdatei"; + return 1; + } + fopen_s(&list, "res\\listParser.txt", "w"); + if (list == 0) + { + cout << "Fehler Listdatei"; + return 1; + } +} + +void pause() +{ +#if DEBUG + system("pause"); +#endif } \ No newline at end of file diff --git a/Hazard/Hazard/Hazard.vcxproj b/Hazard/Hazard/Hazard.vcxproj index 604a595..c4bdb50 100644 --- a/Hazard/Hazard/Hazard.vcxproj +++ b/Hazard/Hazard/Hazard.vcxproj @@ -19,7 +19,7 @@ Application true - v100 + v110 Unicode @@ -81,6 +81,7 @@ + @@ -90,6 +91,7 @@ + diff --git a/Hazard/Hazard/Hazard.vcxproj.filters b/Hazard/Hazard/Hazard.vcxproj.filters index 250ea2b..1ead07a 100644 --- a/Hazard/Hazard/Hazard.vcxproj.filters +++ b/Hazard/Hazard/Hazard.vcxproj.filters @@ -39,6 +39,9 @@ Headerdateien + + Headerdateien + @@ -62,6 +65,9 @@ Quelldateien + + Quelldateien + diff --git a/Hazard/Hazard/KV.h b/Hazard/Hazard/KV.h index 45caab9..4015502 100644 --- a/Hazard/Hazard/KV.h +++ b/Hazard/Hazard/KV.h @@ -3,6 +3,7 @@ #include #include #include "Cell.h" +#include "CellCollection.h" #include "PrimImplikantCollection.h" extern uint dimension; @@ -18,7 +19,7 @@ public: uint height(); // Gibt die Höhe eines KV-Diagramms zurück (heightPx) // Konstruktor - KV(PrimImplikantCollection * globalPic, vector allCells, uint size) + KV(PrimImplikantCollection * globalPic, CellCollection allCells, uint size) : edgeLength(size), numVarX(((uint)floor(dimension/2.0f))), numVarY(((uint)ceil(dimension/2.0f))), numFieldX((uint)pow(2,(float)numVarX)), numFieldY((uint)pow(2,(float)numVarY)), @@ -31,7 +32,7 @@ public: private: PrimImplikantCollection* globalPic; - vector allCells; + CellCollection allCells; uint offsetX; // Der freie Platz nach links in Pixeln uint offsetY; // Der freie Platz nach rechts in Pixeln @@ -53,7 +54,8 @@ private: void Line(uint x1, uint y1, uint x2, uint y2, int color); // Zeichnet eine Linie mit Offset void Text(uint x, uint y, uint size, int color, int bkcolor, int angle, int align, char* theText); // Zeichnet einen Text mit Offset void KV::TextBox(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset - void KV::TextBoxBold(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset und fetter Schrift + void KV::TextBoxBold(uint x1, uint y1, uint x2, uint y2, uint size, int ctext, int cframe, int cfill, int flags, char* theText); // Zeichnet eine TextBox mit Offset und fetter Schrift + void KV::Rechteck(uint x1, uint y1, uint x2, uint y2, int cframe, int cfill); // Zeichnet ein Rechteck mit Offset und fetter Schrift char* Binary(uint x, char length); // Wie itoa(x, char[], 2), allerdings mit fester Breite }; diff --git a/Hazard/Hazard/Wertetabelle.h b/Hazard/Hazard/Wertetabelle.h index d9feeb9..e13b54a 100644 --- a/Hazard/Hazard/Wertetabelle.h +++ b/Hazard/Hazard/Wertetabelle.h @@ -2,9 +2,9 @@ #include #include #include -#include "Cell.h" #include "PrimImplikant.h" #include "Cell.h" +#include "CellCollection.h" using namespace std; @@ -16,7 +16,7 @@ class Wertetabelle public: void Print(); - Wertetabelle(vector* cells, vector* variables) + Wertetabelle(CellCollection* cells, vector* variables) { this->cells = cells; this->variables = variables; @@ -28,7 +28,7 @@ private: void printI(unsigned int i); void printPrimImplikanten(unsigned int i); - vector* cells; + CellCollection* cells; vector* variables; vector padding; uint width;