Atouray
asked on
How to print different colours for the different levels of a binary tree
I got this code which sets all the text to one colour. I want to use it to set different colours at different levels of the tree.
Can anyone help me out on how to do that.
Thanks
Can anyone help me out on how to do that.
Thanks
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c){
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
Check if you understand this code.
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hCon = NULL;
void SetColor(int c)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
int main()
{
for (int i = 0; i < 5; i++)
{
SetColor(i);
cout << "Hello" << endl;
}
}
screenshot.JPG
ASKER
I tried this but, it prints the whole tree more than ones while different colours. I only need the levels of the tree to have different colours for exmple in the tree structure below.
Thanks
Thanks
-
/--------------\ this leve should be red
/ 9
/------\ this level should be yellow
* 7
/---\ this level should be blue
4 +
/-\ this level should be green
A B
A = 10
B = 15
so call SetColor each time you print new data - each line in your case
ASKER
this function diplay the Tree structure:Display(Node_tre e)
and it is called in my main, so now in my main, i call the SetColor() like this
for (int i = 0; i < 5; i++)
{
SetColor(i);
cout << Display(Node_tree) << " ";
}
but this prints the whole tree more then ones and each time it prints the whole tree, it give it a different color. So how can i modify this to give different colour to the levels only and the rest unchanged?
tHANKS
and it is called in my main, so now in my main, i call the SetColor() like this
for (int i = 0; i < 5; i++)
{
SetColor(i);
cout << Display(Node_tree) << " ";
}
but this prints the whole tree more then ones and each time it prints the whole tree, it give it a different color. So how can i modify this to give different colour to the levels only and the rest unchanged?
tHANKS
Because you need to call SetColor from Display. And you don't need a loop here.
Show the Display function
Show the Display function
ASKER
Below is my display function
void Display(node * n)
{
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
std::cout << (*lin) << std::endl;
std::cout << nextConstant(""); // get all constants if any
}
so each time before std::cout you need to set a color. Everywhere in your program.
ASKER
Everywhere in my program, not only in the diplay function, right?
I should set it like this:
for example in the Display function
void Display(node * n)
{
int i=1;
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
std::cout << nextConstant(""); // get all constants if any
}
SetColor(i);
Did i set it correctly above?
I should set it like this:
for example in the Display function
void Display(node * n)
{
int i=1;
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
std::cout << nextConstant(""); // get all constants if any
}
SetColor(i);
Did i set it correctly above?
you got the idea.
but this code is wrong:
for (lin=all.begin(); lin != all.end(); ++lin)
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
it should be:
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
}
but this code is wrong:
for (lin=all.begin(); lin != all.end(); ++lin)
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
it should be:
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor(i); // here, right?
std::cout << (*lin) << std::endl;
}
ASKER
so i have to do these in all the places where there is : std::cout << (*lin) << std::endl; OR only where there is std::cout ?
Should this be Done in all the places of my code?
Should this be Done in all the places of my code?
ASKER
I did what you said, and actyally i have std::cout only in my display fun. So when i run i got the results shown. It makes all the displayed intem blue colour. I only need the color changes on the tree alone not the rest of the text. I am displaying all my texts at once. How can i have only the tree to have different clor at different level
Thanks
screenShot.JPG
Thanks
screenShot.JPG
Ok. You need to continue. Check your code. Try. Test.
Try to modify your code accordingly to this small program.
You see SetColor function now does not have parameters and all line have different colors
You see SetColor function now does not have parameters and all line have different colors
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hCon = NULL;
int color = 2;
void SetColor()
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, color++);
if (color > 12)
color = 2;
}
int main()
{
for (int i = 0; i < 15; i++)
{
SetColor();
cout << "Hello" << endl;
}
}
ASKER
I did the changes and it is now printing different color for each level, but it only does it for when the level is not more than two, but when the level is more then two, it print colour to the rest of the texts which are not trees. I don't want it that way. the rest of the tree should not have color. Only the different levels of the tree should have color.
The tree can have more than two levels, so no matter how long the level is the color should only be on the tree and different levels should have different color and the rest of the texts which are not tree, should remain unchanged. See the screen shot, the texts which reads TEST, should all be white, they shuld have have color.
Thanks
screenResults.JPG
The tree can have more than two levels, so no matter how long the level is the color should only be on the tree and different levels should have different color and the rest of the texts which are not tree, should remain unchanged. See the screen shot, the texts which reads TEST, should all be white, they shuld have have color.
Thanks
screenResults.JPG
ASKER
Also, can the values in the left and right not have color. Only the lines /--------------\ in the tree should have color at different level with different colors.
ASKER
Can pgnatyuk help me out with the issue on this ID: 26214714 and this ID: 26214742 please, Or any experts please.
ASKER
Is anybody here to help me on this Issue?
I do not see your code. You need to check your program and find all places with std::cout.
If you use the latest SetColor function (without parameters), you need to check that before each cout there is SetColor call.
If you use the latest SetColor function (without parameters), you need to check that before each cout there is SetColor call.
ASKER
I have only one std::cout, which is in my diplay function i posted here. and i did what you said on that, but having it like i posted on the picture. So i wanted it like i mention in ID: 26214714 and this ID: 26214742 .
ASKER
This is my latest code for the Display function:
void Display(node * n)
{
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
std::cout << nextConstant(""); // get all constants if any
}
what is displayTree?
what is std::cout << nextConstant?
Each time yo call SetColor, you change the color of the text. If you see few lines with the same color - you print something without SetColor before.
what is std::cout << nextConstant?
Each time yo call SetColor, you change the color of the text. If you see few lines with the same color - you print something without SetColor before.
seems like in vector all you have a set of strings to print. You declare an iterator lin and print all strings on the screen. Before each cout you call SetColor. I suppose that you copied the code of this functions correctly and you did declare these two variables:
HANDLE hCon;
int color = 2;
It has to be so. If there is a mistake with this variable color, you will have a problem with the colors.
HANDLE hCon;
int color = 2;
It has to be so. If there is a mistake with this variable color, you will have a problem with the colors.
ASKER
Yea, i have a set of strings to print and lin count the number of lines.
Check if you can see why i am having this issue
Check if you can see why i am having this issue
This is my DisplayTree:
void displayTree(struct node * n, size_t indent, size_t nlin,
std::vector<std::string> & lines)
{
int d = depth(n);
if (d == 1)
{
std::string data = n->data;
if (data.size() > 2)
data = nextConstant(data); // manages constants
while (nlin >= lines.size())
lines.push_back(std::string(""));
std::string & curline = lines[nlin];
curline.resize(indent, ' ');
curline += data;
return;
}
if (n->left!= NULL)
{
if (nlin == 0 || nlin >= lines.size())
lines.resize(nlin+2);
// calculate indent to left subnode
size_t nextindentl;
if (d > 2)
nextindentl = indent - ((1<<(d-2)) + (1<<(d-3)));
else
nextindentl = indent - 2;
// calling displayTree recursively passing left node,
displayTree(n->left, nextindentl, nlin+2, lines);
size_t nextindentr;
if (d > 2)
nextindentr = indent + ((1<<(d-2)) + (1<<(d-3)));
else
nextindentr = indent + 2;
displayTree(n->right, nextindentr, nlin+2, lines);
//Reference of line string at nlin.
std::string & line = lines[nlin];
if (line.size() < indent)
line.resize(indent, ' '); // offset with blanks
std::string data = n->data;
if (data.size() > 2)
data = nextConstant(data); // manages constants
line = line.substr(0, indent) + data;
std::string & nline = lines[nlin+1];
if (nline.size() < nextindentl)
nline.resize(nextindentl, ' ');// add spaces up to left indent
nline = nline.substr(0, nextindentl);
nline += "/";
nline.resize(nextindentr + 1, '-');
nline += "\\";
}
}
This is my nextConstant function and std::cout << nextConstant
calls this:
std::string nextConstant(const std::string& data)
{
static char constant = 'A';
static std::string constants;
// return all datas if empty input
if (data.empty()) return constants;
// add data to all constants
constants += constant;
constants += " = ";
constants += data;
constants += "\n";
std::string ret(1, constant);
// increment for next constant
constant++;
return ret;
}
ASKER
these are all the function incolved in printing the Tree.
So check and see how the issue could be resolved.
Thanks'
So check and see how the issue could be resolved.
Thanks'
ASKER
Sp i need only the colour of the lines /--------------\ to be chnaged, not any other text. The rest of the text should remain unchanged. The should be white.
I need to see all your code to make such fix. If you will post the code here and I will solve the task for you... I think ZA will say me something not very pleasant.
So let's agree that you will fix your program.
You have a program that can print a tree. You have a function allowing to change the text color. I attach the code of this function again.
Now you need to check carefully your code:
1. Detect all places where you print something. You can comment such lines and check on the screen. I think in this way you will understand the printing code. In the end there will be no output from your program.
2. Uncomment first printing line, check that it works and add SetColor(color) before this line and check again.
3. Do the same with each line of the printing code. Verify each step.
So let's agree that you will fix your program.
You have a program that can print a tree. You have a function allowing to change the text color. I attach the code of this function again.
Now you need to check carefully your code:
1. Detect all places where you print something. You can comment such lines and check on the screen. I think in this way you will understand the printing code. In the end there will be no output from your program.
2. Uncomment first printing line, check that it works and add SetColor(color) before this line and check again.
3. Do the same with each line of the printing code. Verify each step.
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hCon = NULL;
void SetColor(int c)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
int main()
{
for (int i = 0; i < 5; i++)
{
SetColor(i);
cout << "Hello" << endl;
}
}
Can you post your SetColor function too?
Also the place where you declare the global variables: console handle and the color.
I'm sure all lines should have already different color. If you applied everything I posted here correctly.
Also the place where you declare the global variables: console handle and the color.
I'm sure all lines should have already different color. If you applied everything I posted here correctly.
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
If you will comment std::cout << (*lin) << std::endl; the application will not print anything? Right?
Please check that.
{
SetColor();
std::cout << (*lin) << std::endl;
}
If you will comment std::cout << (*lin) << std::endl; the application will not print anything? Right?
Please check that.
ASKER
In This:
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
yes, if i comment std::cout << (*lin) << std::endl; , the tree does not print.
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
yes, if i comment std::cout << (*lin) << std::endl; , the tree does not print.
this is my SetColor fun:
int color = 2;
void SetColor()
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, color++);
if (color > 10)
color = 2;
}
I declare This:
#include <windows.h>
HANDLE hCon = NULL;
using namespace std;
where i have my header files
>>yes, if i comment std::cout << (*lin) << std::endl; , the tree does not print.
nothing. should be nothing. no output at all. I need to be sure.
The code you posted looks a bit strange. Maybe here is a problem. Put SetColor function code to the same file where you declared HANDLE hCon and where you call this SetColor function. Only there.
It should be exactly as I posted:
1. #include
2. Declare HANDLE hCon
3. Declare int color and initialize it with 2.
4. Add the function SetColor - the function code here too.
5. Rest of the code.
Here everything will work - each line will have different color.
Let's get to this point.
nothing. should be nothing. no output at all. I need to be sure.
The code you posted looks a bit strange. Maybe here is a problem. Put SetColor function code to the same file where you declared HANDLE hCon and where you call this SetColor function. Only there.
It should be exactly as I posted:
1. #include
2. Declare HANDLE hCon
3. Declare int color and initialize it with 2.
4. Add the function SetColor - the function code here too.
5. Rest of the code.
Here everything will work - each line will have different color.
Let's get to this point.
ASKER
No. Only the Tree don't print. Thhe rest of the information prints.
rest of the information - what is this? maybe exactly this delimiters you need to printed in a different color?
So when you apply these items from my comment you see the tree printed in different colors? Right?
Then you see the rest of the information printed with the color of the last line from the tree?
So when you apply these items from my comment you see the tree printed in different colors? Right?
Then you see the rest of the information printed with the color of the last line from the tree?
ASKER
I have created the code you posted in : ID: 26214453 as a headed file now, so that it will be exactly the way you post it but without the main.
I incleaded as #include "colour"; in my main program, but it still gives me the same result
as in ID: 26214714.
I am not only Printing the Tree structure, i have other values printed after the Tree structure as indicate in ID: 26214714 with Text TEST, TEST.
So if i comment std::cout << (*lin) << std::endl in
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
only the Tree Structure does not print., But the rest of the information such as TEST,TEST prints with the same color for all of them.
I only need different color printed in the Tree Structure for each level and the rest of the TEST, should all remain to be whiyte color.
I incleaded as #include "colour"; in my main program, but it still gives me the same result
as in ID: 26214714.
I am not only Printing the Tree structure, i have other values printed after the Tree structure as indicate in ID: 26214714 with Text TEST, TEST.
So if i comment std::cout << (*lin) << std::endl in
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor();
std::cout << (*lin) << std::endl;
}
only the Tree Structure does not print., But the rest of the information such as TEST,TEST prints with the same color for all of them.
I only need different color printed in the Tree Structure for each level and the rest of the TEST, should all remain to be whiyte color.
ASKER
"So when you apply these items from my comment you see the tree printed in different colors? Right?
Then you see the rest of the information printed with the color of the last line from the tree?"
Exactly as you said in this statement.
Then you see the rest of the information printed with the color of the last line from the tree?"
Exactly as you said in this statement.
Great.
Let's modify the code. Firstly we need to change the SetColor function so that it will get a color as the input parameter. So it should be just
void SetColor(int color)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HA NDLE);
SetConsoleTextAttribute(hC on, color);
}
When you will do this change, you will get the compilation error in the line where you call SetColor. Change this line for: SetColor(color++);
After this change you should see the same output.
If you are sure it works, in the beginning of printing (first output from your code) call SetColor for white color. After the printing loop add the same. So the tree will be multi-colored, the output before and after will be white.
Then we will do the next step.
Let's modify the code. Firstly we need to change the SetColor function so that it will get a color as the input parameter. So it should be just
void SetColor(int color)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HA
SetConsoleTextAttribute(hC
}
When you will do this change, you will get the compilation error in the line where you call SetColor. Change this line for: SetColor(color++);
After this change you should see the same output.
If you are sure it works, in the beginning of printing (first output from your code) call SetColor for white color. After the printing loop add the same. So the tree will be multi-colored, the output before and after will be white.
Then we will do the next step.
ASKER
#include <iostream>
#include <windows.h>
using namespace std;
HANDLE hCon = NULL;
//int color = 2;
void SetColor(int color)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, color);
}
This is what i have in my header file i created.
and i changed
for (lin=all.begin(); lin != all.end(); ++lin)
{ int color=2;
SetColor(color++);
std::cout << (*lin) << std::endl;
}
in my main program, but it print everything with the same color. Aslo how can i set a color to white?
//int color = 2;
why it's commented?
It's an error. Uncomment.
why it's commented?
It's an error. Uncomment.
I do not understand you with these header files. Again I will lose control and will not know what's going on. Get everything working fine and then modify as you wish.
about the white color - test. maybe it is 15. I do not know.
about the white color - test. maybe it is 15. I do not know.
ASKER
The 15 works for white color. Now it is almost finish, just want to have the values also in white:
for example: in the tree below:
How can i have say 9, /,* and 7 to be white. I need only this: /--------------\ to have different colors
for example: in the tree below:
How can i have say 9, /,* and 7 to be white. I need only this: /--------------\ to have different colors
/--------------\
/ 9
/------\
* 7
you see your code printing the line:
std::cout << (*lin) << std::endl;
do you know how to detect that *lin contains this constant string /--------------\?
How I understand such lines added to the vector. So if you will know what you are going to print, you can change the color before accordingly. For example:
if (*lin == "/--------------\")
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
Another way is better, but will require more changes in the code - replace the string objects in the vector with an object of your class that will contain not only the text but also its color.
So let's do the minimal change with this if statement.
std::cout << (*lin) << std::endl;
do you know how to detect that *lin contains this constant string /--------------\?
How I understand such lines added to the vector. So if you will know what you are going to print, you can change the color before accordingly. For example:
if (*lin == "/--------------\")
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
Another way is better, but will require more changes in the code - replace the string objects in the vector with an object of your class that will contain not only the text but also its color.
So let's do the minimal change with this if statement.
ASKER
In the code im posted in ID: 26274113
the following lines of code print /--------------\
i.e
nline += "/";
nline.resize(nextindentr + 1, '-');
nline += "\\";
so check in the code in ID: 26274113 post to see how we can achieve that.
Thanks
the following lines of code print /--------------\
i.e
nline += "/";
nline.resize(nextindentr + 1, '-');
nline += "\\";
so check in the code in ID: 26274113 post to see how we can achieve that.
Thanks
Please change the style of your comments. You are not my manager. I'm here not for money. All is virtual here.
I think it will work:
if (*lin[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
I think it will work:
if (*lin[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
ASKER
Sorry about my comments. All what you say is true. I am really sorry.
I have just placed you code just before the main and when i compile, i get compilation error.
error C2059: syntax error : 'if'
error C2059: syntax error : 'else'
may i know where i can implement this code please?
Thanks
I have just placed you code just before the main and when i compile, i get compilation error.
error C2059: syntax error : 'if'
error C2059: syntax error : 'else'
may i know where i can implement this code please?
Thanks
The code in the loop should be in {}.
Please show it looks now. Can you post the Display function?
Please show it looks now. Can you post the Display function?
ASKER
Here is the Display function
void BinTree_plot(node * n)
{
int color = 2;
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor(color++);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant(""); // get all constants if any
}
ASKER
That is the display function. I just change the name to BinTree_plot.
In the end of the function:
//no changes till the following line
vector::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (*lin[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
I think this code should work.
//no changes till the following line
vector::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (*lin[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
I think this code should work.
I think this is the same:
vector::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (lin->at(0) == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
vector::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (lin->at(0) == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
ASKER
I did that, but it gives me errors:
Error 1 error C2100: illegal indirection
and this points to
if (*lin[0] == '/')if (*lin[0] == '/')
and also
Error 2 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::basic_string<_Elem,_ Traits,_Ax >' (or there is no acceptable conversion)
Error 1 error C2100: illegal indirection
and this points to
if (*lin[0] == '/')if (*lin[0] == '/')
and also
Error 2 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::basic_string<_Elem,_
ASKER
when i use this one:
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (lin->at(0) == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
I have a runtime error as how in the picture
Error.JPG
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
if (lin->at(0) == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant("");
I have a runtime error as how in the picture
Error.JPG
it should be:
if ((*lin)[0] == '\')
if ((*lin)[0] == '\')
no. This way:
if ((*lin)[0] == '/')
if ((*lin)[0] == '/')
If you see this crash, press Retry - you will see where is the problem in the source code. I think it will be in the end of the function.
ASKER
The error is gone, but when i run it, it makes all text white. and when i change it to this:
if ((*lin)[0] == '/')
SetColor(color++);
else
SetColor(15);
std::cout << (*lin) << std::endl;
It only give the last level a color od green, the rest of the tree including the other text are all white.
if ((*lin)[0] == '/')
SetColor(color++);
else
SetColor(15);
std::cout << (*lin) << std::endl;
It only give the last level a color od green, the rest of the tree including the other text are all white.
It should be so:
for (lin=all.begin(); lin != all.end(); ++lin)
{
if ((*lin)[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
This loop prints all lines from the vector. If the line (the string) begins from character '\' it will be drawn in color 8 (probably, green). All other strings will be printed in white.
It should work in this way.
How I understand, only this delimiters '/---------------\' begin from character '\'.
for (lin=all.begin(); lin != all.end(); ++lin)
{
if ((*lin)[0] == '/')
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
This loop prints all lines from the vector. If the line (the string) begins from character '\' it will be drawn in color 8 (probably, green). All other strings will be printed in white.
It should work in this way.
How I understand, only this delimiters '/---------------\' begin from character '\'.
ASKER
Yes only '/---------------\' begins with character '\', but there is a character division(/) which also begin is like that and is an operator.
Please see in the picture how it is printing as at now
outPut.JPG
Please see in the picture how it is printing as at now
outPut.JPG
Yes, I see.
It should be:
size_t found;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
It should be:
size_t found;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
ASKER
I have everything as white now. No color is printed.
so no line with found = lin->find('/'); were found?
I don't think that it is possible, because the previous implementation found a line that begins with '/'.
Please check on your side or post the code here.
I don't think that it is possible, because the previous implementation found a line that begins with '/'.
Please check on your side or post the code here.
ASKER
Here is the lastest code.
void BinTree_plot(node * n)
{
//size_t found;
int color = 2;
if (n == NULL || n->data.empty()) return;
size_t d = depth(n);
size_t i;
size_t found;
if (d > 1)
i = (1<<(d-1)) + (1<<(d-2))-1; // calculate indent for root
else
i = 0;
std::vector<std::string> all;
displayTree(n, i, 1, all);
vector<string>::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
SetColor(15);
std::cout << nextConstant(""); // get all constants if any
}
ASKER
The code that prints this '/---------------\' is below:
void displayTree(struct node * n, size_t indent, size_t nlin,
std::vector<std::string> & lines)
{
int d = depth(n);
if (d == 1)
{
std::string data = n->data;
if (data.size() > 2)
data = nextConstant(data); // manages constants
while (nlin >= lines.size())
lines.push_back(std::string(""));
std::string & curline = lines[nlin];
curline.resize(indent, ' ');
curline += data;
return;
}
if (n->left!= NULL)
{
if (nlin == 0 || nlin >= lines.size())
lines.resize(nlin+2);
// calculate indent to left subnode
size_t nextindentl;
if (d > 2)
nextindentl = indent - ((1<<(d-2)) + (1<<(d-3)));
else
nextindentl = indent - 2;
// calling displayTree recursively passing left node,
displayTree(n->left, nextindentl, nlin+2, lines);
size_t nextindentr;
if (d > 2)
nextindentr = indent + ((1<<(d-2)) + (1<<(d-3)));
else
nextindentr = indent + 2;
displayTree(n->right, nextindentr, nlin+2, lines);
//Reference of line string at nlin.
std::string & line = lines[nlin];
if (line.size() < indent)
line.resize(indent, ' '); // offset with blanks
std::string data = n->data;
if (data.size() > 2)
data = nextConstant(data); // manages constants
line = line.substr(0, indent) + data;
std::string & nline = lines[nlin+1];
if (nline.size() < nextindentl)
nline.resize(nextindentl, ' ');// add spaces up to left indent
nline = nline.substr(0, nextindentl);
nline += "/";
nline.resize(nextindentr + 1, '-');
nline += "\\";
}
}
vector::iterator lin;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
It's okay. SetColor(15); for SetColor(3) to test what will be the color.
Any problem in SetColor function?
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
It's okay. SetColor(15); for SetColor(3) to test what will be the color.
Any problem in SetColor function?
ASKER
When i implement the below code as you suggested, i have the results as some in the image:
There is nothing wrong with the SetColor function: See the setclor fun. below:
// Function to display colour
int color = 2;
void SetColor(int color)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HA NDLE);
SetConsoleTextAttribute(hC on, color);
}
There is nothing wrong with the SetColor function: See the setclor fun. below:
// Function to display colour
int color = 2;
void SetColor(int color)
{
if (hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HA
SetConsoleTextAttribute(hC
}
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find('/');
if (found != string::npos)
SetColor(8);
else
SetColor(3);
std::cout << (*lin) << std::endl;
}
OUTPUT1.JPG
now you don't need this line:
int color = 2;
We do not use this variable?
int color = 2;
We do not use this variable?
ASKER
Ok, i commented int color = 2;
but i still have the same results as shown above.
but i still have the same results as shown above.
It works. :) just opposite. No?
Check that:
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
SetColor(10);
else
SetColor(5);
std::cout << (*lin) << std::endl;
}
Try to replace 10 and 5 too.
Check that:
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
SetColor(10);
else
SetColor(5);
std::cout << (*lin) << std::endl;
}
Try to replace 10 and 5 too.
You got the idea - we are trying to understand what is the line before printing it out and set a color.
It is not a good way, but it requires less changes in your code. And I want this code to work.
Another way is to make new class:
class MyString : public string
{
int color;
public:
void SetColor(int c) { color = c; }
int GetColor() { return color; }
};
and everywhere you use string in your code, you need to put MyString. Then when you add new line to the vector (that contains your tree), you need to add also the line color. Then, when you print these lines, you need to use this line color as the parameter for SetColor(int color) function.
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor(lin->GetColor());
std::cout << (*lin) << std::endl;
}
It is not a good way, but it requires less changes in your code. And I want this code to work.
Another way is to make new class:
class MyString : public string
{
int color;
public:
void SetColor(int c) { color = c; }
int GetColor() { return color; }
};
and everywhere you use string in your code, you need to put MyString. Then when you add new line to the vector (that contains your tree), you need to add also the line color. Then, when you print these lines, you need to use this line color as the parameter for SetColor(int color) function.
for (lin=all.begin(); lin != all.end(); ++lin)
{
SetColor(lin->GetColor());
std::cout << (*lin) << std::endl;
}
ASKER
This is what i got with this:
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
SetColor(10);
else
SetColor(5);
std::cout << (*lin) << std::endl;
}
and i change the values but still have '/---------------\' have the same colors. It is not different. The should have different colour.
OUTPUT2.JPG
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
SetColor(10);
else
SetColor(5);
std::cout << (*lin) << std::endl;
}
and i change the values but still have '/---------------\' have the same colors. It is not different. The should have different colour.
OUTPUT2.JPG
Fine.
int color = 3;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
{
SetColor(color++);
if (color == 10)
color = 3;
}
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
You can play with the numbers - with the values of this color.
int color = 3;
for (lin=all.begin(); lin != all.end(); ++lin)
{
found = lin->find("/-");
if (found != string::npos)
{
SetColor(color++);
if (color == 10)
color = 3;
}
else
SetColor(15);
std::cout << (*lin) << std::endl;
}
You can play with the numbers - with the values of this color.
ASKER
It seem working almost. This is what i have. See the image.
But just one problem, and that is the '-' sign which is the node of the first level should be white not Green. This is what is left. The '-' sign is an operator, so it should be white.
Thanks
OUTPUT3.JPG
But just one problem, and that is the '-' sign which is the node of the first level should be white not Green. This is what is left. The '-' sign is an operator, so it should be white.
Thanks
OUTPUT3.JPG
ASKER
I am trying to solve this one problem, but still can't. Can you please help me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks you very much. It is working now. Thanks for your patient in teaching me. I really learnt from you.
You are welcome. I wish good luck and all the best in new year.
ASKER
Thanks