📄 王大刚--c语言编程宝典--s.htm
字号:
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
struct palettetype pal; <BR> int color, maxcolor, ht;
<BR> int y = 10; <BR> char msg[80]; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> maxcolor = getmaxcolor(); <BR> ht = 2 *
textheight("W"); <BR>
<P> /* grab a copy of the palette */ <BR>
getpalette(&pal); <BR>
<P> /* display the default palette colors */ <BR>
for (color=1; color<=maxcolor; color++) <BR> {
<BR> setcolor(color);
<BR> sprintf(msg, "Color: %d", color);
<BR> outtextxy(1, y, msg);
<BR> y += ht; <BR> } <BR>
<P> /* wait for a key */ <BR> getch(); <BR>
<P> /* black out the colors one by one */ <BR> for
(color=1; color<=maxcolor; color++) <BR> {
<BR> setpalette(color, BLACK);
<BR> getch(); <BR> } <BR>
<P> /* restore the palette colors */ <BR>
setallpalette(&pal); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setaspectratio <BR>功 能: 设置图形纵横比 <BR>用 法: void far
setaspectratio(int xasp, int yasp); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int xasp, yasp, midx, midy; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> midx = getmaxx() / 2; <BR> midy = getmaxy() /
2; <BR> setcolor(getmaxcolor()); <BR>
<P> /* get current aspect ratio settings */ <BR>
getaspectratio(&xasp, &yasp); <BR>
<P> /* draw normal circle */ <BR> circle(midx,
midy, 100); <BR> getch(); <BR>
<P> /* claer the screen */ <BR> cleardevice();
<BR>
<P> /* adjust the aspect for a wide circle */ <BR>
setaspectratio(xasp/2, yasp); <BR> circle(midx, midy, 100);
<BR> getch(); <BR>
<P> /* adjust the aspect for a narrow circle */
<BR> cleardevice(); <BR> setaspectratio(xasp,
yasp/2); <BR> circle(midx, midy, 100); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setbkcolor <BR>功 能: 用调色板设置当前背景颜色 <BR>用 法: void far
setbkcolor(int color); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* select a driver and mode that
supports */ <BR> /* multiple background
colors.
*/ <BR> int gdriver = EGA, gmode = EGAHI, errorcode;
<BR> int bkcol, maxcolor, x, y; <BR> char msg[80];
<BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> /* maximum color index supported */ <BR>
maxcolor = getmaxcolor(); <BR>
<P> /* for centering text messages */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> x = getmaxx() /
2; <BR> y = getmaxy() / 2; <BR>
<P> /* loop through the available colors */ <BR>
for (bkcol=0; bkcol<=maxcolor; bkcol++) <BR> {
<BR> /* clear the screen */
<BR> cleardevice(); <BR>
<P> /* select a new background color */
<BR> setbkcolor(bkcol); <BR>
<P> /* output a messsage */
<BR> if (bkcol == WHITE) <BR>
setcolor(EGA_BLUE); <BR> sprintf(msg,
"Background color: %d", bkcol); <BR>
outtextxy(x, y, msg); <BR> getch();
<BR> } <BR>
<P> /* clean up */ <BR> closegraph();
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setblock <BR>功 能: 修改先前已分配的DOS存储段大小 <BR>用 法: int
setblock(int seg, int newsize); <BR>程序例: <BR>
<P>#include <dos.h> <BR>#include <alloc.h> <BR>#include
<stdio.h> <BR>#include <stdlib.h> <BR>
<P>int main(void) <BR>{ <BR> unsigned int size, segp;
<BR> int stat; <BR>
<P> size = 64; /* (64 x 16) = 1024 bytes */ <BR>
stat = allocmem(size, &segp); <BR> if (stat == -1)
<BR> printf("Allocated memory at segment:
%X\n", segp); <BR> else <BR> {
<BR> printf("Failed: maximum number of
paragraphs available is %d\n", <BR> stat);
<BR> exit(1); <BR> } <BR>
<P> stat = setblock(segp, size * 2); <BR> if (stat
== -1) <BR> printf("Expanded memory block at
segment: %X\n", segp); <BR> else
<BR> printf("Failed: maximum number of
paragraphs available is %d\n",
<BR>
stat); <BR>
<P> freemem(segp); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setbuf <BR>功 能: 把缓冲区与流相联 <BR>用 法: void setbuf(FILE
*steam, char *buf); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>/* BUFSIZ is defined in stdio.h */ <BR>char outbuf[BUFSIZ]; <BR>
<P>int main(void) <BR>{ <BR> /* attach a buffer to the
standard output stream */ <BR> setbuf(stdout, outbuf); <BR>
<P> /* put some characters into the buffer */ <BR>
puts("This is a test of buffered output.\n\n"); <BR>
puts("This output will go into outbuf\n"); <BR> puts("and
won't appear until the buffer\n"); <BR> puts("fills up or we
flush the stream.\n"); <BR>
<P> /* flush the output buffer */ <BR>
fflush(stdout); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setcbrk <BR>功 能: 设置Control-break <BR>用 法: int
setcbrk(int value); <BR>程序例: <BR>
<P>#include <dos.h> <BR>#include <conio.h> <BR>#include
<stdio.h> <BR>
<P>int main(void) <BR>{ <BR> int break_flag; <BR>
<P> printf("Enter 0 to turn control break off\n");
<BR> printf("Enter 1 to turn control break on\n"); <BR>
<P> break_flag = getch() - 0; <BR>
<P> setcbrk(break_flag); <BR>
<P> if (getcbrk()) <BR>
printf("Cntrl-brk flag is on\n"); <BR> else
<BR> printf("Cntrl-brk flag is off\n");
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: setcolor <BR>功 能: 设置当前画线颜色 <BR>用 法: void far
setcolor(int color); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* select a driver and mode that
supports */ <BR> /* multiple drawing
colors.
*/ <BR> int gdriver = EGA, gmode = EGAHI, errorcode;
<BR> int color, maxcolor, x, y; <BR> char msg[80];
<BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> /* maximum color index supported */ <BR>
maxcolor = getmaxcolor(); <BR>
<P> /* for centering text messages */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> x = getmaxx() /
2; <BR> y = getmaxy() / 2; <BR>
<P> /* loop through the available colors */ <BR>
for (color=1; color<=maxcolor; color++) <BR> {
<BR> /* clear the screen */
<BR> cleardevice(); <BR>
<P> /* select a new background color */
<BR> setcolor(color); <BR>
<P> /* output a messsage */
<BR> sprintf(msg, "Color: %d", color);
<BR> outtextxy(x, y, msg);
<BR> getch(); <BR> } <BR>
<P> /* clean up */ <BR> closegraph();
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: setdate <BR>功 能: 设置DOS日期 <BR>用 法: void setdate(struct
date *dateblk); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <process.h> <BR>#include
<dos.h> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -