C语言打印彩色字符——以(枚举法+字符串查找)为例展示

C语言颜色头文件——自制非常简单的调用函数

显然,C语言是不会提供打印彩色字符的标准函数,而我们有时候为了强调C语言打印的部分字符,或者仅仅是为了做一个明显而好看的标记而需要改变打印的颜色,幸运的是,C语言还是可以实现的。之前在CSDN上搜索的很多方法都在VC6以及DEVC++用不了,很多使用者可能为之头疼,在此介绍一种在VC6和DEVC++里面都可以用的方法。
一般的C编译器都有这个<windows.h>,<winnt.h>头文件,我们如果要方便地使用调整打印颜色的指令的话,不妨事先写好一个color.h的用户头文件,每次需要用到时就放在我们的C程序的同目录下,需要的时候直接#include "color.h"即可。下面就是我写的一个color.h的头文件,仅供大家参考。
color.h头文件如下:

#include <windows.h>
#include <winnt.h>
#include <stdio.h>
//定义颜色 
#define BLACK 0	//黑色 
#define BLUE 1	//蓝色 
#define GREEN 2	//绿色 
#define LAKEBLUE 3	//湖蓝色 
#define RED 4	//红色 
#define PURPLE 5	//紫色 
#define YELLOW 6	//黄色 
#define WHITE 7		//白色 
#define GREY 8	//灰色 
#define LIGHTBLUE 9 //淡蓝色 
#define LIGHTGREEN 10	//淡绿色 
#define LIGHTRED 11	//淡红色 
#define LIGHTPURPLE 12	//淡紫色 
#define LIGHTYELLOW 13	//淡黄色 
#define BRIGHTWHITE 14	//亮白色
void setcolor(int color)
{
	HANDLE hConsoleWnd;
    hConsoleWnd = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsoleWnd,color);
}

这里需要说明的是,系统里面自带的颜色只有15种,但对于我们日常使用已经足够了,如果需要更深入地在颜色上下功夫,可以取Easyx的官网上下载easyx的头文件和lib文件,会获得更佳的视觉效果。
HANDLE是一个句柄操作,这里我们不需要对系统自带的头文件有过多的了解,毕竟我们做的是应用软件不是系统软件,几行代码就涵盖了我们需要修改的颜色,这里把int型color变量作为传递的参数,然后把15中系统自带的颜色定义为符号常量就ok(当然你也可以定义你想要的符号,只要你知道它是什么颜色)。

实战演练——一个基础的枚举变量小程序

现在用一个简单有趣的问题来做打印颜色字符的演示——枚举变量的使用。众所周知,C语言有枚举变量类型enum,我们不妨定义一个袋子里面装了5中颜色的球,一次取一个球,总共取三次,问可能的取法有哪些?这是一个很简单的枚举变量的题目,打印也非常简单,但如果我们将取出的球对应颜色呢?是不是就非常有趣,请看以下代码:
template.c头文件如下:

#include "color.h"//包含了stdio的头文件
enum Color {red,blue,green,yellow,white};
void colorprintf(enum Color color);
int main()
{
	int i,count=0;
	enum Color {red,blue,green,yellow,white};
	enum Color color[3];
	printf("*****************枚举类型变量实验(穷举法算法)*********************\n");
	printf("从红球、蓝球、绿球、黄球和白球中依次取出3个球,求可能的取法:\n");
	for(color[0]=red;color[0]<=white;color[0]++)
	{
		for(color[1]=red;color[1]<=white;color[1]++)
		{
			if(color[0]!=color[1])
			{
				for(color[2]=red;color[2]<=white;color[2]++)
				{
					if(color[2]!=color[0]&&color[2]!=color[1])
					{
						printf("|%d\t",++count);
						for(i=0;i<3;i++)
						{
							colorprintf(color[i]);
							printf("\t");
						}
						printf("\n");
					}
				}
			}
		}
	}
	return 0;
 } 
void colorprintf(enum Color color)
{
	switch(color)
	{ 
		case blue:	setcolor(BLUE);printf("蓝球");break;	//蓝色 
		case green: setcolor(GREEN);printf("绿球");break;	//绿色 
		case red: 	setcolor(RED); printf("红球");break;//红球 
		case yellow:setcolor(YELLOW);printf("黄球");break;//黄色 
		case white: setcolor(WHITE);printf("白球");break;	//白色 
	}
	setcolor(WHITE);//还原系统色 
}

打印的效果如下图:
打印彩色字符实验——枚举变量

牛刀小试——查找字符小程序

现在感觉打印彩色字符还有点小意思,至少花花绿绿的程序看起来就比黑白的炫很多,当然,打印彩色字符还有一个更有用的应用,就是查找类的程序,一般要表示查找到的字符(串)又不损坏原来的字符(串),那么改变字符串的颜色就是最好的选择。现在有一段英文文字,我们要查找单词"the"(当然你也可以在程序执行时输入你想要的单词)。
The man himself lay in the bed.For a long while we just stood there, looking down at the profoundand fleshless grin. The body had apparently once lain in the attitude of anembrace, but now the long sleep that outlasts love, that conquers even thegrimace of love, had cuckolded him. What was left of him, rotted beneath whatwas left of the nightshirt, had become inextricable from the bed in which helay; and upon him and upon the pillow beside him lay that even coating of thepatient and biding dust.Then we noticed that in the second pillow was the indentation of ahead. One of us lifted something from it, and leaning forward, that faint andinvisible dust dry and acrid in the nostrils, we saw a long strand of iron-grayhair.

【注】本段文字来源网络《献给艾米丽的玫瑰》
将这段文字导入文本文档,建立一个txt文件,假设就叫它sample.txt吧,放在C文件的同目录下,然后再将我们之前编好的color.h头文件放在C程序的同目录下,好戏开始:
main.c文件代码:

#include "color.h"
#include <string.h>
#include <stdlib.h>
void find_str(char *p,char *p0);
void move(char *p,int n);//
int main()
{
	char p[2000],p0[30],*ch;
	FILE *fp;//文件指针 
	printf("读入sample文件中...:\n");
	fp=fopen("sample.txt","r");
	if(fp==NULL)
	{
		printf("读入文件失败!");
		return 2;//非正常返回 
	}
	for(ch=p;!feof(fp);ch++)
	{
		*ch=fgetc(fp);
		putchar(*ch);//打印到文件夹 
	}
	*ch=0;//添加一个字符串结束符 
	printf("\n读入文件成功!\n需要查找的字符串:\n");
	gets(p0);
	if (strstr(p,p0)==NULL)
	{
		printf("未找到该字符串!\n");
		return 1;//非正常返回 
	}
	find_str(p,p0);
	return 0;
}
void find_str(char *p,char *p0)
{
	char *p1,*p2;
	p1=p; 
	p2=strstr(p,p0);
	while(p2!=NULL)
	{
		move(p2,1);
		*p2=0;
		printf("%s",p1);
		p1=p2+1;
		p2+=strlen(p0)+1;
		move(p2,1);
		*p2=0;
		setcolor(LAKEBLUE);
		printf("%s",p1);//打印被查找的字符串
		setcolor(WHITE);
		p1=p2+1;//p1到达p2查找到的字符串的末尾
		p2=strstr(p1,p0);//p2沿着p1的起始位置继续查找 
	}
	printf("%s",p1); 
}
void move(char *p,int n)//p为操作点处的地址,n为偏移量 
{
	int i;
	char *q;
	if(n>0)//若n>0,数组后移,先操作的应该是数组最后一个元素
	{
		for(i=0,q=p+strlen(p);q-p>=0;q--)//从p字符串的结束符'\0'开始后移
		{
			*(q+n)=*q;
		}
	}
	else if(n<0)//若n<0,数组前移,先操作的是数组的第一个元素
	{
		for(i=0,q=p;q-p<=strlen(p);q++)//从第一个字符开始前移,一直移动到p字符串的结束符'\0'
		{
			*(q+n)=*q;
		}
	}
	//n=0时误操作
}

这个程序是字符串指针的应用,还是有点难度的,对初学者来看可能有点难理解,不过多写写就会发现也没有想象得那么难,你们如果运行这串代码,输出效果图就会如下图所示:
查找字符串变量
OK,看起来还不错,由于我们是按字符读入并且查找的,所以会把含有the的所有字符查找进去,比如说there,但如果我们稍稍修改一下我们存储的数据结构,将fgetc,fputc改成fgetc和fgets即可,它的操作将会比这个还要简单。当然,还有一些细节部分需要改动,由于本博客主要分享一下怎么打印彩色字符,在这里我就不再赘述了。
希望本文对您有帮助,谢谢阅读。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章