更改数组中元素的文本颜色
Changing Text Color of an Element in an Array
我有一个程序可以打印出数组中冒泡排序的遍历,并想尝试添加显示(通过文本颜色更改)每次遍历数组中发生交换的功能。到目前为止,我尝试过的所有操作要么更改所有文本颜色,要么什么都不更改(如当前示例所示)。谁有想法?
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>
using namespace std;
void sortArrayAscending(int *array, int size);
void printArray(int *array, int);
void printUnsortedArray(int *array, int size);
int main()
{
HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(a, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
string hyphen;
const string progTitle = "Array Sorting Program";
const int numHyphens = 100;
hyphen.assign(numHyphens, '-');
const int size = 8;
int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };
cout << hyphen << endl;
cout << " " << progTitle << endl;
cout << hyphen << endl;
cout << "\n This program will sort two identical arrays of numbers using a Bubble Sort"<< endl;
cout << "\n Array 1 -- Ascending Order: \n" << endl;
printUnsortedArray(values, size);
cout << endl;
sortArrayAscending(values, size);
cin.ignore(cin.rdbuf()->in_avail());
cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
cin.get();
}
void sortArrayAscending(int *array, int size)
{
const int regTextColor = 2;
const int swapTextColorChange = 4;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
int temp;
bool swapTookPlace;
int pass = 0;
do
{
swapTookPlace = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
swapTookPlace = true;
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
if (swapTookPlace)
{
SetConsoleTextAttribute(screen, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
if (array[count] > array[count + 1])
{
SetConsoleTextAttribute(screen, swapTextColorChange);
}
if (pass < 9)
{
cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
pass += 1;
printArray(&array[0], size);
}
else if (pass >= 9)
{
cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
pass += 1;
printArray(&array[0], size);
}
}
}
}
} while (swapTookPlace);
}
void printArray(int *array, int size)
{
for (int count = 0; count < size; ++count)
cout << " " << array[count] << " ";
cout << endl;
}
void printUnsortedArray(int *array, int size)
{
cout << " Unsorted ";
for (int count = 0; count < size; ++count)
cout << " " << array[count] << " ";
cout << endl;
您需要有一种基本颜色,以便在没有任何内容被交换时使用(白色)。你从那个颜色开始。然后你保存当前的属性设置:
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO Info;
WORD defaultAttributes = 0;
GetConsoleScreenBufferInfo(handle, &Info);
defaultAttributes = Info.wAttributes;
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
当你想显示交换时(就像你已经在做的那样)然后将颜色更改为你的交换颜色并在没有交换时设置回默认属性:
SetConsoleTextAttribute(handle, defaultAttributes);
已编辑
这是解决方案。
在带有 -std=c++11 标志的 g++ 5.1.0 上编译。
执行自 Windows cmd.exe
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>
#include <array>
#include <algorithm>
using namespace std;
void sortArrayAscending(int *array, int size);
void printArray(int *array, bool *swaps, int , HANDLE &, WORD , WORD );
void printUnsortedArray(int *array, int size);
int main()
{
string hyphen;
const string progTitle = "Array Sorting Program";
const int numHyphens = 100;
hyphen.assign(numHyphens, '-');
const int size = 8;
int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };
cout << hyphen << endl;
cout << " " << progTitle << endl;
cout << hyphen << endl;
cout << "\n This program will sort two identical arrays of numbers using a Bubble Sort"<< endl;
cout << "\n Array 1 -- Ascending Order: \n" << endl;
printUnsortedArray(values, size);
cout << endl;
sortArrayAscending(values, size);
cin.ignore(cin.rdbuf()->in_avail());
cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
cin.get();
}
void sortArrayAscending(int *array, int size)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
//default config
CONSOLE_SCREEN_BUFFER_INFO Info;
WORD defaultAttributes = 0;
GetConsoleScreenBufferInfo(screen, &Info);
defaultAttributes = Info.wAttributes;
//swap attribute
WORD swapAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int regTextColor = 2;
const int swapTextColorChange = 4;
int temp;
bool swapTookPlace;
int pass = 0;
do
{
swapTookPlace = false;
bool swapped[size];
//let's initialzie swapped to be all-false at the beginning
for_each(swapped,swapped+size,[](bool &b){b = false;});
for (int count = 0; count < (size - 1); ++count)
{
if (array[count] > array[count + 1])
{
swapTookPlace = true;
std::swap(array[count],array[count+1]);
swapped[count] = true;
swapped[count+1] = true;
}else{
swapped[count] = swapped[count] | false; //set to unswapped unless previously set to swapped
swapped[count+1] = false;
}
}
cout << " Pass # " << pass;
printArray(array,swapped,size, screen, defaultAttributes, swapAttributes);
pass++;
} while (swapTookPlace);
}
void printArray(int *array, bool *swaps, int size, HANDLE &handle, WORD defaultConfig, WORD swapConfig)
{
for (int count = 0; count < size; ++count){
if (swaps[count]){
SetConsoleTextAttribute(handle,swapConfig);
}else{
SetConsoleTextAttribute(handle,defaultConfig);
}
cout << " " << array[count] << " ";
}
SetConsoleTextAttribute(handle,defaultConfig);
cout << endl;
}
void printUnsortedArray(int *array, int size)
{
cout << " Unsorted ";
for (int count = 0; count < size; ++count){
cout << " " << array[count] << " ";
}
cout << endl;
}
我有一个程序可以打印出数组中冒泡排序的遍历,并想尝试添加显示(通过文本颜色更改)每次遍历数组中发生交换的功能。到目前为止,我尝试过的所有操作要么更改所有文本颜色,要么什么都不更改(如当前示例所示)。谁有想法?
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>
using namespace std;
void sortArrayAscending(int *array, int size);
void printArray(int *array, int);
void printUnsortedArray(int *array, int size);
int main()
{
HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(a, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
string hyphen;
const string progTitle = "Array Sorting Program";
const int numHyphens = 100;
hyphen.assign(numHyphens, '-');
const int size = 8;
int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };
cout << hyphen << endl;
cout << " " << progTitle << endl;
cout << hyphen << endl;
cout << "\n This program will sort two identical arrays of numbers using a Bubble Sort"<< endl;
cout << "\n Array 1 -- Ascending Order: \n" << endl;
printUnsortedArray(values, size);
cout << endl;
sortArrayAscending(values, size);
cin.ignore(cin.rdbuf()->in_avail());
cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
cin.get();
}
void sortArrayAscending(int *array, int size)
{
const int regTextColor = 2;
const int swapTextColorChange = 4;
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
int temp;
bool swapTookPlace;
int pass = 0;
do
{
swapTookPlace = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
swapTookPlace = true;
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
if (swapTookPlace)
{
SetConsoleTextAttribute(screen, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
if (array[count] > array[count + 1])
{
SetConsoleTextAttribute(screen, swapTextColorChange);
}
if (pass < 9)
{
cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
pass += 1;
printArray(&array[0], size);
}
else if (pass >= 9)
{
cout << fixed << setw(2) << " Pass # " << (pass + 1) << " : ";
pass += 1;
printArray(&array[0], size);
}
}
}
}
} while (swapTookPlace);
}
void printArray(int *array, int size)
{
for (int count = 0; count < size; ++count)
cout << " " << array[count] << " ";
cout << endl;
}
void printUnsortedArray(int *array, int size)
{
cout << " Unsorted ";
for (int count = 0; count < size; ++count)
cout << " " << array[count] << " ";
cout << endl;
您需要有一种基本颜色,以便在没有任何内容被交换时使用(白色)。你从那个颜色开始。然后你保存当前的属性设置:
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO Info;
WORD defaultAttributes = 0;
GetConsoleScreenBufferInfo(handle, &Info);
defaultAttributes = Info.wAttributes;
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
当你想显示交换时(就像你已经在做的那样)然后将颜色更改为你的交换颜色并在没有交换时设置回默认属性:
SetConsoleTextAttribute(handle, defaultAttributes);
已编辑
这是解决方案。
在带有 -std=c++11 标志的 g++ 5.1.0 上编译。
执行自 Windows cmd.exe
#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <string>
#include <array>
#include <algorithm>
using namespace std;
void sortArrayAscending(int *array, int size);
void printArray(int *array, bool *swaps, int , HANDLE &, WORD , WORD );
void printUnsortedArray(int *array, int size);
int main()
{
string hyphen;
const string progTitle = "Array Sorting Program";
const int numHyphens = 100;
hyphen.assign(numHyphens, '-');
const int size = 8;
int values[size] = { 21, 16, 23, 18, 17, 22, 20, 19 };
cout << hyphen << endl;
cout << " " << progTitle << endl;
cout << hyphen << endl;
cout << "\n This program will sort two identical arrays of numbers using a Bubble Sort"<< endl;
cout << "\n Array 1 -- Ascending Order: \n" << endl;
printUnsortedArray(values, size);
cout << endl;
sortArrayAscending(values, size);
cin.ignore(cin.rdbuf()->in_avail());
cout << "\n\n\n\nPress only the 'Enter' key to exit program: ";
cin.get();
}
void sortArrayAscending(int *array, int size)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
//default config
CONSOLE_SCREEN_BUFFER_INFO Info;
WORD defaultAttributes = 0;
GetConsoleScreenBufferInfo(screen, &Info);
defaultAttributes = Info.wAttributes;
//swap attribute
WORD swapAttributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const int regTextColor = 2;
const int swapTextColorChange = 4;
int temp;
bool swapTookPlace;
int pass = 0;
do
{
swapTookPlace = false;
bool swapped[size];
//let's initialzie swapped to be all-false at the beginning
for_each(swapped,swapped+size,[](bool &b){b = false;});
for (int count = 0; count < (size - 1); ++count)
{
if (array[count] > array[count + 1])
{
swapTookPlace = true;
std::swap(array[count],array[count+1]);
swapped[count] = true;
swapped[count+1] = true;
}else{
swapped[count] = swapped[count] | false; //set to unswapped unless previously set to swapped
swapped[count+1] = false;
}
}
cout << " Pass # " << pass;
printArray(array,swapped,size, screen, defaultAttributes, swapAttributes);
pass++;
} while (swapTookPlace);
}
void printArray(int *array, bool *swaps, int size, HANDLE &handle, WORD defaultConfig, WORD swapConfig)
{
for (int count = 0; count < size; ++count){
if (swaps[count]){
SetConsoleTextAttribute(handle,swapConfig);
}else{
SetConsoleTextAttribute(handle,defaultConfig);
}
cout << " " << array[count] << " ";
}
SetConsoleTextAttribute(handle,defaultConfig);
cout << endl;
}
void printUnsortedArray(int *array, int size)
{
cout << " Unsorted ";
for (int count = 0; count < size; ++count){
cout << " " << array[count] << " ";
}
cout << endl;
}