i.txt
资源名称:C.rar [点击查看]
上传用户:harvey99
上传日期:2020-01-11
资源大小:938k
文件大小:11k
源码类别:

其他书籍

开发平台:

CHM

  1. 函数名: imagesize
  2. 功  能: 返回保存位图像所需的字节数
  3. 用  法: unsigned far imagesize(int left, int top, int right, int bottom);
  4. 程序例: 
  5. #include <graphics.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #define ARROW_SIZE 10 
  10. void draw_arrow(int x, int y);
  11. int main(void)
  12. {
  13.    /* request autodetection */
  14.    int gdriver = DETECT, gmode, errorcode;
  15.    void *arrow;
  16.    int x, y, maxx;
  17.    unsigned int size;
  18.    /* initialize graphics and local variables */
  19.    initgraph(&gdriver, &gmode, "");
  20.    /* read result of initialization */
  21.    errorcode = graphresult();
  22.    if (errorcode != grOk)  /* an error occurred */
  23.    {
  24.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  25.       printf("Press any key to halt:");
  26.       getch();
  27.       exit(1); /* terminate with an error code */
  28.    }
  29.    maxx = getmaxx();
  30.    x = 0;
  31.    y = getmaxy() / 2;
  32.    /* draw the image to be grabbed */
  33.    draw_arrow(x, y);
  34.    /* calculate the size of the image */
  35.    size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);
  36.    /* allocate memory to hold the image */
  37.    arrow = malloc(size);
  38.    /* grab the image */
  39.    getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow);
  40.    /* repeat until a key is pressed */
  41.    while (!kbhit())
  42.    {
  43.       /* erase old image */
  44.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
  45.       x += ARROW_SIZE;
  46.       if (x >= maxx)
  47.           x = 0;
  48.       /* plot new image */
  49.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
  50.    }
  51.    /* clean up */
  52.    free(arrow);
  53.    closegraph();
  54.    return 0;
  55. }
  56. void draw_arrow(int x, int y)
  57. {
  58.    /* draw an arrow on the screen */
  59.    moveto(x, y);
  60.    linerel(4*ARROW_SIZE, 0);
  61.    linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
  62.    linerel(0, 2*ARROW_SIZE);
  63.    linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
  64. }
  65.  
  66.  
  67.  
  68. 函数名: initgraph
  69. 功  能: 初始化图形系统
  70. 用  法: void far initgraph(int far *graphdriver, int far *graphmode,
  71.     char far *pathtodriver);
  72. 程序例:
  73. #include <graphics.h>
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <conio.h>
  77. int main(void)
  78. {
  79.    /* request auto detection */
  80.    int gdriver = DETECT, gmode, errorcode;
  81.    /* initialize graphics mode */
  82.    initgraph(&gdriver, &gmode, "");
  83.    /* read result of initialization */
  84.    errorcode = graphresult();
  85.    if (errorcode != grOk)  /* an error occurred */
  86.    {
  87.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  88.       printf("Press any key to halt:");
  89.       getch();
  90.       exit(1);             /* return with error code */
  91.    }
  92.    /* draw a line */
  93.    line(0, 0, getmaxx(), getmaxy());
  94.    /* clean up */
  95.    getch();
  96.    closegraph();
  97.    return 0;
  98. }
  99.  
  100.  
  101. 函数名: inport
  102. 功  能: 从硬件端口中输入
  103. 用  法: int inp(int protid);
  104. 程序例:
  105. #include <stdio.h>
  106. #include <dos.h>
  107. int main(void)
  108. {
  109.    int result;
  110.    int port = 0;  /* serial port 0 */
  111.    result = inport(port);
  112.    printf("Word read from port %d = 0x%Xn", port, result);
  113.    return 0;
  114. }
  115.  
  116.  
  117. 函数名: insline
  118. 功  能: 在文本窗口中插入一个空行
  119. 用  法: void insline(void);
  120. 程序例:
  121. #include <conio.h>
  122. int main(void)
  123. {
  124.    clrscr();
  125.    cprintf("INSLINE inserts an empty line in the text windowrn");
  126.    cprintf("at the cursor position using the current textrn");
  127.    cprintf("background color.  All lines below the empty onern");
  128.    cprintf("move down one line and the bottom line scrollsrn");
  129.    cprintf("off the bottom of the window.rn");
  130.    cprintf("rnPress any key to continue:");
  131.    gotoxy(1, 3);
  132.    getch();
  133.    insline();
  134.    getch();
  135.    return 0;
  136. }
  137.  
  138.  
  139.  
  140. 函数名: installuserdriver
  141. 功  能: 安装设备驱动程序到BGI设备驱动程序表中
  142. 用  法: int far installuserdriver(char far *name, int (*detect)(void));
  143. 程序例:
  144. #include <graphics.h>
  145. #include <stdlib.h>
  146. #include <stdio.h>
  147. #include <conio.h>
  148. /* function prototypes */
  149. int huge detectEGA(void);
  150. void checkerrors(void);
  151. int main(void)
  152. {
  153.    int gdriver, gmode;
  154.    /* install a user written device driver */
  155.    gdriver = installuserdriver("EGA", detectEGA);
  156.    /* must force use of detection routine */
  157.    gdriver = DETECT;
  158.    /* check for any installation errors */
  159.    checkerrors();
  160.    /* initialize graphics and local variables */
  161.    initgraph(&gdriver, &gmode, "");
  162.    /* check for any initialization errors */
  163.    checkerrors();
  164.    /* draw a line */
  165.    line(0, 0, getmaxx(), getmaxy());
  166.    /* clean up */
  167.    getch();
  168.    closegraph();
  169.    return 0;
  170. }
  171. /* detects EGA or VGA cards */
  172. int huge detectEGA(void)
  173. {
  174.    int driver, mode, sugmode = 0;
  175.    detectgraph(&driver, &mode);
  176.    if ((driver == EGA) || (driver == VGA))
  177.       /* return suggested video mode number */
  178.       return sugmode;
  179.    else
  180.       /* return an error code */
  181.       return grError;
  182. }
  183. /* check for and report any graphics errors */
  184. void checkerrors(void)
  185. {
  186.    int errorcode;
  187.    /* read result of last graphics operation */
  188.    errorcode = graphresult();
  189.    if (errorcode != grOk)
  190.    {
  191.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  192.       printf("Press any key to halt:");
  193.       getch();
  194.       exit(1);
  195.    }
  196. }
  197. 函数名: installuserfont
  198. 功  能: 安装未嵌入BGI系统的字体文件(CHR)
  199. 用  法: int far installuserfont(char far *name);
  200. 程序例:
  201. #include <graphics.h>
  202. #include <stdlib.h>
  203. #include <stdio.h>
  204. #include <conio.h>
  205. /* function prototype */
  206. void checkerrors(void);
  207. int main(void)
  208. {
  209.    /* request auto detection */
  210.    int gdriver = DETECT, gmode;
  211.    int userfont;
  212.    int midx, midy;
  213.    /* initialize graphics and local variables */
  214.    initgraph(&gdriver, &gmode, "");
  215.    midx = getmaxx() / 2;
  216.    midy = getmaxy() / 2;
  217.    /* check for any initialization errors */
  218.    checkerrors();
  219.    /* install a user defined font file */
  220.    userfont = installuserfont("USER.CHR");
  221.    /* check for any installation errors */
  222.    checkerrors();
  223.    /* select the user font */
  224.    settextstyle(userfont, HORIZ_DIR, 4);
  225.    /* output some text */
  226.    outtextxy(midx, midy, "Testing!");
  227.    /* clean up */
  228.    getch();
  229.    closegraph();
  230.    return 0;
  231. }
  232. /* check for and report any graphics errors */
  233. void checkerrors(void)
  234. {
  235.    int errorcode;
  236.    /* read result of last graphics operation */
  237.    errorcode = graphresult();
  238.    if (errorcode != grOk)
  239.    {
  240.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  241.       printf("Press any key to halt:");
  242.       getch();
  243.       exit(1);
  244.    }
  245.  }
  246.  
  247.  
  248.  
  249. 函数名: int86
  250. 功  能: 通用8086软中断接口
  251. 用  法: int int86(int intr_num, union REGS *inregs, union REGS *outregs);
  252. 程序例:
  253. #include <stdio.h>
  254. #include <conio.h>
  255. #include <dos.h>
  256. #define VIDEO 0x10
  257. void movetoxy(int x, int y)
  258. {
  259.    union REGS regs;
  260.    regs.h.ah = 2;  /* set cursor postion */
  261.    regs.h.dh = y;
  262.    regs.h.dl = x;
  263.    regs.h.bh = 0;  /* video page 0 */
  264.    int86(VIDEO, &regs, &regs);
  265. }
  266. int main(void)
  267. {
  268.    clrscr();
  269.    movetoxy(35, 10);
  270.    printf("Hellon");
  271.    return 0;
  272. }
  273.  
  274.  
  275. 函数名: int86x
  276. 功  能: 通用8086软中断接口
  277. 用  法: int int86x(int intr_num, union REGS *insegs, union REGS *outregs, struct SREGS *segregs);
  278. 程序例:
  279. #include <dos.h>
  280. #include <process.h>
  281. #include <stdio.h>
  282. int main(void)
  283. {
  284.    char filename[80];
  285.    union REGS inregs, outregs;
  286.    struct SREGS segregs;
  287.    printf("Enter filename: ");
  288.    gets(filename);
  289.    inregs.h.ah = 0x43;
  290.    inregs.h.al = 0x21;
  291.    inregs.x.dx = FP_OFF(filename);
  292.    segregs.ds = FP_SEG(filename);
  293.    int86x(0x21, &inregs, &outregs, &segregs);
  294.    printf("File attribute: %Xn", outregs.x.cx);
  295.    return 0;
  296. }
  297.  
  298.  
  299.  
  300. 函数名: intdos
  301. 功  能: 通用DOS接口
  302. 用  法: int intdos(union REGS *inregs, union REGS *outregs);
  303. 程序例:
  304. #include <stdio.h>
  305. #include <dos.h>
  306. /* deletes file name; returns 0 on success, nonzero on failure */
  307. int delete_file(char near *filename)
  308. {
  309.    union REGS regs;
  310.    int ret;
  311.    regs.h.ah = 0x41;                            /* delete file */
  312.    regs.x.dx = (unsigned) filename;
  313.    ret = intdos(&regs, &regs);
  314.    /* if carry flag is set, there was an error */
  315.    return(regs.x.cflag ? ret : 0);
  316. }
  317. int main(void)
  318. {
  319.    int err;
  320.    err = delete_file("NOTEXIST.$$$");
  321.    if (!err)
  322.       printf("Able to delete NOTEXIST.$$$n");
  323.    else
  324.       printf("Not Able to delete NOTEXIST.$$$n");
  325.    return 0;
  326. }
  327.  
  328.  
  329.  
  330. 函数名: intdosx
  331. 功  能: 通用DOS中断接口
  332. 用  法: int intdosx(union REGS *inregs, union REGS *outregs, struct SREGS *segregs);
  333. 程序例:
  334. #include <stdio.h>
  335. #include <dos.h>
  336. /* deletes file name; returns 0 on success, nonzero on failure */
  337. int delete_file(char far *filename)
  338. {
  339.    union REGS regs; struct SREGS sregs;
  340.    int ret;
  341.    regs.h.ah = 0x41;                      /* delete file */
  342.    regs.x.dx = FP_OFF(filename);
  343.    sregs.ds = FP_SEG(filename);
  344.    ret = intdosx(&regs, &regs, &sregs);
  345.    /* if carry flag is set, there was an error */
  346.    return(regs.x.cflag ? ret : 0);
  347. }
  348. int main(void)
  349. {
  350.    int err;
  351.    err = delete_file("NOTEXIST.$$$");
  352.    if (!err)
  353.       printf("Able to delete NOTEXIST.$$$n");
  354.    else
  355.       printf("Not Able to delete NOTEXIST.$$$n");
  356.    return 0;
  357. }
  358.  
  359.  
  360. 函数名: intr
  361. 功  能: 改变软中断接口
  362. 用  法: void intr(int intr_num, struct REGPACK *preg);
  363. 程序例:
  364. #include <stdio.h>
  365. #include <string.h>
  366. #include <dir.h>
  367. #include <dos.h>
  368. #define CF 1  /* Carry flag */
  369. int main(void)
  370. {
  371.    char directory[80];
  372.    struct REGPACK reg;
  373.    printf("Enter directory to change to: ");
  374.    gets(directory);
  375.    reg.r_ax = 0x3B << 8;         /* shift 3Bh into  AH */
  376.    reg.r_dx = FP_OFF(directory);
  377.    reg.r_ds = FP_SEG(directory);
  378.    intr(0x21, &reg);
  379.    if (reg.r_flags & CF)
  380.       printf("Directory change failedn");
  381.    getcwd(directory, 80);
  382.    printf("The current directory is: %sn", directory);
  383.    return 0;
  384. }
  385.  
  386.  
  387. 函数名: ioctl
  388. 功  能: 控制I/O设备
  389. 用  法: int ioctl(int handle, int cmd[,int *argdx, int argcx]);
  390. 程序例:
  391. #include <stdio.h>
  392. #include <dir.h>
  393. #include <io.h>
  394. int main(void)
  395. {
  396.    int stat;
  397.    /* use func 8 to determine if the default drive is removable */
  398.    stat = ioctl(0, 8, 0, 0);
  399.    if (!stat)
  400.       printf("Drive %c is removable.n", getdisk() + 'A');
  401.    else
  402.       printf("Drive %c is not removable.n", getdisk() + 'A');
  403.    return 0;
  404. }
  405.  
  406.  
  407.  
  408. 函数名: isatty
  409. 功  能: 检查设备类型
  410. 用  法: int isatty(int handle);
  411. 程序例:
  412. #include <stdio.h>
  413. #include <io.h>
  414. int main(void)
  415. {
  416.    int handle;
  417.    handle = fileno(stdprn);
  418.    if (isatty(handle))
  419.       printf("Handle %d is a device typen", handle);
  420.    else
  421.       printf("Handle %d isn't a device typen", handle);
  422.    return 0;
  423. }
  424.  
  425.  
  426.  
  427. 函数名: itoa
  428. 功  能: 把一整数转换为字符串
  429. 用  法: char *itoa(int value, char *string, int radix);
  430. 程序例:
  431. #include <stdlib.h>
  432. #include <stdio.h>
  433. int main(void)
  434. {
  435.    int number = 12345;
  436.    char string[25];
  437.    itoa(number, string, 10);
  438.    printf("integer = %d string = %sn", number, string);
  439.    return 0;
  440. }