( •̀ ω •́ )y( •̀ ω •́ )y精心整理C两百例【120例-180例】,持续更新.....

前言

( •̀ ω •́ )y( •̀ ω •́ )y精心整理C两百例【1-60例】,持续更新…

( •̀ ω •́ )y( •̀ ω •́ )y精心整理C两百例【60-120例】,持续更新…

121、绘制椭圆

#include<stdio.h>
#include<stdlib.h>
#include <graphics.h>

void main()
{
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	moveto(160, 120);
	linerel(320, 0);
	linerel(0, 240);
	linerel(-320, 0);
	linerel(0, -240);
	system("pause");
	closegraph();
}

122、设置背景色和前景色

#include <graphics.h>
#include <stdlib.h>
void main()
{
	int cb, cf;
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	for (cb = 0; cb <= 15; cb++)
	{
		setbkcolor(cb);
		for (cf = 0; cf <= 15; cf++)
		{
			setcolor(cf);
			circle(100 + cf * 25, 240, 100);

		}
		getch();
	}
	getch();
	system("pause");
	closegraph();
}

123、设置线条类型

#include <graphics.h>
#include <stdlib.h>

void main()
{
	int i, j, c, x = 50, y = 50, k = 1;
	int gdriver = DETECT, gmode;

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(9);
	setcolor(4);
	for (j = 1; j <= 2; j++)
	{
		for (i = 0; i<4; i++)
		{
			setlinestyle(i, 0, k);
			line(50, 50 + i * 50 + (j - 1) * 200, 200, 200 + i * 50 + (j - 1) * 200);
			rectangle(x, y, x + 210, y + 80);
			circle(100 + i * 50 + (j - 1) * 200, 240, 100);
		}
		k = 3;
		x = 50;
		y = 250;
	}
	system("pause");
	closegraph();
}

124、设置填充类型和填充颜色

#include <graphics.h>
#include <stdlib.h>

void main()
{
	int i, c = 4, x = 5, y = 6;
	int gdriver = DETECT, gmode;

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setcolor(c);
	for (i = c; i<c + 8; i++)
	{
		setcolor(i);
		rectangle(x, y, x + 140, y + 104);
		x += 70;
		y += 52;
		setfillstyle(1, i);
		floodfill(x, y, i);
	}
	outtextxy(80, 400, "Press any key to quit...");
	system("pause");
	closegraph();
}

125、图形文本的输出

#include <graphics.h>
#include <stdlib.h>

void main()
{
	int x;
	int gdriver = DETECT, gmode;

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setcolor(4);
	for (x = 160; x <= 480; x += 20)
	{
		line(160, 240, x, 120);
		line(160, 240, x, 360);
		line(480, 240, 640 - x, 360);
		line(480, 240, 640 - x, 120);
	}
	outtextxy(80, 400, "Press any key to quit...");
	system("pause");
	closegraph();
}

126、金刚石图案

/* diamond */
#include <graphics.h>
#include <math.h>
#include <stdlib.h>

void main()
{
	float t;
	int x0 = 320, y0 = 240;
	int n, i, j, r;
	int x[50], y[50];
	int gdriver = DETECT, gmode;
	printf("Please input n(23<=n<=31) and r(100<=r<=200):\n");
	scanf("%d%d", &n, &r);

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setcolor(4);
	outtextxy(80, 20, "This program show the Diamond picture.");
	t = 6.28318 / n;

	for (i = 0; i<n; i++)
	{
		x[i] = x0 + r*cos(i*t);
		y[i] = y0 + r*sin(i*t);
	}
	for (i = 0; i <= n - 2; i++)
	{
		for (j = i + 1; j <= n - 1; j++)
			line(x[i], y[i], x[j], y[j]);
	}
	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

127、飘带图案

/* linesin */
#include <graphics.h>
#include <math.h>
#include <stdlib.h>

#define PI 3.1415926
void main()
{
	double a;
	int x1, y1, x2, px = 320, py = 240;
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setcolor(4);
	outtextxy(80, 20, "This program show the Linesin picture.");

	for (a = 0; a <= PI; a += PI / 380)
	{
		x1 = px + 280 * cos(1.6*a);
		y1 = 479 - ((90 * sin(8 * a))*cos(a / 2.5) + py);
		x2 = py + 280 * cos(1.8*a);
		line(x1, y1, x2, y1);
	}
	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

128、圆环图案

/* circles */
#include <graphics.h>
#include <math.h>
#include <stdlib.h>

#define PI 3.1415926
void main()
{
	double a;
	int x, y, r1, rs;
	int gdriver = DETECT, gmode;
	printf("Please input R1(<100) and Rs:\n");
	scanf("%d%d", &r1, &rs);

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setcolor(4);
	outtextxy(80, 20, "This program show the Circles picture.");

	for (a = 0; a <= 2 * PI; a += PI / 18)
	{
		x = 320 + r1*cos(a);
		y = 240 + r1*sin(a);
		circle(x, y, rs);
	}
	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

129、肾形图案

/* Kidney */
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
void main()
{
	double a;
	int x, y, r, r1;
	int gdriver = 9, gmode = 2;
	initgraph(&gdriver, &gmode, "c:\\tc");
	printf("Please input Radus(<150): ");
	scanf("%d", &r);
	cleardevice();
	setbkcolor(MAGENTA);
	setcolor(WHITE);
	outtextxy(80, 20, "This program show the Kidney picture.");

	for (a = 0; a <= 2 * PI; a += PI / 27)
	{
		x = 320 + r*cos(a);
		y = 240 + r*sin(a);
		r1 = abs(x - 320);
		circle(x, y, r1);
	}
	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

130、心脏形图案

/* Heart */
#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926
void main()
{
	double a;
	int x, y, y1, r, r1;
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	printf("Please input Radus(<80): ");
	scanf("%d", &r);
	cleardevice();
	setbkcolor(9);
	setcolor(4);
	outtextxy(80, 20, "This program show the Kidney picture.");

	y1 = 240 - r;
	for (a = 0; a <= 2 * PI; a += PI / 27)
	{
		x = 320 + r*cos(a);
		y = 240 + r*sin(a);
		r1 = sqrt((x - 320)*(x - 320) + (y - y1)*(y - y1));
		circle(x, y, r1);
	}
	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

131、渔网图案

#include <graphics.h>
#include <stdlib.h>

void main()
{
	int x, y, x1, y1, x0 = 320, y0 = 50;
	int i, j, n = 5, r = 20;
	int gdriver = VGA, gmode = VGAHI;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(9);
	outtextxy(80, 20, "This program show the Fishing Net picture.");

	for (i = 0; i <= 2 * n; i++)
	{
		x1 = x0 - i*r;
		y1 = y0 + i*r;
		for (j = 0; j <= n - 1; j++)
		{
			x = x1 + 2 * j*r;
			y = y1 + 2 * j*r;
			arc(x, y, 180, 270, r);
			arc(x, y + 2 * r, 0, 90, r);
		}
	}
	x1 = x0 - 2 * r;
	y1 = y0;
	for (i = 0; i <= 2 * n; i++)
	{
		x1 = x1 + r;
		y1 = y1 + r;
		for (j = 0; j <= n - 1; j++)
		{
			x = x1 - 2 * j*r;
			y = y1 + 2 * j*r;
			arc(x, y, 90, 180, r);
			arc(x - 2 * r, y, 270, 360, r);
		}
	}

	outtextxy(80, 460, "Press any key to quit...");
	system("pause");
	closegraph();
}

132、沙丘图案

/* Sandhill */
#include <graphics.h>
#include <math.h>
#define PI 3.1415926
void main()
{
	double a,b,c;
	int x,y,py;
	int gdriver=DETECT,gmode;
	initgraph(&gdriver,&gmode,"c:\\tc");
	cleardevice();
	setbkcolor(9);
	setcolor(14);
	outtextxy(80,3,"This program show the Sandhill picture.");

	for(py=20;py<=380;py+=3)
	{
		c=(py-20)*4*PI/360;
		b=PI*cos(c);
		for(a=0;a<=6*PI;a+=PI/7)
		{
			x=600/(6*PI)*a+20;
			y=10*sin(a+cos(a)*PI-b)*cos(c)+py;
			if(a==0)
				moveto(x,y);
			else
				lineto(x,y);
		}
	}
	outtextxy(80,460,"Press any key to quit...");
	getch();
	closegraph();
}

133、设置图形方式下的文本类型

/* text */
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
void main()
{
	int i, t, x = 300, y = 50;
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	setbkcolor(9);
	setcolor(4);
	printf("Please input delay time (1-10): ");
	scanf("%d", &t);
	outtextxy(80, 20, "This program show the Text in graphic mode.");

	for (i = 1; i <= 10; i++)
	{
		x = x - 15;
		y = y + 15;
		settextstyle(1, 0, i);
		/*cleardevice();*/
		outtextxy(x, y, "Hello!");
		delay(1000 * t);
	}
	settextstyle(1, 0, 1);
	outtextxy(80, 420, "Press any key to quit...");
	system("pause");
	closegraph();
}

134、绘制正多边形

#include <graphics.h>
#include <math.h>

void polygon(x0,y0,a,n,af)
int x0,y0,a,n;
float af;
{
	int x,y,i;
	float dtheta,theta;
	if(n<3)
		return;
	dtheta=6.28318/n;
	theta=af*0.0174533;
	moveto(x0,y0);
	x=x0;
	y=y0;
	for(i=0;i<n;i++)
	{
		x=x+a*cos(theta);
		y=y+a*sin(theta);
		lineto(x,y);
		theta=theta+dtheta;
	}
	lineto(x0,y0);
}
void main()
{
	int i,a=80,x=200,y=100;
	int gdriver=DETECT,gmode;
	initgraph(&gdriver,&gmode,"C:\\tc");
	cleardevice();
	setbkcolor(9);
	setcolor(4);
	for(i=3;i<=10;i++)
		polygon(x,y,a,i,0.);
	getch();
	closegraph();
}

135、正六边形螺旋图案

#include <graphics.h>
#include <math.h>
#include <stdlib.h>

void polygonc();
void main()
{
	int i, r, n, x = 320, y = 240;
	int gdriver = 9, gmode = 2;
	float theta, alfa;
	n = 30;
	theta = 6.0;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(MAGENTA);
	setcolor(WHITE);
	r = 160;
	alfa = 60.0;
	for (i = 0; i<n; i++)
	{
		polygonc(x, y, r, 6, alfa);
		r = (int)(0.866*r / cos((30.0 - theta)*0.0174533));
		alfa = alfa + theta;
	}
	system("pause");
	closegraph();
}
void polygonc(int x0, int y0, int r, int n, float af)
{
	int x, y, xs, ys, i;
	float dtheta, theta;
	if (n<3)
		return;
	dtheta = 6.28318 / n;
	theta = af*0.0174533;
	xs = x0 + r*cos(theta);
	ys = y0 + r*sin(theta);
	moveto(xs, ys);
	for (i = 1; i<n; i++)
	{
		theta = theta + dtheta;
		x = x0 + r*cos(theta);
		y = y0 + r*sin(theta);
		lineto(x, y);
	}
	lineto(xs, ys);
}

136、正方形螺旋拼块图案

#include <graphics.h>
#include <math.h>
#include <stdlib.h>
void polygonc();
void block();

void main()
{
	int i, j, a, length, n, theta, x = 100, y = 350;
	int gdriver = DETECT, gmode;
	printf("input length, n theta:");
	scanf("%d%d%d", &length, &n, &theta);
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(9);
	setcolor(4);
	a = length / 4;
	for (i = 1; i <= 4; i++)
	{
		for (j = 1; j <= 4; j++)
		{
			block(x, y, a, n, theta);
			theta = -theta;
			x = x + a;
		}
		x = 100;
		y = y - a;
		theta = -theta;
	}
	system("pause");
	closegraph();
}
void polygonc(int x0, int y0, int r, int n, float af)
{
	int x, y, xs, ys, i;
	float dtheta, theta;
	if (n<3)
		return;
	dtheta = 6.28318 / n;
	theta = af*0.0174533;
	xs = x0 + r*cos(theta);
	ys = y0 + r*sin(theta);
	moveto(xs, ys);
	for (i = 1; i<n; i++)
	{
		theta = theta + dtheta;
		x = x0 + r*cos(theta);
		y = y0 + r*sin(theta);
		lineto(x, y);
	}
	lineto(xs, ys);
}
void block(int x, int y, int a, int n, int theta)
{
	int x0, y0, i, r;
	float t, f, af = 45.;
	t = fabs(theta*0.0174533);
	f = 1.0 / (cos(t) + sin(t));
	r = a / 1.414;
	x0 = x + 0.5*a;
	y0 = y + 0.5*a;
	for (i = 1; i <= n; i++)
	{
		polygonc(x0, y0, r, 4, af);
		r = r*f;
		af = af - theta;
	}
}

137、图形法绘制圆

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
/*这是根据给出的圆心坐标和点坐标分别在八个象限画点的子程序*/
void circlePoint(int xCenter, int yCenter, int x, int y)
{
	putpixel(xCenter + x, yCenter + y, YELLOW);
	putpixel(xCenter - x, yCenter + y, YELLOW);
	putpixel(xCenter + x, yCenter - y, YELLOW);
	putpixel(xCenter - x, yCenter - y, YELLOW);
	putpixel(xCenter + y, yCenter + x, YELLOW);
	putpixel(xCenter - y, yCenter + x, YELLOW);
	putpixel(xCenter + y, yCenter - x, YELLOW);
	putpixel(xCenter - y, yCenter - x, YELLOW);
}

void myCircle(int xCenter, int yCenter, int radius)
{
	int x, y, p;
	/*初始化各个参数*/
	x = 0;
	y = radius;
	p = 1 - radius;
	circlePoint(xCenter, yCenter, x, y);
	/*循环中计算圆上的各点坐标*/
	while (x < y) {
		x++;
		if (p < 0)
			p += 2 * x + 1;
		else
		{
			y--;
			p += 2 * (x - y) + 1;
		}
		circlePoint(xCenter, yCenter, x, y);
	}
}

void main()
{
	int gdriver = DETECT, gmode;  /*这是用c画图时必须要使用的图像入口*/
	int i;
	int xCenter, yCenter, radius;
	printf("Please input center coordinate :(x,y) ");
	scanf("%d,%d", &xCenter, &yCenter);
	printf("Please input radius : ");
	scanf("%d", &radius);
	/*这条语句初始化整个屏幕并把入口传给gdriver,注意引号中是tc中bgi目录的完整路径*/
	registerbgidriver(EGAVGA_driver);
	initgraph(&gdriver, &gmode, "..\\bgi");
	setcolor(BLUE);
	myCircle(xCenter, yCenter, radius);
	sleep(3);
	closegraph();
	system("pause");
	return;
}

138、递归法绘制三角形图案

#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>

void tria(int xa, int ya, int xb, int yb, int xc, int yc, int n)
{
	int xp, yp, xq, yq, xr, yr;
	if (n>0)
	{
		xp = (xa + xb) / 2;
		yp = (ya + yb) / 2;
		xq = (xb + xc) / 2;
		yq = (yb + yc) / 2;
		xr = (xc + xa) / 2;
		yr = (yc + ya) / 2;
		moveto(xp, yp);
		lineto(xq, yq);
		lineto(xr, yr);
		lineto(xp, yp);
		tria(xa, ya, xp, yp, xr, yr, n - 1);
		tria(xb, yb, xq, yq, xp, yp, n - 1);
		tria(xc, yc, xr, yr, xq, yq, n - 1);
	}
}

void main()
{
	int n, xa = 10, ya = 10, xb = 10, yb = 470, xc = 630, yc = 470;
	int gdriver = DETECT, gmode;
	printf("Input recursion depth( for exmaple, 7): ");
	scanf("%d", &n);
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(8);
	setcolor(2);
	moveto(xa, ya);
	lineto(xb, yb);
	lineto(xc, yc);
	lineto(xa, ya);
	tria(xa, ya, xb, yb, xc, yc, n);
	system("pause");
	closegraph();
}

139、图形法绘制椭圆

#include <graphics.h>
#include <math.h>
#include <stdlib.h>


void ellipse1(int x0, int y0, int a, int b, int dt)
{
	int x, y, n, i;
	float t1, t = 0.0;
	t1 = dt*0.0174533;
	n = 360 / dt;
	moveto(x0 + a, y0);
	for (i = 1; i<n; i++)
	{
		t = t + t1;
		x = x0 + a*cos(t);
		y = y0 + b*sin(t);
		lineto(x, y);
	}
	lineto(x0 + a, y0);
}

void main()
{
	int i, a = 200, x = 320, y = 240;
	int gdriver = DETECT, gmode;
	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(8);
	setcolor(2);
	for (i = 0; i <= 200; i = i + 10)
		ellipse1(x, y, a - i, i, 10);
	system("pause");
	closegraph();
}

140、抛物样条曲线

/* draw Parspl */
#include <graphics.h>
#include <math.h>
#include <stdio.h>

void parspl(int p[][2],int n,int k,int e)
{
	int x, y, i, j, m;
	float t1, t2, t3, t, a, b, c, d;
	if (e == 1)
	{
		m = n;
		p[0][0] = p[1][0];
		p[0][1] = p[1][1];
		p[n + 1][0] = p[n][0];
		p[n + 1][1] = p[n][1];
	}
	else
	{
		m = n + 1;
		p[0][0] = p[n][0];
		p[0][1] = p[n][1];
		p[m][0] = p[1][0];
		p[m][1] = p[1][1];
		p[m + 1][0] = p[2][0];
		p[m + 1][1] = p[2][1];
	}
	t = 0.5 / k;
	moveto(p[1][0], p[1][1]);
	for (i = 0; i<m - 1; i++)
	{
		for (j = 1; j<k; j++)
		{
			t1 = j*t;
			t2 = t1*t1;
			t3 = t2*t1;
			a = 4.0*t2 - t1 - 4.0*t3;
			b = 1.0 - 10.0*t2 + 12.0*t3;
			c = t1 + 8.0*t2 - 12.0*t3;
			d = 4.0*t3 - 2.0*t2;
			x = a*p[i][0] + b*p[i + 1][0] + c*p[i + 2][0] + d*p[i + 3][0];
			y = a*p[i][1] + b*p[i + 1][1] + c*p[i + 2][1] + d*p[i + 3][1];
			lineto(x, y);
		}
		lineto(p[i + 2][0], p[i + 2][1]);
	}

}

void marking(int a[][2],int n)
{
	int i;
	setfillstyle(1, WHITE);
	for (i = 1; i <= n; i++)
	{
		circle(a[i][0], a[i][1], 2);
		floodfill(a[i][0], a[i][1], RED);
	}

}

void main()
{
	int i, n, c, p[50][2], px, py;
	char fname[10], ch;
	FILE *fp;
	int gdriver = DETECT, gmode;
	clrscr();
	printf("Please input point numer: ");
	scanf("%d", &n);
	printf("Please input name of the file with the point data:\n >> ");
	scanf("%s", fname);
	if ((fp = fopen(fname, "r")) == NULL)
	{
		printf("File %s does not exist! Do you want to create it? Y/N ", fname);
		ch = getch();
		if (ch == 'Y' || ch == 'y')
		{
			if ((fp = fopen(fname, "w+")) == NULL)
			{
				printf("\nError! Can't create file %s!", fname);
				exit(1);
			}
			printf("\nPlease input %d pair coordinates (x,y) of the points:\n", n);
			for (i = 0; i<n; i++)
			{
				scanf("%d%d", &px, &py);
				fprintf(fp, "%d %d ", px, py);
			}
			rewind(fp);
		}
		else
			exit(1);
	}
	rewind(fp);
	for (i = 1; i <= n; i++)
		fscanf(fp, "%d%d", &p[i][0], &p[i][1]);
	fclose(fp);
	printf("Please input the figure type: FREE--1, Close--2: ");
	scanf("%d", &c);

	initgraph(&gdriver, &gmode, "c:\\tc");
	cleardevice();
	setbkcolor(9);
	setcolor(4);
	marking(p, n);
	outtextxy(80, 460, "Press any key to continue...");
	getch();
	parspl(p, n, 10, c);
	outtextxy(80, 460, "Press any key to quit...    ");
	system("pause");
	closegraph();
}

141、Mandelbrot分形图案

/*
原理:

1. 使用叠代公式:
z[0] = zInit;
z[k] = z[k-1]*z[k-1] + z[0]
其中z[i]是复数,要使用复数的运算法则。

2. Mandelbort图形集的初始化要求
-2.25<= Re(zInit) <= 0.75
-1.25<= Im(zInit) <= 1.25
其中Re(z)表示z的实部, Im(z)表示z的虚部
*/
#include <graphics.h>
#include <stdlib.h>

typedef struct { float x, y; } complex; /*定义复数的结构,x表示实部,y表示虚部*/

complex complexSquare(complex c)
/*计算复数的平方
(x+yi)^2 = (x^2-y^2) + 2xyi
*/
{
	complex csq;
	csq.x = c.x * c.x - c.y * c.y;
	csq.y = 2 * c.x * c.y;
	return csq;
}

int iterate(complex zInit, int maxIter)
/*叠代计算颜色,maxIter是最多叠代的次数,*/
{
	complex z = zInit;
	int cnt = 0;

	/* 当 z*z > 4的时候退出 */
	while ((z.x * z.x + z.y * z.y <= 4.0) && (cnt < maxIter))
	{
		/*叠代公式:z[k] = z[k-1]^2 + zInit, cnt是叠代次数*/
		z = complexSquare(z);
		z.x += zInit.x;
		z.y += zInit.y;
		cnt++;
	}
	return cnt;
}

void mandelbrot(int nx, int ny, int maxIter, float realMin, float realMax, float imagMin, float imagMax)
/*画Mandelbrot图形的主程序,参数意义如下:
nx: x轴的最大值
ny: y轴的最大值
maxIter: 叠代的最大次数
realMin: 初值zInit的实部最小值
realMax: 初值zInit的实部最大值
imagMin: 初值zInit的虚部最小值
imagMax: 初值zInit的虚部最大值
*/
{
	float realInc = (realMax - realMin) / nx; /*x轴叠代的步长*/
	float imagInc = (imagMax - imagMin) / ny; /*y轴叠代的步长*/
	complex z; /*初值zInit*/
	int x, y; /*点(x,y)的横纵坐标*/
	int cnt; /*叠代的次数*/

	for (x = 0, z.x = realMin; x<nx; x++, z.x += realInc)
	{
		for (y = 0, z.y = imagMin; y < ny; y++, z.y += imagInc)
		{
			cnt = iterate(z, maxIter); /*计算叠代次数*/
			if (cnt == maxIter) /*当叠代最大时,为黑色*/
				putpixel(x, y, BLACK);
			else /*否则将叠代次数作为颜色*/
				putpixel(x, y, cnt);
		}
	}
}

void main()
{
	int gdriver = 9, gmode = 2;
	/*registerbgidriver( EGAVGA_driver );*/
	initgraph(&gdriver, &gmode, "e:\\tc\\bgi");
	mandelbrot(640, 480, 255, -2.0, 0.55, -1.0, 1.25);
	system("pause");
	closegraph();
}

142、绘制布朗运动曲线


/* 一维布朗运动曲线 */

/* 本程序利用分形技术画一维的布朗运动曲线 */
/* BC 3.1编译                               */
/* 其中函数initgraph的第三个参数可能需要修改  */
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#define  MAX_SIZE 1000
#define MAX_LEVEL 9 /* 递归深度*/
double Delta[MAX_LEVEL];
double Array[MAX_SIZE];
double H;
double Sigma = 150.0;
double Gauss(void);
void CreateFractalImage(int y1, int y2);
void MiddlePoint(int p1, int p2, int CurrentLevel);
void DrawFractalImage(void);
/*=============================================================*/
main()
{
	int GraphDriver = DETECT;
	int GraphMode;
	int k, mod;
	double TempX, TempY, StartX, StartY;
	initgraph(&GraphDriver, &GraphMode, "c:\\tc");
	/*===============================================================*/
	/*Draw fMB curve 递归画一条分形布朗运动曲线*/
	/*==============================================================*/
	randomize();
	setcolor(GREEN);
	H = 0.3;
	settextstyle(TRIPLEX_FONT, HORIZ_DIR, 0);
	setusercharsize(2, 1, 1, 1);
	outtextxy(450, 400, "Wait...");
	CreateFractalImage(150, 150);
	DrawFractalImage();
	H = 0.7;
	CreateFractalImage(400, 400);
	DrawFractalImage();
	getch();
	closegraph();
	return 0;
}
/*===============================================================*/
void CreateFractalImage(int y1, int y2)
{
	int N, i;
	N = (int)pow(2.0, (double)MAX_LEVEL);
	for (i = 0; i<MAX_LEVEL; i++)
		Delta[i] = Sigma*pow(0.5, i*H)*sqrt(1.0 - pow(2.0, 2 * H - 2));
	Array[0] = y1;
	Array[N] = y2;
	MiddlePoint(0, N, 0);
}
/*===============================================================*/
/*Recursive procedure 二分法递归*/
/*===============================================================*/
void MiddlePoint(int p1, int p2, int CurrentLevel)
{
	int middle;
	middle = (p1 + p2) / 2;
	if (CurrentLevel>MAX_LEVEL) return;
	if ((middle != p1) && (middle != p2))
	{
		Array[middle] = (Array[p1] + Array[p2]) / 2.0 + Delta[CurrentLevel] * Gauss();
		MiddlePoint(p1, middle, CurrentLevel + 1);
		MiddlePoint(middle, p2, CurrentLevel + 1);
	}
}
/*===============================================================*/
void DrawFractalImage(void)
{
	int i, x, step, number;
	number = (int)pow(2.0, (double)MAX_LEVEL);
	step = getmaxx() / number * 3 / 2;
	moveto(0, (int)Array[0]);
	for (i = 1, x = step; i<number; x += step, i++) lineto(x, (int)Array[i]);
}
/*==============================================================*/
double Gauss()
{
	double g = 0.0;
	int RANGE = 12000;
	int COUNT = 50;
	int m;
	for (m = 1; m <= COUNT; m++) g += (double)random(RANGE);
	g = g / COUNT / (RANGE - 1);
	if (random(RANGE) % 2) g = -g;
	return g;
}

143、艺术清屏

#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>


void goto_xy(int x, int y);
void dcls(int x1, int x2, int y1, int y2);
void bcls(int x1, int x2, int y1, int y2);
void recls(int x1, int x2, int y1, int y2);
void zcls(int x1, int x2, int y1, int y2);
void puta(void);

/*--------------演示程序---------------------*/
void main()
{
	puta();
	getch();
	dcls(0, 4, 0, 79);
	getch();
	puta();
	getch();
	bcls(0, 25, 0, 79);
	getch();
	puta();
	getch();
	zcls(0, 25, 0, 79);
	system("pause");
}
/*********center clear screen(中心清屏)***********/
void zcls(int x1, int x2, int y1, int y2)
{
	int x00, y00, x0, y0, i, d;
	if ((y2 - y1)>(x2 - x1)){
		d = (x2 - x1) / 2;
		x0 = (x1 + x2) / 2;
		y0 = y1 + d;
		y00 = y2 - d;
		for (i = 0; i<(d + 1); i++)
			recls((x0 - i), (x00 + i), (y0 - i), (y00 + i));
	}
	else{
		d = (y2 - y1) / 2;
		y0 = (y1 + y2) / 2;
		x0 = x1 + d;
		x00 = x2 - d;
		for (i = 0; i<d + 1; i++)
			recls(x0 - i, x00 + i, y0 - i, y00 + i);
	}
}

/************* clear rectangle side(矩形边清屏)***********************/

void recls(int x1, int x2, int y1, int y2)
{
	int i, j;
	for (i = y1; i<y2; i++){
		goto_xy(x1, i);
		putchar(' ');
		goto_xy(x2, i);
		putchar(' ');
	}
	for (j = x1; j<x2; j++){
		goto_xy(i, y1);
		putchar(' ');
		goto_xy(j, y2);
		putchar(' ');
	}
}

/*****************close screen clear*****闭幕式清屏*******************/

void bcls(int x1, int x2, int y1, int y2)
{
	int t, s, j;
	t = y1;
	s = y2;
	for (t = y1; t<(y1 + y2) / 2; t++, s--)
	for (j = x1; j<x2; j++){
		goto_xy(j, t);
		putchar(' ');
		goto_xy(j, s);
		putchar(' ');
		delay(1);
	}
}
/******************bottom screen clear(自下清屏)********************/

void dcls(int x1, int x2, int y1, int y2)
{
	int t, s, j, i;
	t = s = (y1 + y2) / 2;
	for (j = x2; j>x1; j--)
	for (i = y1; i<y2; i++){
		goto_xy(j, i);
		putchar(' ');
		delay(1);
	}
}
/******************设置光标子函数******************/

void goto_xy(int x, int y)
{
	union REGS r;
	r.h.ah = 2;
	r.h.dl = y;
	r.h.dh = x;
	r.h.bh = 0;
	int86(0x10, &r, &r);
}

/**********************在屏幕上打出一连串的@字母用于演示程序******************/

void puta(void)
{
	int i, j;
	for (i = 0; i<24; i++){
		for (j = 0; j<79; j++){
			goto_xy(i, j);
			printf("@");
		}
	}
}

144、矩形区域的颜色填充

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

void main()
{
	int gd = VGA, gm = VGALO;
	/*registerbgidriver(EGAVGA_driver);*/
	initgraph(&gd, &gm, "e:\\tc\\bgi"); /*设置图形模式*/
	setcolor(YELLOW);
	rectangle(105, 105, 175, 135); /*画正方形*/
	full(120, 120, YELLOW); /*调填充函数*/
	system("pause");
	closegraph(); /*关闭图形模式*/
}

#define DELAY_TIME 5/*填充点后延长的时间,用来观看填充的过程,单位:毫秒*/

int full(int x, int y, int color1)/*递归的填充函数*/
{
	int color2, x1, y1;
	x1 = x; y1 = y;
	if (kbhit())return;
	color2 = getpixel(x1, y1); /*读(x,y)点颜色值*/
	if (color2 != color1) /*判断是否与填充色相等*/
	{
		putpixel(x1, y1, color1); /*画点(x1,y1) */
		delay(DELAY_TIME);
		getch();
		x1++;
		full(x1, y1, color1); /*递归调用*/
	}
	x1 = x; y1 = y;
	color2 = getpixel(x1 - 1, y1); /*读(x1-1,y1)点颜色值*/
	if (color2 != color1) /*判断是否与填充色相等*/
	{
		putpixel(x1, y1, color1); /*画点(x1,y1) */
		delay(DELAY_TIME);
		x1--;
		full(x1, y1, color1); /*递归调用*/
	}
	x1 = x; y1 = y;
	color2 = getpixel(x1, y1 + 1); /*读(x1,y1+1)点颜色值*/
	if (color2 != color1) /*判断是否与填充色相等*/
	{
		putpixel(x1, y1, color1); /*画点(x1,y1) */
		delay(DELAY_TIME);
		y1++;
		full(x1, y1, color1); /*递归调用*/
	}
	x1 = x; y1 = y;
	color2 = getpixel(x1, y1 - 1); /*读(x1,y1+1)点颜色值*/
	if (color2 != color1) /*判断是否与填充色相等*/
	{
		putpixel(x1, y1, color1); /*画点(x1,y1) */
		delay(DELAY_TIME);
		y1--;
		full(x1, y1, color1); /*递归调用*/
	}
	return;
}

145、VGA256色模式编程

/*
VGA256.c -- VGA 256 色编程
*/
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

void InitScr();
void RstScr();
void PutPoint(int x, int y, int Color);
void Rect(int x1, int y1, int x2, int y2, int Color);
void LineV(int x1, int y1, int x2, int y2, int Color);

int main()
{
	int x1, y1, x2, y2, i, j;
	x1 = y1 = 0;
	x2 = 319;
	y2 = 199;
	InitScr();
	for (i = 0; i < 256; i++)
		LineV(i, 0, i, 199, i);
	for (i = 18; i < 100; i++)
		Rect(x1++, y1++, x2--, y2--, i);
	for (i = 18; i < 50; i++)
		Rect(x1--, y1--, x2++, y2++, i);
	system("pause");
	RstScr();
}

void InitScr()
{
	union REGS In;
	In.x.ax = 0x13;                 /*进入13H模式  */
	int86(0x10, &In, &In);
}

void RstScr()
{
	union REGS In;
	In.x.ax = 0x03;             /* 退出13H模式 */
	int86(0x10, &In, &In);
}

/* 直接写视频缓冲区 */
void PutPoint(int x, int y, int Color)   /* 画点函数 */
{
	char far *p;
	p = (char far *) (0x0a0000000L);
	*(x + y * 320 + p) = Color;
}

/* 利用VGA BIOS中断在屏幕上画点, 速度慢
void PutPoint(int x, int y, int Color)
{
union REGS  In;
In.h.ah = 0x0C;
In.h.al = Color;
In.x.cx = x;
In.x.dx = y;
In.h.bh = 0;
int86(0x10, &In, &In);
}
*/

void LineV(int x1, int y1, int x2, int y2, int Color)  /* 画一垂直线 */
{
	int i;
	for (i = 0; i < 199; i++)
		PutPoint(x1, i, Color);
}

void Rect(int x1, int y1, int x2, int y2, int Color)  /* 画一矩形*/
{
	int i;
	for (i = x1; i <= x2; i++)
	{
		PutPoint(i, y1, Color);
		PutPoint(i, y2, Color);
	}
	for (i = y1; i <= y2; i++)
	{
		PutPoint(x1, i, Color);
		PutPoint(x2, i, Color);
	}
}

146、绘制蓝天图案

#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <stdlib.h>

long Addr[768];
int Mode;

Set_Mode(int mode) /*设置显示的模式,参数为INT 10H的子功能号*/
{
	union REGS r;
	r.h.ah = 0;
	r.h.al = mode;
	int86(0x10, &r, &r);
}
Set_Graphics_Mode(unsigned x, unsigned y) /*设置图形模式的分辨率*/
{
	long i;
	if ((x<321) && (y<201))  {
		Set_Mode(0x13);
		Mode = 0x13;
		for (i = 0; i<200; i++)  Addr[i] = 320 * i;
	}
	else if ((x<641) && (y<401))  {
		Set_Mode(0x5c);
		Mode = 0x5c;
		for (i = 0; i<400; i++)  Addr[i] = 640 * i;
	}
	else if ((x<641) && (y<481))  {
		Set_Mode(0x5d);
		Mode = 0x5d;
		for (i = 0; i<480; i++)  Addr[i] = 640 * i;
	}
	else if ((x<801) && (y<601))  {
		Set_Mode(0x5e);
		Mode = 0x5e;
		for (i = 0; i<600; i++)  Addr[i] = 800 * i;
	}
	else if ((x<1025) && (y<769))  {
		Set_Mode(0x62);
		Mode = 0x62;
		for (i = 0; i<768; i++)  Addr[i] = 1024 * i;
	}
	else  {
		Set_Mode(3);
		printf("Not support this mode .\n");
		getch();
	}
}
set_pattern() /*设置调色版*/
{
	int i;
	unsigned char pat[256][3];
	struct SREGS inreg;
	union REGS reg;
	pat[0][0] = 0; pat[0][1] = 0; pat[0][2] = 0;
	for (i = 1; i <= 255; i++)  { /*设置调色版的颜色*/
		pat[i][0] = (unsigned char)((float)(abs(i - 127) * 63) / 127.0 + 0.5);
		pat[i][1] = (unsigned char)((float)(abs(i - 127) * 63) / 127.0 + 0.5);
		pat[i][2] = 63;
	}
	reg.x.ax = 0x1012; /*设置VIDEO调色版的颜色块*/
	reg.x.bx = 0;
	reg.x.cx = 256;
	reg.x.dx = FP_OFF(pat);
	inreg.es = FP_SEG(pat);
	int86x(0x10, &reg, &reg, &inreg);
}
plot(int x, int y, unsigned char color) /*使用指定的颜色画点*/
{
	long offset;
	char Page;
	unsigned char far *address;
	switch (Mode)  {
	case 0x13:
		offset = Addr[y] + x;
		address = (unsigned char far *)(0xa0000000L + offset);
		*address = color;
		break;
	case 0x5c:
	case 0x5d:
	case 0x5e:
		offset = Addr[y] + x;
		Page = (offset >> 16);
		outportb(0x3c4, 0xe);
		outportb(0x3c5, Page ^ 0x02);
		offset = offset & 65535;
		address = (unsigned char far *)(0xa0000000L + offset);
		*address = color;
		break;
	case 0x62:
		offset = Addr[y] + x;
		Page = y >> 6;
		outportb(0x3c4, 0xe);
		outportb(0x3c5, Page ^ 0x02);
		offset = offset & 65535;
		address = (unsigned char far *)(0xa0000000L + offset);
		*address = color;
		break;
	default:
		break;
	}
}
get_pixel(int x, int y) /*获得点颜色*/
{
	long offset;
	char Page;
	unsigned char far *address;
	unsigned char color;
	switch (Mode)  {
	case 0x13:
		offset = Addr[y] + x;
		address = (unsigned char far *)(0xa0000000 + offset);
		color = *address;
		break;
	case 0x5c:
	case 0x5d:
	case 0x5e:
		offset = Addr[y] + x;
		Page = (offset >> 16);
		outportb(0x3c4, 0xe);
		outportb(0x3c5, Page ^ 0x02);
		offset = offset & 65535;
		address = (unsigned char far *)(0xa0000000L + offset);
		color = *address;
		break;
	case 0x62:
		offset = Addr[y] + x;
		Page = y >> 6;
		outportb(0x3c4, 0xe);
		outportb(0x3c5, Page ^ 0x02);
		offset = offset & 65535;
		address = (unsigned char far *)(0xa0000000L + offset);
		color = *address;
		break;
	default:
		break;
	}
	return (color);
}
randint(unsigned int range) /*生成随机数*/
{
	float sigma = 423.1966;
	static double OldRand = 0.4231967;
	double temp;
	temp = sigma*OldRand;
	OldRand = temp - (int)temp;
	return (int)(OldRand*(float)range);
}
void New_Col(int xa, int ya, int x, int y, int xb, int yb) /*生成新的颜色*/
{
	unsigned int color;
	color = abs(xa - xb) + abs(ya - yb);
	color = randint(color << 1) - color;
	color = color + (get_pixel(xa, ya) + get_pixel(xb, yb) + 1) >> 1;
	if (color<1)  color = 1;
	else if (color>255) color = 255;
	if ((get_pixel(x, y) == 0)) plot(x, y, color);
}
void Sub_Divide(int x1, int y1, int x2, int y2) /*递归填充一块区域*/
{
	int x, y;
	unsigned char color;
	if (!((x2 - x1<2) && (y2 - y1<2)))  {
		x = (x1 + x2) >> 1;
		y = (y1 + y2) >> 1;
		New_Col(x1, y1, x, y1, x2, y1);
		New_Col(x2, y1, x2, y, x2, y2);
		New_Col(x1, y2, x, y2, x2, y2);
		New_Col(x1, y1, x1, y, x1, y2);
		color = (get_pixel(x1, y1) + get_pixel(x2, y1) + get_pixel(x2, y2) + get_pixel(x1, y2) + 2) >> 2;
		plot(x, y, color);
		Sub_Divide(x1, y1, x, y);
		Sub_Divide(x, y1, x2, y);
		Sub_Divide(x, y, x2, y2);
		Sub_Divide(x1, y, x, y2);
	}
}
void main()
{
	int x, y;
	x = 320; y = 200;
	Set_Graphics_Mode(x, y);
	set_pattern();
	plot(0, 0, randint(254) + 1);
	plot(x - 1, 0, randint(254) + 1);
	plot(x - 1, y - 1, randint(254) + 1);
	plot(0, y - 1, randint(254) + 1);
	Sub_Divide(0, 0, x - 1, y - 1);
	system("pause");
	Set_Mode(0x03);
}

147、屏幕检测程序

#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#define INTR 0x1c

void interrupt(*oldhandler) ();
int fre[] = { 523, 659, 587, 659, 587, 494, 587, 523, 440, 440, 440, 330, 392, 440, 494, 494, 330, 415, 494, 523, 523, 523 };

void interrupt handler(){
	unsigned i;
	static int flag;
	static int k = 0;
	union{
		long divisor;
		unsigned char c[2];
	}music;
	unsigned char port;
	flag++;
	if (flag >= 4){
		flag = 0;
		music.divisor = 1190000 / fre[k];
		outportb(67, 182);
		outportb(66, music.c[0]);
		outportb(66, music.c[1]);
		port = inportb(97);
		outport(97, port | 3);
		k++;
		if (k >= 22) k = 1;
	}
	oldhandler();
}

int main(void)
{
	int gdriver = 9, gmode = 2, errorcode;
	int midx, midy, i;
	oldhandler = getvect(INTR);
	setvect(INTR, handler);
	/*registerbgidriver( EGAVGA_driver );*/
	initgraph(&gdriver, &gmode, "c:\\tc");
	errorcode = graphresult();
	if (errorcode != grOk)
	{
		printf("graphic error:%s\n", grapherrormsg(errorcode));
		printf("press any key to halt");
		getch();
		exit(1);
	}
	midx = getmaxx() / 2;
	midy = getmaxy() / 2;
	for (i = SOLID_FILL; i<USER_FILL; i++)
	{
		setfillstyle(i, getmaxcolor());
		bar(midx - 50, midy - 50, midx + 50, midy + 50);
		delay(2000);
		getch();
	}
	system("pause");
	closegraph();
	setvect(INTR, oldhandler);
	nosound();
	return(0);
}

148、运动的小车动画

#include<time.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>

#define step 10
#define R 10

void main()
{
	int gdriver = 9, gmode = 2;
	static int startx = 5;
	static int starty = 100;
	int maxx, l = 1, n = 1;
	double dalta = 20, angle;
	int size;
	void *image;
	/*registerbgidriver( EGAVGA_driver );*/ /*注册vga驱动*/
	initgraph(&gdriver, &gmode, "c:\\tc"); /*初始化图形界面*/
	cleardevice(); /*清除屏幕*/
	setbkcolor(BLUE); /*将背景色设置成蓝色*/
	size = imagesize(startx, starty, startx + 60, starty + 60); /*计算生成60×60个象素的图需要的字节数*/
	image = (unsigned char *)malloc(size); /*分配内存*/
	maxx = getmaxx(); /*获得屏幕显示x轴的最大值*/
	while (!kbhit()) /*如果没有按键就不停的循环*/
	{
		if (l == 1) /*从左到右*/
		{
			n++;
			angle = -1 * (n*step) / M_PI * 180 / R;
			if ((int)(-1 * angle) % 360<dalta)
				angle -= dalta;
			if (n>(maxx - 70) / step)
				l = 0;
		}
		if (l == 0) /*从右到左*/
		{
			--n;
			angle = -1 * (n*step) / R / M_PI * 180;
			if ((int)(-1 * angle) % 360<dalta)
				angle -= dalta;
			if (n == 1)l = 1;
		}
		rectangle(startx + n*step, starty, startx + n*step + 60, starty + 40); /*画车厢*/
		pieslice(startx + n*step + 15, starty + 50, angle, angle - dalta, 10); /*画轮上的小片扇形部分*/
		pieslice(startx + n*step + 45, starty + 50, angle, angle - dalta, 10);
		setcolor(YELLOW); /*设置前景色为黄色*/
		setfillstyle(SOLID_FILL, YELLOW); /*设置填充模式为实填充,颜色为黄色*/
		circle(startx + n*step + 15, starty + 50, 10); /*画车轮*/
		circle(startx + n*step + 45, starty + 50, 10);
		circle(startx + n*step + 15, starty + 50, 3);
		circle(startx + n*step + 45, starty + 50, 3);
		getimage(startx + n*step, starty, startx + n*step + 60, starty + 60, image); /*获取当前的图片*/
		delay(100);
		putimage(startx + n*step, starty, image, XOR_PUT); /*使用异或模式将图片显示上去*/
	}
	free(image);
	closegraph();
}

149、动态显示位图


#include <stdio.h>
#include <dir.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>

char *malloc();/*malloc转换*/

char bmp_to_dat(char *bmp, char *dat)
/*将16色BMP文件转换为可以用putimage输出的格式,bmp为原BMP文件,dat为转化文件*/
{
	unsigned char c[8], scan_times, scan_pixs;
	unsigned char workpos; int i, j, k, n, nowpos, iw, ih;
	static int color[16] = { 0, 4, 2, 6, 1, 5, 3, 7, 8, 12, 10, 14, 9, 13, 11, 15 };
	unsigned char workline[640], scanline[640];
	FILE *fp, *targetfp;
	union
	{
		unsigned char value;
		struct
		{
			unsigned cl : 4;
			unsigned ch : 4;
		}color;
	}mycolor;

	if ((fp = fopen(bmp, "rb")) == NULL)return(0);
	targetfp = fopen(dat, "wb");

	fseek(fp, 18, SEEK_SET);
	iw = 0; ih = 0;
	fread(&iw, 4, 1, fp);		/*读图像宽度*/
	fread(&ih, 4, 1, fp);		/*读图像高度*/
	if (iw == 0 && ih == 0 && iw>640 && ih>480)
	{
		fclose(fp); fclose(targetfp); return(0);
	}

	iw--; ih--;			/*∵putimage中的长宽比实际数值少1*/
	scan_times = iw / 8 + 1;		/*行处理单位数*/
	scan_pixs = scan_times * 4;		/*行像素字节数∵1单位=4字节*/

	fputc(iw % 256, targetfp);		/*填充信息头:长、宽部分*/
	fputc(iw / 256, targetfp);
	fputc(ih % 256, targetfp);
	fputc(ih / 256, targetfp);
	fseek(fp, -scan_pixs, SEEK_END);

	for (j = 0; j <= ih; j++)
	{
		nowpos = 0;
		fread(scanline, scan_pixs, 1, fp);
		fseek(fp, -scan_pixs * 2, SEEK_CUR);
		for (n = 3; n >= 0; n--)		/*解码4个位面*/
		{
			for (i = 0; i<scan_times; i++)	/*解码各编码单位*/
			{
				workpos = 0;
				for (k = 0; k<4; k++)		/*分离出8个像素*/
				{
					mycolor.value = scanline[i * 4 + k];
					c[k * 2] = color[mycolor.color.ch];
					c[k * 2 + 1] = color[mycolor.color.cl];
				}
				for (k = 0; k<8; k++)workpos += (c[k] >> n & 1) << (7 - k);
				workline[nowpos] = workpos; nowpos++;
			}
		}
		fwrite(workline, scan_pixs, 1, targetfp);
	}
	fclose(fp); fclose(targetfp);
	return(1);
}

void main()
{
	int gd = VGA, gm = VGAHI, n;
	char *buffer, bmpfile[13], _16file[13] = { 0 };
	FILE *fp;
	struct ffblk *ff;
	/*registerbgidriver(EGAVGA_driver);*/
	initgraph(&gd, &gm, "C:\\tc");
	printf("Now start to display 16 color bmp.\n\n");
	puts("Please choice: ");
	printf("0. Quit \n1. BMP file name\n");
	while (1)
	{
		n = getch();
		switch (n)
		{
		case 0:continue;
		case '0':exit(0);
		case '1':
		{
					printf("Please input BMP file name(*.bmp):");
					gets(bmpfile);
					goto OUT;
		}
		}
	}
OUT:
	strncpy(_16file, bmpfile, strlen(bmpfile) - 3);
	strcat(_16file, "dat");
	if (!bmp_to_dat(bmpfile, _16file))
	{
		puts("Can't open file!");
		closegraph();
		exit(0);
	}
	fp = fopen(_16file, "rb");
	findfirst(_16file, ff, FA_ARCH);
	if ((buffer = malloc(ff->ff_fsize)) == NULL)exit(0);
	n = 0;
	while (!feof(fp))
	{
		buffer[n] = fgetc(fp);
		n++;
	}
	for (n = 0; n<100; n++)
	{
		putimage(350 - n, n / 2, buffer, COPY_PUT);
	}
	system("pause");
	closegraph();
	free(buffer);
	fclose(fp);
}

150、利用图形页实现动画

#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <math.h>
void main()
{
	void * ptr;
	int gd = 9, gm = 2;
	float i;
	/*registerbgidriver(EGAVGA_driver);*/
	initgraph(&gd, &gm, "C:\\TC");/*图形初始化*/
	setactivepage(1);/*设置页面1为当前活动页*/
	setcolor(RED);/*设置当前画笔颜色为红色*/
	setfillstyle(1, 14);/*设置填充模式为实填充,颜色为黄色*/
	fillellipse(10, 10, 10, 10);/*画一个实心的椭圆*/
	ptr = malloc(imagesize(0, 0, 20, 20));/*动态分配一个动态空间,其中可以存储一个20×20大小的图片*/
	getimage(0, 0, 20, 20, ptr);/*将当前屏幕上的图片写入内存*/
	randomize();/*随机数初始化*/
	setactivepage(0);/*设置页面1为当前活动页*/
	for (i = 0; i<3000; i++)
		putpixel(random(640), random(480), random(15));/*在随机的位置以随机的颜色填充点*/
	for (i = 0; i<6.28 * 4; i = i + 0.005)
	{
		/*以下两句是将内存中的图形以特定图形块显示在屏幕上*/
		putimage(300 + 8 * (cos(i) + i*sin(i)), 240 + 6 * (sin(i) - i*cos(i)), ptr, COPY_PUT);
		putimage(300 + 8 * (cos(i) + (i)*sin(i)), 240 + 6 * (sin(i) - (i)*cos(i)), ptr, COPY_PUT);
		if (kbhit()) break;/*获得一个键盘信号停止*/
		delay(10);
	}
	system("pause");
	closegraph();
}

151、图形时钟

#include<math.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>

#define CENTERX 320   /*表盘中心位置*/
#define CENTERY 175
#define CLICK 100  /*喀嗒声频率*/
#define CLICKDELAY 30 /*喀嗒声延时*/
#define HEBEEP 10000 /*高声频率*/
#define LOWBEEP 500 /*低声频率*/
#define BEEPDELAY 200 /*报时声延时*/

/*表盘刻度形状*/
int Mrk_1[8] = { -5, -160, 5, -160, 5, -130, -5, -130, };
int Mrk_2[8] = { -5, -160, 5, -160, 2, -130, -2 - 130, };

/*时针形状*/
int HourHand[8] = { -3, -100, 3, -120, 4, 10, -4, 10 };

/*分针形状*/
int MiHand[8] = { -3, -120, 3, -120, 4, 10, -4, 10 };

/*秒针形状*/
int SecHand[8] = { -2, -150, 2, -150, 3, 10, -3, 10 };

/*发出喀嗒声*/
void Click()
{
	sound(CLICK);
	delay(CLICKDELAY);
	nosound();
}

/*高声报时*/
void  HighBeep()
{
	sound(HEBEEP);
	delay(BEEPDELAY);
	nosound;
}

/*低声报时*/
void LowBeep()
{
	sound(LOWBEEP);
}

/*按任意角度画多边形*/
void DrawPoly(int *data, int angle, int color)
{
	int usedata[8];
	float sinang, cosang;
	int i;
	sinang = sin((float)angle / 180 * 3.14);
	cosang = cos((float)angle / 180 * 3.14);
	for (i = 0; i<8; i += 2)
	{
		usedata[i] = CENTERX + cosang*data[i] - sinang*data[i + 1] + .5;
		usedata[i + 1] = CENTERY + sinang*data[i] + cosang*data[i + 1] + .5;
	}
	setfillstyle(SOLID_FILL, color);
	fillpoly(4, usedata);
}

/*画表盘*/
void DrawClock(struct time *cutime)
{
	int ang;
	float hourrate, minrate, secrate;

	setbkcolor(BLACK);
	cleardevice();
	setcolor(WHITE);

	/* 画刻度*/
	for (ang = 0; ang<360; ang += 90)
	{
		DrawPoly(Mrk_1, ang, WHITE);
		DrawPoly(Mrk_2, ang + 30, WHITE);
		DrawPoly(Mrk_2, ang + 60, WHITE);
	}
	secrate = (float)cutime->ti_sec / 60;
	minrate = ((float)cutime->ti_min + secrate) / 60;
	hourrate = (((float)cutime->ti_hour / 12) + minrate) / 12;
	ang = hourrate * 360;
	DrawPoly(HourHand, ang, YELLOW);/*画时针*/
	ang = minrate * 360;
	DrawPoly(MiHand, ang, GREEN);/*画分针*/
	ang = secrate * 360;
	DrawPoly(SecHand, ang, RED);/*画秒针*/
}
void main()
{
	int gdriver = EGA,
		gmode = EGAHI;
	int curpage;
	struct time curtime, newtime;
	initgraph(&gdriver, &gmode, "c:\\tc");
	setbkcolor(BLUE);
	cleardevice();
	gettime(&curtime);
	curpage = 0;
	DrawClock(&curtime);
	while (1)
	{
		if (kbhit())
			break; /*按任意键退出*/
		gettime(&newtime); /*检测系统时间*/
		if (newtime.ti_sec != curtime.ti_sec)/*每1秒更新一次时间*/
		{
			if (curpage == 0)
				curpage = 1;
			else
				curpage = 0;
			curtime = newtime;
			/*设置绘图页*/
			setactivepage(curpage);
			/*在图页上画表盘*/
			DrawClock(&curtime);
			/*设置绘图页为当前可见页*/
			setvisualpage(curpage);
			/*0分0秒高声报时*/
			if (newtime.ti_min == 0 && newtime.ti_sec == 0)
				HighBeep();
			/* 59分55至秒时低声报时*/
			else if (newtime.ti_min == 59 &&
				newtime.ti_sec <= 59)
				LowBeep();/*其他时间只发出喀嗒声*/
			else
				Click();
		}
	}
	system("pause");
	closegraph();
}

152、修改环境变量

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
#include <dos.h>

int main(void)
{
   char *path, *ptr;
   int i = 0;
   clrscr();
   puts(" This program is to get the Path and change it.");
   /* 获得当前环境变量中的path信息 */
   ptr = getenv("PATH");

   /* 更新path */
   path = (char *) malloc(strlen(ptr)+15); /*分配内存空间,大小比path的长度多15个字节*/
   strcpy(path,"PATH="); /*复制path*/
   strcat(path,ptr);
   strcat(path,";c:\\temp"); /*在当前的path后追加一个路径*/

   /* 更新路径信息并显示所有的环境变量 */
   putenv(path); /*设置环境变量*/
   while (environ[i]) /*循环输出所有的环境变量*/
       printf(" >> %s\n",environ[i++]);
   printf(" Press any key to quit...");
   getch();
   return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jxiepc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值