c语言中用户自定义函数的功能,c语言一些自定义函数的功能示例

C语言函数及示例代码2006-12-03 20:10函数名: abs

功  能: 求整数的绝对值

用  法: int abs(int i);

程序例:

#include

#include

main(){

int number;

printf("input a number: ");

scanf("%d",&number);

printf("number: %d  absolute value: %d\n", number, abs(number));

getch();

}

函数名: getch

功  能: 从控制台无回显地取一个字符

用  法: int getch(void);

程序例:

#include

#include

main() {

char ch;

printf("Input a character:");

ch = getche();

printf("\nYou input a '%c'\n", ch);

return 0;

}

函数名: fabs

功  能: 返回浮点数的绝对值

用  法: double fabs(double x);

程序例:

#include

#include

main(){

float  number = -1234.0;

printf("number: %f  absolute value: %f\n",

number, fabs(number));

return 0;

}

函数名: getx

功  能: 返回当前图形位置的x坐标

用  法: int far getx(void);

程序例:

#include

#include

#include

#include

main(){

int gdriver = DETECT, gmode, errorcode;

char msg[80];

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

if (errorcode != grOk)

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

moveto(getmaxx() / 2, getmaxy() / 2);

sprintf(msg, "

outtext(msg);

getch();

closegraph();

return 0;

}

函数名: gety

功  能: 返回当前图形位置的y坐标

用  法: int far gety(void);

程序例:

#include

#include

#include

#include

main(){

int gdriver = DETECT, gmode, errorcode;

char msg[80];

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

if (errorcode != grOk)

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

moveto(getmaxx() / 2, getmaxy() / 2);

sprintf(msg, "

outtext(msg);

getch();

closegraph();

return 0;

}

函数名: gotoxy

功  能: 在文本窗口中设置光标

用  法: void gotoxy(int x, int y);

程序例:

#include

main() {

clrscr();

gotoxy(35, 12);

cprintf("Hello world");

getch();

return 0;

}

函数名: getmaxx

功  能: 返回屏幕的最大x坐标

用  法: int far getmaxx(void);

程序例:

#include

#include

#include

#include

main(){

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

char xrange[80], yrange[80];

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

if (errorcode != grOk)

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

midx = getmaxx() / 2;

midy = getmaxy() / 2;

sprintf(xrange, "X values range from 0..%d", getmaxx());

sprintf(yrange, "Y values range from 0..%d", getmaxy());

settextjustify(CENTER_TEXT, CENTER_TEXT);

outtextxy(midx, midy, xrange);

outtextxy(midx, midy+textheight("W"), yrange);

getch();

closegraph();

return 0;

}

函数名: getmaxy

功  能: 返回屏幕的最大y坐标

用  法: int far getmaxy(void);

程序例:

#include

#include

#include

#include

main() {

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

char xrange[80], yrange[80];

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

if (errorcode != grOk) {

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

midx = getmaxx() / 2;

midy = getmaxy() / 2;

sprintf(xrange, "X values range from 0..%d", getmaxx());

sprintf(yrange, "Y values range from 0..%d", getmaxy());

settextjustify(CENTER_TEXT, CENTER_TEXT);

outtextxy(midx, midy, xrange);

outtextxy(midx, midy+textheight("W"), yrange);

getch();

closegraph();

return 0;

}

函数名: fillellipse

功  能: 画出并填充一椭圆

用  法: void far fillellipse(int x, int y, int xradius, int yradius);

程序例:

#include

#include

main(){

int gdriver = DETECT, gmode;

int xcenter, ycenter, i;

initgraph(&gdriver,&gmode,"");

xcenter = getmaxx() / 2;

ycenter = getmaxy() / 2;

for (i=0; i<13; i++){

setfillstyle(i,WHITE);

fillellipse(xcenter,ycenter,100,50);

getch();

}

closegraph();

return 0;

}

函数名: ctime

功  能: 把日期和时间转换为字符串

用  法: char *ctime(const time_t *time);

程序例:

#include

#include

main()

{

time_t t;

time(&t);

printf("Today's date and time: %s\n", ctime(&t));

return 0;

}

函数名: circle

功  能: 在给定半径以(x, y)为圆心画圆

用  法: void far circle(int x, int y, int radius);

程序例:

#include

#include

#include

#include

main(){

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

int radius = 100;

initgraph(&gdriver, &gmode, "");

errorcode = graphresult();

if (errorcode != grOk)

{

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

}

midx = getmaxx() / 2;

midy = getmaxy() / 2;

setcolor(getmaxcolor());

circle(midx, midy, radius);

getch();

closegraph();

return 0;

}

函数名: fopen

功  能: 打开一个流

用  法: FILE *fopen(char *filename, char *type);

程序例:

#include

#include

#include

main(){

char *s;

char drive[MAXDRIVE];

char dir[MAXDIR];

char file[MAXFILE];

char ext[MAXEXT];

int flags;

s=getenv("COMSPEC");

flags=fnsplit(s,drive,dir,file,ext);

printf("Command processor info:\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);

return 0;

}

函数名: fprintf

功  能: 传送格式化输出到一个流中

用  法: int fprintf(FILE *stream, char *format[, argument,...]);

程序例:

/* Program to create backup of the

AUTOEXEC.BAT file */

#include

main()

{

FILE *in, *out;

if ((in = fopen("\\AUTOEXEC.BAT", "rt"))

== NULL)

{

fprintf(stderr, "Cannot open input \

file.\n");

return 1;

}

if ((out = fopen("\\AUTOEXEC.BAK", "wt"))

== NULL)

{

fprintf(stderr, "Cannot open output \

file.\n");

return 1;

}

while (!feof(in))

fputc(fgetc(in), out);

fclose(in);

fclose(out);

return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值