C/C++——小编谈C语言函数那些事(20)

 

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。

 

1.     setallpallette函数

setallpallette函数的功能是按指定方式改变所有的调色板颜色,其用法为:void far setallpallette(struct palette, far *pallette);程序实例如下:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
    int gdriver = DETECT, gmode, errorcode;
   struct palettetype pal;
   int color, maxcolor, ht;
   int y = 10;
   char msg[80];
     initgraph(&gdriver, &gmode, "");
     errorcode = graphresult();
   if (errorcode != grOk) 
   {
      printf("Graphics error: %s/n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); 
   }
   maxcolor = getmaxcolor();
   ht = 2 * textheight("W");
    getpalette(&pal);
    for (color=1; color<=maxcolor; color++)
   {
      setcolor(color);
      sprintf(msg, "Color: %d", color);
      outtextxy(1, y, msg);
      y += ht;
   }
     getch();

   for (color=1; color<=maxcolor; color++)
   {
      setpalette(color, BLACK);
      getch();
   }
    setallpalette(&pal);

   getch();
   closegraph();
   return 0;
}

 2.   setaspectratio函数

setaspectratio函数的功能是设置图形纵横比,其用法为void far setaspectratio(int xasp, int yasp);程序实例代码如下:

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   int gdriver = DETECT, gmode, errorcode;

   int xasp, yasp, midx, midy;

   initgraph(&gdriver, &gmode, "");

   errorcode = graphresult();

   if (errorcode != grOk)

   {

      printf("Graphics error: %s/n", grapherrormsg(errorcode));

      printf("Press any key to halt:");

      getch();

      exit(1);

   }

   midx = getmaxx() / 2;

   midy = getmaxy() / 2;

   setcolor(getmaxcolor());

    getaspectratio(&xasp, &yasp);

   circle(midx, midy, 100);

   getch();

   cleardevice();

   setaspectratio(xasp/2, yasp);

   circle(midx, midy, 100);

   getch();

   cleardevice();

   setaspectratio(xasp, yasp/2);

   circle(midx, midy, 100);

   getch();

   closegraph();

   return 0;

}3.     setblock函数

setblock函数的功能是修改先前已分配的DOS存储段大小, 其用法为:int setblock(int seg, int newsize);程序实例代码如下:

#include <dos.h>
#include <alloc.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   unsigned int size, segp;
   int stat;
   size = 64;
   stat = allocmem(size, &segp);
   if (stat == -1)
      printf("Allocated memory at segment: %X/n", segp);
   else
   {
      printf("Failed: maximum number of paragraphs available is %d/n",
      stat);
      exit(1);
   }
   stat = setblock(segp, size * 2);
   if (stat == -1)
      printf("Expanded memory block at segment: %X/n", segp);
   else
      printf("Failed: maximum number of paragraphs available is %d/n",
             stat);
   freemem(segp);
   return 0;

4.     setdate函数

setdate函数的功能是设置DOS日期,其用法为:void setdate(struct date *dateblk);程序实例代码如下:

#include <stdio.h>
#include <process.h>
#include <dos.h>
int main(void)
{
   struct date reset;
   struct date save_date;
   getdate(&save_date);
   printf("Original date:/n");
   system("date");
   reset.da_year = 2001;
   reset.da_day = 1;
   reset.da_mon = 1;
   setdate(&reset);
   printf("Date after setting:/n");
   system("date");
   setdate(&save_date);
   printf("Back to original date:/n");
   system("date");
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值