p.txt
Upload User: harvey99
Upload Date: 2020-01-11
Package Size: 938k
Code Size: 14k
Category:

Other Books

Development Platform:

CHM

  1. 函数名: parsfnm
  2. 功  能: 分析文件名
  3. 用  法: char *parsfnm (char *cmdline, struct fcb *fcbptr, int option);
  4. 程序例: 
  5. #include <process.h> 
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <dos.h>
  9. int main(void)
  10. {
  11.    char line[80];
  12.    struct fcb blk;
  13.    /* get file name */
  14.    printf("Enter drive and file name (no path - ie. a:file.dat)n");
  15.    gets(line);
  16.    /* put file name in fcb */
  17.    if (parsfnm(line, &blk, 1) == NULL)
  18.       printf("Error in parsfm calln");
  19.    else
  20.       printf("Drive #%d  Name: %11sn", blk.fcb_drive, blk.fcb_name);
  21.    return 0;
  22. }
  23.  
  24.  
  25. 函数名: peek
  26. 功  能: 检查存储单元
  27. 用  法: int peek(int segment, unsigned offset);
  28. 程序例:
  29. #include <stdio.h>
  30. #include <conio.h>
  31. #include <dos.h>
  32. int main(void)
  33. {
  34.    int value = 0;
  35.    printf("The current status of your keyboard is:n");
  36.    value = peek(0x0040, 0x0017);
  37.    if (value & 1)
  38.       printf("Right shift onn");
  39.    else
  40.       printf("Right shift offn");
  41.    if (value & 2)
  42.       printf("Left shift onn");
  43.    else
  44.       printf("Left shift offn");
  45.    if (value & 4)
  46.       printf("Control key onn");
  47.    else
  48.       printf("Control key offn");
  49.    if (value & 8)
  50.       printf("Alt key onn");
  51.    else
  52.       printf("Alt key offn");
  53.    if (value & 16)
  54.       printf("Scroll lock onn");
  55.    else
  56.       printf("Scroll lock offn");
  57.    if (value & 32)
  58.       printf("Num lock onn");
  59.    else
  60.       printf("Num lock offn");
  61.    if (value & 64)
  62.       printf("Caps lock onn");
  63.    else
  64.       printf("Caps lock offn");
  65.    return 0;
  66. }
  67.  
  68.  
  69. 函数名: peekb
  70. 功  能: 检查存储单元
  71. 用  法: char peekb (int segment, unsigned offset);
  72. 程序例:
  73. #include <stdio.h>
  74. #include <conio.h>
  75. #include <dos.h>
  76. int main(void)
  77. {
  78.    int value = 0;
  79.    printf("The current status of your keyboard is:n");
  80.    value = peekb(0x0040, 0x0017);
  81.    if (value & 1)
  82.       printf("Right shift onn");
  83.    else
  84.       printf("Right shift offn");
  85.    if (value & 2)
  86.       printf("Left shift onn");
  87.    else
  88.       printf("Left shift offn");
  89.    if (value & 4)
  90.       printf("Control key onn");
  91.    else
  92.       printf("Control key offn");
  93.    if (value & 8)
  94.       printf("Alt key onn");
  95.    else
  96.       printf("Alt key offn");
  97.    if (value & 16)
  98.       printf("Scroll lock onn");
  99.    else
  100.       printf("Scroll lock offn");
  101.    if (value & 32)
  102.       printf("Num lock onn");
  103.    else
  104.       printf("Num lock offn");
  105.    if (value & 64)
  106.       printf("Caps lock onn");
  107.    else
  108.       printf("Caps lock offn");
  109.    return 0;
  110. }
  111.  
  112.  
  113. 函数名: perror
  114. 功  能: 系统错误信息
  115. 用  法: void perror(char *string);
  116. 程序例:
  117. #include <stdio.h>
  118. int main(void)
  119. {
  120.    FILE *fp;
  121.    fp = fopen("perror.dat", "r");
  122.    if (!fp)
  123.       perror("Unable to open file for reading");
  124.    return 0;
  125. }
  126.  
  127.  
  128. 函数名: pieslice
  129. 功  能: 绘制并填充一个扇形
  130. 用  法: void far pieslice(int x, int stanle, int endangle, int radius);
  131. 程序例:
  132. #include <graphics.h>
  133. #include <stdlib.h>
  134. #include <stdio.h>
  135. #include <conio.h>
  136. int main(void)
  137. {
  138.    /* request auto detection */
  139.    int gdriver = DETECT, gmode, errorcode;
  140.    int midx, midy;
  141.    int stangle = 45, endangle = 135, radius = 100;
  142.    /* initialize graphics and local variables */
  143.    initgraph(&gdriver, &gmode, "");
  144.    /* read result of initialization */
  145.    errorcode = graphresult();
  146.    if (errorcode != grOk)  /* an error occurred */
  147.    {
  148.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  149.       printf("Press any key to halt:");
  150.       getch();
  151.       exit(1); /* terminate with an error code */
  152.    }
  153.    midx = getmaxx() / 2;
  154.    midy = getmaxy() / 2;
  155.    /* set fill style and draw a pie slice */
  156.    setfillstyle(EMPTY_FILL, getmaxcolor());
  157.    pieslice(midx, midy, stangle, endangle, radius);
  158.    /* clean up */
  159.    getch();
  160.    closegraph();
  161.    return 0;
  162. }
  163.  
  164.  
  165. 函数名: poke
  166. 功  能: 存值到一个给定存储单元
  167. 用  法: void poke(int segment, int offset, int value);
  168. 程序例:
  169. #include <dos.h>
  170. #include <conio.h>
  171. int main(void)
  172. {
  173.    clrscr();
  174.    cprintf("Make sure the scroll lock key is off and press any keyrn");
  175.    getch();
  176.    poke(0x0000,0x0417,16);
  177.    cprintf("The scroll lock is now onrn");
  178.    return 0;
  179. }
  180.  
  181.  
  182. 函数名: pokeb
  183. 功  能: 存值到一个给定存储单元
  184. 用  法: void pokeb(int segment, int offset, char value);
  185. 程序例:
  186. #include <dos.h>
  187. #include <conio.h>
  188. int main(void)
  189. {
  190.    clrscr();
  191.    cprintf("Make sure the scroll lock key is off and press any keyrn");
  192.    getch();
  193.    pokeb(0x0000,0x0417,16);
  194.    cprintf("The scroll lock is now onrn");
  195.    return 0;
  196. }
  197.  
  198.  
  199. 函数名: poly
  200. 功  能: 根据参数产生一个多项式
  201. 用  法: double poly(double x, int n, double c[]);
  202. 程序例:
  203. #include <stdio.h>
  204. #include <math.h>
  205. /* polynomial:  x**3 - 2x**2 + 5x - 1 */
  206. int main(void)
  207. {
  208.    double array[] = { -1.0, 5.0, -2.0, 1.0 };
  209.    double result;
  210.    result = poly(2.0, 3, array);
  211.    printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lfn",
  212.            result);
  213.    return 0;
  214. }
  215.  
  216.  
  217. 函数名: pow
  218. 功  能: 指数函数(x的y次方)
  219. 用  法: double pow(double x, double y);
  220. 程序例:
  221. #include <math.h>
  222. #include <stdio.h>
  223. int main(void)
  224. {
  225.    double x = 2.0, y = 3.0;
  226.    printf("%lf raised to %lf is %lfn", x, y, pow(x, y));
  227.    return 0;
  228. }
  229.  
  230. 函数名: pow10
  231. 功  能: 指数函数(10的p次方)
  232. 用  法: double pow10(int p);
  233. 程序例:
  234. #include <math.h>
  235. #include <stdio.h>
  236. int main(void)
  237. {
  238.    double p = 3.0;
  239.    printf("Ten raised to %lf is %lfn", p, pow10(p));
  240.    return 0;
  241. }
  242.  
  243.  
  244. 函数名: printf
  245. 功  能: 产生格式化输出的函数
  246. 用  法: int printf(char *format...);
  247. 程序例:
  248. #include <stdio.h>
  249. #include <string.h>
  250. #define I 555
  251. #define R 5.5
  252. int main(void)
  253. {
  254.    int i,j,k,l;
  255.    char buf[7];
  256.    char *prefix = buf;
  257.    char tp[20];
  258.    printf("prefix  6d      6o      8x        10.2e        "
  259.           "10.2fn");
  260.    strcpy(prefix,"%");
  261.    for (i = 0; i < 2; i++)
  262.    {
  263.       for (j = 0; j < 2; j++)
  264.          for (k = 0; k < 2; k++)
  265.      for (l = 0; l < 2; l++)
  266.             {
  267.                if (i==0)  strcat(prefix,"-");
  268.                if (j==0)  strcat(prefix,"+");
  269.                if (k==0)  strcat(prefix,"#");
  270.                if (l==0)  strcat(prefix,"0");
  271.                printf("%5s |",prefix);
  272.                strcpy(tp,prefix);
  273.                strcat(tp,"6d |");
  274.                printf(tp,I);
  275.                strcpy(tp,"");
  276.                strcpy(tp,prefix);
  277.                strcat(tp,"6o |");
  278.                printf(tp,I);
  279.                strcpy(tp,"");
  280.                strcpy(tp,prefix);
  281.                strcat(tp,"8x |");
  282.                printf(tp,I);
  283.                strcpy(tp,"");
  284.                strcpy(tp,prefix);
  285.         strcat(tp,"10.2e |");
  286.         printf(tp,R);
  287.         strcpy(tp,prefix);
  288.         strcat(tp,"10.2f |");
  289.         printf(tp,R);
  290.         printf("  n");
  291.         strcpy(prefix,"%");
  292.      }
  293.        }
  294.    return 0;
  295. }
  296.  
  297.  
  298. 函数名: putc
  299. 功  能: 输出一字符到指定流中
  300. 用  法: int putc(int ch, FILE *stream);
  301. 程序例:
  302. #include <stdio.h>
  303. int main(void)
  304. {
  305.    char msg[] = "Hello worldn";
  306.    int i = 0;
  307.    while (msg[i])
  308.       putc(msg[i++], stdout);
  309.    return 0;
  310. }
  311.  
  312.  
  313. 函数名: putch
  314. 功  能: 输出字符到控制台
  315. 用  法: int putch(int ch);
  316. 程序例:
  317. #include <stdio.h>
  318. #include <conio.h>
  319. int main(void)
  320. {
  321.    char ch = 0;
  322.    printf("Input a string:");
  323.    while ((ch != 'r'))
  324.    {
  325.       ch = getch();
  326.       putch(ch);
  327.    }
  328.    return 0;
  329. }
  330.  
  331.  
  332. 函数名: putchar
  333. 功  能: 在stdout上输出字符
  334. 用  法: int putchar(int ch);
  335. 程序例:
  336. #include <stdio.h>
  337. /* define some box-drawing characters */
  338. #define LEFT_TOP  0xDA
  339. #define RIGHT_TOP 0xBF
  340. #define HORIZ     0xC4
  341. #define VERT      0xB3
  342. #define LEFT_BOT  0xC0
  343. #define RIGHT_BOT 0xD9
  344. int main(void)
  345. {
  346.    char i, j;
  347.    /* draw the top of the box */
  348.    putchar(LEFT_TOP);
  349.    for (i=0; i<10; i++)
  350.       putchar(HORIZ);
  351.    putchar(RIGHT_TOP);
  352.    putchar('n');
  353.    /* draw the middle */
  354.    for (i=0; i<4; i++)
  355.    {
  356.       putchar(VERT);
  357.       for (j=0; j<10; j++)
  358.          putchar(' ');
  359.       putchar(VERT);
  360.       putchar('n');
  361.    }
  362.    /* draw the bottom */
  363.    putchar(LEFT_BOT);
  364.    for (i=0; i<10; i++)
  365.       putchar(HORIZ);
  366.    putchar(RIGHT_BOT);
  367.    putchar('n');
  368.    return 0;
  369. }
  370.  
  371.  
  372. 函数名: putenv
  373. 功  能: 把字符串加到当前环境中
  374. 用  法: int putenv(char *envvar);
  375. 程序例:
  376. #include <stdio.h>
  377. #include <stdlib.h>
  378. #include <alloc.h>
  379. #include <string.h>
  380. #include <dos.h>
  381. int main(void)
  382. {
  383.    char *path, *ptr;
  384.    int i = 0;
  385.    /* get the current path environment */
  386.    ptr = getenv("PATH");
  387.    /* set up new path */
  388.    path = malloc(strlen(ptr)+15);
  389.    strcpy(path,"PATH=");
  390.    strcat(path,ptr);
  391.    strcat(path,";c:\temp");
  392.    /* replace the current path and display current environment */
  393.    putenv(path);
  394.    while (environ[i])
  395.        printf("%sn",environ[i++]);
  396.    return 0;
  397. }
  398.  
  399.  
  400. 函数名: putimage
  401. 功  能: 在屏幕上输出一个位图
  402. 用  法: void far putimage(int x, int y, void far *bitmap, int op);
  403. 程序例:
  404. #include <graphics.h>
  405. #include <stdlib.h>
  406. #include <stdio.h>
  407. #include <conio.h>
  408. #define ARROW_SIZE 10
  409. void draw_arrow(int x, int y);
  410. int main(void)
  411. {
  412.    /* request autodetection */
  413.    int gdriver = DETECT, gmode, errorcode;
  414.    void *arrow;
  415.    int x, y, maxx;
  416.    unsigned int size;
  417.    /* initialize graphics and local variables */
  418.    initgraph(&gdriver, &gmode, "");
  419.    /* read result of initialization */
  420.    errorcode = graphresult();
  421.    if (errorcode != grOk)  /* an error occurred */
  422.    {
  423.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  424.       printf("Press any key to halt:");
  425.       getch();
  426.       exit(1); /* terminate with an error code */
  427.    }
  428.    maxx = getmaxx();
  429.    x = 0;
  430.    y = getmaxy() / 2;
  431.    /* draw the image to be grabbed */
  432.    draw_arrow(x, y);
  433.    /* calculate the size of the image */
  434.    size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);
  435.    /* allocate memory to hold the image */
  436.    arrow = malloc(size);
  437.    /* grab the image */
  438.    getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow);
  439.    /* repeat until a key is pressed */
  440.    while (!kbhit())
  441.    {
  442.       /* erase old image */
  443.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
  444.       x += ARROW_SIZE;
  445.       if (x >= maxx)
  446.           x = 0;
  447.       /* plot new image */
  448.       putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
  449.    }
  450.    /* clean up */
  451.    free(arrow);
  452.    closegraph();
  453.    return 0;
  454. }
  455. void draw_arrow(int x, int y)
  456. {
  457.    /* draw an arrow on the screen */
  458.    moveto(x, y);
  459.    linerel(4*ARROW_SIZE, 0);
  460.    linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
  461.    linerel(0, 2*ARROW_SIZE);
  462.    linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
  463. }
  464.  
  465.  
  466. 函数名: putpixel
  467. 功  能: 在指定位置画一像素
  468. 用  法: void far putpixel (int x, int y, int pixelcolor);
  469. 程序例:
  470. #include <graphics.h>
  471. #include <stdlib.h>
  472. #include <stdio.h>
  473. #include <conio.h>
  474. #include <dos.h>
  475. #define PIXEL_COUNT 1000
  476. #define DELAY_TIME  100  /* in milliseconds */
  477. int main(void)
  478. {
  479.    /* request autodetection */
  480.    int gdriver = DETECT, gmode, errorcode;
  481.    int i, x, y, color, maxx, maxy, maxcolor, seed;
  482.    /* initialize graphics and local variables */
  483.    initgraph(&gdriver, &gmode, "");
  484.    /* read result of initialization */
  485.    errorcode = graphresult();
  486.    if (errorcode != grOk)  /* an error occurred */
  487.    {
  488.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  489.       printf("Press any key to halt:");
  490.       getch();
  491.       exit(1); /* terminate with an error code */
  492.    }
  493.    maxx = getmaxx() + 1;
  494.    maxy = getmaxy() + 1;
  495.    maxcolor = getmaxcolor() + 1;
  496.    while (!kbhit())
  497.    {
  498.       /* seed the random number generator */
  499.       seed = random(32767);
  500.       srand(seed);
  501.       for (i=0; i<PIXEL_COUNT; i++)
  502.       {
  503.   x = random(maxx);
  504.          y = random(maxy);
  505.          color = random(maxcolor);
  506.          putpixel(x, y, color);
  507.       }
  508.       delay(DELAY_TIME);
  509.       srand(seed);
  510.       for (i=0; i<PIXEL_COUNT; i++)
  511.       {
  512.   x = random(maxx);
  513.   y = random(maxy);
  514.   color = random(maxcolor);
  515.   if (color == getpixel(x, y))
  516.      putpixel(x, y, 0);
  517.       }
  518.    }
  519.    /* clean up */
  520.    getch();
  521.    closegraph();
  522.    return 0;
  523. }
  524.  
  525.  
  526. 函数名: puts
  527. 功  能: 送一字符串到流中
  528. 用  法: int puts(char *string);
  529. 程序例:
  530. #include <stdio.h>
  531. int main(void)
  532. {
  533.    char string[] = "This is an example output stringn";
  534.    puts(string);
  535.    return 0;
  536. }
  537.  
  538.  
  539. 函数名: puttext
  540. 功  能: 将文本从存储区拷贝到屏幕
  541. 用  法: int puttext(int left, int top, int right, int bottom, void *source);
  542. 程序例:
  543. #include <conio.h>
  544. int main(void)
  545. {
  546.    char buffer[512];
  547.    /* put some text to the console */
  548.    clrscr();
  549.    gotoxy(20, 12);
  550.    cprintf("This is a test.  Press any key to continue ...");
  551.    getch();
  552.    /* grab screen contents */
  553.    gettext(20, 12, 36, 21,buffer);
  554.    clrscr();
  555.    /* put selected characters back to the screen */
  556.    gotoxy(20, 12);
  557.    puttext(20, 12, 36, 21, buffer);
  558.    getch();
  559.    return 0;
  560. }
  561.  
  562.  
  563. 函数名: putw
  564. 功  能: 把一字符或字送到流中
  565. 用  法: int putw(int w, FILE *stream);
  566. 程序例:
  567. #include <stdio.h>
  568. #include <stdlib.h>
  569. #define FNAME "test.$$$"
  570. int main(void)
  571. {
  572.    FILE *fp;
  573.    int word;
  574.    /* place the word in a file */
  575.    fp = fopen(FNAME, "wb");
  576.    if (fp == NULL)
  577.    {
  578.       printf("Error opening file %sn", FNAME);
  579.       exit(1);
  580.    }
  581.    word = 94;
  582.    putw(word,fp);
  583.    if (ferror(fp))
  584.        printf("Error writing to filen");
  585.    else
  586.        printf("Successful writen");
  587.    fclose(fp);
  588.    /* reopen the file */
  589.    fp = fopen(FNAME, "rb");
  590.    if (fp == NULL)
  591.    {
  592.       printf("Error opening file %sn", FNAME);
  593.       exit(1);
  594.    }
  595.    /* extract the word */
  596.    word = getw(fp);
  597.    if (ferror(fp))
  598.        printf("Error reading filen");
  599.    else
  600.        printf("Successful read: word = %dn", word);
  601.    /* clean up */
  602.    fclose(fp);
  603.    unlink(FNAME);
  604.    return 0;
  605. }
  606.