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

📄 fg.htm

📁 c语言基本的应用便于初学者学习使用 简单易懂
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="Author" content="wdg">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>fg</title>
</head>

<body>

<p>&nbsp; </p>
<div align="center"><center>

<table border="1" cellpadding="4" width="640"
bordercolordark="#FFFFFF" bordercolorlight="#FFFFFF">
    <tr>
        <td bgcolor="#FFE6B0" bordercolor="#8080FF" class="p9"><font
        color="#BB0000">导航条:--&gt;</font> <a
        href="../../index.html">网上学堂</a> --&gt; <a
        href="../tcindex.htm"><font face="宋体">C</font>语言编程宝典之一</a>
        --&gt;</td>
    </tr>
    <tr>
        <td bordercolor="#8080FF" class="p9"> 

<p>函数名: gcvt <br>
功&nbsp; 能: 把浮点数转换成字符串 <br>
用&nbsp; 法: char *gcvt(double value, int ndigit, char *buf); <br>
程序例: </p>

<p>#include &lt;stdlib.h&gt; <br>
#include &lt;stdio.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; char str[25]; <br>
&nbsp;&nbsp; double num; <br>
&nbsp;&nbsp; int sig = 5; /* significant digits */ </p>

<p>&nbsp;&nbsp; /* a regular number */ <br>
&nbsp;&nbsp; num = 9.876; <br>
&nbsp;&nbsp; gcvt(num, sig, str); <br>
&nbsp;&nbsp; printf(&quot;string = %s\n&quot;, str); </p>

<p>&nbsp;&nbsp; /* a negative number */ <br>
&nbsp;&nbsp; num = -123.4567; <br>
&nbsp;&nbsp; gcvt(num, sig, str); <br>
&nbsp;&nbsp; printf(&quot;string = %s\n&quot;, str); </p>

<p>&nbsp;&nbsp; /* scientific notation */ <br>
&nbsp;&nbsp; num = 0.678e5; <br>
&nbsp;&nbsp; gcvt(num, sig, str); <br>
&nbsp;&nbsp; printf(&quot;string = %s\n&quot;, str); </p>

<p>&nbsp;&nbsp; return(0); <br>
} <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: geninterrupt <br>
功&nbsp; 能: 产生一个软中断 <br>
用&nbsp; 法: void geninterrupt(int intr_num); <br>
程序例: </p>

<p>#include &lt;conio.h&gt; <br>
#include &lt;dos.h&gt; </p>

<p>/* function prototype */ <br>
void writechar(char ch); </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; clrscr(); <br>
&nbsp;&nbsp; gotoxy(80,25); <br>
&nbsp;&nbsp; writechar('*'); <br>
&nbsp;&nbsp; getch(); <br>
&nbsp;&nbsp; return 0; <br>
} </p>

<p>/* <br>
&nbsp;&nbsp; outputs a character at the current cursor <br>
&nbsp;&nbsp; position using the video BIOS to avoid the <br>
&nbsp;&nbsp; scrolling of the screen when writing to <br>
&nbsp;&nbsp; location (80,25). <br>
*/ </p>

<p>void writechar(char ch) <br>
{ <br>
&nbsp;&nbsp; struct text_info ti; <br>
&nbsp;&nbsp; /* grab current text settings */ <br>
&nbsp;&nbsp; gettextinfo(&amp;ti); <br>
&nbsp;&nbsp; /* interrupt 0x10 sub-function 9 */ <br>
&nbsp;&nbsp; _AH = 9; <br>
&nbsp;&nbsp; /* character to be output */ <br>
&nbsp;&nbsp; _AL = ch; <br>
&nbsp;&nbsp; _BH = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* video page */ <br>
&nbsp;&nbsp; _BL = ti.attribute;&nbsp; /* video attribute */ <br>
&nbsp;&nbsp; _CX = 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
/* repetition factor */ <br>
&nbsp;&nbsp; geninterrupt(0x10);&nbsp; /* output the char */ <br>
} <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getarccoords <br>
功&nbsp; 能: 取得最后一次调用arc的坐标 <br>
用&nbsp; 法: void far getarccoords(struct arccoordstype far
*arccoords); <br>
程序例: </p>

<p>#include &lt;graphics.h&gt; <br>
#include &lt;stdlib.h&gt; <br>
#include &lt;stdio.h&gt; <br>
#include &lt;conio.h&gt; </p>

<p>int main(void) <br>
{ <br>
/* request auto detection */ <br>
&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <br>
&nbsp;&nbsp; struct arccoordstype arcinfo; <br>
&nbsp;&nbsp; int midx, midy; <br>
&nbsp;&nbsp; int stangle = 45, endangle = 270; <br>
&nbsp;&nbsp; char sstr[80], estr[80]; </p>

<p>/* initialize graphics and local variables */ <br>
&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, &quot;&quot;); </p>

<p>/* read result of initialization */ <br>
&nbsp;&nbsp; errorcode = graphresult(); <br>
/* an error occurred */ <br>
&nbsp;&nbsp; if (errorcode != grOk) <br>
&nbsp;&nbsp; { <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Graphics error:
%s\n&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
grapherrormsg(errorcode)); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Press any key to
halt:&quot;); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); <br>
/* terminate with an error code */ <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <br>
&nbsp;&nbsp; } </p>

<p>&nbsp;&nbsp; midx = getmaxx() / 2; <br>
&nbsp;&nbsp; midy = getmaxy() / 2; </p>

<p>/* draw arc and get coordinates */ <br>
&nbsp;&nbsp; setcolor(getmaxcolor()); <br>
&nbsp;&nbsp; arc(midx, midy, stangle, endangle, 100); <br>
&nbsp;&nbsp; getarccoords(&amp;arcinfo); </p>

<p>/* convert arc information into strings */ <br>
&nbsp;&nbsp; sprintf(sstr, &quot;*- (%d, %d)&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
arcinfo.xstart, arcinfo.ystart); <br>
&nbsp;&nbsp; sprintf(estr, &quot;*- (%d, %d)&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
arcinfo.xend, arcinfo.yend); </p>

<p>&nbsp;&nbsp; /* output the arc information */ <br>
&nbsp;&nbsp; outtextxy(arcinfo.xstart, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
arcinfo.ystart, sstr); <br>
&nbsp;&nbsp; outtextxy(arcinfo.xend, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
arcinfo.yend, estr); </p>

<p>&nbsp;&nbsp; /* clean up */ <br>
&nbsp;&nbsp; getch(); <br>
&nbsp;&nbsp; closegraph(); <br>
&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getaspectratio <br>
功&nbsp; 能: 返回当前图形模式的纵横比 <br>
用&nbsp; 法: void far getaspectratio(int far *xasp, int far
*yasp); <br>
程序例: </p>

<p>#include &lt;graphics.h&gt; <br>
#include &lt;stdlib.h&gt; <br>
#include &lt;stdio.h&gt; <br>
#include &lt;conio.h&gt; </p>

<p>int main(void) <br>
{ <br>
/* request auto detection */ <br>
&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <br>
&nbsp;&nbsp; int xasp, yasp, midx, midy; </p>

<p>/* initialize graphics and local variables */ <br>
&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, &quot;&quot;); </p>

<p>/* read result of initialization */ <br>
&nbsp;&nbsp; errorcode = graphresult(); <br>
/* an error occurred */ <br>
&nbsp;&nbsp; if (errorcode != grOk) <br>
&nbsp;&nbsp; { <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Graphics error:
%s\n&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
grapherrormsg(errorcode)); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Press any key to
halt:&quot;); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); <br>
/* terminate with an error code */ <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <br>
&nbsp;&nbsp; } </p>

<p>&nbsp;&nbsp; midx = getmaxx() / 2; <br>
&nbsp;&nbsp; midy = getmaxy() / 2; <br>
&nbsp;&nbsp; setcolor(getmaxcolor()); </p>

<p>/* get current aspect ratio settings */ <br>
&nbsp;&nbsp; getaspectratio(&amp;xasp, &amp;yasp); </p>

<p>/* draw normal circle */ <br>
&nbsp;&nbsp; circle(midx, midy, 100); <br>
&nbsp;&nbsp; getch(); </p>

<p>/* draw wide circle */ <br>
&nbsp;&nbsp; cleardevice(); <br>
&nbsp;&nbsp; setaspectratio(xasp/2, yasp); <br>
&nbsp;&nbsp; circle(midx, midy, 100); <br>
&nbsp;&nbsp; getch(); </p>

<p>/* draw narrow circle */ <br>
&nbsp;&nbsp; cleardevice(); <br>
&nbsp;&nbsp; setaspectratio(xasp, yasp/2); <br>
&nbsp;&nbsp; circle(midx, midy, 100); </p>

<p>/* clean up */ <br>
&nbsp;&nbsp; getch(); <br>
&nbsp;&nbsp; closegraph(); <br>
&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getbkcolor <br>
功&nbsp; 能: 返回当前背景颜色 <br>
用&nbsp; 法: int far getbkcolor(void); <br>
程序例: </p>

<p>#include &lt;graphics.h&gt; <br>
#include &lt;stdlib.h&gt; <br>
#include &lt;string.h&gt; <br>
#include &lt;stdio.h&gt; <br>
#include &lt;conio.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; /* request auto detection */ <br>
&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <br>
&nbsp;&nbsp; int bkcolor, midx, midy; <br>
&nbsp;&nbsp; char bkname[35]; </p>

<p>/* initialize graphics and local variables */ <br>
&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, &quot;&quot;); </p>

<p>/* read result of initialization */ <br>
&nbsp;&nbsp; errorcode = graphresult(); <br>
/* an error occurred */ <br>
&nbsp;&nbsp; if (errorcode != grOk) <br>
&nbsp;&nbsp; { <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Graphics error:
%s\n&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
grapherrormsg(errorcode)); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Press any key to
halt:&quot;); <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch(); <br>
/* terminate with an error code */ <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <br>
&nbsp;&nbsp; } </p>

<p>&nbsp;&nbsp; midx = getmaxx() / 2; <br>
&nbsp;&nbsp; midy = getmaxy() / 2; <br>
&nbsp;&nbsp; setcolor(getmaxcolor()); </p>

<p>/* for centering text on the display */ <br>
&nbsp;&nbsp; settextjustify(CENTER_TEXT, CENTER_TEXT); </p>

<p>/* get the current background color */ <br>
&nbsp;&nbsp; bkcolor = getbkcolor(); </p>

<p>/* convert color value into a string */ <br>
&nbsp;&nbsp; itoa(bkcolor, bkname, 10); <br>
&nbsp;&nbsp; strcat(bkname, <br>
&nbsp;&quot; is the current background color.&quot;); </p>

<p>/* display a message */ <br>
&nbsp;&nbsp; outtextxy(midx, midy, bkname); </p>

<p>/* clean up */ <br>
&nbsp;&nbsp; getch(); <br>
&nbsp;&nbsp; closegraph(); <br>
&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getc <br>
功&nbsp; 能: 从流中取字符 <br>
用&nbsp; 法: int getc(FILE *stream); <br>
程序例: </p>

<p>#include &lt;stdio.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; char ch; </p>

<p>&nbsp;&nbsp; printf(&quot;Input a character:&quot;); <br>
/* read a character from the <br>
&nbsp;&nbsp; standard input stream */ <br>
&nbsp;&nbsp; ch = getc(stdin); <br>
&nbsp;&nbsp; printf(&quot;The character input was: '%c'\n&quot;, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch); <br>
&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getcbrk <br>
功&nbsp; 能: 获取Control_break设置 <br>
用&nbsp; 法: int getcbrk(void); <br>
程序例: </p>

<p>#include &lt;stdio.h&gt; <br>
#include &lt;dos.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; if (getcbrk()) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Cntrl-brk flag is
on\n&quot;); <br>
&nbsp;&nbsp; else <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Cntrl-brk flag is
off\n&quot;); </p>

<p>&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getch <br>
功&nbsp; 能: 从控制台无回显地取一个字符 <br>
用&nbsp; 法: int getch(void); <br>
程序例: </p>

<p>#include &lt;stdio.h&gt; <br>
#include &lt;conio.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; char ch; </p>

<p>&nbsp;&nbsp; printf(&quot;Input a character:&quot;); <br>
&nbsp;&nbsp; ch = getche(); <br>
&nbsp;&nbsp; printf(&quot;\nYou input a '%c'\n&quot;, ch); <br>
&nbsp;&nbsp; return 0; <br>
} <br>
&nbsp; <br>
&nbsp; </p>

<p>函数名: getchar <br>
功&nbsp; 能: 从stdin流中读字符 <br>
用&nbsp; 法: int getchar(void); <br>
程序例: </p>

<p>#include &lt;stdio.h&gt; </p>

<p>int main(void) <br>
{ <br>
&nbsp;&nbsp; int c; </p>

<p>&nbsp;&nbsp; /* Note that getchar reads from stdin and <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is line buffered; this means it
will <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; not return until you press ENTER.
*/ </p>

<p>&nbsp;&nbsp; while ((c = getchar()) != '\n') <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;%c&quot;, c); </p>

⌨️ 快捷键说明

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