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

📄 023.htm

📁 一个好的讲DSP中C语言编程的电子书
💻 HTM
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>王大刚-->C语言编程宝典-->D</TITLE>
<META NAME="keywords" CONTENT="王大刚 C语言编程宝典 D">
<META NAME="description" CONTENT="王大刚 - C语言编程宝典 - D">

<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="024.htm">后一页</A><BR>
<A HREF="022.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">D</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<P>函数名: delay
<BR>功&nbsp; 能: 将程序的执行暂停一段时间(毫秒)
<BR>用&nbsp; 法: void delay(unsigned milliseconds);
<BR>程序例:
<BR>/* Emits a 440-Hz tone for 500 milliseconds */
<BR>#include &lt;dos.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; sound(440);
<BR>&nbsp;&nbsp; delay(500);
<BR>&nbsp;&nbsp; nosound();
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: delline
<BR>功&nbsp; 能: 在文本窗口中删去一行
<BR>用&nbsp; 法: void delline(void);
<BR>程序例:
<BR>
<P>#include &lt;conio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; clrscr();
<BR>&nbsp;&nbsp; cprintf("The function DELLINE deletes \
<BR>&nbsp;&nbsp;&nbsp; the line containing the\r\n");
<BR>&nbsp;&nbsp; cprintf("cursor and moves all lines \
<BR>&nbsp;&nbsp;&nbsp; below it one line up.\r\n");
<BR>&nbsp;&nbsp; cprintf("DELLINE operates within the \
<BR>&nbsp;&nbsp;&nbsp; currently active text\r\n");
<BR>&nbsp;&nbsp; cprintf("window.&nbsp; Press any key to \
<BR>&nbsp;&nbsp;&nbsp; continue . . .");
<BR>&nbsp;&nbsp; gotoxy(1,2);&nbsp; /* Move the cursor to the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; second line and first column */
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; delline();
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>
<P>函数名: detectgraph
<BR>功&nbsp; 能: 通过检测硬件确定图形驱动程序和模式
<BR>用&nbsp; 法: void far detectgraph(int far *graphdriver, int far *graphmode);
<BR>程序例:
<BR>
<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<P>/* names of the various cards supported */
<BR>char *dname[] = { "requests detection",
<BR>&nbsp;&nbsp;&nbsp; "a CGA",
<BR>&nbsp;&nbsp;&nbsp; "an MCGA",
<BR>&nbsp;&nbsp;&nbsp; "an EGA",
<BR>&nbsp;&nbsp;&nbsp; "a 64K EGA",
<BR>&nbsp;&nbsp;&nbsp; "a monochrome EGA",
<BR>&nbsp;&nbsp;&nbsp; "an IBM 8514",
<BR>&nbsp;&nbsp;&nbsp; "a Hercules monochrome",
<BR>&nbsp;&nbsp;&nbsp; "an AT&amp;T 6300 PC",
<BR>&nbsp;&nbsp;&nbsp; "a VGA",
<BR>&nbsp;&nbsp;&nbsp; "an IBM 3270 PC"
<BR>&nbsp; };
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* returns detected hardware info. */
<BR>&nbsp;&nbsp; int gdriver, gmode, errorcode;
<BR>
<P>&nbsp; /* detect graphics hardware available */
<BR>&nbsp;&nbsp; detectgraph(&amp;gdriver, &amp;gmode);
<BR>
<P>&nbsp;&nbsp; /* read result of detectgraph call */
<BR>&nbsp;&nbsp; errorcode = graphresult();
<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; occurred */
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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
<BR>&nbsp;&nbsp;&nbsp; code */
<BR>&nbsp;&nbsp; }
<BR>
<P>&nbsp;&nbsp; /* display the information detected */
<BR>&nbsp;&nbsp; clrscr();
<BR>&nbsp;&nbsp; printf("You have %s video display \
<BR>&nbsp;&nbsp; card.\n", dname[gdriver]);
<BR>&nbsp;&nbsp; printf("Press any key to halt:");
<BR>&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: difftime
<BR>功&nbsp; 能: 计算两个时刻之间的时间差
<BR>用&nbsp; 法: double difftime(time_t time2, time_t time1);
<BR>程序例:
<BR>
<P>#include &lt;time.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;dos.h>
<BR>#include &lt;conio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; time_t first, second;
<BR>
<P>&nbsp;&nbsp; clrscr();
<BR>&nbsp;&nbsp; first = time(NULL);&nbsp; /* Gets system
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time */
<BR>&nbsp;&nbsp; delay(2000);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* Waits 2 secs */
<BR>&nbsp;&nbsp; second = time(NULL); /* Gets system time
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; again */
<BR>
<P>&nbsp;&nbsp; printf("The difference is: %f \
<BR>&nbsp;&nbsp; seconds\n",difftime(second,first));
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: disable
<BR>功&nbsp; 能: 屏蔽中断
<BR>用&nbsp; 法: void disable(void);
<BR>程序例:
<BR>
<P>/***NOTE: This is an interrupt service
<BR>&nbsp;routine. You cannot compile this program
<BR>&nbsp;with Test Stack Overflow turned on and
<BR>&nbsp;get an executable file that operates
<BR>&nbsp;correctly. */
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;dos.h>
<BR>#include &lt;conio.h>
<BR>
<P>#define INTR 0X1C&nbsp;&nbsp;&nbsp; /* The clock tick
<BR>&nbsp;&nbsp; interrupt */
<BR>
<P>void interrupt ( *oldhandler)(void);
<BR>
<P>int count=0;
<BR>
<P>void interrupt handler(void)
<BR>{
<BR>/* disable interrupts during the handling of
<BR>&nbsp;&nbsp; the interrupt */
<BR>&nbsp;&nbsp; disable();
<BR>/* increase the global counter */
<BR>&nbsp;&nbsp; count++;
<BR>/* reenable interrupts at the end of the
<BR>&nbsp;&nbsp; handler */
<BR>&nbsp;&nbsp; enable();
<BR>/* call the old routine */
<BR>&nbsp;&nbsp; oldhandler();
<BR>}
<BR>
<P>int main(void)
<BR>{
<BR>/* save the old interrupt vector */
<BR>&nbsp;&nbsp; oldhandler = getvect(INTR);
<BR>
<P>/* install the new interrupt handler */
<BR>&nbsp;&nbsp; setvect(INTR, handler);
<BR>
<P>/* loop until the counter exceeds 20 */
<BR>&nbsp;&nbsp; while (count &lt; 20)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("count is %d\n",count);
<BR>
<P>/* reset the old interrupt handler */
<BR>&nbsp;&nbsp; setvect(INTR, oldhandler);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>
<P>函数名: div
<BR>功&nbsp; 能: 将两个整数相除, 返回商和余数
<BR>用&nbsp; 法: div_t (int number, int denom);
<BR>程序例:
<BR>
<P>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>
<P>div_t x;
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; x = div(10,3);
<BR>&nbsp;&nbsp; printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: dosexterr
<BR>功&nbsp; 能: 获取扩展DOS错误信息
<BR>用&nbsp; 法: int dosexterr(struct DOSERR *dblkp);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;dos.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; FILE *fp;
<BR>&nbsp;&nbsp; struct DOSERROR info;
<BR>
<P>&nbsp;&nbsp; fp = fopen("perror.dat","r");
<BR>&nbsp;&nbsp; if (!fp) perror("Unable to open file for
<BR>&nbsp;&nbsp;&nbsp;&nbsp; reading");
<BR>&nbsp;&nbsp; dosexterr(&amp;info);
<BR>
<P>&nbsp;&nbsp; printf("Extended DOS error \
<BR>&nbsp;&nbsp; information:\n");
<BR>&nbsp;&nbsp; printf("&nbsp;&nbsp; Extended error: \
<BR>&nbsp;&nbsp; %d\n",info.exterror);
<BR>&nbsp;&nbsp; printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Class: \
<BR>&nbsp;&nbsp; %x\n",info.class);
<BR>&nbsp;&nbsp; printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Action: \
<BR>&nbsp;&nbsp; %x\n",info.action);
<BR>&nbsp;&nbsp; printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Error Locus: \
<BR>&nbsp;&nbsp; %x\n",info.locus);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: dostounix
<BR>功&nbsp; 能: 转换日期和时间为UNIX时间格式
<BR>用&nbsp; 法: long dostounix(struct date *dateptr, struct time *timeptr);
<BR>程序例:
<BR>
<P>&nbsp;#include &lt;time.h>
<BR>&nbsp;#include &lt;stddef.h>
<BR>&nbsp;#include &lt;dos.h>
<BR>&nbsp;#include &lt;stdio.h>
<BR>
<P>&nbsp;int main(void)
<BR>&nbsp;{
<BR>&nbsp;&nbsp;&nbsp; time_t t;
<BR>&nbsp;&nbsp;&nbsp; struct time d_time;
<BR>&nbsp;&nbsp;&nbsp; struct date d_date;
<BR>&nbsp;&nbsp;&nbsp; struct tm *local;
<BR>
<P>&nbsp;&nbsp;&nbsp; getdate(&amp;d_date);
<BR>&nbsp;&nbsp;&nbsp; gettime(&amp;d_time);
<BR>
<P>&nbsp;&nbsp;&nbsp; t = dostounix(&amp;d_date, &amp;d_time);
<BR>&nbsp;&nbsp;&nbsp; local = localtime(&amp;t);
<BR>&nbsp;&nbsp;&nbsp; printf("Time and Date: %s\n", \
<BR>&nbsp;&nbsp;&nbsp; asctime(local));
<BR>
<P>&nbsp;&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: drawpoly
<BR>功&nbsp; 能: 画多边形
<BR>用&nbsp; 法: void far drawpoly(int numpoints, int far *polypoints);
<BR>程序例:
<BR>
<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* request auto detection */
<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;
<BR>&nbsp;&nbsp; int maxx, maxy;
<BR>
<P>&nbsp;&nbsp; /* our polygon array */
<BR>&nbsp;&nbsp; int poly[10];
<BR>
<P>&nbsp;&nbsp; /* initialize graphics and local
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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)
<BR>&nbsp;&nbsp; /* an error occurred */
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grapherrormsg(errorcode));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; /* terminate with an error code */
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
<BR>&nbsp;&nbsp; }
<BR>
<P>&nbsp;&nbsp; maxx = getmaxx();
<BR>&nbsp;&nbsp; maxy = getmaxy();
<BR>
<P>&nbsp;&nbsp; poly[0] = 20;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* 1st vertext */
<BR>&nbsp;&nbsp; poly[1] = maxy / 2;
<BR>
<P>&nbsp;&nbsp; poly[2] = maxx - 20; /* 2nd */
<BR>&nbsp;&nbsp; poly[3] = 20;
<BR>
<P>&nbsp;&nbsp; poly[4] = maxx - 50; /* 3rd */
<BR>&nbsp;&nbsp; poly[5] = maxy - 20;
<BR>
<P>&nbsp;&nbsp; poly[6] = maxx / 2;&nbsp; /* 4th */
<BR>&nbsp;&nbsp; poly[7] = maxy / 2;
<BR>/*
<BR>&nbsp;&nbsp; drawpoly doesn't automatically close
<BR>&nbsp;&nbsp; the polygon, so we close it.
<BR>*/
<BR>&nbsp;&nbsp; poly[8] = poly[0];
<BR>&nbsp;&nbsp; poly[9] = poly[1];
<BR>
<P>&nbsp;&nbsp; /* draw the polygon */
<BR>&nbsp;&nbsp; drawpoly(5, poly);
<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>函数名: dup
<BR>功&nbsp; 能: 复制一个文件句柄
<BR>用&nbsp; 法: int dup(int handle);
<BR>程序例:
<BR>
<P>#include &lt;string.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>#include &lt;io.h>
<BR>
<P>void flush(FILE *stream);
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; FILE *fp;
<BR>&nbsp;&nbsp; char msg[] = "This is a test";
<BR>
<P>&nbsp;&nbsp; /* create a file */
<BR>&nbsp;&nbsp; fp = fopen("DUMMY.FIL", "w");
<BR>
<P>&nbsp;&nbsp; /* write some data to the file */
<BR>&nbsp;&nbsp; fwrite(msg, strlen(msg), 1, fp);
<BR>
<P>&nbsp;&nbsp; clrscr();
<BR>&nbsp;&nbsp; printf("Press any key to flush \
<BR>&nbsp;&nbsp; DUMMY.FIL:");
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; /* flush the data to DUMMY.FIL without
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; closing it */
<BR>&nbsp;&nbsp; flush(fp);
<BR>
<P>&nbsp;&nbsp; printf("\nFile was flushed, Press any \
<BR>&nbsp;&nbsp; key to quit:");
<BR>&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>
<P>void flush(FILE *stream)
<BR>{
<BR>&nbsp;&nbsp; int duphandle;
<BR>
<P>&nbsp;&nbsp; /* flush TC's internal buffer */
<BR>&nbsp;&nbsp; fflush(stream);
<BR>
<P>&nbsp;&nbsp; /* make a duplicate file handle */
<BR>&nbsp;&nbsp; duphandle = dup(fileno(stream));
<BR>
<P>&nbsp;&nbsp; /* close the duplicate handle to flush the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DOS buffer */
<BR>&nbsp;&nbsp; close(duphandle);
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: dup2
<BR>功&nbsp; 能: 复制文件句柄
<BR>用&nbsp; 法: int dup2(int oldhandle, int newhandle);
<BR>程序例:
<BR>
<P>#include &lt;sys\stat.h>
<BR>#include &lt;string.h>
<BR>#include &lt;fcntl.h>
<BR>#include &lt;io.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; #define STDOUT 1
<BR>
<P>&nbsp;&nbsp; int nul, oldstdout;
<BR>&nbsp;&nbsp; char msg[] = "This is a test";
<BR>
<P>&nbsp;&nbsp; /* create a file */
<BR>&nbsp;&nbsp; nul = open("DUMMY.FIL", O_CREAT | O_RDWR,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; S_IREAD | S_IWRITE);
<BR>
<P>&nbsp;&nbsp; /* create a duplicate handle for standard
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output */
<BR>&nbsp;&nbsp; oldstdout = dup(STDOUT);
<BR>&nbsp;&nbsp; /*
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redirect standard output to DUMMY.FIL
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; by duplicating the file handle onto
the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file handle for standard output.
<BR>&nbsp;&nbsp; */
<BR>&nbsp;&nbsp; dup2(nul, STDOUT);
<BR>
<P>&nbsp;&nbsp; /* close the handle for DUMMY.FIL */
<BR>&nbsp;&nbsp; close(nul);
<BR>
<P>&nbsp;&nbsp; /* will be redirected into DUMMY.FIL */
<BR>&nbsp;&nbsp; write(STDOUT, msg, strlen(msg));
<BR>
<P>&nbsp;&nbsp; /* restore original standard output
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; handle */
<BR>&nbsp;&nbsp; dup2(oldstdout, STDOUT);
<BR>
<P>&nbsp;&nbsp; /* close duplicate handle for STDOUT */
<BR>&nbsp;&nbsp; close(oldstdout);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>
<hr color="#EE9B73" size="1" width="94%">

</TD>
<TD CLASS="tt3" VALIGN="bottom" width="8%"  bgcolor="#e0e0e0"><strong><A HREF="024.htm">后一页</A><BR>
<A HREF="022.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 + -