2014-11-23 56 views
0

有没有办法给数组中的一个元素着色?将颜色应用于C中数组中的特定元素

说,我的2D阵列是这样的:

main() 
{ 
int x,y; 
char arr[3][3]; 
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
arr[x][y]='a'; 

arr[1][1]='b'; 
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
printf("%c", arr[x][y]); 
} 

如何将颜色应用到只有字符 'b' 位于ARR [1] [1]?

+3

颜色的元素? :/ – Maroun 2014-11-23 08:58:04

+0

你的意思是像#ffffff这样的颜色代码吗? – Rizier123 2014-11-23 09:01:38

+0

[C中的stdlib和彩色输出]的可能重复(http://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c) – Tarik 2014-11-23 12:24:37

回答

0

我找到了解决办法。这是Windows的。如果你的情况和我一样,那么我就是这么做的。 我this question

使用SetConsoleTextAttribute()函数,得益于很好的答案,所以我的代码是

#include <windows.h> 
#include <stdio.h> 

main 
{ 
enter code here 
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
CONSOLE_SCREEN_BUFFER_INFO consoleInfo; 
WORD saved_attributes; 
char arr[3][3]; 
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
arr[x][y]='a'; 
arr[1][1]='b'; 
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
{ 
if (arr[x][y]=='b') 
{ 
SetConsoleTextAttribute(hConsole, FOREGROUND_RED); 
printf("%c", arr[x][y]); 
SetConsoleTextAttribute(hConsole, saved_attributes); 
} 
else 
{ 
printf("%c", arr[x][y]); 
} 
} 
相关问题