1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C语言函数大全--g开头的函数(下)

C语言函数大全--g开头的函数(下)

时间:2022-08-06 02:49:07

相关推荐

C语言函数大全--g开头的函数(下)

C语言函数大全

本篇介绍C语言函数大全–g开头的函数(下)

17. getmodename

17.1 函数说明

17.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int midx, midy, mode;char numname[80], modename[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;mode = getgraphmode();sprintf(numname, "%d is the current mode number.", mode);sprintf(modename, "%s is the current graphics mode.", getmodename(mode));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, numname);outtextxy(midx, midy+2*textheight("W"), modename);getch();closegraph();return 0;}

17.3 运行结果

18. getmoderange

18.1 函数说明

18.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char mrange[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;getmoderange(gdriver, &low, &high);sprintf(mrange, "This driver supports modes %d~%d", low, high);settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, mrange);getch();closegraph();return 0;}

18.3 运行结果

19. getpalette

19.1 函数说明

19.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;struct palettetype pal;char psize[80], pval[20];int i, ht;int y = 10;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}getpalette(&pal);sprintf(psize, "The palette has %d modifiable entries.", pal.size);outtextxy(0, y, psize);if (pal.size != 0){ht = textheight("W");y += 2*ht;outtextxy(0, y, "Here are the current values:");y += 2*ht;for (i=0; i < pal.size; i++, y+=ht){sprintf(pval, "palette[%02d]: 0x%02X", i, pal.colors[i]);outtextxy(0, y, pval);}}getch();closegraph();return 0;}

19.3 运行结果

20. getpixel

20.1 函数说明

20.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#define PIXEL_COUNT 1000#define DELAY_TIME 100int main(void){int gdriver = DETECT, gmode, errorcode;int midx, midy, x, y, color, maxx, maxy, maxcolor;char mPixel[50];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;maxx = getmaxx() + 1;maxy = getmaxy() + 1;maxcolor = getmaxcolor() + 1;while (!kbhit()){srand((unsigned)time(NULL));x = rand() % maxx;y = rand() % maxy;color = rand() % maxcolor;putpixel(x, y, color);sprintf(mPixel, "color of pixel at (%d,%d) = %d", x, y, getpixel(x, y));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, mPixel);delay(DELAY_TIME);cleardevice();}getch();closegraph();return 0;}

20.3 运行结果

21. gets

21.1 函数说明

注意: gets函数可以无限读取,易发生溢出。如果溢出,多出来的字符将被写入到堆栈中,这就覆盖了堆栈原先的内容,破坏一个或多个不相关变量的值。

21.2 演示示例

#include <stdio.h>int main(){char string[80];printf("Input a string:");gets(string);printf("The string input was: %s\n", string);return 0;}

21.3 运行结果

22. gettextsettings

22.1 函数说明

22.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>// 文本字体char *font[] = {"DEFAULT_FONT","TRIPLEX_FONT","SMALL_FONT","SANS_SERIF_FONT","GOTHIC_FONT"};// 文本方向char *dir[] = {"HORIZ_DIR", "VERT_DIR" };// 文本水平对齐方式char *hjust[] = {"LEFT_TEXT", "CENTER_TEXT", "RIGHT_TEXT" };// 文本垂直对齐方式char *vjust[] = {"BOTTOM_TEXT", "CENTER_TEXT", "TOP_TEXT" };int main(){int gdriver = DETECT, gmode, errorcode;struct textsettingstype textinfo;int midx, midy, ht;char fontstr[80], dirstr[80], sizestr[80];char hjuststr[80], vjuststr[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;// 获取有关当前图形文本字体的信息gettextsettings(&textinfo);sprintf(fontstr, "%s is the text style.", font[textinfo.font]);sprintf(dirstr, "%s is the text direction.", dir[textinfo.direction]);sprintf(sizestr, "%d is the text size.", textinfo.charsize);sprintf(hjuststr, "%s is the horizontal justification.", hjust[textinfo.horiz]);sprintf(vjuststr, "%s is the vertical justification.", vjust[textinfo.vert]);ht = textheight("W");settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, fontstr);outtextxy(midx, midy+2*ht, dirstr);outtextxy(midx, midy+4*ht, sizestr);outtextxy(midx, midy+6*ht, hjuststr);outtextxy(midx, midy+8*ht, vjuststr);getch();closegraph();return 0;}

22.3 运行结果

23. getviewsettings

23.1 函数说明

23.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>char *clip[] = {"OFF", "ON" };int main(){int gdriver = DETECT, gmode, errorcode;struct viewporttype viewinfo;int midx, midy, ht;char topstr[80], botstr[80], clipstr[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;// 获取有关当前视区的信息getviewsettings(&viewinfo);sprintf(topstr, "(%d, %d) is the upper left viewport corner.", viewinfo.left, viewinfo.top);sprintf(botstr, "(%d, %d) is the lower right viewport corner.", viewinfo.right, viewinfo.bottom);sprintf(clipstr, "Clipping is turned %s.", clip[viewinfo.clip]);settextjustify(CENTER_TEXT, CENTER_TEXT);ht = textheight("W");outtextxy(midx, midy, topstr);outtextxy(midx, midy+2*ht, botstr);outtextxy(midx, midy+4*ht, clipstr);getch();closegraph();return 0;}

23.3 运行结果

24. getw

24.1 函数说明

24.2 演示示例

#include <stdio.h>#include <stdlib.h>#define FNAME "test.txt"int main(void){FILE *fp;int word;fp = fopen(FNAME, "wb");if (fp == NULL){printf("Error opening file %s\n", FNAME);exit(1);}word = 94;putw(word,fp);if (ferror(fp))printf("Error writing to file\n");elseprintf("Successful write\n");fclose(fp);fp = fopen(FNAME, "rb");if (fp == NULL){printf("Error opening file %s\n", FNAME);exit(1);}word = getw(fp);if (ferror(fp))printf("Error reading file\n");elseprintf("Successful read: word = %d\n", word);fclose(fp);unlink(FNAME);return 0;}

24.3 运行结果

25. getx,gety

25.1 函数说明

25.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(void){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, "<-(%d, %d) is the here.", getx(), gety());outtext(msg);getch();closegraph();return 0;}

25.3 运行结果

26. gmtime

26.1 函数说明

26.2 演示示例

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <dos.h>// 太平洋标准时间和夏令时char *tzstr = "TZ=PST8PDT";int main(void){time_t t;struct tm *gmt, *area;putenv(tzstr); // 用来改变或增加环境变量的内容tzset(); // UNIX时间兼容函数// 获取当前的系统时间,其值表示从协调世界时(Coordinated Universal Time)// 1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。t = time(NULL); area = localtime(&t); // 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间// asctime 把timeptr指向的tm结构体中储存的时间转换为字符串printf("Local time is: %s", asctime(area));// 把日期和时间转换为格林尼治标准时间(GMT)gmt = gmtime(&t);printf("GMT is: %s", asctime(gmt));return 0;}

26.3 运行结果

27. graphdefaults

27.1 函数说明

27.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int maxx, maxy;initgraph(&gdriver, &gmode, "c:\\bor\\Borland\\bgi");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}maxx = getmaxx();maxy = getmaxy();setlinestyle(DOTTED_LINE, 0, 3);line(0, 0, maxx, maxy);outtextxy(maxx/2, maxy/3, "Before default values are restored.");getch();// 将所有图形设置复位为它们的缺省值graphdefaults();cleardevice();line(0, 0, maxx, maxy);outtextxy(maxx/2, maxy/3, "After restoring default values.");getch();closegraph();return 0;}

27.3 运行结果

28. grapherrormsg

28.1 函数说明

28.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#define NONSENSE -50int main(void){// FORCE AN ERROR TO OCCURint gdriver = NONSENSE, gmode, errorcode;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}line(0, 0, getmaxx(), getmaxy());getch();closegraph();return 0;}

28.3 运行结果

29. graphresult

29.1 函数说明

29.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){// request auto detectionint gdriver = DETECT, gmode, errorcode;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}line(0, 0, getmaxx(), getmaxy());getch();closegraph();return 0;}

29.3 运行结果

30. getmaxwidth,getmaxheight

30.1 函数说明

30.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;int midx, midy;char ch[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(ch, "maxwidth = %d, maxheight = %d", getmaxwidth(), getmaxheight());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;}

30.3 运行结果

31. getdisplaycolor

31.1 函数说明

注意:color = -1 , 则返回 WHITE = 15 的颜色值;color < - 1 或 color > 15,则输出一个8位整数。

31.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;int midx, midy;char ch[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(ch, "color = %d, displaycolor(-1) = %d, displaycolor(16) = %d", getcolor(), getdisplaycolor(-1), getdisplaycolor(16));settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;}

31.3 运行结果

32. getwindowwidth,getwindowheight

32.1 函数说明

32.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char ch[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(ch, "windowwidth = %d, windowheight = %d", getwindowwidth(), getwindowheight());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;}

32.3 运行结果

33. getrefreshingbgi

33.1 函数说明

33.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(){int gdriver = DETECT, gmode, errorcode;int midx, midy, low, high;char ch[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(ch, "refreshingbgi = %d", getrefreshingbgi());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, ch);getch();closegraph();return 0;}

33.3 运行结果

参考

[API Reference Document][gets]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。