📄 030.htm
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>王大刚-->C语言编程宝典-->L</TITLE>
<META NAME="keywords" CONTENT="王大刚 C语言编程宝典 L">
<META NAME="description" CONTENT="王大刚 - C语言编程宝典 - L">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>
<TD CLASS="tt3" VALIGN="top" width="8%" bgcolor="#e0e0e0"><strong><A HREF="031.htm">后一页</A><BR>
<A HREF="029.htm">前一页</A><BR>
<A HREF="index.html">回目录</A><BR>
<A HREF="../../../../index.htm">回首页</A><BR>
</strong>
</TD>
<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">L</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<P>函数名: labs
<BR>功 能: 取长整型绝对值
<BR>用 法: long labs(long n);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>#include <math.h>
<BR>
<P>int main(void)
<BR>{
<BR> long result;
<BR> long x = -12345678L;
<BR>
<P> result= labs(x);
<BR> printf("number: %ld abs value: %ld\n",
<BR> x, result);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: ldexp
<BR>功 能: 计算value*2的幂
<BR>用 法: double ldexp(double value, int exp);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>#include <math.h>
<BR>
<P>int main(void)
<BR>{
<BR> double value;
<BR> double x = 2;
<BR>
<P> /* ldexp raises 2 by a power of 3
<BR> then multiplies the result by 2
*/
<BR> value = ldexp(x,3);
<BR> printf("The ldexp value is: %lf\n",
<BR> value);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: ldiv
<BR>功 能: 两个长整型数相除, 返回商和余数
<BR>用 法: ldiv_t ldiv(long lnumer, long ldenom);
<BR>程序例:
<BR>
<P>/* ldiv example */
<BR>
<P>#include <stdlib.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> ldiv_t lx;
<BR>
<P> lx = ldiv(100000L, 30000L);
<BR> printf("100000 div 30000 = %ld remainder %ld\n", lx.quot,
lx.rem);
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: lfind
<BR>功 能: 执行线性搜索
<BR>用 法: void *lfind(void *key, void *base, int *nelem, int width,
<BR> int (*fcmp)());
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>#include <stdlib.h>
<BR>
<P>int compare(int *x, int *y)
<BR>{
<BR> return( *x - *y );
<BR>}
<BR>
<P>int main(void)
<BR>{
<BR> int array[5] = {35, 87, 46, 99, 12};
<BR> size_t nelem = 5;
<BR> int key;
<BR> int *result;
<BR>
<P> key = 99;
<BR> result = lfind(&key, array, &nelem,
<BR> sizeof(int), (int(*)(const
void *,const void *))compare);
<BR> if (result)
<BR> printf("Number %d found\n",key);
<BR> else
<BR> printf("Number %d not found\n",key);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: line
<BR>功 能: 在指定两点间画一直线
<BR>用 法: void far line(int x0, int y0, int x1, int y1);
<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 xmax, ymax;
<BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, "");
<BR>
<P> /* read result of initialization */
<BR> errorcode = graphresult();
<BR> /* an error occurred */
<BR> if (errorcode != grOk)
<BR> {
<BR> printf("Graphics error: %s\n",
<BR>
grapherrormsg(errorcode));
<BR> printf("Press any key to halt:");
<BR> getch();
<BR> exit(1);
<BR> }
<BR>
<P> setcolor(getmaxcolor());
<BR> xmax = getmaxx();
<BR> ymax = getmaxy();
<BR>
<P> /* draw a diagonal line */
<BR> line(0, 0, xmax, ymax);
<BR>
<P> /* clean up */
<BR> getch();
<BR> closegraph();
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: linerel
<BR>功 能: 从当前位置点(CP)到与CP有一给定相对距离的点画一直线
<BR>用 法: void far linerel(int dx, int dy);
<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> 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)
<BR> {
<BR> printf("Graphics error: %s\n",
<BR> grapherrormsg(errorcode));
<BR> printf("Press any key to halt:");
<BR> getch();
<BR> exit(1);
<BR> }
<BR>
<P> /* move the C.P. to location (20, 30) */
<BR> moveto(20, 30);
<BR>
<P> /* create and output a
<BR> message at (20, 30) */
<BR> sprintf(msg, " (%d, %d)", getx(), gety());
<BR> outtextxy(20, 30, msg);
<BR>
<P> /* draw a line to a point a relative
<BR> distance away from the current
<BR> value of C.P. */
<BR> linerel(100, 100);
<BR>
<P> /* create and output a message at C.P. */
<BR> sprintf(msg, " (%d, %d)", getx(), gety());
<BR> outtext(msg);
<BR>
<P> /* clean up */
<BR> getch();
<BR> closegraph();
<BR> return 0;
<BR>}
<BR>
<BR>
<P>函数名: localtime
<BR>功 能: 把日期和时间转变为结构
<BR>用 法: struct tm *localtime(long *clock);
<BR>程序例:
<BR>
<P>#include <time.h>
<BR>#include <stdio.h>
<BR>#include <dos.h>
<BR>
<P>int main(void)
<BR>{
<BR> time_t timer;
<BR> struct tm *tblock;
<BR>
<P> /* gets time of day */
<BR> timer = time(NULL);
<BR>
<P> /* converts date/time to a structure */
<BR> tblock = localtime(&timer);
<BR>
<P> printf("Local time is: %s", asctime(tblock));
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: lock
<BR>功 能: 设置文件共享锁
<BR>用 法: int lock(int handle, long offset, long length);
<BR>程序例:
<BR>
<P>#include <io.h>
<BR>#include <fcntl.h>
<BR>#include <sys\stat.h>
<BR>#include <process.h>
<BR>#include <share.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> int handle, status;
<BR> long length;
<BR>
<P> /* Must have DOS Share.exe loaded for */
<BR> /* file locking to function properly */
<BR>
<P> handle = sopen("c:\\autoexec.bat",
<BR> O_RDONLY,SH_DENYNO,S_IREAD);
<BR>
<P> if (handle < 0)
<BR> {
<BR> printf("sopen failed\n");
<BR> exit(1);
<BR> }
<BR>
<P> length = filelength(handle);
<BR> status = lock(handle,0L,length/2);
<BR>
<P> if (status == 0)
<BR> printf("lock succeeded\n");
<BR> else
<BR> printf("lock failed\n");
<BR>
<P> status = unlock(handle,0L,length/2);
<BR>
<P> if (status == 0)
<BR> printf("unlock succeeded\n");
<BR> else
<BR> printf("unlock failed\n");
<BR>
<P> close(handle);
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: log
<BR>功 能: 对数函数ln(x)
<BR>用 法: double log(double x);
<BR>程序例:
<BR>
<P>#include <math.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> double result;
<BR> double x = 8.6872;
<BR>
<P> result = log(x);
<BR> printf("The natural log of %lf is %lf\n", x, result);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: log10
<BR>功 能: 对数函数log
<BR>用 法: double log10(double x);
<BR>程序例:
<BR>
<P>#include <math.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> double result;
<BR> double x = 800.6872;
<BR>
<P> result = log10(x);
<BR> printf("The common log of %lf is %lf\n", x, result);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: longjump
<BR>功 能: 执行非局部转移
<BR>用 法: void longjump(jmp_buf env, int val);
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>#include <setjmp.h>
<BR>#include <stdlib.h>
<BR>
<P>void subroutine(jmp_buf);
<BR>
<P>int main(void)
<BR>{
<BR>
<P> int value;
<BR> jmp_buf jumper;
<BR>
<P> value = setjmp(jumper);
<BR> if (value != 0)
<BR> {
<BR> printf("Longjmp with value %d\n", value);
<BR> exit(value);
<BR> }
<BR> printf("About to call subroutine ... \n");
<BR> subroutine(jumper);
<BR>
<P> return 0;
<BR>}
<BR>
<P>void subroutine(jmp_buf jumper)
<BR>{
<BR> longjmp(jumper,1);
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: lowvideo
<BR>功 能: 选择低亮度字符
<BR>用 法: void lowvideo(void);
<BR>程序例:
<BR>
<P>#include <conio.h>
<BR>
<P>int main(void)
<BR>{
<BR> clrscr();
<BR>
<P> highvideo();
<BR> cprintf("High Intesity Text\r\n");
<BR> lowvideo();
<BR> gotoxy(1,2);
<BR> cprintf("Low Intensity Text\r\n");
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: lrotl, _lrotl
<BR>功 能: 将无符号长整型数向左循环移位
<BR>用 法: unsigned long lrotl(unsigned long lvalue, int count);
<BR> unsigned long _lrotl(unsigned long lvalue, int count);
<BR>程序例:
<BR>
<P>/* lrotl example */
<BR>#include <stdlib.h>
<BR>#include <stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR> unsigned long result;
<BR> unsigned long value = 100;
<BR>
<P> result = _lrotl(value,1);
<BR> printf("The value %lu rotated left one bit is: %lu\n",
value, result);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函数名: lsearch
<BR>功 能: 线性搜索
<BR>用 法: void *lsearch(const void *key, void *base, size_t *nelem,
<BR> size_t width, int (*fcmp)(const
void *, const void *));
<BR>程序例:
<BR>
<P>#include <stdio.h>
<BR>#include <stdlib.h>
<BR>
<P>int compare(int *x, int *y)
<BR>{
<BR> return( *x - *y );
<BR>}
<BR>
<P>int main(void)
<BR>{
<BR> int array[5] = {35, 87, 46, 99, 12};
<BR> size_t nelem = 5;
<BR> int key;
<BR> int *result;
<BR>
<P> key = 99;
<BR> result = lfind(&key, array, &nelem,
<BR>
sizeof(int), (int(*)(const void *,const void *))compare);
<BR> if (result)
<BR> printf("Number %d found\n",key);
<BR> else
<BR> printf("Number %d not found\n",key);
<BR>
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<BR>
<P>函数名: lseek
<BR>功 能: 移动文件读/写指针
<BR>用 法: long lseek(int handle, long offset, int fromwhere);
<BR>程序例:
<BR>
<P>#include <sys\stat.h>
<BR>#include <string.h>
<BR>#include <stdio.h>
<BR>#include <fcntl.h>
<BR>#include <io.h>
<BR>
<P>int main(void)
<BR>{
<BR> int handle;
<BR> char msg[] = "This is a test";
<BR> char ch;
<BR>
<P> /* create a file */
<BR> handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD |
S_IWRITE);
<BR>
<P> /* write some data to the file */
<BR> write(handle, msg, strlen(msg));
<BR>
<P> /* seek to the begining of the file */
<BR> lseek(handle, 0L, SEEK_SET);
<BR>
<P> /* reads chars from the file until we hit EOF */
<BR> do
<BR> {
<BR> read(handle, &ch, 1);
<BR> printf("%c", ch);
<BR> } while (!eof(handle));
<BR>
<P> close(handle);
<BR> return 0;
<BR>}
<BR>
<BR>
<hr color="#EE9B73" size="1" width="94%">
</TD>
<TD CLASS="tt3" VALIGN="bottom" width="8%" bgcolor="#e0e0e0"><strong><A HREF="031.htm">后一页</A><BR>
<A HREF="029.htm">前一页</A><BR>
<A HREF="index.html">回目录</A><BR>
<A HREF="../../../../index.htm">回首页</A><BR>
</strong>
</TD>
</TR>
</table>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -