数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz

C语言的函数!

  [复制链接]
 楼主| 发表于 2004-5-8 17:52:40 | 显示全部楼层
<>函数名: grapherrormsg
功  能: 返回一个错误信息串的指针
用  法: char *far grapherrormsg(int errorcode);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>#define NONSENSE -50 <P>int main(void)
{
   /* FORCE AN ERROR TO OCCUR */
   int gdriver = NONSENSE, gmode, errorcode; <P>   /* initialize graphics mode */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>   /* read result of initialization */
   errorcode = graphresult(); <P>   /* if an error occurred, then output a */
   /* descriptive error message.          */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   } <P>   /* draw a line */
   line(0, 0, getmaxx(), getmaxy()); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:52:53 | 显示全部楼层
<>函数名: graphresult
功  能: 返回最后一次不成功的图形操作的错误代码
用  法: int far graphresult(void);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode; <P>   /* initialize graphics and local variables */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>   /* read result of initialization */
   errorcode = graphresult(); <P>   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   } <P>   /* draw a line */
   line(0, 0, getmaxx(), getmaxy()); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:53:04 | 显示全部楼层
<>函数名: _graphfreemem
功  能: 用户可修改的图形存储区释放函数
用  法: void far _graphfreemem(void far *ptr, unsigned size);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;alloc.h&gt; <>int main(void)
{
       /* request auto detection */
       int gdriver = DETECT, gmode, errorcode;
       int midx, midy; <P>       /* clear the text screen */
       clrscr();
       printf("Press any key to initialize graphics mode:");
       getch();
       clrscr(); <P>       /* initialize graphics and local variables */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>       /* read result of initialization */
       errorcode = graphresult();
       if (errorcode != grOk)  /* an error occurred */
       {
          printf("Graphics error: %s\n", grapherrormsg(errorcode));
          printf("Press any key to halt:");
          getch();
          exit(1); /* terminate with an error code */
       } <P>       midx = getmaxx() / 2;
       midy = getmaxy() / 2; <P>       /* display a message */
       settextjustify(CENTER_TEXT, CENTER_TEXT);
       outtextxy(midx, midy, "Press any key to exit graphics mode:"); <P>       /* clean up */
       getch();
       closegraph();
       return 0;
} <P>/* called by the graphics kernel to allocate memory */
void far * far _graphgetmem(unsigned size)
{
       printf("_graphgetmem called to allocate %d bytes.\n", size);
       printf("hit any key:");
       getch();
       printf("\n"); <P>       /* allocate memory from far heap */
       return farmalloc(size);
} <P>/* called by the graphics kernel to free memory */
void far _graphfreemem(void far *ptr, unsigned size)
{
       printf("_graphfreemem called to free %d bytes.\n", size);
       printf("hit any key:");
       getch();
       printf("\n"); <P>      /* free ptr from far heap */
      farfree(ptr);
}
  </P>
 楼主| 发表于 2004-5-8 17:53:23 | 显示全部楼层
<>函数名: _graphgetmem
功  能: 用户可修改的图形存储区分配函数
用  法: void far *far _graphgetmem(unsigned size);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;alloc.h&gt; <>int main(void)
{
   /* request autodetection */
   int gdriver = DETECT, gmode, errorcode;
   int midx, midy; <P>   /* clear the text screen */
   clrscr();
   printf("Press any key to initialize graphics mode:");
   getch();
   clrscr(); <P>   /* initialize graphics and local variables */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)      /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);                 /* terminate with an error code */
   } <P>   midx = getmaxx() / 2;
   midy = getmaxy() / 2; <P>   /* display a message */
   settextjustify(CENTER_TEXT, CENTER_TEXT);
   outtextxy(midx, midy, "Press any key to exit graphics mode:"); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
} <P>/* called by the graphics kernel to allocate memory */
void far * far _graphgetmem(unsigned size)
{
   printf("_graphgetmem called to allocate %d bytes.\n", size);
   printf("hit any key:");
   getch();
   printf("\n"); <P>   /* allocate memory from far heap */
   return farmalloc(size);
} <P>/* called by the graphics kernel to free memory */
void far _graphfreemem(void far *ptr, unsigned size)
{
   printf("_graphfreemem called to free %d bytes.\n", size);
   printf("hit any key:");
   getch();
   printf("\n"); <P>   /* free ptr from far heap */
   farfree(ptr);
}
  </P>
 楼主| 发表于 2004-5-9 00:44:48 | 显示全部楼层
<>函数名: harderr
功  能: 建立一个硬件错误处理程序
用  法: void harderr(int (*fptr)());
程序例:
/*This program will trap disk errors and prompt
the user for action. Try running it with no
disk in drive A: to invoke its functions.*/ <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;dos.h&gt;
#define IGNORE  0
#define RETRY   1
#define ABORT   2
int buf[500];
/*define the error messages for trapping disk problems*/
static char *err_msg[] = {
    "write protect",
    "unknown unit",
    "drive not ready",
    "unknown command",
    "data error (CRC)",
    "bad request",
    "seek error",
    "unknown media type",
    "sector not found",
    "printer out of paper",
    "write fault",
    "read fault",
    "general failure",
    "reserved",
    "reserved",
    "invalid disk change"
}; <>error_win(char *msg)
{
   int retval; <P>   cputs(msg); <P>/*prompt for user to press a key to abort, retry, ignore*/
   while(1)
   {
       retval= getch();
       if (retval == 'a' || retval == 'A')
       {
    retval = ABORT;
    break;
       }
       if (retval == 'r' || retval == 'R')
       {
    retval = RETRY;
    break;
       }
       if (retval == 'i' || retval == 'I')
       {
           retval = IGNORE;
           break;
       }
   } <P>   return(retval);
} <P>/*pragma warn -par reduces warnings which occur
due to the non use of the parameters errval,
bp and si to the handler.*/
#pragma warn -par <P>int handler(int errval,int ax,int bp,int si)
{
   static char msg[80];
   unsigned di;
   int drive;
   int errorno;
   di= _DI;
/*if this is not a disk error then it was
another device having trouble*/ <P>   if (ax &lt; 0)
   {
      /* report the error */
      error_win("Device error");
      /* and return to the program directly requesting abort */
      hardretn(ABORT);
   }
/* otherwise it was a disk error */
   drive = ax &amp; 0x00FF;
   errorno = di &amp; 0x00FF;
/* report which error it was */
   sprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",
    err_msg[errorno], 'A' + drive);
/*
return to the program via dos interrupt 0x23 with abort, retry,
or ignore as input by the user.
*/
   hardresume(error_win(msg));
   return ABORT;
}
#pragma warn +par <P>int main(void)
{
/*
install our handler on the hardware problem interrupt
*/
   harderr(handler);
   clrscr();
   printf("Make sure there is no disk in drive A:\n");
   printf("Press any key ....\n");
   getch();
   printf("Trying to access drive A:\n");
   printf("fopen returned %p\n",fopen("A:temp.dat", "w"));
   return 0;
}
</P>
 楼主| 发表于 2004-5-9 00:45:07 | 显示全部楼层
<>函数名: hardresume
功  能: 硬件错误处理函数
用  法: void hardresume(int rescode);
程序例:
  <>/* This program will trap disk errors and prompt the user for action. */
/* Try running it with no disk in drive A: to invoke its functions    */ <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;dos.h&gt; <P>#define IGNORE  0
#define RETRY   1
#define ABORT   2 <P>int buf[500]; <P>/* define the error messages for trapping disk problems */
static char *err_msg[] = {
    "write protect",
    "unknown unit",
    "drive not ready",
    "unknown command",
    "data error (CRC)",
    "bad request",
    "seek error",
    "unknown media type",
    "sector not found",
    "printer out of paper",
    "write fault",
    "read fault",
    "general failure",
    "reserved",
    "reserved",
    "invalid disk change"
}; <P>error_win(char *msg)
{
   int retval; <P>   cputs(msg); <P>/* prompt for user to press a key to abort, retry, ignore */
   while(1)
   {
       retval= getch();
       if (retval == 'a' || retval == 'A')
       {
           retval = ABORT;
           break;
       }
       if (retval == 'r' || retval == 'R')
       {
           retval = RETRY;
           break;
       }
       if (retval == 'i' || retval == 'I')
       {
           retval = IGNORE;
           break;
       }
   } <P>   return(retval);
} <P>/* pragma warn -par reduces warnings which occur due to the non use */
/* of the parameters errval, bp and si to the handler.              */
#pragma warn -par <P>int handler(int errval,int ax,int bp,int si)
{
   static char msg[80];
   unsigned di;
   int drive;
   int errorno; <P>   di= _DI;
/* if this is not a disk error then it was another device having trouble */ <P>   if (ax &lt; 0)
   {
      /* report the error */
      error_win("Device error");
      /* and return to the program directly
      requesting abort */
      hardretn(ABORT);
   }
/* otherwise it was a disk error */
   drive = ax &amp; 0x00FF;
   errorno = di &amp; 0x00FF;
/* report which error it was */
   sprintf(msg, "Error: %s on drive %c\r\nA)bort, R)etry, I)gnore: ",
           err_msg[errorno], 'A' + drive);
/* return to the program via dos interrupt 0x23 with abort, retry */
/* or ignore as input by the user.  */
   hardresume(error_win(msg));
   return ABORT;
}
#pragma warn +par <P>int main(void)
{
/* install our handler on the hardware problem interrupt */
   harderr(handler);
   clrscr();
   printf("Make sure there is no disk in drive A:\n");
   printf("Press any key ....\n");
   getch();
   printf("Trying to access drive A:\n");
   printf("fopen returned %p\n",fopen("A:temp.dat", "w"));
   return 0;
}
  
</P>
 楼主| 发表于 2004-5-9 00:45:25 | 显示全部楼层
<>函数名: highvideo
功  能: 选择高亮度文本字符
用  法: void highvideo(void);
程序例: <>#include &lt;conio.h&gt; <>int main(void)
{
   clrscr(); <P>   lowvideo();
   cprintf("Low Intensity text\r\n");
   highvideo();
   gotoxy(1,2);
   cprintf("High Intensity Text\r\n"); <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-9 00:48:33 | 显示全部楼层
<>函数名: hypot
功  能: 计算直角三角形的斜边长
用  法: double hypot(double x, double y);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;math.h&gt; <>int main(void)
{
   double result;
   double x = 3.0;
   double y = 4.0; <P>   result = hypot(x, y);
   printf("The hypotenuse is: %lf\n", result); <P>   return 0;
}
</P>
 楼主| 发表于 2004-5-9 00:48:56 | 显示全部楼层
<>函数名: imagesize
功  能: 返回保存位图像所需的字节数
用  法: unsigned far imagesize(int left, int top, int right, int bottom);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>#define ARROW_SIZE 10 <P>void draw_arrow(int x, int y); <P>int main(void)
{
   /* request autodetection */
   int gdriver = DETECT, gmode, errorcode;
   void *arrow;
   int x, y, maxx;
   unsigned int size; <P>   /* initialize graphics and local variables */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   } <P>   maxx = getmaxx();
   x = 0;
   y = getmaxy() / 2; <P>   /* draw the image to be grabbed */
   draw_arrow(x, y); <P>   /* calculate the size of the image */
   size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE); <P>   /* allocate memory to hold the image */
   arrow = malloc(size); <P>   /* grab the image */
   getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow); <P>   /* repeat until a key is pressed */
   while (!kbhit())
   {
      /* erase old image */
      putimage(x, y-ARROW_SIZE, arrow, XOR_PUT); <P>      x += ARROW_SIZE;
      if (x &gt;= maxx)
          x = 0; <P>      /* plot new image */
      putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
   } <P>   /* clean up */
   free(arrow);
   closegraph();
   return 0;
} <P>void draw_arrow(int x, int y)
{
   /* draw an arrow on the screen */
   moveto(x, y);
   linerel(4*ARROW_SIZE, 0);
   linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
   linerel(0, 2*ARROW_SIZE);
   linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
}
  
  
</P>
 楼主| 发表于 2004-5-9 00:49:19 | 显示全部楼层
<>函数名: initgraph
功  能: 初始化图形系统
用  法: void far initgraph(int far *graphdriver, int far *graphmode,
    char far *pathtodriver);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode; <P>   /* initialize graphics mode */
   initgraph(&amp;gdriver, &amp;gmode, ""); <P>   /* read result of initialization */
   errorcode = graphresult(); <P>   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   } <P>   /* draw a line */
   line(0, 0, getmaxx(), getmaxy()); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
  
</P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-11-27 01:31 , Processed in 0.053307 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表