From d956946ae39f3e33faf81b2c55288d5cac359ba9 Mon Sep 17 00:00:00 2001 From: Diego Reis Date: Fri, 2 Dec 2022 20:39:32 -0300 Subject: [PATCH] adding console colors --- 4c.txt | 23 +++++++----- 4d.txt | 22 ++++++----- color.cpp | 48 ----------------------- src/NFA_Machine.cpp | 13 ++++--- src/NFA_Printer.cpp | 69 ++++++++++++++++++++++------------ src/NFA_Printer.hpp | 2 + src/Utils/ConsoleFormatter.cpp | 29 ++++++++++++++ src/Utils/ConsoleFormatter.hpp | 11 ++++++ src/Utils/NaryTree.hpp | 16 +++----- src/main.cpp | 1 + 10 files changed, 125 insertions(+), 109 deletions(-) delete mode 100644 color.cpp create mode 100644 src/Utils/ConsoleFormatter.cpp create mode 100644 src/Utils/ConsoleFormatter.hpp diff --git a/4c.txt b/4c.txt index a57f7c6..3a00b88 100644 --- a/4c.txt +++ b/4c.txt @@ -1,12 +1,15 @@ -alfabeto=0,1 -estados=q0,q1,q2,q3 +alfabeto=a,b +estados=q0,q1,q2,q3,q4 inicial=q0 -final=q3 +final=q1,q4 transicoes -q0,q0,0 -q0,q0,1 -q0,q1,1 -q1,q2,1 -q2,q3,0 -q3,q3,1 -q3,q3,0 +q0,q1,epsilon +q0,q3,epsilon +q1,q1,b +q1,q2,a +q2,q1,a +q2,q2,b +q3,q3,a +q3,q4,b +q4,q4,a +q4,q3,b diff --git a/4d.txt b/4d.txt index a57f7c6..01a3876 100644 --- a/4d.txt +++ b/4d.txt @@ -1,12 +1,14 @@ -alfabeto=0,1 -estados=q0,q1,q2,q3 +alfabeto=a,b +estados=q0,q1,q2,q3,q4,q5,q6,q7 inicial=q0 -final=q3 +final=q0,q7 transicoes -q0,q0,0 -q0,q0,1 -q0,q1,1 -q1,q2,1 -q2,q3,0 -q3,q3,1 -q3,q3,0 +q0,q1,epsilon +q1,q2,epsilon +q1,q6,epsilon +q2,q3,a +q3,q4,epsilon +q4,q5,b +q5,q1,epsilon +q6,q7,a +q7,q1,epsilon \ No newline at end of file diff --git a/color.cpp b/color.cpp deleted file mode 100644 index 70c2837..0000000 --- a/color.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include - -using namespace std; - -#define RED_CONSOLE_COLOR 4 -#define GREEN_CONSOLE_COLOR 2 -#define WHITE_CONSOLE_COLOR 15 - -#define IS_ON_WINDOWS defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) - -#if IS_ON_WINDOWS -#include // Used to change color of text -void SetConsoleColor_Windows(int color) -{ - - HANDLE console_color = GetStdHandle(STD_OUTPUT_HANDLE); - SetConsoleTextAttribute(console_color, color); -} -#endif - -void SetConsoleColor(int color) -{ -#if IS_ON_WINDOWS - SetConsoleColor_Windows(color); -#endif -} - -void ResetConsoleColor() -{ - SetConsoleColor(WHITE_CONSOLE_COLOR); -} - -// Driver Code -int main() -{ - SetConsoleColor(RED_CONSOLE_COLOR); - cout << " Hello Geeks, " << "good night!!!"; - - SetConsoleColor(WHITE_CONSOLE_COLOR); - cout << " Hello Geeks, " << "good night!!!"; - - SetConsoleColor(GREEN_CONSOLE_COLOR); - cout << " Hello Geeks, " << "good night!!!"; - - ResetConsoleColor(); - - return 0; -} \ No newline at end of file diff --git a/src/NFA_Machine.cpp b/src/NFA_Machine.cpp index 3e9f8d0..31a2b31 100644 --- a/src/NFA_Machine.cpp +++ b/src/NFA_Machine.cpp @@ -62,25 +62,25 @@ std::string NFA_Machine::ToString() { std::string s = ""; - s += "\n-----Alphabet-----\n"; + s += "\n\n-----Alphabet-----\n"; for(int i = 0; i < alphabet->Length(); i++) s += alphabet->At(i)->GetValue() + " "; - s += "\n-----States-----\n"; + s += "\n\n-----States-----\n"; for(int i = 0; i < states->Length(); i++) s += states->At(i)->GetName() + " "; - s += "\n-----Initial state-----\n"; + s += "\n\n-----Initial state-----\n"; s += initialState->GetName() + " "; - s += "\n-----End States-----\n"; + s += "\n\n-----End States-----\n"; for(int i = 0; i < states->Length(); i++) { if(states->At(i)->IsAFinalState()) s += states->At(i)->GetName() + " "; } - s += "\n-----Transitions-----\n"; + s += "\n\n-----Transitions-----\n"; for(int i = 0; i < states->Length(); i++) { s += states->At(i)->GetTransitionsStr(); @@ -200,6 +200,7 @@ void NFA_Machine::ProcessEpsilon(int iterationIndex, NaryTree* proc // add new state at the same level as the parent node in the processment tree Transition* _chainProcessed = new Transition(_states.At(j), epsilon); processmentTree->AddLeaf(_chainProcessed, node, node->GetHeight()); + current.Push(processmentTree->GetLastAdded()); // allow new state to be processed by epsilon too } } } @@ -219,7 +220,7 @@ NaryTree* NFA_Machine::StartProcessment(NFA_Machine* machine, MyLis // Processing epsilon before initiate all symbols processing machine->ProcessEpsilon(firstEpsilonProcessingHeight, processmentTree); curInteration++; - + // Processing all chain symbols for (int i = 0; i < chain->Length(); i++) { diff --git a/src/NFA_Printer.cpp b/src/NFA_Printer.cpp index 6e9e652..0015f98 100644 --- a/src/NFA_Printer.cpp +++ b/src/NFA_Printer.cpp @@ -1,5 +1,7 @@ #include "NFA_Printer.hpp" +using namespace ConsoleFormatter; + void NFA_Printer::PrintProcessmentTree(NaryTree* processmentTree) { // Get all crashed chains and all chain at the deepest level (bigger height value) @@ -7,46 +9,63 @@ void NFA_Printer::PrintProcessmentTree(NaryTree* processmentTree) crashedsChains = processmentTree->GetWithHeight(NFA_Machine::CRASH_STATE_HEIGHT); finishedsChains = processmentTree->GetWithHeight(processmentTree->GetMaxHeight()); + std::cout << "-----Processment-----"; PrintProcessmentList(crashedsChains); PrintProcessmentList(finishedsChains); } void NFA_Printer::PrintProcessmentList(MyList*> node) { - AlphabetSymbol* symbol; + AlphabetSymbol* symbol; + + for(int i = 0; i < node.Length(); i++) + { + NaryTree_Node* s = node.At(i); + MyList chainList = MyList(); - for(int i = 0; i < node.Length(); i++) + // Getting all processing chain of the last state + // Iterate from bottom to top + while(s != nullptr) { - NaryTree_Node* s = node.At(i); - MyList chainList = MyList(); + chainList.Push(s->GetContent()); + s = s->GetParent(); + } - while(s != nullptr) - { - chainList.Push(s->GetContent()); - s = s->GetParent(); - } + bool chainIsAccepted = chainList.At(0)->GetDestinationState()->IsAFinalState(); - Transition* _transition = chainList.GetLast(); - std::cout << _transition->GetDestinationState()->GetName(); + // Make text green case chain is accepted, red otherwise + UpdateConsoleColor(chainIsAccepted); - for(int j = chainList.Length()-2; j >= 0; j--) - { - Transition* _transition = chainList.At(j); + Transition* _transition = chainList.GetLast(); - symbol = _transition->GetTransitionSymbol(); + // Print initial state + std::cout << std::endl << _transition->GetDestinationState()->GetName(); - if(symbol != nullptr) - std::cout << " -> " << symbol->GetValue() << " -> " ; + for(int j = chainList.Length()-2; j >= 0; j--) + { + Transition* _transition = chainList.At(j); - std::cout << _transition->GetDestinationState()->GetName(); - } + symbol = _transition->GetTransitionSymbol(); - // Print the status of the last state in the chain - if(chainList.At(0)->GetDestinationState()->IsAFinalState()) - std::cout << " " << NFA_Printer::CHAIN_IS_ACCEPTED_SYMBOL; - else - std::cout << " " << NFA_Printer::CHAIN_IS_NOT_ACCEPTED_SYMBOL; + if(symbol != nullptr) + std::cout << " -> " << symbol->GetValue() << " -> " ; - std::cout << "\n"; + std::cout << _transition->GetDestinationState()->GetName(); } + + if(chainIsAccepted) + std::cout << " " << NFA_Printer::CHAIN_IS_ACCEPTED_SYMBOL; + else + std::cout << " " << NFA_Printer::CHAIN_IS_NOT_ACCEPTED_SYMBOL; + + ConsoleFormatter::ResetConsoleColor(); + } +} + +void NFA_Printer::UpdateConsoleColor(bool chainIsAccepted) +{ + int consoleColor = chainIsAccepted ? ConsoleFormatter::GREEN_CONSOLE_COLOR + : ConsoleFormatter::RED_CONSOLE_COLOR; + + ConsoleFormatter::SetConsoleColor(consoleColor); } \ No newline at end of file diff --git a/src/NFA_Printer.hpp b/src/NFA_Printer.hpp index c5f437e..dfe28f6 100644 --- a/src/NFA_Printer.hpp +++ b/src/NFA_Printer.hpp @@ -3,6 +3,7 @@ #include #include "Transition.hpp" #include "Utils/NaryTree.hpp" +#include "Utils/ConsoleFormatter.hpp" #include "NFA_Machine.hpp" namespace NFA_Printer @@ -14,4 +15,5 @@ namespace NFA_Printer void PrintProcessmentTree(NaryTree* processmentTree); void PrintProcessmentList(MyList*> node); + void UpdateConsoleColor(bool chainIsAccepted); // Make text green case chain is accepted, red otherwise } \ No newline at end of file diff --git a/src/Utils/ConsoleFormatter.cpp b/src/Utils/ConsoleFormatter.cpp new file mode 100644 index 0000000..e577e20 --- /dev/null +++ b/src/Utils/ConsoleFormatter.cpp @@ -0,0 +1,29 @@ +#include "ConsoleFormatter.hpp" + +#define IS_ON_WINDOWS defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) + +#if IS_ON_WINDOWS + #include // Used to change color of text +#endif + +void ConsoleFormatter::SetConsoleColor(int color) +{ +#if IS_ON_WINDOWS + HANDLE console_color = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(console_color, color); + //SetConsoleColor_Windows(color); +#endif +} + +void ConsoleFormatter::ResetConsoleColor() +{ + SetConsoleColor(WHITE_CONSOLE_COLOR); +} + +// void ConsoleFormatter::SetConsoleColor_Windows(int color) +// { +// #if IS_ON_WINDOWS +// HANDLE console_color = GetStdHandle(STD_OUTPUT_HANDLE); +// SetConsoleTextAttribute(console_color, color); +// #endif +// } \ No newline at end of file diff --git a/src/Utils/ConsoleFormatter.hpp b/src/Utils/ConsoleFormatter.hpp new file mode 100644 index 0000000..fa629ec --- /dev/null +++ b/src/Utils/ConsoleFormatter.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace ConsoleFormatter +{ + const int RED_CONSOLE_COLOR = 12; + const int GREEN_CONSOLE_COLOR = 10; + const int WHITE_CONSOLE_COLOR = 15; + + void SetConsoleColor(int color); + void ResetConsoleColor(); +} \ No newline at end of file diff --git a/src/Utils/NaryTree.hpp b/src/Utils/NaryTree.hpp index 3083c28..70be531 100644 --- a/src/Utils/NaryTree.hpp +++ b/src/Utils/NaryTree.hpp @@ -10,9 +10,11 @@ class NaryTree MyList*> children; public: NaryTree(); + MyList*> GetWithHeight(int height); + NaryTree_Node* GetLastAdded(); int GetMaxHeight(); - void AddLeaf(T content, NaryTree_Node* parent); + void AddLeaf(T content, NaryTree_Node* parent, int height); }; @@ -39,9 +41,8 @@ MyList*> NaryTree::GetWithHeight(int height) } template -void NaryTree::AddLeaf(T content, NaryTree_Node* parent) +void NaryTree::AddLeaf(T content, NaryTree_Node* parent, int height) { - int height = parent == nullptr ? 0 : parent->GetHeight() +1; NaryTree_Node* node = new NaryTree_Node(content, parent, height); if(height > maxHeight) @@ -51,14 +52,9 @@ void NaryTree::AddLeaf(T content, NaryTree_Node* parent) } template -void NaryTree::AddLeaf(T content, NaryTree_Node* parent, int height) +NaryTree_Node* NaryTree::GetLastAdded() { - NaryTree_Node* node = new NaryTree_Node(content, parent, height); - - if(height > maxHeight) - maxHeight = height; - - children.Push(node); + return children.GetLast(); } template diff --git a/src/main.cpp b/src/main.cpp index e2115a5..ff692ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,7 @@ int main() NFA_Machine* machine = new NFA_Machine(dataReadedFromFile); NaryTree* processmentTree = machine->StartProcessment(machine, chainToProcess); + cout << machine->ToString(); NFA_Printer::PrintProcessmentTree(processmentTree); } catch(FileNotFoundException e)