【问题标题】:Changing Text Color of an Element in an Array更改数组中元素的文本颜色
【发布时间】:2017-08-08 18:00:28
【问题描述】:

我有一个程序可以打印出数组中冒泡排序的传递,并希望尝试添加显示(通过文本颜色更改)在每次传递时在数组中发生交换的位置的功能。到目前为止,我尝试过的所有内容要么更改所有文本颜色,要么不更改任何内容(如当前示例所示)。谁有想法?

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

【问题讨论】:

    标签: c++ arrays colors bubble-sort


    【解决方案1】:

    您需要有一种基本颜色,以便在没有任何东西交换时使用(白色)。你从那个颜色开始。然后你保存当前的属性设置:

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

    【讨论】:

    • 此代码取消引用未初始化的指针。相反,您可以只使用WORD,而不是指针
    • 感谢您的帮助!我对 C++ 和编程非常陌生,所以我对您的解决方案有几个问题:这些行在程序中的什么位置?我尝试以一种或另一种方式实现它,但无法让程序运行。我的第二个问题是这个解决方案是否能够仅格式化数组中交换顺序的元素,例如,如果我有一个数组 {1, 3, 2, 4} 并且按升序排序但只想为 [3] 和 @ 着色987654328@,因为它们将是物品交换的地方。
    • 乐于助人。好的,所以在您的程序中,这是您写入控制台时的要点。你这样做是成功的,否则当我运行你的代码时,我将无法看到所有带有冒泡排序迭代的行。现在您需要做的是,在写入控制台之前,设置您想要使用的颜色。如果 swapTookPlace = true,那么我们要使用绿色。否则,我们要使用默认颜色。在程序开始时,您将使用我的第一个代码块来获取默认配置(defaultAttributes)。然后您将创建另一个配置 [..]
    • [...] 字交换属性 = FOREGROUND_GREEN |您想要的 ANY_OTHER_PARAMETER;此时,您将使用一个属性或另一个属性,具体取决于是否存在交换,正如我上面所说的。您可以使用我提供的第二个代码块来执行此操作。这也是您在自己的代码中使用的。坚持下去,你会得到它的工作。
    • 谢谢你,埃德。这真的很有帮助。我能够让代码工作......但我认为我的if() 循环一定是不正确的,因为当我编译程序时,文本颜色确实发生了变化,但它改变了数组中的 all 值而不仅仅是那些已经交换的。我是否应该为此使用某种形式的 for() 循环,而这将如何充实以仅针对某些元素?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2021-01-06
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多