C语言函数大全(q,r开头)
函数名:qsort
函数:按快速排序例程排序
用法:void qsort (void * base,int nelem,int width,int(* fcmp)());
程序示例:
# include
# include
# int sort _ function(const void * a,const void * b);
char listint main(void)
{
int x;
qsort((void *)list,5,sizeof(list[0]),sort _ function);
for(x = 0;x < 5;x++)
printf("%s\n ",list[x]);
返回0;
}
int sort _ function(const void * a,const void * b)
{
return(strcmp(a,b));
}函数名:qsort
函数:按快速排序例程排序
用法:void qsort (void * base,int nelem,int width,int(* fcmp)());
程序示例:
# include
# include
# int sort _ function(const void * a,const void * b);
char list[5][4] = { "猫"、"车"、"驾驶室"、"帽"、"罐" }; = { "猫"、"车"、"驾驶室"、"帽"、"罐" };
int main(void)
{
int x;
qsort((void *)list,5,sizeof(list[0]),sort _ function);
for(x = 0;x < 5;x++)
printf("%s\n ",list[x]);
返回0;
}
int sort _ function(const void * a,const void * b)
{
return(strcmp(a,b));
}
[4]
[5]
函数名:raise
函数:向正在执行的程序发送信号
用法:int raise(int SIG);
程序示例:
# include
int main(void)
{
int a,b;
a = 10;
b = 0;
if (b == 0)
/* preempt除以零误差*/
raise(SIGFPE);
a = a/b;
返回0;
}
函数名:rand
函数:随机数生成器
用法:void rand(void);
程序示例:
# include
# include
int main(void)
{
int I;
printf("从0到99的十个随机数\ n \ n ");
for(I = 0;iprintf("%d\n ",rand()% 100);
返回0;
}
函数名:randbrd
函数:随机块读取
用法:int randbrd (structfcb * fcbptr,int rec CNT);
程序示例:
# include
# include
# include
int main(void)
{
char far * save _ save
char line[80],buffer[256];
struct fcb blk;
int i,result
/*获取dta的用户输入文件名*/
printf("输入驱动器和文件名(无路径-即a:file . dat)\ n ");
gets(行);
/*将文件名放入fcb */
if(!parsfnm(line,&blk,1))
{
printf("调用parsfnm时出错\ n ");
退出(1);
}
printf(" Drive # % d File:% s \ n \ n ",blk.fcb_drive,blk . fcb _ name);
/*用DOS打开文件FCB打开文件*/
bdosptr(0x0F,&blk,0);
/*保存旧的dta,设置新的*/
save _ DTA = get DTA();
setdta(缓冲区);
/*为新的dta设置信息*/
blk . fcb _ recsize = 128;
blk . fcb _ random = 0L;
result = randbrd(&blk,1);
/*检查randbrd */
if(!result)
printf(" Read OK \ n \ n ");
else
{
perror("读取期间出错");
退出(1);
}
/*从新dta */
printf("前128个字符为:\n ")读入数据;
for(I = 0;iputchar(buffer[I]);
/*还原以前的DTA */
set DTA(save _ DTA);
返回0;
}
函数名:randbwr
函数:随机写块
用法:int randbwr (structfcp * fcbptr,int rec CNT);
程序示例:
# include
# include
# include
int main(void)
{
char far * save _ save
char line[80];
char buffer[256]= " rand BWR test!";
struct fcb blk;
int result;
/*从用户处获取新文件名*/
printf("输入要创建的文件名(无路径- ie。a:file . dat \ n ");
gets(行);
/*将新文件名解析到dta */
parsfnm(line,&blk,1);
printf(" Drive # % d File:% s \ n ",blk.fcb_drive,blk . fcb _ name);
/*请求DOS服务创建文件*/
if (bdosptr(0x16,&blk,0) == -1)
{
perror("创建文件时出错");
退出(1);
}
/*保存旧的dta并设置新的DTA */
save _ DTA = get DTA();
setdta(缓冲区);
/*写入新记录*/
blk . fcb _ recsize = 256;
blk . fcb _ random = 0L;
result = randbwr(&blk,1);
如果(!结果)
printf(" Write OK \ n ");
else
{
perror("磁盘错误");
退出(1);
}
/*请求DOS服务关闭文件*/
if (bdosptr(0x10,&blk,0) == -1)
{
perror("错误关闭文件");
退出(1);
}
/*重置旧的DTA */
set DTA(save _ DTA);
返回0;
}
函数名:random
函数:随机数生成器
用法:int random(int num);
程序示例:
# include
# include
# include
*打印0到99范围内的随机数*/
int main(void)
{[
printf(" 0-99范围内的随机数:%d\n ",Random(100));
返回0;
}
函数名:randomize
函数:初始化随机数生成器
用法:void randomize(void);
程序示例:
# include
# include
# int main(void)
{
int I;
randomize();
printf("从0到99的十个随机数\ n \ n ");
for(I = 0;iprintf("%d\n ",rand()% 100);
返回0;
}
函数名:read
函数:从文件中读取
用法:int read (int handle,void * buf,int nbyte);
程序示例:
# include
# include
# include
# include
# include
int main(void)[
int handle,bytes
buf = malloc(10);
/*
在当前目录中查找名为TEST的文件。$$$并尝试
从中读取10个字节。要使用此示例,您应该创建
文件测试。$ $ $
*/
if((handle =
open(" TEST。$$$ ",O_RDONLY | O_BINARY,S _ I write | S _ IREAD))= =-1)
{
printf("打开文件时出错\ n ");
退出(1);
}
if((bytes = Read(handle,buf,10)) == -1) {
printf("读取失败。\ n ");
退出(1);
}
else {
printf(" Read:% d bytes Read。\n”,字节);
}
返回0;
}
函数名:realloc
函数:重新分配主存
用法:void * realloc (void * ptr,unsigned new size);
程序示例:
# include
# include
# int main(void)
{
char * str;
/*为string */
str = malloc(10)分配内存;
/*将“Hello”复制到string */
strcpy(str," Hello ");
printf("String是%s\n Address是%p\n ",str,str);
str = realloc(str,20);
printf("String是%s\n新地址是%p\n ",str,str);
/*空闲内存*/
free(str);
返回0;
}
函数名:rectangle
函数:绘制矩形
用法:void far rectangle (int left,int top,int right,int bottom);
程序示例:
# include
# include
# include
int main(void)
{
/*请求自动检测
int left,top,right,bottom
/*初始化图形和局部变量*/
initgraph(&gdriver,&gmode," ");
/*读取初始化结果*/
error code = graph result();
if(错误代码!= grOk) /*出现错误*/
{
printf("图形错误:%s\n ",grapherrormsg(error code));
printf("按任意键暂停:");
getch();
退出(1);/*以错误代码*/
}
left = get maxx()/2-50终止;
top = get maxy()/2-50;
right = get maxx()/2+50;
bottom = get maxy()/2+50;
/*画一个矩形*/
rectangle(左、上、右、下);
/* clean up */
getch();
closegraph();
返回0;
}
函数名:registerbgidriver
函数:登录连接的图形驱动程序代码
用法:int registerbgidriver(void(* driver)(void));
程序示例:
# include
# include
# include
int main(void)
{
/*请求自动检测
/*注册一个添加到graphics中的驱动程序. lib */
error code = registerbgidriver(EGA VGA _ driver);
/*报告任何注册错误*/
if(error code < 0)
{
printf(" Graphics error:% s \ n ",grapherrormsg(error code));
printf("按任意键暂停:");
getch();
退出(1);/*以错误代码*/
}
/*初始化图形和局部变量*/
initgraph(&gdriver,&gmode," ");
/*读取初始化结果*/
error code = graph result();
if(错误代码!= grOk) /*出现错误*/
{
printf("图形错误:%s\n ",grapherrormsg(error code));
printf("按任意键暂停:");
getch();
退出(1);/*以错误代码*/
}
/*画线*/
line(0,0,getmaxx(),get maxy());
/* clean up */
getch();
closegraph();
返回0;
}
函数名:remove
函数:删除一个文件
用法:int remove(char * filename);
程序示例:
# include
int main(void)
{
char file[80];
/*提示输入要删除的文件名*/
printf("要删除的文件:");
获取(文件);
/*删除文件*/
if(remove(file)= = 0)
printf(" Removed % s . % n ",file);
else
perror(" remove ");
返回0;
}
函数名:rename
函数:重命名文件
用法:int rename (char * old name,char * newname);
程序示例:
# include
int main(void)
{
charold name[80],newname[80];
/*提示要重命名的文件和新名称*/
printf("要重命名的文件:");
gets(old name);
printf("新名称:");
gets(newname);
/*重命名文件*/
if (rename(oldname,newname) == 0)
printf("重命名%s为%s,.\n ",oldname,newname);
else
perror(" rename ");
返回0;
}
函数名:restortmode
函数:将屏幕模式恢复到之前的imitgraph设置
用法:void far restoretmode(void);
程序示例:
# include
# include
# include
int main(void)
{
/*请求自动检测
int x,y;
/*初始化图形和局部变量*/
initgraph(&gdriver,&gmode," ");
/*读取初始化结果*/
error code = graph result();
if(错误代码!= grOk) /*出现错误*/
{
printf("图形错误:%s\n ",grapherrormsg(error code));
printf("按任意键暂停:");
getch();
退出(1);/*以错误代码*/
}
x = getmaxx() / 2终止;
y = get maxy()/2;
/*输出一条消息*/
settextjustify(CENTER _ TEXT,CENTER _ TEXT);
outtextxy(x,y,"按任意键退出图形:");
getch();
/*将系统还原到文本模式*/
restorrectmode();
printf("我们现在处于文本模式。\ n ");
printf("按任意键返回图形模式:");
getch();
/*返回图形模式*/
setgraphmode(getgraphmode());
/*输出一条消息*/
settextjustify(CENTER _ TEXT,CENTER _ TEXT);
outtextxy(x,y,"我们又回到了图形模式。");
outtextxy(x,y+textheight("W "),"按任意键暂停:");
/* clean up */
getch();
closegraph();
返回0;
}
0条评论