Skip to content

Commit

Permalink
adding console colors
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolrs committed Dec 2, 2022
1 parent 544e70e commit d956946
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 109 deletions.
23 changes: 13 additions & 10 deletions 4c.txt
Original file line number Diff line number Diff line change
@@ -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
22 changes: 12 additions & 10 deletions 4d.txt
Original file line number Diff line number Diff line change
@@ -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
48 changes: 0 additions & 48 deletions color.cpp

This file was deleted.

13 changes: 7 additions & 6 deletions src/NFA_Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -200,6 +200,7 @@ void NFA_Machine::ProcessEpsilon(int iterationIndex, NaryTree<Transition*>* 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
}
}
}
Expand All @@ -219,7 +220,7 @@ NaryTree<Transition*>* 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++)
{
Expand Down
69 changes: 44 additions & 25 deletions src/NFA_Printer.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
#include "NFA_Printer.hpp"

using namespace ConsoleFormatter;

void NFA_Printer::PrintProcessmentTree(NaryTree<Transition*>* processmentTree)
{
// Get all crashed chains and all chain at the deepest level (bigger height value)
MyList<NaryTree_Node<Transition*>*> crashedsChains, finishedsChains;
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<NaryTree_Node<Transition*>*> node)
{
AlphabetSymbol* symbol;
AlphabetSymbol* symbol;

for(int i = 0; i < node.Length(); i++)
{
NaryTree_Node<Transition*>* s = node.At(i);
MyList<Transition*> chainList = MyList<Transition*>();

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<Transition*>* s = node.At(i);
MyList<Transition*> chainList = MyList<Transition*>();
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);
}
2 changes: 2 additions & 0 deletions src/NFA_Printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include "Transition.hpp"
#include "Utils/NaryTree.hpp"
#include "Utils/ConsoleFormatter.hpp"
#include "NFA_Machine.hpp"

namespace NFA_Printer
Expand All @@ -14,4 +15,5 @@ namespace NFA_Printer

void PrintProcessmentTree(NaryTree<Transition*>* processmentTree);
void PrintProcessmentList(MyList<NaryTree_Node<Transition*>*> node);
void UpdateConsoleColor(bool chainIsAccepted); // Make text green case chain is accepted, red otherwise
}
29 changes: 29 additions & 0 deletions src/Utils/ConsoleFormatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "ConsoleFormatter.hpp"

#define IS_ON_WINDOWS defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)

#if IS_ON_WINDOWS
#include <windows.h> // 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
// }
11 changes: 11 additions & 0 deletions src/Utils/ConsoleFormatter.hpp
Original file line number Diff line number Diff line change
@@ -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();
}
16 changes: 6 additions & 10 deletions src/Utils/NaryTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class NaryTree
MyList<NaryTree_Node<T>*> children;
public:
NaryTree<T>();

MyList<NaryTree_Node<T>*> GetWithHeight(int height);
NaryTree_Node<T>* GetLastAdded();
int GetMaxHeight();
void AddLeaf(T content, NaryTree_Node<T>* parent);

void AddLeaf(T content, NaryTree_Node<T>* parent, int height);
};

Expand All @@ -39,9 +41,8 @@ MyList<NaryTree_Node<T>*> NaryTree<T>::GetWithHeight(int height)
}

template <typename T>
void NaryTree<T>::AddLeaf(T content, NaryTree_Node<T>* parent)
void NaryTree<T>::AddLeaf(T content, NaryTree_Node<T>* parent, int height)
{
int height = parent == nullptr ? 0 : parent->GetHeight() +1;
NaryTree_Node<T>* node = new NaryTree_Node<T>(content, parent, height);

if(height > maxHeight)
Expand All @@ -51,14 +52,9 @@ void NaryTree<T>::AddLeaf(T content, NaryTree_Node<T>* parent)
}

template <typename T>
void NaryTree<T>::AddLeaf(T content, NaryTree_Node<T>* parent, int height)
NaryTree_Node<T>* NaryTree<T>::GetLastAdded()
{
NaryTree_Node<T>* node = new NaryTree_Node<T>(content, parent, height);

if(height > maxHeight)
maxHeight = height;

children.Push(node);
return children.GetLast();
}

template <typename T>
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int main()
NFA_Machine* machine = new NFA_Machine(dataReadedFromFile);
NaryTree<Transition*>* processmentTree = machine->StartProcessment(machine, chainToProcess);

cout << machine->ToString();
NFA_Printer::PrintProcessmentTree(processmentTree);
}
catch(FileNotFoundException e)
Expand Down

0 comments on commit d956946

Please sign in to comment.