⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 王大刚--c语言编程宝典--s.htm

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
      <P>&nbsp;&nbsp; /* output some messages */ <BR>&nbsp;&nbsp; sprintf(msg, 
      "Graphics buffer size: %d", BUFSIZE); <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(x, y, 
      msg); <BR>&nbsp;&nbsp; sprintf(msg, "Old graphics buffer size: %d", 
      oldsize); <BR>&nbsp;&nbsp; outtextxy(x, y+textheight("W"), msg); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: setgraphmode <BR>功&nbsp; 能: 将系统设置成图形模式且清屏 <BR>用&nbsp; 法: void far 
      setgraphmode(int mode); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int x, y; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; x = getmaxx() / 2; <BR>&nbsp;&nbsp; y = getmaxy() / 2; 
<BR>
      <P>&nbsp;&nbsp; /* output a message */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(x, y, 
      "Press any key to exit graphics:"); <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; /* restore system to text mode */ <BR>&nbsp;&nbsp; 
      restorecrtmode(); <BR>&nbsp;&nbsp; printf("We're now in text mode.\n"); 
      <BR>&nbsp;&nbsp; printf("Press any key to return to graphics mode:"); 
      <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; /* return to graphics mode */ <BR>&nbsp;&nbsp; 
      setgraphmode(getgraphmode()); <BR>
      <P>&nbsp;&nbsp; /* output a message */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(x, y, 
      "We're back in graphics mode."); <BR>&nbsp;&nbsp; outtextxy(x, 
      y+textheight("W"), "Press any key to halt:"); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: setjmp <BR>功&nbsp; 能: 非局部转移 <BR>用&nbsp; 法: int setjmp(jmp_buf 
      env); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;process.h&gt; <BR>#include 
      &lt;setjmp.h&gt; <BR>
      <P>void subroutine(void); <BR>
      <P>jmp_buf jumper; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; int value; <BR>
      <P>&nbsp;&nbsp; value = setjmp(jumper); <BR>&nbsp;&nbsp; if (value != 0) 
      <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Longjmp with 
      value %d\n", value); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(value); 
      <BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; printf("About to call subroutine ... 
      \n"); <BR>&nbsp;&nbsp; subroutine(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>
      <P>void subroutine(void) <BR>{ <BR>&nbsp;&nbsp; longjmp(jumper,1); <BR>} 
      <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: setlinestyle <BR>功&nbsp; 能: 设置当前画线宽度和类型 <BR>用&nbsp; 法: void far 
      setlinestyle(int linestype, unsigned upattern); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;string.h&gt; <BR>#include &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; 
      <BR>
      <P>/* the names of the line styles supported */ <BR>char *lname[] = { 
      <BR>&nbsp;&nbsp; "SOLID_LINE", <BR>&nbsp;&nbsp; "DOTTED_LINE", 
      <BR>&nbsp;&nbsp; "CENTER_LINE", <BR>&nbsp;&nbsp; "DASHED_LINE", 
      <BR>&nbsp;&nbsp; "USERBIT_LINE" <BR>&nbsp;&nbsp; }; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>
      <P>&nbsp;&nbsp; int style, midx, midy, userpat; <BR>&nbsp;&nbsp; char 
      stylestr[40]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; midx = getmaxx() / 2; <BR>&nbsp;&nbsp; midy = getmaxy() / 
      2; <BR>
      <P>&nbsp;&nbsp; /* a user defined line pattern */ <BR>&nbsp;&nbsp; /* 
      binary: "0000000000000001"&nbsp; */ <BR>&nbsp;&nbsp; userpat = 1; <BR>
      <P>&nbsp;&nbsp; for (style=SOLID_LINE; style&lt;=USERBIT_LINE; style++) 
      <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* select the line 
      style */ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setlinestyle(style, userpat, 
      1); <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* convert style into a string */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; strcpy(stylestr, lname[style]); <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* draw a line */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; line(0, 0, midx-10, midy); <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* draw a rectangle */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rectangle(0, 0, getmaxx(), getmaxy()); 
      <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* output a message */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outtextxy(midx, midy, stylestr); <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* wait for a key */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cleardevice(); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; closegraph(); 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: setmem <BR>功&nbsp; 能: 存值到存储区 <BR>用&nbsp; 法: void setmem(void 
      *addr, int len, char value); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;alloc.h&gt; <BR>#include 
      &lt;mem.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char *dest; <BR>
      <P>&nbsp;&nbsp; dest = calloc(21, sizeof(char)); <BR>&nbsp;&nbsp; 
      setmem(dest, 20, 'c'); <BR>&nbsp;&nbsp; printf("%s\n", dest); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: setmode <BR>功&nbsp; 能: 设置打开文件方式 <BR>用&nbsp; 法: int setmode(int 
      handle, unsigned mode); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;fcntl.h&gt; <BR>#include 
      &lt;io.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; int result; <BR>
      <P>&nbsp;&nbsp; result = setmode(fileno(stdprn), O_TEXT); <BR>&nbsp;&nbsp; 
      if (result == -1) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("Mode not 
      available\n"); <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Mode successfully switched\n"); <BR>&nbsp;&nbsp; return 0; <BR>} 
      <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: setpalette <BR>功&nbsp; 能: 改变调色板的颜色 <BR>用&nbsp; 法: void far 
      setpalette(int index, int actural_color); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int color, maxcolor, ht; <BR>&nbsp;&nbsp; int y = 10; <BR>&nbsp;&nbsp; 
      char msg[80]; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; maxcolor = getmaxcolor(); <BR>&nbsp;&nbsp; ht = 2 * 
      textheight("W"); <BR>
      <P>&nbsp;&nbsp; /* display the default colors */ <BR>&nbsp;&nbsp; for 
      (color=1; color&lt;=maxcolor; color++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setcolor(color); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf(msg, "Color: %d", color); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outtextxy(1, y, msg); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y += ht; <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* wait for a key */ <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; /* black out the colors one by one */ <BR>&nbsp;&nbsp; for 
      (color=1; color&lt;=maxcolor; color++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setpalette(color, BLACK); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; closegraph(); 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: setrgbpalette <BR>功&nbsp; 能: 定义IBM8514图形卡的颜色 <BR>用&nbsp; 法: void 
      far setrgbpalette(int colornum, int red, int green, int blue); <BR>程序例: 
      <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* select a driver and mode that 
      supports the use */ <BR>&nbsp;&nbsp; /* of the setrgbpalette 
      function.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      */ <BR>&nbsp;&nbsp; int gdriver = VGA, gmode = VGAHI, errorcode; 
      <BR>&nbsp;&nbsp; struct palettetype pal; <BR>&nbsp;&nbsp; int i, ht, y, 
      xmax; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <P>&nbsp;&nbsp; /* read result of initialization */ <BR>&nbsp;&nbsp; 
      errorcode = graphresult(); <BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; 
      /* an error occurred */ <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", 
      grapherrormsg(errorcode)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Press any key to halt:"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      getch(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* grab a copy of the palette */ <BR>&nbsp;&nbsp; 
      getpalette(&amp;pal); <BR>
      <P>&nbsp;&nbsp; /* create gray scale */ <BR>&nbsp;&nbsp; for (i=0; 
      i&lt;pal.size; i++) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      setrgbpalette(pal.colors[i], i*4, i*4, i*4); <BR>
      <P>&nbsp;&nbsp; /* display the gray scale */ <BR>&nbsp;&nbsp; ht = 
      getmaxy() / 16; <BR>&nbsp;&nbsp; xmax = getmaxx(); <BR>&nbsp;&nbsp; y = 0; 
      <BR>&nbsp;&nbsp; for (i=0; i&lt;pal.size; i++) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setfillstyle(SOLID_FILL, i); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar(0, y, xmax, y+ht); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y += ht; <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: settextjustify <BR>功&nbsp; 能: 为图形函数设置文本的对齐方式 <BR>用&nbsp; 法: void 
      far settextjustify(int horiz, int vert); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>/* function prototype */ <BR>void xat(int x, int y); <BR>
      <P>/* horizontal text justification settings */ <BR>char *hjust[] = { 
      "LEFT_TEXT", 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -