数模论坛

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

C语言的函数!

  [复制链接]
 楼主| 发表于 2004-5-8 17:20:05 | 显示全部楼层
<>函数名: calloc
功  能: 分配主存储器
用  法: void *calloc(size_t nelem, size_t elsize);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;alloc.h&gt; <>int main(void)
{
   char *str = NULL; <P>   /* allocate memory for string */
   str = calloc(10, sizeof(char)); <P>   /* copy "Hello" into string */
   strcpy(str, "Hello"); <P>   /* display string */
   printf("String is %s\n", str); <P>   /* free memory */
   free(str); <P>   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:20:20 | 显示全部楼层
<>函数名: ceil
功  能: 向上舍入
用  法: double ceil(double x);
程序例: <>#include &lt;math.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   double number = 123.54;
   double down, up; <P>   down = floor(number);
   up = ceil(number); <P>   printf("original number     %5.2lf\n", number);
   printf("number rounded down %5.2lf\n", down);
   printf("number rounded up   %5.2lf\n", up); <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:20:32 | 显示全部楼层
<>函数名: cgets
功  能: 从控制台读字符串
用  法: char *cgets(char *str);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>int main(void)
{
   char buffer[83];
   char *p; <P>   /* There's space for 80 characters plus the NULL terminator */
   buffer[0] = 81; <P>   printf("Input some chars:");
   p = cgets(buffer);
   printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
   printf("The returned pointer is %p, buffer[0] is at %p\n", p, &amp;buffer); <P>   /* Leave room for 5 characters plus the NULL terminator */
   buffer[0] = 6; <P>   printf("Input some chars:");
   p = cgets(buffer);
   printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
   printf("The returned pointer is %p, buffer[0] is at %p\n", p, &amp;buffer);
   return 0;
}
  
  
</P>
 楼主| 发表于 2004-5-8 17:20:42 | 显示全部楼层
<>函数名: chdir
功  能: 改变工作目录
用  法: int chdir(const char *path);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;dir.h&gt; <>char old_dir[MAXDIR];
char new_dir[MAXDIR]; <P>int main(void)
{
   if (getcurdir(0, old_dir))
   {
      perror("getcurdir()");
      exit(1);
   }
   printf("Current directory is: \\%s\n", old_dir); <P>   if (chdir("\\"))
   {
      perror("chdir()");
      exit(1);
   } <P>   if (getcurdir(0, new_dir))
   {
      perror("getcurdir()");
      exit(1);
   }
   printf("Current directory is now: \\%s\n", new_dir); <P>   printf("\nChanging back to orignal directory: \\%s\n", old_dir);
   if (chdir(old_dir))
   {
      perror("chdir()");
      exit(1);
   } <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:20:52 | 显示全部楼层
<>函数名: _chmod, chmod
功  能: 改变文件的访问方式
用  法: int chmod(const char *filename, int permiss);
程序例: <>#include &lt;sys\stat.h&gt;
#include &lt;stdio.h&gt;
#include &lt;io.h&gt; <>void make_read_only(char *filename); <P>int main(void)
{
   make_read_only("NOTEXIST.FIL");
   make_read_only("MYFILE.FIL");
   return 0;
} <P>void make_read_only(char *filename)
{
   int stat; <P>   stat = chmod(filename, S_IREAD);
   if (stat)
      printf("Couldn't make %s read-only\n", filename);
   else
      printf("Made %s read-only\n", filename);
}
  
</P>
 楼主| 发表于 2004-5-8 17:21:02 | 显示全部楼层
<>函数名: chsize
功  能: 改变文件大小
用  法: int chsize(int handle, long size);
程序例: <>#include &lt;string.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;io.h&gt; <>int main(void)
{
   int handle;
   char buf[11] = "0123456789"; <P>   /* create text file containing 10 bytes */
   handle = open("DUMMY.FIL", O_CREAT);
   write(handle, buf, strlen(buf)); <P>   /* truncate the file to 5 bytes in size */
   chsize(handle, 5); <P>   /* close the file */
   close(handle);
   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:21:13 | 显示全部楼层
<>函数名: circle
功  能: 在给定半径以(x, y)为圆心画圆
用  法: void far circle(int x, int y, int radius);
程序例: <>#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;
   int midx, midy;
   int radius = 100; <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;
   setcolor(getmaxcolor()); <P>   /* draw the circle */
   circle(midx, midy, radius); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-8 17:21:23 | 显示全部楼层
<>函数名: cleardevice
功  能: 清除图形屏幕
用  法: void far cleardevice(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;
   int midx, midy; <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;
   setcolor(getmaxcolor()); <P>   /* for centering screen messages */
   settextjustify(CENTER_TEXT, CENTER_TEXT); <P>   /* output a message to the screen */
   outtextxy(midx, midy, "press any key to clear the screen:"); <P>   /* wait for a key */
   getch(); <P>   /* clear the screen */
   cleardevice(); <P>   /* output another message */
   outtextxy(midx, midy, "press any key to quit:"); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
  
  
  </P>
 楼主| 发表于 2004-5-8 17:21:33 | 显示全部楼层
<>函数名: clearerr
功  能: 复位错误标志
用  法:void clearerr(FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   FILE *fp;
   char ch; <P>   /* open a file for writing */
   fp = fopen("DUMMY.FIL", "w"); <P>   /* force an error condition by attempting to read */
   ch = fgetc(fp);
   printf("%c\n",ch); <P>   if (ferror(fp))
   {
      /* display an error message */
      printf("Error reading from DUMMY.FIL\n"); <P>      /* reset the error and EOF indicators */
      clearerr(fp);
   } <P>   fclose(fp);
   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:21:42 | 显示全部楼层
<>函数名: clearviewport
功  能: 清除图形视区
用  法: void far clearviewport(void);
程序例: <>#include &lt;graphics.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>#define CLIP_ON 1   /* activates clipping in viewport */ <P>int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int ht; <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>   setcolor(getmaxcolor());
   ht = textheight("W"); <P>   /* message in default full-screen viewport */
   outtextxy(0, 0, "* &lt;-- (0, 0) in default viewport"); <P>   /* create a smaller viewport */
   setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); <P>   /* display some messages */
   outtextxy(0, 0, "* &lt;-- (0, 0) in smaller viewport");
   outtextxy(0, 2*ht, "Press any key to clear viewport:"); <P>   /* wait for a key */
   getch(); <P>   /* clear the viewport */
   clearviewport(); <P>   /* output another message */
   outtextxy(0, 0, "Press any key to quit:"); <P>   /* clean up */
   getch();
   closegraph();
   return 0;
}
</P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

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

GMT+8, 2024-11-26 20:35 , Processed in 0.047521 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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