更改数组中元素的文本颜色

时间:2017-03-17 08:57:44

标签: c++ arrays colors bubble-sort

我有一个程序打印出数组中冒泡排序的传递,并希望尝试添加显示(通过文本颜色更改)的功能,其中每次传递时数组内的交换发生。到目前为止,我所尝试的所有内容都会更改所有文本颜色或不更改任何内容(如当前示例所示)。有人有主意吗?

#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;

1 个答案:

答案 0 :(得分:2)

当交换任何内容(白色)时,您需要使用基本颜色。你从那种颜色开始。然后使用以下命令保存当前属性设置:

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);

<强> EDITED

这是解决方案。

使用-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;
}