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;
}