网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

spin函数c语言 spc函数

C语言中的俄罗斯方块

我给你,我有源代码,可以运行

义安网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

/*************************

Russia Diamonds Ver 1.0

Copyright by forever_chang

forever_chang@china点抗

2001.11.1

*************************/

/*****************************************************************************************/

#include "graphics.h" /*头文件*/

#include "time.h"

#include "stdlib.h"

#include "bios.h"

#include "dos.h"

#include "stdio.h"

#define ESC 0x11b /*键盘扫描码*/

#define UP 0x4800

#define DOWN 0x5000

#define LEFT 0x4b00

#define F1 0x3b00

#define RIGHT 0x4d00

#define YES 0x1579

#define NO 0x316e

#define RESTART 0x1372

/*****************************************************************************************/

struct diamond /*记录每种方块每种状态的信息*/

{

int x[4];

int y[4];

int start_x;

int start_y;

int color;

struct diamond *next;

};

int grid[12][23]; /*记录所有格子的状态 (0)没有方块 (1)有固定方块 (2)有活动方块*/

int x; /*活动方块所在位置*/

int y;

int level; /*游戏难度*/

int count; /*计数器*/

int score;/*得分*/

struct diamond *nowinfo; /*当前活动方块*/

struct diamond *nextinfo; /*下一个方块*/

int color;/*画格子的颜色*/

int backcolor;/*游戏区域背景色*/

/*****************************************************************************************/

void show_interface (int mode);/*显示游戏界面*/

void show_copsign (int topx,int topy,int size,int color);/*显示公司标志--恒基伟业*/

void show_intro (int xs,int ys);/*显示软件介绍区*/

/*void print(); 测试用函数*/

void scandel();/*扫描所有格子看是否有可消除行*/

void show_down ();/*方块下落后的下一个状态*/

void show_next ();/*方块翻转后的下一个状态*/

void show_left ();/*方块向左移动后的下一个状态*/

void show_right ();/*方块向右移动后的下一个状态*/

void interrupt (*oldtimer)();/*指向未安装前的中断向量,即函数指针,指向一段可执行的代码*/

void install();/*安装新的中断向量,即将中断服务程序安装到中断向量表中*/

void interrupt newtimer();/*中断服务程序*/

struct diamond *get_diamond();/*随机得到一个方块*/

struct diamond *create_I();/*函数用来构造各种类形方块的环形链表*/

struct diamond *create_T();/*返回链表中随机一个状态的指针*/

struct diamond *create_L();

struct diamond *create_J();

struct diamond *create_Z();

struct diamond *create_N();

struct diamond *create_H();

void delinfo (struct diamond *info);/*释放当前方块所占用的空间*/

void addtobuffer(int c);/*向键盘缓冲区中增加一个DOWN*/

/*void clrkey();调用dos中断清空键盘缓冲区,未使用此方法.*/

void showsubwin(struct diamond *next);/*在小窗口显示下一个方块*/

void showscore(int scoreget);/*显示分数*/

void startset();/*初始化游戏*/

/*****************************************************************************************/

main ()

{

int driver=VGA;

int mode=VGAHI;

int i;/*计数器*/

int j;

int key;/*记录键盘扫描码*/

initgraph (driver,mode,"");/*初始化*/

color=8;

backcolor=7;

level=1;

score=298;

count=0;

show_interface (9);/*显示界面*/

setfillstyle(SOLID_FILL,1);

bar(0,465,640,480);

outtextxy(5,469,"Press any key to start...");

getch();

setfillstyle(SOLID_FILL,9);

bar(0,465,640,480);

showscore(0);/*显示分数*/

randomize();

nowinfo=get_diamond ();/*得到一个当前方块*/

oldtimer=getvect(0x1c);

install(newtimer);

for(i=0;i=21;i++)/*初始化grid[12][23]*/

{

for(j=1;j=10;j++)

grid[j][i]=0;

}

for(i=0;i=22;i++)

{

grid[0][i]=1;

grid[11][i]=1;

}

for(i=0;i=11;i++)

{

grid[i][22]=1;

}

x=nowinfo-start_x;/*初始化方块位置*/

y=nowinfo-start_y;

nextinfo=get_diamond ();/*得到下一个方块*/

showsubwin(nextinfo);

for (;

{

key=bioskey(0);/*得到键盘扫描码*/

if (key)

{

switch(key)

{

case DOWN:{

show_down ();

break;

}

case UP:{

show_next ();

break;

}

case LEFT:{

show_left();

break;

}

case RIGHT:{

show_right();

break;

}

case RESTART:{

install(oldtimer);

setfillstyle(SOLID_FILL,1);

bar(0,465,640,480);

outtextxy(5,469,"Are you sure to restart (Y/N)...");

for (;

{

key=bioskey(0);/*得到键盘扫描码*/

if (key==YES)

{

startset();

setfillstyle(SOLID_FILL,9);

bar(0,465,640,480);

break;

}

if (key==NO)

{

setfillstyle(SOLID_FILL,9);

bar(0,465,640,480);

install(newtimer);

break;

}

}

break;

}

/* case F1:{

print();

break;

}

*/

case ESC:{

install(oldtimer);

setfillstyle(SOLID_FILL,1);

bar(0,465,640,480);

outtextxy(5,469,"Are you sure to exit (Y/N)...");

for (;

{

key=bioskey(0);/*得到键盘扫描码*/

if (key==YES)

{

closegraph();

exit(0);

}

if (key==NO)

{

setfillstyle(SOLID_FILL,9);

bar(0,465,640,480);

install(newtimer);

break;

}

}

}

}

}

}

}

/*****************************************************************************************/

struct diamond *get_diamond()

{

struct diamond *now;

switch (random(7)+1)

{

case 1:{

now=create_I();

return now;

}

case 2:{

now=create_T();

return now;

}

case 3:{

now=create_L();

return now;

}

case 4:{

now=create_J();

return now;

}

case 5:{

now=create_Z();

return now;

}

case 6:{

now=create_N();

return now;

}

case 7:{

now=create_H();

return now;

}

}

}

/*****************************************************************************************/

void show_interface (int fill_mode)

{

int i;

setbkcolor (9);

setcolor (color);

setfillstyle (SOLID_FILL,backcolor);

bar (100,60,300,420);

bar (360,60,440,140);

rectangle (100,60,300,420);

rectangle (96,56,304,424);

rectangle (360,60,440,140);

rectangle (356,56,444,144);

setfillstyle (fill_mode,14);

floodfill (97,57,color);

floodfill (397,57,color);

setcolor(1);

rectangle(96,56,304,424);

setcolor (color);

for (i=80;i=400;i+=20)

{

line (100,i,300,i);

}

for (i=120;i=280;i+=20)

{

line (i,60,i,420);

}

for (i=80;i=120;i+=20)

{

line (360,i,440,i);

}

for (i=380;i=420;i+=20)

{

line (i,60,i,140);

}

show_intro (360,180);

show_copsign (475,320,40,1);

setfillstyle(SOLID_FILL,1);

setcolor(14);

rectangle(420,405,534,417);

floodfill (421,406,14);

outtextxy(422,408,"HI-TECH WEALTH");

}

/*****************************************************************************************/

void show_copsign (int topx,int topy,int size,int color)

{

int halfsize,qtrsize;

int xadd,xdel,yadd1,yadd2;

halfsize=0.5*size;

qtrsize=0.25*size;

xadd=topx+size;

xdel=topx-size;

yadd1=topy+size;

yadd2=topy+2*size;

setcolor(color);

line (topx,topy,xdel,yadd1);

line (xdel,yadd1,topx,yadd2);

line (topx,yadd2,xadd,yadd1);

line (xadd,yadd1,topx,topy);

rectangle (topx-halfsize,topy+halfsize,topx+halfsize,yadd1+halfsize);

setfillstyle (SOLID_FILL,color);

floodfill (topx,topy+1,color);

floodfill (xdel+1,yadd1,color);

floodfill (topx,yadd2-1,color);

floodfill (xadd-1,yadd1,color);

rectangle (topx-halfsize,yadd1-qtrsize,topx-0.75*halfsize,yadd1+qtrsize);

floodfill (topx-halfsize+1,yadd1-qtrsize+1,color);

rectangle (topx-qtrsize,yadd1-halfsize,topx+qtrsize,yadd1-0.25*halfsize);

floodfill (topx-qtrsize+1,yadd1-halfsize+1,color);

rectangle (topx+0.75*halfsize,yadd1-qtrsize,topx+halfsize,yadd1+qtrsize);

floodfill (topx+0.75*halfsize+1,yadd1-qtrsize+1,color);

rectangle (topx-qtrsize,yadd1+0.25*halfsize,topx+qtrsize,yadd2-halfsize);

floodfill (topx-qtrsize+1,yadd1+0.25*halfsize+1,color);

setcolor(14);

line (topx,topy-1,xdel-1,yadd1);

line (xdel-1,yadd1,topx,yadd2+1);

line (topx,yadd2+1,xadd+1,yadd1);

line (xadd+1,yadd1,topx,topy-1);

setfillstyle (SOLID_FILL,14);

floodfill (topx,yadd1,color);

}

/*****************************************************************************************/

void show_intro (int xs,int ys)

{

char stemp[20];

setcolor (15);

rectangle(xs-3,ys-3,xs+239,ys+115);

line (xs-3,ys+26,xs+239,ys+26);

line (xs-3,ys+72,xs+239,ys+72);

outtextxy(xs,ys,"Level:");

outtextxy(xs,ys+15,"Score:");

sprintf(stemp," -Roll -Downwards");

stemp[0]=24;

stemp[7]=25;

outtextxy(xs,ys+30,"Help:");

setcolor(14);

outtextxy(xs+40,ys+30,stemp);

outtextxy(xs+40,ys+45,"-Turn Left -Turn Right");

outtextxy(xs+40,ys+60,"Esc-Exit R-Restart");

outtextxy(xs,ys+75,"Russia Diamonds Ver 1.0");

outtextxy(xs,ys+90,"CopyRight By ChangYong.01-11-1");

outtextxy(xs,ys+105,"Email:forever_chang@163点抗 ");

}

/*****************************************************************************************/

struct diamond *create_I()

{

struct diamond *info;

struct diamond *first;

first=(struct diamond *)malloc(sizeof(struct diamond));

info=(struct diamond *)malloc(sizeof(struct diamond));

first-next=info;

info-next=first;

first-x[0]=0;

first-y[0]=0;

first-x[1]=-1;

first-y[1]=0;

first-x[2]=1;

first-y[2]=0;

first-x[3]=2;

first-y[3]=0;

first-start_x=5;

first-start_y=3;

first-color=2;

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=-1;

info-x[2]=0;

info-y[2]=1;

info-x[3]=0;

info-y[3]=2;

info-start_x=5;

info-start_y=1;

info-color=2;

if(random(2)==0) {return first;}

else {return first-next;}

}

/*****************************************************************************************/

struct diamond *create_T()

{

struct diamond *info;

struct diamond *first;

struct diamond *last;

int i;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=1;

info-y[3]=0;

info-start_x=5;

info-start_y=3;

info-color=4;

first=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=1;

info-y[3]=0;

info-start_x=5;

info-start_y=2;

info-color=4;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=1;

info-y[2]=0;

info-x[3]=0;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=4;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=-1;

info-y[3]=0;

info-start_x=5;

info-start_y=2;

info-color=4;

last-next=info;

last=info;

last-next=first;

for (i=0;i=random(4);i++)

{

first=first-next;

}

return first;

}

/*****************************************************************************************/

struct diamond *create_L()

{

struct diamond *info;

struct diamond *first;

struct diamond *last;

int i;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=1;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=5;

first=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=1;

info-y[2]=0;

info-x[3]=-1;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=5;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=-1;

info-y[3]=-1;

info-start_x=5;

info-start_y=2;

info-color=5;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=1;

info-y[2]=0;

info-x[3]=1;

info-y[3]=-1;

info-start_x=5;

info-start_y=2;

info-color=5;

last-next=info;

last=info;

last-next=first;

for (i=0;i=random(4);i++)

{

first=first-next;

}

return first;

}

/*****************************************************************************************/

struct diamond *create_J()

{

struct diamond *info;

struct diamond *first;

struct diamond *last;

int i;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=-1;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=6;

first=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=1;

info-y[2]=0;

info-x[3]=-1;

info-y[3]=-1;

info-start_x=5;

info-start_y=2;

info-color=6;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=0;

info-y[2]=-1;

info-x[3]=1;

info-y[3]=-1;

info-start_x=5;

info-start_y=2;

info-color=6;

last-next=info;

last=info;

info=(struct diamond *)malloc(sizeof(struct diamond));

info-x[0]=0;

info-y[0]=0;

info-x[1]=-1;

info-y[1]=0;

info-x[2]=1;

info-y[2]=0;

info-x[3]=1;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=6;

last-next=info;

last=info;

last-next=first;

for (i=0;i=random(4);i++)

{

first=first-next;

}

return first;

}

/*****************************************************************************************/

struct diamond *create_Z()

{

struct diamond *info;

struct diamond *first;

first=(struct diamond *)malloc(sizeof(struct diamond));

info=(struct diamond *)malloc(sizeof(struct diamond));

first-next=info;

info-next=first;

first-x[0]=0;

first-y[0]=0;

first-x[1]=-1;

first-y[1]=0;

first-x[2]=0;

first-y[2]=1;

first-x[3]=1;

first-y[3]=1;

first-start_x=5;

first-start_y=2;

first-color=9;

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=1;

info-x[2]=1;

info-y[2]=0;

info-x[3]=1;

info-y[3]=-1;

info-start_x=5;

info-start_y=2;

info-color=9;

if(random(2)==0) {return first;}

else {return first-next;}

}

/*****************************************************************************************/

struct diamond *create_N()

{

struct diamond *info;

struct diamond *first;

first=(struct diamond *)malloc(sizeof(struct diamond));

info=(struct diamond *)malloc(sizeof(struct diamond));

first-next=info;

info-next=first;

first-x[0]=0;

first-y[0]=0;

first-x[1]=0;

first-y[1]=1;

first-x[2]=-1;

first-y[2]=1;

first-x[3]=1;

first-y[3]=0;

first-start_x=5;

first-start_y=2;

first-color=14;

info-x[0]=0;

info-y[0]=0;

info-x[1]=0;

info-y[1]=-1;

info-x[2]=1;

info-y[2]=0;

info-x[3]=1;

info-y[3]=1;

info-start_x=5;

info-start_y=2;

info-color=14;

if(random(2)==0) {return first;}

else {return first-next;}

}

/*****************************************************************************************/

struct diamond *create_H()

{

struct diamond *first;

first=(struct diamond *)malloc(sizeof(struct diamond));

first-next=first;

first-x[0]=0;

first-y[0]=0;

first-x[1]=0;

first-y[1]=1;

first-x[2]=1;

first-y[2]=0;

first-x[3]=1;

first-y[3]=1;

first-start_x=5;

first-start_y=2;

first-color=1;

return first;

}

/*****************************************************************************************/

void show_next ()

{

int nowx;/*记录当前每个格子的位置*/

int nowy;

int i;/*计数器*/

int j;

int haveit;/*当前格子是否已经显示*/

struct diamond *next;/*当前方块的翻转后的下一个状态*/

next=nowinfo-next;

if (next==NULL) {gotoxy(1,1);printf("null");}

for (i=0;i=3;i++)/*判断是否能够翻转,若不能,就直接退出该函数*/

{

if (grid[x+next-x[i]][y+next-y[i]]==1)

{

return;

}

}

setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/

for (i=0;i=3;i++)

{

haveit=0;

for (j=0;j=3;j++)

{

if (next-x[j]==nowinfo-x[i]next-y[j]==nowinfo-y[i]) {haveit=1;break;}

}

if (haveit==0) /*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/

{

grid[x+nowinfo-x[i]][y+nowinfo-y[i]]=0;

if (y+nowinfo-y[i]=4)/*判断该格子是否到了可以显示的区域*/

floodfill(80+(nowinfo-x[i]+x)*20+1,-20+(nowinfo-y[i]+y)*20+1,color);

}

}

nowinfo=next;

nowx=x;

nowy=y;

setfillstyle(SOLID_FILL,nowinfo-color);/*设置填冲色为方块的颜色*/

for (i=0;i=3;i++)

{

if (grid[x+nowinfo-x[i]][y+nowinfo-y[i]]!=2)/*如果该格子还没有被显示*/

{

nowx=x+nowinfo-x[i];

nowy=y+nowinfo-y[i];

if (y+nowinfo-y[i]=4)

floodfill(80+nowx*20+1,-20+nowy*20+1,color);

grid[nowx][nowy]=2; /*设置该格子当前有活动方块*/

}

}

return;

}

/*****************************************************************************************/

void show_left ()

{

int i;/*计数器*/

int j;

int haveit;/*当前格子是否已经显示*/

int nowx;/*记录当前每个格子的位置*/

int nowy;

for (i=0;i=3;i++)/*判断是否可以向左移动*/

{

if (grid[x-1+nowinfo-x[i]][y+nowinfo-y[i]]==1)

{

return;

}

}

setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/

for (i=0;i=3;i++)

{

haveit=0;

for (j=0;j=3;j++)

{

if (nowinfo-x[i]==nowinfo-x[j]-1nowinfo-y[i]==nowinfo-y[j]) {haveit=1;break;}

}

if (haveit==0) /*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/

{

grid[x+nowinfo-x[i]][y+nowinfo-y[i]]=0;

if (y+nowinfo-y[i]=4)/*判断该格子是否到了可以显示的区域*/

floodfill(80+(nowinfo-x[i]+x)*20+1,-20+(nowinfo-y[i]+y)*20+1,color);

}

}

setfillstyle(SOLID_FILL,nowinfo-color);/*设置填冲色为方块的颜色*/

for (i=0;i=3;i++)

{

nowx=x+nowinfo-x[i]-1;

nowy=y+nowinfo-y[i];

if (grid[nowx][nowy]!=2)/*如果该格子还没有被显示*/

{

if (nowy=4)

floodfill(80+nowx*20+1,-20+nowy*20+1,color);

grid[nowx][nowy]=2;

}

}

x=x-1;

return;

}

/*****************************************************************************************/

void show_right ()

{

int i;/*计数器*/

int j;

int haveit;/*当前格子是否已经显示*/

int nowx;/*记录当前每个格子的位置*/

int nowy;

for (i=0;i=3;i++)

{

if (grid[x+1+nowinfo-x[i]][y+nowinfo-y[i]]==1)

{

return;/*判断是否可以向右移动*/

}

}

setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/

for (i=0;i=3;i++)

{

haveit=0;

for (j=0;j=3;j++)

{

if (nowinfo-x[i]==nowinfo-x[j]+1nowinfo-y[i]==nowinfo-y[j]) {haveit=1;break;}

}

if (haveit==0)/*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/

{

grid[x+nowinfo-x[i]][y+nowinfo-y[i]]=0;

if (y+nowinfo-y[i]=4)/*判断该格子是否到了可以显示的区域*/

floodfill(80+(nowinfo-x[i]+x)*20+1,-20+(nowinfo-y[i]+y)*20+1,color);

}

}

setfillstyle(SOLID_FILL,nowinfo-color);/*设置填冲色为方块的颜色*/

for (i=0;i=3;i++)

{

nowx=x+nowinfo-x[i]+1;

nowy=y+nowinfo-y[i];

if (grid[nowx][nowy]!=2)

{

if (nowy=4)/*判断该格子是否到了可以显示的区域*/

floodfill(80+nowx*20+1,-20+nowy*20+1,color);

grid[nowx][nowy]=2;

}

}

x=x+1;

return;

}

/*****************************************************************************************/

void show_down ()

{

int i;/*计数器*/

int j;

int haveit;/*当前格子是否已经显示*/

int nowx;/*记录当前每个格子的位置*/

int nowy;

int key;

for (i=0;i=3;i++)

{

if (grid[x+nowinfo-x[i]][y+nowinfo-y[i]+1]==1)/*判断方块是否能够下落*/

{

for (j=0;j=3;j++)

{

grid[x+nowinfo-x[j]][y+nowinfo-y[j]]=1;

if (y+nowinfo-y[j]=3)

{/*判断游戏是否已经玩完*/

install(oldtimer);

setfillstyle(SOLID_FILL,1);

bar(0,465,640,480);

outtextxy(5,469,"Do you want to restart (Y/N)...");

for (;

{

key=bioskey(0);

if (key==YES)

{

startset();

setfillstyle(SOLID_FILL,9);

bar(0,465,640,480);

return;

}

if (key==NO)

{

closegraph();

exit (0);}

}

}

}

delinfo(nowinfo);

scandel();/*扫描,删除*/

delay(2500);

while(bioskey(1)) bioskey(0);/*清除键盘缓冲区*/

/* clrkey();*/

nowinfo=nextinfo;/*得到新的方块*/

nextinfo=get_diamond();/*得到下一个方块*/

showsubwin(nextinfo);

x=nowinfo-start_x;/*重新设置方块位置*/

y=nowinfo-start_y;

return;

}

}

setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/

for (i=0;i=3;i++)

{

haveit=0;

for (j=0;j=3;j++)

{

if (nowinfo-x[i]==nowinfo-x[j]nowinfo-y[i]==nowinfo-y[j]+1) {haveit=1;break;}

}

......

详见:

c的signal函数

signal()是一种系统调用,用于通知运行时系统,当某种特定的“软件中断”发生时调用特定的程序。它的真正的名字应该是“Call_that_routine_when_this_interrupt_Comes_in(当该中断发生时调用那个程序)”,调用signal()函数,并通过参数传递告诉它终端类型以及用于处理中断的程序。ANSIC 标准中,signal()函数的声明如下:

void (*signal (int sig ,void (*func)(int))) (int) ;

函数返回与给定sig信号相关联的func的以前值

这个函数的模样很恐怖,它的意思是:

signal是一个函数,他返回一个函数指针,后者所指向的函数接受一个int参数并返回void 。

可以用typedef进行简化:

typedef void (*ptr_to_func) (int) ;

/*表示ptr_to_func是一个函数指针,该函数接受一个Int参数,并返回void */

ptr_to_func signal (int , ptr_to_func );

实例:捕捉段错误信号的信号处理函数:

#include signal.h

#include stdio.h

void handler(int s)

{

if (s == SIGBUS) printf(" now got a bus error signal\n");

if (s == SIGSEGV) printf(" now got a segmentation violation signal\n");

if (s == SIGILL) printf(" now got an illegal instruction signal\n");

exit(1);

}

main ()

{

int *p=NULL;

signal(SIGBUS, handler);

signal(SIGSEGV, handler);

signal(SIGILL, handler);

*p=0;

}

由于p是一个空指针,对空指针赋值会引发一个段错误,于是程序捕捉到SIGSEGV信号,并打印消息”now got a segmentation violation signal“后退出。

打开windows下的signal.h头文件,发现在这个头文件里定义了很多符号,但是没有找到SIGBUS,所以上面的代码在windows下编译不通过:

error C2065: 'SIGBUS' : undeclared identifier

把涉及到SIGBUS的相关部分删去即可。

使用setjmp、longjmp从信号中恢复:

#include setjmp.h

#include signal.h

#include stdio.h

jmp_buf buf;

void handler(int s)

{

if (s == SIGINT) printf(" now got a SIGINT signal\n");

longjmp(buf, 1);

/*NOTREACHED*/

}

main ()

{

signal(SIGINT, handler);

if (setjmp(buf)) {

printf("back in main\n");

return 0;

}else

printf("first time through\n");

loop:

/* spin here, waiting for ctrl-c */

goto loop;

}

单片机C语言中为何要定义 如: .. void delay() { unsigned char i;for(i=0;i

你好

这个问题回答透彻比较繁琐,楼上的两位说的都不错,我这样解释,不知你能明白吗。

1.void delay() 是一个子程序的名字,是软件

2.{ for(i=0;i10;i++) } 是这个子程序的内容,作用 操作硬件的软件,操作的硬件i(名字)。

3. unsigned char i;定义该子程序操作的硬件(内存中的一个寄存器,数值范围0-255),无符号8位变量,想这个你应该知道。

你要是学习过汇编,这个就不难理解,下面给出这个程序的汇编,你对比一下,示例说明,不一定能用,哈哈,好久没玩过汇编,都快忘记了。就是说明c语言与汇编的异同。

equ i 0030H //定义内存地址30(16进制)的寄存器为i,在汇编中,也可不定义,直接对30H进行操作。汇编中数字不带H,为10进制,后缀带H为16进制。

delay: //定义子程序,方便调用,也可为其他名字,如loop1等。

L1:inc i //寄存器i(30H)加一。L1为行号。

cjne i 10 L1 //比较不等则转,这里回到上一行。相等则往下执行。

ret //子程序返回

对于你对楼上两位的追问,c语言就是一个平台(或者说环境),就像一个游戏,你想玩好就必须遵从游戏规则,或者说他就是这样用才行,否则免谈。

再说一下unsigned char i;就是c语言按自身的规定,或随机给你定义一个可操作的51内存变量,不一定是30H,可能是30H-07FFH中的任何一个,想搞明白,的详细研究c了。不过作为c51的应用,会用就行,就行office的办公软件,会用就好,没必要去追究他是怎么运行的。


网站题目:spin函数c语言 spc函数
分享URL:http://bjjierui.cn/article/ddgieei.html

其他资讯