首页 > 其他分享 >C语言函数大全--g开头的函数(下)

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

时间:2023-04-14 12:00:49浏览次数:45  
标签:函数 -- C语言 errorcode int midx printf include

C语言函数大全

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

17. getmodename

17.1 函数说明

函数声明 函数功能
char * getmodename(int mode_name); 获取指定的图形模式名

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 函数说明

函数声明 函数功能
void getmoderange(int graphdriver, int *lomode, int *himode); 获取给定图形驱动程序的模式范围

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 函数说明

函数声明 函数功能
void getpalette(struct palettetype *palette); 获取有关当前调色板的信息

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 函数说明

函数声明 函数功能
int getpixel(int x, int y); 获取得指定像素的颜色

20.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define PIXEL_COUNT 1000
#define DELAY_TIME  100

int 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 函数说明

函数声明 函数功能
从标准输入流中读取字符串,直至遇到到换行符或EOF时停止,并将读取的结果存放在 buffer 指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为 '\0' 空字符,并由此来结束字符串。

注意: 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 函数说明

函数声明 函数功能
void gettextsettings(struct textsettingstype *textinfo); 获取有关当前图形文本字体的信息

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 函数说明

函数声明 函数功能
void getviewsettings(struct viewporttype *viewport); 获取有关当前视区的信息

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 函数说明

函数声明 函数功能
int getw(FILE *strem); 从 stream 所指向文件读取下一个整数

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");
    else
        printf("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");
    else
        printf("Successful read: word = %d\n", word);

    fclose(fp);
    unlink(FNAME);

    return 0;
}

24.3 运行结果

在这里插入图片描述

25. getx,gety

25.1 函数说明

函数声明 函数功能
int getx(void); 获取当前图形位置的 x 坐标
获取当前图形位置的 y 坐标

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 函数说明

函数声明 函数功能
struct tm *gmtime(long *clock); 把日期和时间转换为格林尼治标准时间(GMT)

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 函数说明

函数声明 函数功能
void graphdefaults(void); 将所有图形设置复位为它们的缺省值

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 函数说明

函数声明 函数功能
char * grapherrormsg(int errorcode); 返回一个错误信息串的指针

28.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>

#define NONSENSE -50

int main(void)
{
    // FORCE AN ERROR TO OCCUR
    int 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 函数说明

函数声明 函数功能
int graphresult(void); 返回最后一次不成功的图形操作的错误代码

29.2 演示示例

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
    // request auto detection
    int 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 函数说明

函数声明 函数功能
int getmaxwidth(void); 获取屏幕的最大宽度
int getmaxheight(void); 获取屏幕的最大高度

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 函数说明

函数声明 函数功能
int getdisplaycolor( int color ); 根据 color ,返回要显示的颜色值

注意: 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 函数说明

函数声明 函数功能
int getwindowwidth(); 获取图形界面窗口宽度
int getwindowheight(void); 获取图形界面窗口高度

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 函数说明

函数声明 函数功能
bool getrefreshingbgi(void); 获取刷新基础图形界面标识

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 运行结果

在这里插入图片描述

参考

  1. [API Reference Document]
  2. [gets]

标签:函数,--,C语言,errorcode,int,midx,printf,include
From: https://blog.51cto.com/huazie/6189865

相关文章

  • Python学习笔记一:列表
    一.列表1.定义列表,是由一系列按照特定顺序排列的元素组成的一个有序集合。其中可以包含字母,数字,或者其他任何元素,每一个元素之间不一定有关系。不过,在创建列表时,建议还是将相同类型的元素或者相互之间有关联的元素放在一个列表中。鉴于包含的元素的数量,通常在给列表......
  • Postman接口测试之当多个接口都需要使用自定义的函数时解决方案
    //自定义时间戳的动态参数//vartimes=Date.now()//pm.globals.set("times",times);//需要随机出一个范围内的整数数,函数//constrandomInt=(min,max)=>Math.floor(Math.random()*(max-min+1))+min//pm.globals.set("randomNumber",randomInt(1000,3000));......
  • 微众银行笔试-大湮灭术
    大湮灭术题目说明世间充斥看正负两种能量,正能量对人体有益,而负能量对人体是有害的。已知地图上有n个排应一列的地域,每个地域的能量都不一样,可以用一个数字来代表某个地域中正负能量的数,正数代表正能量比负能量多,反之亦然。现在大漫灭术的卷轴只剩下了两个,你可以对任何一个连续......
  • 如何实现一个vscode插件
    前言有时候,需要提高一些开发效率,我们通常会使用一些优秀的代码编辑器,比如vscode。在使用vscode的时候,会用到很多插件,有时候也会萌发想要去开发这个插件的念头。既然想到了,那就动手试一下。开发过程我感觉最快的上手方式不是讲一些虚头巴脑的概念,先去试一下怎么去实现一个简......
  • OpenHarmony社区运营报告(2023年3月)
     本月快讯•《OpenHarmony2022年度运营报告》于3月正式发布,2022年OpenAtomOpenHarmony(以下简称“OpenHarmony”)开源项目潜心务实、深耕发展,OpenHarmony位居Gitee活跃度指数第一名,已有51家共建单位,130+生态伙伴,超过5100位代码共建者,拥有超过220款软硬件产品通过兼容性测评......
  • HashMap内部的bucket(桶)数组长度为什么一直都是2的整数次幂?
    这样做有两个好处:第一,可以通过(table.length-1)&key.hash()这样的位运算快速寻址,第二,在HashMap扩容的时候可以保证同一个桶中的元素均匀的散列到新的桶中,具体一点就是同一个桶中的元素在扩容后一半留在原先的桶中,一半放到了新的桶中。......
  • jvm 内存结构
    jvm内存结构和java内存模型不是同一个东西线程私有线程共享程序计数器堆虚拟机栈方法区本地方法区堆外内存(Java7的永久代或JDK8的元空间、代码缓存)程序计数器也叫PC寄存器,存储下一条程序行号(严格是机器码行号),比如分支、循环、线程切换之后的唤醒等......
  • Midjourney魔法解锁:打造电商AI模特,实现无限场景换装
    在网上看到过下图这样一篇《模特不存在了》的帖子:是一个卖内衣的店主,通过Midjourney把石膏模特身上的衣服,穿到了AI生成的模特身上。网上看到的把石膏模特的内衣穿在了AI模特身上可以看到这张图片上左侧的衣服,几乎无差别的穿到了AI模特的身上。但这个帖子没有公布方法和技巧,那......
  • [Linux]回环设备的作用是什么?
    在计算机网络中,回环设备(loopbackdevice)是指一种虚拟网络接口,通常装备在操作系统中,用于向系统本身发送网络数据包,而不需要使用物理网络接口。它可以使应用程序像使用网络接口一样访问本地主机,这样可以方便的测试、开发和调试应用程序,确保应用程序的可靠性和正确性。回环设备的作......
  • SLBR通过自校准的定位和背景细化来去除可见的水印
    一、简要介绍 本文简要介绍了论文“VisibleWatermarkRemovalviaSelf-calibratedLocalizationandBackgroundRefinement”的相关工作。在图像上叠加可见的水印,为解决版权问题提供了一种强大的武器。现代的水印去除方法可以同时进行水印定位和背景恢复,这可以看......