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

其他书籍

开发平台:

CHM

  1. 函数名: bar
  2. 功  能: 画一个二维条形图
  3. 用  法: void far bar(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. int main(void)
  10. {
  11.    /* request auto detection */
  12.    int gdriver = DETECT, gmode, errorcode;
  13.    int midx, midy, i;
  14.    /* initialize graphics and local variables */
  15.    initgraph(&gdriver, &gmode, "");
  16.    /* read result of initialization */
  17.    errorcode = graphresult();
  18.    if (errorcode != grOk)  /* an error occurred */
  19.    {
  20.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  21.       printf("Press any key to halt:");
  22.       getch();
  23.       exit(1); /* terminate with an error code */
  24.    }
  25.    midx = getmaxx() / 2;
  26.    midy = getmaxy() / 2;
  27.    /* loop through the fill patterns */
  28.    for (i=SOLID_FILL; i<USER_FILL; i++)
  29.    {
  30.       /* set the fill style */
  31.       setfillstyle(i, getmaxcolor());
  32.       /* draw the bar */
  33.       bar(midx-50, midy-50, midx+50,
  34.          midy+50);
  35.       getch();
  36.    }
  37.    /* clean up */
  38.    closegraph();
  39.    return 0;
  40. }
  41.  
  42.  
  43.  
  44. 函数名: bar3d
  45. 功  能: 画一个三维条形图
  46. 用  法: void far bar3d(int left, int top, int right, int bottom,
  47.                        int depth, int topflag);
  48. 程序例:
  49. #include <graphics.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <conio.h>
  53. int main(void)
  54. {
  55.    /* request auto detection */
  56.    int gdriver = DETECT, gmode, errorcode;
  57.    int midx, midy, i;
  58.    /* initialize graphics, local variables */
  59.    initgraph(&gdriver, &gmode, "");
  60.    /* read result of initialization */
  61.    errorcode = graphresult();
  62.    if (errorcode != grOk)  /* an error occurred */
  63.    {
  64.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  65.       printf("Press any key to halt:");
  66.       getch();
  67.       exit(1); /* terminate with error code */
  68.    }
  69.    midx = getmaxx() / 2;
  70.    midy = getmaxy() / 2;
  71.    /* loop through the fill patterns */
  72.    for (i=EMPTY_FILL; i<USER_FILL; i++)
  73.    {
  74.       /* set the fill style */
  75.       setfillstyle(i, getmaxcolor());
  76.       /* draw the 3-d bar */
  77.       bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);
  78.       getch();
  79.    }
  80.    /* clean up */
  81.    closegraph();
  82.    return 0;
  83. }
  84.  
  85.  
  86.  
  87. 函数名: bdos
  88. 功  能: DOS系统调用
  89. 用  法: int bdos(int dosfun, unsigned dosdx, unsigned dosal);
  90. 程序例:
  91. #include <stdio.h>
  92. #include <dos.h>
  93. /* Get current drive as 'A', 'B', ... */
  94. char current_drive(void)
  95. {
  96.    char curdrive;
  97.    /* Get current disk as 0, 1, ... */
  98.    curdrive = bdos(0x19, 0, 0);
  99.    return('A' + curdrive);
  100. }
  101. int main(void)
  102. {
  103.    printf("The current drive is %c:n", current_drive());
  104.    return 0;
  105. }
  106.  
  107.  
  108.  
  109. 函数名: bdosptr
  110. 功  能: DOS系统调用
  111. 用  法: int bdosptr(int dosfun, void *argument, unsigned dosal);
  112. 程序例:
  113. #include <string.h>
  114. #include <stdio.h>
  115. #include <dir.h>
  116. #include <dos.h>
  117. #include <errno.h>
  118. #include <stdlib.h>
  119. #define  BUFLEN  80
  120. int main(void)
  121. {
  122.    char  buffer[BUFLEN];
  123.    int   test;
  124.    printf("Enter full pathname of a directoryn");
  125.    gets(buffer);
  126.    test = bdosptr(0x3B,buffer,0);
  127.       if(test)
  128.       {
  129.   printf("DOS error message: %dn", errno);
  130.   /* See errno.h for error listings */
  131.   exit (1);
  132.       }
  133.    getcwd(buffer, BUFLEN);
  134.    printf("The current directory is: %sn", buffer);
  135.    return 0;
  136. }
  137.  
  138.  
  139.  
  140. 函数名: bioscom
  141. 功  能: 串行I/O通信
  142. 用  法: int bioscom(int cmd, char abyte, int port);
  143. 程序例:
  144. #include <bios.h>
  145. #include <conio.h>
  146. #define COM1       0
  147. #define DATA_READY 0x100
  148. #define TRUE       1
  149. #define FALSE      0
  150. #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
  151. int main(void)
  152. {
  153.    int in, out, status, DONE = FALSE;
  154.    bioscom(0, SETTINGS, COM1);
  155.    cprintf("... BIOSCOM [ESC] to exit ...n");
  156.    while (!DONE)
  157.    {
  158.       status = bioscom(3, 0, COM1);
  159.       if (status & DATA_READY)
  160.   if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
  161.      putch(out);
  162.   if (kbhit())
  163.   {
  164.      if ((in = getch()) == 'x1B')
  165.         DONE = TRUE;
  166.      bioscom(1, in, COM1);
  167.   }
  168.    }
  169.    return 0;
  170. }
  171.  
  172.  
  173.  
  174. 函数名: biosdisk
  175. 功  能: 软硬盘I/O
  176. 用  法: int biosdisk(int cmd, int drive, int head, int track, int sector
  177.        int nsects, void *buffer);
  178. 程序例:
  179. #include <bios.h>
  180. #include <stdio.h>
  181. int main(void)
  182. {
  183.    int result;
  184.    char buffer[512];
  185.    printf("Testing to see if drive a: is readyn");
  186.    result = biosdisk(4,0,0,0,0,1,buffer);
  187.    result &= 0x02;
  188.    (result) ? (printf("Drive A: Readyn")) :
  189.        (printf("Drive A: Not Readyn"));
  190.    return 0;
  191. }
  192.  
  193.  
  194.  
  195. 函数名: biosequip
  196. 功  能: 检查设备
  197. 用  法: int biosequip(void);
  198. 程序例:
  199. #include <bios.h>
  200. #include <stdio.h>
  201. int main(void)
  202. {
  203.    int result;
  204.    char buffer[512];
  205.    printf("Testing to see if drive a: is readyn");
  206.    result = biosdisk(4,0,0,0,0,1,buffer);
  207.    result &= 0x02;
  208.    (result) ? (printf("Drive A: Readyn")) :
  209.        (printf("Drive A: Not Readyn"));
  210.    return 0;
  211. }
  212.  
  213.  
  214.  
  215. 函数名: bioskey
  216. 功  能: 直接使用BIOS服务的键盘接口
  217. 用  法: int bioskey(int cmd);
  218. 程序例:
  219. #include <stdio.h>
  220. #include <bios.h>
  221. #include <ctype.h>
  222. #define RIGHT  0x01
  223. #define LEFT   0x02
  224. #define CTRL   0x04
  225. #define ALT    0x08
  226. int main(void)
  227. {
  228.    int key, modifiers;
  229.    /* function 1 returns 0 until a key is pressed */
  230.    while (bioskey(1) == 0);
  231.    /* function 0 returns the key that is waiting */
  232.    key = bioskey(0);
  233.    /* use function 2 to determine if shift keys were used */
  234.    modifiers = bioskey(2);
  235.    if (modifiers)
  236.    {
  237.       printf("[");
  238.       if (modifiers & RIGHT) printf("RIGHT");
  239.       if (modifiers & LEFT)  printf("LEFT");
  240.       if (modifiers & CTRL)  printf("CTRL");
  241.       if (modifiers & ALT)   printf("ALT");
  242.       printf("]");
  243.    }
  244.    /* print out the character read */
  245.    if (isalnum(key & 0xFF))
  246.       printf("'%c'n", key);
  247.    else
  248.       printf("%#02xn", key);
  249.    return 0;
  250. }
  251.  
  252.  
  253. 函数名: biosmemory
  254. 功  能: 返回存储块大小
  255. 用  法:int biosmemory(void);
  256. 程序例:
  257. #include <stdio.h>
  258. #include <bios.h>
  259. int main(void)
  260. {
  261.    int memory_size;
  262.    memory_size = biosmemory();  /* returns value up to 640K */
  263.    printf("RAM size = %dKn",memory_size);
  264.    return 0;
  265. }
  266.  
  267.  
  268.  
  269. 函数名: biosprint
  270. 功  能: 直接使用BIOS服务的打印机I/O
  271. 用  法: int biosprint(int cmd, int byte, int port);
  272. 程序例:
  273. #include <stdio.h>
  274. #include <conio.h>
  275. #include <bios.h>
  276. int main(void)
  277. {
  278.    #define STATUS  2    /* printer status command */
  279.    #define PORTNUM 0    /* port number for LPT1 */
  280.    int status, abyte=0;
  281.    printf("Please turn off your printer.  Press any key to continuen");
  282.    getch();
  283.    status = biosprint(STATUS, abyte, PORTNUM);
  284.    if (status & 0x01)
  285.       printf("Device time out.n");
  286.    if (status & 0x08)
  287.       printf("I/O error.n");
  288.    if (status & 0x10)
  289.       printf("Selected.n");
  290.    if (status & 0x20)
  291.       printf("Out of paper.n");
  292.    if (status & 0x40)
  293.       printf("Acknowledge.n");
  294.    if (status & 0x80)
  295.       printf("Not busy.n");
  296.    return 0;
  297. }
  298.  
  299.  
  300.  
  301. 函数名: biostime
  302. 功  能: 读取或设置BIOS时间
  303. 用  法: long biostime(int cmd, long newtime);
  304. 程序例:
  305. #include <stdio.h>
  306. #include <bios.h>
  307. #include <time.h>
  308. #include <conio.h>
  309. int main(void)
  310. {
  311.    long bios_time;
  312.    clrscr();
  313.    cprintf("The number of clock ticks since midnight is:rn");
  314.    cprintf("The number of seconds since midnight is:rn");
  315.    cprintf("The number of minutes since midnight is:rn");
  316.    cprintf("The number of hours since midnight is:rn");
  317.    cprintf("rnPress any key to quit:");
  318.    while(!kbhit())
  319.    {
  320.       bios_time = biostime(0, 0L);
  321.       gotoxy(50, 1);
  322.       cprintf("%lu", bios_time);
  323.       gotoxy(50, 2);
  324.       cprintf("%.4f", bios_time / CLK_TCK);
  325.       gotoxy(50, 3);
  326.       cprintf("%.4f", bios_time / CLK_TCK / 60);
  327.       gotoxy(50, 4);
  328.       cprintf("%.4f", bios_time / CLK_TCK / 3600);
  329.    }
  330.    return 0;
  331. }
  332.  
  333.  
  334.  
  335. 函数名: brk
  336. 功  能: 改变数据段空间分配
  337. 用  法: int brk(void *endds);
  338. 程序例:
  339. #include <stdio.h>
  340. #include <alloc.h>
  341. int main(void)
  342. {
  343.    char *ptr;
  344.    printf("Changing allocation with brk()n");
  345.    ptr = malloc(1);
  346.    printf("Before brk() call: %lu bytes freen", coreleft());
  347.    brk(ptr+1000);
  348.    printf(" After brk() call: %lu bytes freen", coreleft());
  349.    return 0;
  350. }
  351.  
  352.  
  353.  
  354. 函数名: bsearch
  355. 功  能: 二分法搜索
  356. 用  法: void *bsearch(const void *key, const void *base, size_t *nelem,  size_t width, int(*fcmp)(const void *, const *));
  357. 程序例:
  358. #include <stdlib.h>
  359. #include <stdio.h>
  360. #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
  361. int numarray[] = {123, 145, 512, 627, 800, 933};
  362. int numeric (const int *p1, const int *p2)
  363. {
  364.    return(*p1 - *p2);
  365. }
  366. int lookup(int key)
  367. {
  368.    int *itemptr;
  369.    /* The cast of (int(*)(const void *,const void*))
  370.       is needed to avoid a type mismatch error at
  371.       compile time */
  372.    itemptr = bsearch (&key, numarray, NELEMS(numarray),
  373.       sizeof(int), (int(*)(const void *,const void *))numeric);
  374.    return (itemptr != NULL);
  375. }
  376. int main(void)
  377. {
  378.    if (lookup(512))
  379.       printf("512 is in the table.n");
  380.    else
  381.       printf("512 isn't in the table.n");
  382.    return 0;
  383. }