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

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

时间:2019-10-06 01:03:06

相关推荐

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

C语言函数大全

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

1. gcvt

1.1 函数说明

参数:

value:被转换的值。

ndigit:存储的有效数字位数。

buf:结果的存储位置。

注意:gcvt函数把一个浮点值转换成一个字符串 (包括一个小数点和可能的符号字节) 并存储该字符串在buffer中。该buffer应足够大以便容纳转换的值加上结尾的 结束符'\0',它是自动添加的。

如果一个缓冲区的大小为ndigit + 1,则gcvt函数将覆盖该缓冲区的末尾。这是因为转换的字符串包括一个小数点以及可能包含符号和指数信息。

1.2 演示示例

#include <stdlib.h>#include <stdio.h>int main(void){char str[25];double num;int sig = 5; num = 1.23;gcvt(num, sig, str);printf("string = %s\n", str);num = -456.78912;gcvt(num, sig, str);printf("string = %s\n", str);num = 0.345e5;gcvt(num, sig, str);printf("string = %s\n", str);return(0);}

1.3 运行结果

2. getarccoords

2.1 函数说明

2.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(){int gdriver = DETECT, gmode, errorcode;struct arccoordstype arcinfo;int midx, midy;int stangle = 45, endangle = 270;char sstr[80], estr[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;setcolor(getmaxcolor());arc(midx, midy, stangle, endangle, 100);// 取最后一次调用arc的坐标getarccoords(&arcinfo);sprintf(sstr, "*- (%d, %d)", arcinfo.xstart, arcinfo.ystart);sprintf(estr, "*- (%d, %d)", arcinfo.xend, arcinfo.yend);outtextxy(arcinfo.xstart, arcinfo.ystart, sstr);outtextxy(arcinfo.xend, arcinfo.yend, estr);getch();closegraph();return 0;}

2.3 运行结果

3. getbkcolor

3.1 函数说明

3.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <conio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int bkcolor, midx, midy;char bkname[35];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());settextjustify(CENTER_TEXT, CENTER_TEXT);cleardevice();for (int i = WHITE; i >= 0; i--){setbkcolor(i);bkcolor = getbkcolor(); // 获取当前背景颜色if (i == WHITE) setcolor(BLACK);else setcolor(WHITE);itoa(bkcolor, bkname, 10);strcat(bkname," is the current background color.");outtextxy(midx, midy, bkname);getch();cleardevice();}getch();closegraph();return 0;}

3.3 运行结果

4. getc

4.1 函数说明

4.2 演示示例

#include <stdio.h>int main(){char ch;printf("Input a character:");ch = getc(stdin);printf("The character input was: '%c'\n", ch);return 0;}

4.3 运行结果

5. getchar

5.1 函数说明

5.2 演示示例

#include <stdio.h>int main(void){int c;while ((c = getchar()) != '\n')printf("%c ", c);return 0;}

5.3 运行结果

6. getcolor

6.1 函数说明

6.2 演示示例

#include <graphics.h>int main(void){int gdriver = DETECT, gmode, errorcode;int color, midx, midy;char colname[35];initgraph(&gdriver, &gmode, "");errorcode = graphresult();/* an error occurred */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());settextjustify(CENTER_TEXT, CENTER_TEXT);for (int i = WHITE; i > 0; i--){color = getcolor();itoa(color, colname, 10);strcat(colname, " is the current drawing color.");outtextxy(midx, midy, colname);getch();cleardevice();setcolor(i - 1);}getch();closegraph();return 0;}

6.3 运行结果

7. getcwd

7.1 函数说明

注意:getcwd函数是将当前工作目录的绝对路径复制到参数buffer所指的内存空间中,参数maxlenbuffer的空间大小。

7.2 演示示例

#include <stdio.h>#include <dir.h>#define MAXPATH 1000int main(){char buffer[MAXPATH];getcwd(buffer, MAXPATH);printf("The current directory is: %s\n", buffer);return 0;}

7.3 运行结果

8. getdefaultpalette

8.1 函数说明

8.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int i, midx, midy;;struct palettetype far *pal=NULL;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() / 3;midy = getmaxy() / 2;setcolor(getmaxcolor());// 获取调色板定义结构pal = getdefaultpalette();char buffer[100];for (i=BLACK; i<WHITE + 1; i++){sprintf(buffer, "colors[%d] = %d", i, pal->colors[i]);outtextxy(midx, midy, buffer);getch();cleardevice();}getch();closegraph();return 0;}

8.3 运行结果

9. getdrivername

9.1 函数说明

9.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){int gdriver = DETECT, gmode, errorcode;char *drivername;initgraph(&gdriver, &gmode, "");errorcode = graphresult();if (errorcode != grOk){printf("Graphics error: %s\n", grapherrormsg(errorcode));printf("Press any key to halt:");getch();exit(1);}setcolor(getmaxcolor());// 当前图形驱动程序名字drivername = getdrivername();settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(getmaxx() / 2, getmaxy() / 2, drivername);getch();closegraph();return 0;}

9.3 运行结果

10. getfillpattern

10.1 函数说明

10.2 演示示例

#include <graphics.h>#include <stdio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int maxx, maxy;char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};initgraph(&gdriver, &gmode, "");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();setcolor(getmaxcolor());// 选择用户定义的填充模式setfillpattern(pattern, getmaxcolor());bar(0, 0, maxx, maxy);getch();// 将用户定义的填充模式拷贝到内存中getfillpattern(pattern);pattern[0] += 1;pattern[1] -= 2;pattern[2] += 3;pattern[3] -= 4;pattern[4] += 5;pattern[5] -= 6;pattern[6] += 7;pattern[7] -= 8;// 选择用户定义的填充模式setfillpattern(pattern, getmaxcolor());bar(0, 0, maxx, maxy);getch();closegraph();return 0;}

10.3 运行结果

11. getfillsettings

11.1 函数说明

11.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>// the names of the fill styles supportedchar *fname[] = {"EMPTY_FILL","SOLID_FILL","LINE_FILL","LTSLASH_FILL","SLASH_FILL","BKSLASH_FILL","LTBKSLASH_FILL","HATCH_FILL","XHATCH_FILL","INTERLEAVE_FILL","WIDE_DOT_FILL","CLOSE_DOT_FILL","USER_FILL"};int main(){int gdriver = DETECT, gmode, errorcode;struct fillsettingstype fillinfo;int midx, midy;char patstr[40], colstr[40];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;// 获取有关当前填充模式和填充颜色的信息getfillsettings(&fillinfo);sprintf(patstr, "%s is the fill style.", fname[fillinfo.pattern]);sprintf(colstr, "%d is the fill color.", fillinfo.color);settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, patstr);outtextxy(midx, midy+2*textheight("W"), colstr);getch();closegraph();return 0;}

11.3 运行结果

12. getgraphmode

12.1 函数说明

12.2 演示示例

#include <graphics.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;}

12.3 运行结果

13. getimage

13.1 函数说明

13.2 演示示例

#include<graphics.h>int main(){int driver,mode;unsigned size;void *buf;driver=DETECT;mode=0; initgraph(&driver,&mode,"");setcolor(15);rectangle(20,20,200,200);setcolor(RED);line(20,20,200,200);setcolor(GREEN);line(20,200,200,20);getch();size=imagesize(20,20,200,200);if(size!=-1){buf=malloc(size);if(buf){getimage(20,20,200,200,buf);putimage(100,100, buf,COPY_PUT);putimage(300,50, buf,COPY_PUT);}}outtext("press a key");getch();return 0;}

13.3 运行结果

14. getlinesettings

14.1 函数说明

14.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>// the names of the line styles supportedchar *lname[] = {"SOLID_LINE","DOTTED_LINE","CENTER_LINE","DASHED_LINE","USERBIT_LINE"};int main(){int gdriver = DETECT, gmode, errorcode;struct linesettingstype lineinfo;int midx, midy;char lstyle[80], lpattern[80], lwidth[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;// 取当前线型、模式和宽度getlinesettings(&lineinfo);sprintf(lstyle, "%s is the line style.", lname[lineinfo.linestyle]);sprintf(lpattern, "0x%X is the user-defined line pattern.", lineinfo.upattern);sprintf(lwidth, "%d is the line thickness.", lineinfo.thickness);settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, lstyle);outtextxy(midx, midy+2*textheight("W"), lpattern);outtextxy(midx, midy+4*textheight("W"), lwidth);getch();closegraph();return 0;}

14.3 运行结果

15. getmaxcolor

15.1 函数说明

15.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(void){int gdriver = DETECT, gmode, errorcode;int midx, midy;char colstr[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(colstr, "This mode supports colors 0~%d", getmaxcolor());settextjustify(CENTER_TEXT, CENTER_TEXT);outtextxy(midx, midy, colstr);getch();closegraph();return 0;}

15.3 运行结果

16. getmaxx,getmaxy

16.1 函数说明

16.2 演示示例

#include <graphics.h>#include <stdlib.h>#include <stdio.h>int main(void){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+2*textheight("W"), yrange);getch();closegraph();return 0;}

16.3 运行结果

参考

[API Reference Document]

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