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