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;
}