📄 王大刚--c语言编程宝典--c.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.hjflying.8u8.com/cl/022.htm -->
<HTML><HEAD><TITLE>王大刚-->C语言编程宝典-->C</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content="王大刚 C语言编程宝典 C" name=keywords>
<META content="王大刚 - C语言编程宝典 - C" name=description>
<STYLE>#page {
LEFT: 0px; POSITION: absolute; TOP: 0px
}
.tt3 {
FONT: 9pt/12pt "宋体"
}
.tt2 {
FONT: 12pt/15pt "宋体"
}
A {
TEXT-DECORATION: none
}
A:hover {
COLOR: blue; TEXT-DECORATION: underline
}
</STYLE>
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY text=#000000 vLink=#006699 aLink=#9900ff link=#006699 bgColor=#ffffff
leftMargin=3 topMargin=3 marginwidth="3" marginheight="3">
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
<TBODY>
<TR>
<TD class=tt3 vAlign=top width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/023.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/021.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>
<TD class=tt2 width="84%" bgColor=#f5f8f8>
<CENTER><B><FONT style="FONT-SIZE: 16.5pt" face=楷体_GB2312
color=#ff6666>C</FONT></B></CENTER>
<HR width="94%" color=#ee9b73 SIZE=1>
<P>函数名: cabs <BR>功 能: 计算复数的绝对值 <BR>用 法: double cabs(struct
complex z); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <math.h> <BR>
<P>int main(void) <BR>{ <BR> struct complex z;
<BR> double val; <BR>
<P> z.x = 2.0; <BR> z.y = 1.0; <BR>
val = cabs(z); <BR>
<P> printf("The absolute value of %.2lfi %.2lfj is %.2lf",
z.x, z.y, val); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: calloc <BR>功 能: 分配主存储器 <BR>用 法: void *calloc(size_t
nelem, size_t elsize); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <alloc.h> <BR>
<P>int main(void) <BR>{ <BR> char *str = NULL; <BR>
<P> /* allocate memory for string */ <BR> str =
calloc(10, sizeof(char)); <BR>
<P> /* copy "Hello" into string */ <BR>
strcpy(str, "Hello"); <BR>
<P> /* display string */ <BR> printf("String is
%s\n", str); <BR>
<P> /* free memory */ <BR> free(str); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: ceil <BR>功 能: 向上舍入 <BR>用 法: double ceil(double x);
<BR>程序例: <BR>
<P>#include <math.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> double number = 123.54;
<BR> double down, up; <BR>
<P> down = floor(number); <BR> up = ceil(number);
<BR>
<P> printf("original number %5.2lf\n",
number); <BR> printf("number rounded down %5.2lf\n", down);
<BR> printf("number rounded up %5.2lf\n", up);
<BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: cgets <BR>功 能: 从控制台读字符串 <BR>用 法: char *cgets(char
*str); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> char buffer[83]; <BR>
char *p; <BR>
<P> /* There's space for 80 characters plus the NULL
terminator */ <BR> buffer[0] = 81; <BR>
<P> printf("Input some chars:"); <BR> p =
cgets(buffer); <BR> printf("\ncgets read %d characters:
\"%s\"\n", buffer[1], p); <BR> printf("The returned pointer is
%p, buffer[0] is at %p\n", p, &buffer); <BR>
<P> /* Leave room for 5 characters plus the NULL terminator */
<BR> buffer[0] = 6; <BR>
<P> printf("Input some chars:"); <BR> p =
cgets(buffer); <BR> printf("\ncgets read %d characters:
\"%s\"\n", buffer[1], p); <BR> printf("The returned pointer is
%p, buffer[0] is at %p\n", p, &buffer); <BR> return 0;
<BR>} <BR> <BR> <BR> <BR>
<P>函数名: chdir <BR>功 能: 改变工作目录 <BR>用 法: int chdir(const char
*path); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <stdlib.h> <BR>#include
<dir.h> <BR>
<P>char old_dir[MAXDIR]; <BR>char new_dir[MAXDIR]; <BR>
<P>int main(void) <BR>{ <BR> if (getcurdir(0, old_dir))
<BR> { <BR>
perror("getcurdir()"); <BR> exit(1);
<BR> } <BR> printf("Current directory is: \\%s\n",
old_dir); <BR>
<P> if (chdir("\\")) <BR> {
<BR> perror("chdir()");
<BR> exit(1); <BR> } <BR>
<P> if (getcurdir(0, new_dir)) <BR> {
<BR> perror("getcurdir()");
<BR> exit(1); <BR> }
<BR> printf("Current directory is now: \\%s\n", new_dir); <BR>
<P> printf("\nChanging back to orignal directory: \\%s\n",
old_dir); <BR> if (chdir(old_dir)) <BR> {
<BR> perror("chdir()");
<BR> exit(1); <BR> } <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: _chmod, chmod <BR>功 能: 改变文件的访问方式 <BR>用 法: int
chmod(const char *filename, int permiss); <BR>程序例: <BR>
<P>#include <sys\stat.h> <BR>#include <stdio.h> <BR>#include
<io.h> <BR>
<P>void make_read_only(char *filename); <BR>
<P>int main(void) <BR>{ <BR> make_read_only("NOTEXIST.FIL");
<BR> make_read_only("MYFILE.FIL"); <BR> return 0;
<BR>} <BR>
<P>void make_read_only(char *filename) <BR>{ <BR> int stat;
<BR>
<P> stat = chmod(filename, S_IREAD); <BR> if
(stat) <BR> printf("Couldn't make %s
read-only\n", filename); <BR> else
<BR> printf("Made %s read-only\n",
filename); <BR>} <BR> <BR> <BR> <BR>
<P>函数名: chsize <BR>功 能: 改变文件大小 <BR>用 法: int chsize(int handle,
long size); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <fcntl.h> <BR>#include
<io.h> <BR>
<P>int main(void) <BR>{ <BR> int handle; <BR> char
buf[11] = "0123456789"; <BR>
<P> /* create text file containing 10 bytes */
<BR> handle = open("DUMMY.FIL", O_CREAT); <BR>
write(handle, buf, strlen(buf)); <BR>
<P> /* truncate the file to 5 bytes in size */
<BR> chsize(handle, 5); <BR>
<P> /* close the file */ <BR> close(handle);
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: circle <BR>功 能: 在给定半径以(x, y)为圆心画圆 <BR>用 法: void far
circle(int x, int y, int radius); <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 midx, midy; <BR> int radius = 100; <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> /* draw the circle */ <BR> circle(midx, midy,
radius); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: cleardevice <BR>功 能: 清除图形屏幕 <BR>用 法: void far
cleardevice(void); <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 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> /* for centering screen messages */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>
<P> /* output a message to the screen */ <BR>
outtextxy(midx, midy, "press any key to clear the screen:"); <BR>
<P> /* wait for a key */ <BR> getch(); <BR>
<P> /* clear the screen */ <BR> cleardevice();
<BR>
<P> /* output another message */ <BR>
outtextxy(midx, midy, "press any key to quit:"); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: clearerr <BR>功 能: 复位错误标志 <BR>用 法:void clearerr(FILE
*stream); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *fp; <BR> char
ch; <BR>
<P> /* open a file for writing */ <BR> fp =
fopen("DUMMY.FIL", "w"); <BR>
<P> /* force an error condition by attempting to read */
<BR> ch = fgetc(fp); <BR> printf("%c\n",ch); <BR>
<P> if (ferror(fp)) <BR> {
<BR> /* display an error message */
<BR> printf("Error reading from
DUMMY.FIL\n"); <BR>
<P> /* reset the error and EOF indicators */
<BR> clearerr(fp); <BR> } <BR>
<P> fclose(fp); <BR> return 0; <BR>} <BR>
<BR> <BR> <BR>
<P>函数名: clearviewport <BR>功 能: 清除图形视区 <BR>用 法: void far
clearviewport(void); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>#define CLIP_ON 1 /* activates clipping in viewport */ <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int ht; <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -