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

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

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
      strcpy(tp,""); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcpy(tp,prefix); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcat(tp,"8x |"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf(tp,I); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcpy(tp,""); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcpy(tp,prefix); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcat(tp,"10.2e |"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf(tp,R); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcpy(tp,prefix); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcat(tp,"10.2f |"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf(tp,R); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("&nbsp; \n"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      strcpy(prefix,"%"); <BR>&nbsp;&nbsp;&nbsp;&nbsp; } 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>&nbsp;&nbsp; return 0; 
      <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: putc <BR>功&nbsp; 能: 输出一字符到指定流中 <BR>用&nbsp; 法: int putc(int ch, 
      FILE *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char msg[] = "Hello world\n"; 
      <BR>&nbsp;&nbsp; int i = 0; <BR>
      <P>&nbsp;&nbsp; while (msg[i]) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      putc(msg[i++], stdout); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: putch <BR>功&nbsp; 能: 输出字符到控制台 <BR>用&nbsp; 法: int putch(int ch); 
      <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char ch = 0; <BR>
      <P>&nbsp;&nbsp; printf("Input a string:"); <BR>&nbsp;&nbsp; while ((ch != 
      '\r')) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch = getch(); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putch(ch); <BR>&nbsp;&nbsp; } 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: putchar <BR>功&nbsp; 能: 在stdout上输出字符 <BR>用&nbsp; 法: int putchar(int 
      ch); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>/* define some box-drawing characters */ <BR>#define LEFT_TOP&nbsp; 
      0xDA <BR>#define RIGHT_TOP 0xBF <BR>#define HORIZ&nbsp;&nbsp;&nbsp;&nbsp; 
      0xC4 <BR>#define VERT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xB3 <BR>#define 
      LEFT_BOT&nbsp; 0xC0 <BR>#define RIGHT_BOT 0xD9 <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char i, j; <BR>
      <P>&nbsp;&nbsp; /* draw the top of the box */ <BR>&nbsp;&nbsp; 
      putchar(LEFT_TOP); <BR>&nbsp;&nbsp; for (i=0; i&lt;10; i++) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(HORIZ); <BR>&nbsp;&nbsp; 
      putchar(RIGHT_TOP); <BR>&nbsp;&nbsp; putchar('\n'); <BR>
      <P>&nbsp;&nbsp; /* draw the middle */ <BR>&nbsp;&nbsp; for (i=0; i&lt;4; 
      i++) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(VERT); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=0; j&lt;10; j++) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(' '); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(VERT); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar('\n'); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* draw the bottom */ <BR>&nbsp;&nbsp; putchar(LEFT_BOT); 
      <BR>&nbsp;&nbsp; for (i=0; i&lt;10; i++) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(HORIZ); <BR>&nbsp;&nbsp; 
      putchar(RIGHT_BOT); <BR>&nbsp;&nbsp; putchar('\n'); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: putenv <BR>功&nbsp; 能: 把字符串加到当前环境中 <BR>用&nbsp; 法: int putenv(char 
      *envvar); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;alloc.h&gt; <BR>#include &lt;string.h&gt; <BR>#include &lt;dos.h&gt; 
      <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char *path, *ptr; 
      <BR>&nbsp;&nbsp; int i = 0; <BR>
      <P>&nbsp;&nbsp; /* get the current path environment */ <BR>&nbsp;&nbsp; 
      ptr = getenv("PATH"); <BR>
      <P>&nbsp;&nbsp; /* set up new path */ <BR>&nbsp;&nbsp; path = 
      malloc(strlen(ptr)+15); <BR>&nbsp;&nbsp; strcpy(path,"PATH="); 
      <BR>&nbsp;&nbsp; strcat(path,ptr); <BR>&nbsp;&nbsp; 
      strcat(path,";c:\\temp"); <BR>
      <P>&nbsp;&nbsp; /* replace the current path and display current 
      environment */ <BR>&nbsp;&nbsp; putenv(path); <BR>&nbsp;&nbsp; while 
      (environ[i]) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("%s\n",environ[i++]); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: putimage <BR>功&nbsp; 能: 在屏幕上输出一个位图 <BR>用&nbsp; 法: void far 
      putimage(int x, int y, void far *bitmap, int op); <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>#define ARROW_SIZE 10 <BR>
      <P>void draw_arrow(int x, int y); <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request autodetection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      void *arrow; <BR>&nbsp;&nbsp; int x, y, maxx; <BR>&nbsp;&nbsp; unsigned 
      int size; <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; maxx = getmaxx(); <BR>&nbsp;&nbsp; x = 0; <BR>&nbsp;&nbsp; 
      y = getmaxy() / 2; <BR>
      <P>&nbsp;&nbsp; /* draw the image to be grabbed */ <BR>&nbsp;&nbsp; 
      draw_arrow(x, y); <BR>
      <P>&nbsp;&nbsp; /* calculate the size of the image */ <BR>&nbsp;&nbsp; 
      size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE); <BR>
      <P>&nbsp;&nbsp; /* allocate memory to hold the image */ <BR>&nbsp;&nbsp; 
      arrow = malloc(size); <BR>
      <P>&nbsp;&nbsp; /* grab the image */ <BR>&nbsp;&nbsp; getimage(x, 
      y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow); <BR>
      <P>&nbsp;&nbsp; /* repeat until a key is pressed */ <BR>&nbsp;&nbsp; while 
      (!kbhit()) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* erase 
      old image */ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putimage(x, y-ARROW_SIZE, 
      arrow, XOR_PUT); <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x += ARROW_SIZE; 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x &gt;= maxx) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 0; <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* plot new image */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putimage(x, y-ARROW_SIZE, arrow, 
      XOR_PUT); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; free(arrow); 
      <BR>&nbsp;&nbsp; closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>
      <P>void draw_arrow(int x, int y) <BR>{ <BR>&nbsp;&nbsp; /* draw an arrow 
      on the screen */ <BR>&nbsp;&nbsp; moveto(x, y); <BR>&nbsp;&nbsp; 
      linerel(4*ARROW_SIZE, 0); <BR>&nbsp;&nbsp; linerel(-2*ARROW_SIZE, 
      -1*ARROW_SIZE); <BR>&nbsp;&nbsp; linerel(0, 2*ARROW_SIZE); 
      <BR>&nbsp;&nbsp; linerel(2*ARROW_SIZE, -1*ARROW_SIZE); <BR>} <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: putpixel <BR>功&nbsp; 能: 在指定位置画一像素 <BR>用&nbsp; 法: void far putpixel 
      (int x, int y, int pixelcolor); <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>#include &lt;dos.h&gt; 
      <BR>
      <P>#define PIXEL_COUNT 1000 <BR>#define DELAY_TIME&nbsp; 100&nbsp; /* in 
      milliseconds */ <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request autodetection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int i, x, y, color, maxx, maxy, maxcolor, seed; <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; maxx = getmaxx() + 1; <BR>&nbsp;&nbsp; maxy = getmaxy() + 
      1; <BR>&nbsp;&nbsp; maxcolor = getmaxcolor() + 1; <BR>
      <P>&nbsp;&nbsp; while (!kbhit()) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* seed the random number generator */ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seed = random(32767); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; srand(seed); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0; i&lt;PIXEL_COUNT; i++) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <BR>&nbsp; x = random(maxx); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y = random(maxy); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color = 
      random(maxcolor); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      putpixel(x, y, color); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(DELAY_TIME); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; srand(seed); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0; i&lt;PIXEL_COUNT; i++) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <BR>&nbsp; x = random(maxx); 
      <BR>&nbsp; y = random(maxy); <BR>&nbsp; color = random(maxcolor); 
      <BR>&nbsp; if (color == getpixel(x, y)) <BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      putpixel(x, y, 0); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <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>
      <P>函数名: puts <BR>功&nbsp; 能: 送一字符串到流中 <BR>用&nbsp; 法: int puts(char 
      *string); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>int main(void) <BR>{ <BR>&nbsp;&nbsp; char 
      string[] = "This is an example output string\n"; <BR>
      <P>&nbsp;&nbsp; puts(string); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: puttext <BR>功&nbsp; 能: 将文本从存储区拷贝到屏幕 <BR>用&nbsp; 法: int puttext(int 
      left, int top, int right, int bottom, void *source); <BR>程序例: <BR>
      <P>#include &lt;conio.h&gt; <BR>int main(void) <BR>{ <BR>&nbsp;&nbsp; char 
      buffer[512]; <BR>
      <P>&nbsp;&nbsp; /* put some text to the console */ <BR>&nbsp;&nbsp; 
      clrscr(); <BR>&nbsp;&nbsp; gotoxy(20, 12); <BR>&nbsp;&nbsp; cprintf("This 
      is a test.&nbsp; Press any key to continue ..."); <BR>&nbsp;&nbsp; 
      getch(); <BR>
      <P>&nbsp;&nbsp; /* grab screen contents */ <BR>&nbsp;&nbsp; gettext(20, 
      12, 36, 21,buffer); <BR>&nbsp;&nbsp; clrscr(); <BR>
      <P>&nbsp;&nbsp; /* put selected characters back to the screen */ 
      <BR>&nbsp;&nbsp; gotoxy(20, 12); <BR>&nbsp;&nbsp; puttext(20, 12, 36, 21, 
      buffer); <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: putw <BR>功&nbsp; 能: 把一字符或字送到流中 <BR>用&nbsp; 法: int putw(int w, FILE 
      *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>
      <P>#define FNAME "test.$$$" <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; FILE *fp; <BR>&nbsp;&nbsp; int 
      word; <BR>
      <P>&nbsp;&nbsp; /* place the word in a file */ <BR>&nbsp;&nbsp; fp = 
      fopen(FNAME, "wb"); <BR>&nbsp;&nbsp; if (fp == NULL) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error opening file %s\n", 
      FNAME); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } 
<BR>
      <P>&nbsp;&nbsp; word = 94; <BR>&nbsp;&nbsp; putw(word,fp); 
      <BR>&nbsp;&nbsp; if (ferror(fp)) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Error writing to file\n"); <BR>&nbsp;&nbsp; else 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Successful write\n"); 
      <BR>&nbsp;&nbsp; fclose(fp); <BR>
      <P>&nbsp;&nbsp; /* reopen the file */ <BR>&nbsp;&nbsp; fp = fopen(FNAME, 
      "rb"); <BR>&nbsp;&nbsp; if (fp == NULL) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error opening file %s\n", 
      FNAME); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } 
<BR>
      <P>&nbsp;&nbsp; /* extract the word */ <BR>&nbsp;&nbsp; word = getw(fp); 
      <BR>&nbsp;&nbsp; if (ferror(fp)) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Error reading file\n"); <BR>&nbsp;&nbsp; else 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Successful read: word = 
      %d\n", word); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; fclose(fp); 
      <BR>&nbsp;&nbsp; unlink(FNAME); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>
      <HR width="94%" color=#ee9b73 SIZE=1>
    </TD>
    <TD class=tt3 vAlign=bottom width="8%" bgColor=#e0e0e0><STRONG><A 
      href="http://www.hjflying.8u8.com/cl/034.htm">后一页</A><BR><A 
      href="http://www.hjflying.8u8.com/cl/032.htm">前一页</A><BR><A 
      href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A 
      href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD></TR></TBODY></TABLE></BODY></HTML>

⌨️ 快捷键说明

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