常用函数.docx
- 文档编号:25152845
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:54
- 大小:34.91KB
常用函数.docx
《常用函数.docx》由会员分享,可在线阅读,更多相关《常用函数.docx(54页珍藏版)》请在冰豆网上搜索。
常用函数
函数名:
detectgraph
功能:
通过检测硬件确定图形驱动程序和模式
用法:
voidfardetectgraph(intfar*graphdriver,intfar*graphmode);
程序例:
#include
#include
#include
#include
/*namesofthevariouscardssupported*/
char*dname[]={"requestsdetection",
"aCGA",
"anMCGA",
"anEGA",
"a64KEGA",
"amonochromeEGA",
"anIBM8514",
"aHerculesmonochrome",
"anAT&T6300PC",
"aVGA",
"anIBM3270PC"
};
intmain(void)
{
/*returnsdetectedhardwareinfo.*/
intgdriver,gmode,errorcode;
/*detectgraphicshardwareavailable*/
detectgraph(&gdriver,&gmode);
/*readresultofdetectgraphcall*/
errorcode=graphresult();
if(errorcode!
=grOk)/*anerror
occurred*/
{
printf("Graphicserror:
%s\n",\
grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerror
code*/
}
/*displaytheinformationdetected*/
clrscr();
printf("Youhave%svideodisplay\
card.\n",dname[gdriver]);
printf("Pressanykeytohalt:
");
getch();
return0;
}
函数名:
circle
功能:
在给定半径以(x,y)为圆心画圆
用法:
voidfarcircle(intx,inty,intradius);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intmidx,midy;
intradius=100;
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)/*anerroroccurred*/
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
/*drawthecircle*/
circle(midx,midy,radius);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
closegraph
功能:
关闭图形系统
用法:
voidfarclosegraph(void);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intx,y;
/*initializegraphicsmode*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)/*anerror
occurred*/
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
x=getmaxx()/2;
y=getmaxy()/2;
/*outputamessage*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(x,y,"Pressakeytoclosethegraphicssystem:
");
/*waitforakey*/
getch();
/*closesdownthegraphicssystem*/
closegraph();
printf("We'renowbackintextmode.\n");
printf("Pressanykeytohalt:
");
getch();
return0;
}
exit函数
函数名:
exit
功能:
终止程序
用法:
voidexit(intstatus);
#include
#include
#include
intmain(void)
{
intstatus;
printf("Entereither1or2\n");
status=getch();
/*SetsDOSerrorlevel*/
exit(status-'0');
/*Note:
thislineisneverreached*/
return0;
}
fabs函数
函数名:
fabs
功能:
返回浮点数的绝对值
用法:
doublefabs(doublex);
程序例:
#include
#include
intmain(void)
{
floatnumber=-1234.0;
printf("number:
%fabsolutevalue:
%f\n",
number,fabs(number));
return0;
}
函数名:
farfree
功能:
从远堆中释放一块
用法:
voidfarfree(void);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farcalloc(10,sizeof(char));
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecauseyoumightbeinasmalldatamodel,
inwhichcaseanormalstringcopyroutinecan'tbeusedsinceit
assumesthepointersizeisnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farmalloc
功能:
从远堆中分配存储块
用法:
voidfar*farmalloc(unsignedlongsize);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farmalloc(10);
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecausewemight
beinasmalldatamodel,inwhichcase
anormalstringcopyroutinecannotbe
usedsinceitassumesthepointersize
isnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
fclose
功能:
关闭一个流
用法:
intfclose(FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*fp;
charbuf[11]="0123456789";
/*createafilecontaining10bytes*/
fp=fopen("DUMMY.FIL","w");
fwrite(&buf,strlen(buf),1,fp);
/*closethefile*/
fclose(fp);
return0;
}
函数名:
feof
功能:
检测流上的文件结束符
用法:
intfeof(FILE*stream);
程序例:
#include
intmain(void)
{
FILE*stream;
/*openafileforreading*/
stream=fopen("DUMMY.FIL","r");
/*readacharacterfromthefile*/
fgetc(stream);
/*checkforEOF*/
if(feof(stream))
printf("Wehavereachedend-of-file\n");
/*closethefile*/
fclose(stream);
return0;
}
函数名:
ferror
功能:
检测流上的错误
用法:
intferror(FILE*stream);
程序例:
#include
intmain(void)
{
FILE*stream;
/*openafileforwriting*/
stream=fopen("DUMMY.FIL","w");
/*forceanerrorconditionbyattemptingtoread*/
(void)getc(stream);
if(ferror(stream))/*testforanerroronthestream*/
{
/*displayanerrormessage*/
printf("ErrorreadingfromDUMMY.FIL\n");
/*resettheerrorandEOFindicators*/
clearerr(stream);
}
fclose(stream);
return0;
}
浮点型(float)
一、浮点数说明
TurboC中有以下两种类型的浮点数:
float单浮点数。
字长为4个字节共32位二进制数,数的范围是3.4x10-38E~3.4x10+38E。
double双浮点数。
字长为8个字节共64位二进制数,数的范围是1.7x10-308E~1.7x10+308E。
说明:
浮点数均为有符号浮点数,没有无符号浮点数。
二、浮点型变量定义
可以用下列语句定义浮点型变量:
floata,f;/*a,f被定义为单浮点型变量*/
doubleb;/*b被定义为双浮点型变量*/
三、浮点常数表示
例如:
+29.56,-56.33,-6.8e-18,6.365
说明:
1.浮点常数只有一种进制(十进制)。
2.所有浮点常数都被默认为double。
3.绝对值小于1的浮点数,其小数点前面的零可以省略。
如:
0.22可写为.22,
-0.0015E-3可写为-.0015E-3。
4.TurboC默认格式输出浮点数时,最多只保留小数点后六位。
函数名:
fopen
功能:
打开一个流
用法:
FILE*fopen(char*filename,char*type);
程序例:
#include
#include
#include
intmain(void)
{
char*s;
chardrive[MAXDRIVE];
chardir[MAXDIR];
charfile[MAXFILE];
charext[MAXEXT];
intflags;
s=getenv("COMSPEC");/*getthecomspecenvironmentparameter*/
flags=fnsplit(s,drive,dir,file,ext);
printf("Commandprocessorinfo:
\n");
if(flags&DRIVE)
printf("\tdrive:
%s\n",drive);
if(flags&DIRECTORY)
printf("\tdirectory:
%s\n",dir);
if(flags&FILENAME)
printf("\tfile:
%s\n",file);
if(flags&EXTENSION)
printf("\textension:
%s\n",ext);
return0;
}
函数名:
fprintf
功能:
传送格式化输出到一个流中
用法:
intfprintf(FILE*stream,char*format[,argument,...]);
程序例:
/*Programtocreatebackupofthe
AUTOEXEC.BATfile*/
#include
intmain(void)
{
FILE*in,*out;
if((in=fopen("\\AUTOEXEC.BAT","rt"))
==NULL)
{
fprintf(stderr,"Cannotopeninput\
file.\n");
return1;
}
if((out=fopen("\\AUTOEXEC.BAK","wt"))
==NULL)
{
fprintf(stderr,"Cannotopenoutput\
file.\n");
return1;
}
while(!
feof(in))
fputc(fgetc(in),out);
fclose(in);
fclose(out);
return0;
}
函数名:
fread
功能:
从一个流中读数据
用法:
intfread(void*ptr,intsize,intnitems,FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*stream;
charmsg[]="thisisatest";
charbuf[20];
if((stream=fopen("DUMMY.FIL","w+"))
==NULL)
{
fprintf(stderr,
"Cannotopenoutputfile.\n");
return1;
}
/*writesomedatatothefile*/
fwrite(msg,strlen(msg)+1,1,stream);
/*seektothebeginningofthefile*/
fseek(stream,SEEK_SET,0);
/*readthedataanddisplayit*/
fread(buf,strlen(msg)+1,1,stream);
printf("%s\n",buf);
fclose(stream);
return0;
}
函数名:
free
功能:
释放已分配的块
用法:
voidfree(void*ptr);
程序例:
#include
#include
#include
intmain(void)
{
char*str;
/*allocatememoryforstring*/
str=malloc(10);
/*copy"Hello"tostring*/
strcpy(str,"Hello");
/*displaystring*/
printf("Stringis%s\n",str);
/*freememory*/
free(str);
return0;
}
函数名:
fscanf
功能:
从一个流中执行格式化输入
用法:
intfscanf(FILE*stream,char*format[,argument...]);
程序例:
#include
#include
intmain(void)
{
inti;
printf("Inputaninteger:
");
/*readanintegerfromthe
standardinputstream*/
if(fscanf(stdin,"%d",&i))
printf("Theintegerreadwas:
%i\n",
i);
else
{
fprintf(stderr,"Errorreadingan\
integerfromstdin.\n");
exit(1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常用 函数