2017-03-17 110 views
1

我有一個程序打印出數組中冒泡排序的通行證,並且想要嘗試添加顯示(通過文本顏色更改)在哪裏進行交換的功能每次傳遞數組。到目前爲止,我嘗試過的所有內容都會更改所有文本顏色,或者不會更改任何內容(如當前示例所示)。有人有主意嗎?更改數組中元素的文本顏色

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

回答

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

這裏是個e解決方案。

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

此代碼取消引用未初始化的指針執行的。相反,你可以只使用'WORD',而不是指針 –

+0

正確,我會編輯 – Edd

+0

謝謝你的幫助!我對C++和編程非常陌生,所以我對你的解決方案有幾個疑問:這些代碼在程序中的哪個位置?我嘗試在其中一個或另一個實施它,無法讓程序運行。我的第二個問題是,這種解決方案是否能夠僅格式化數組中的交換順序的元素,例如,如果我有一個數組「{1,3,2,4}」,並且按升序排序,但想要只會對'[3]'和'[2]'着色,因爲它們是物品交換的地方。 – McBraunie