当前位置:首页 » 编程语言 » c语言点灯游戏
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言点灯游戏

发布时间: 2022-11-29 23:24:10

c语言有一个8层灯塔,每层所点灯数为上一层两倍,共有765盏灯,求塔底灯数 (运用函数实现如递归函数)

#include <stdio.h>

size_t foo(size_t n)
{
return (n==1) ? 1 : f(n-1) + (1<<(n-1));
}

void main(void)
{
printf("%d\n", 765-foo(7));
}

② 如何使用C语言编写简单小游戏

C语言是计算机专业都要学习的一门基础学科。一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦 ,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣。

例如2048这款游戏:

方法/步骤:

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<conio.h>

#include<windows.h>

#define SIZE 4

static int score=0;

void putn(int n[][SIZE]);

void getn(int n[][SIZE]);

int isempty(int n[][SIZE]);

int isfull(int n[][SIZE]);

void math(int n[][SIZE],char c);

void tow(int n[][SIZE]);

void toa(int n[][SIZE]);

void tos(int n[][SIZE]);

void tod(int n[][SIZE]);

  • //主函数

    int main()

    {

    int i,j;

    int n[SIZE][SIZE];

    char c=' ';

    for(i=0;i<SIZE;i++)

    {

    for(j=0;j<SIZE;j++)

    {

    n[i][j]=0;

    }

    }

    printf( "*********************** "

    " 2048(%dX%d) "

    " control:W/A/S/D "

    "press any key to begin "

    "*********************** ",SIZE,SIZE);

    getch();

    system("cls");

    //n[0][1]=2048;

    //n[0][3]=2048;

    while(1)

    {

    if(isempty(n))

    getn(n);

    putn(n);

    if(!isempty(n)&&isfull(n))

    break;

    sleep(200);

    c=getch();

    while(c!='w'&&c!='a'&&c!='s'&&c!='d')

    c=getch();

    math(n,c);

    system("cls");

    }

    printf(" Game Over! ",score);

    return 0;

    }

  • //函数

    void putn(int n[][SIZE])

    {

    int i,j;

    for(i=0;i<SIZE;i++)

    {

    for(j=0;j<SIZE;j++)

    printf("| ");

    printf("| ");

    for(j=0;j<SIZE;j++)

    {

    if(n[i][j]==0)

    printf("| ");

    else

    printf("|%4d ",n[i][j]);

    }

    printf("| ");

    for(j=0;j<SIZE;j++)

    printf("|_____");

    printf("| ");

    }

    printf("score: %d",score);

    }

    void getn(int n[][SIZE])

    {

    int a,b;

    a=rand()%SIZE;

    b=rand()%SIZE;

    while(n[a][b]!=0)

    {

    a=rand()%SIZE;

    b=rand()%SIZE;

    }

    n[a][b]=2;

    }

    int isempty(int n[][SIZE])

    {

    int i,j,count=0;

    for(i=0;i<SIZE;i++)

    for(j=0;j<SIZE;j++)

    if(n[i][j]==0)

    count++;

    return count;

    }

    int isfull(int n[][SIZE])

    {

    int i,j,count=0;

    for(i=0;i<SIZE;i++)

    {

    for(j=1;j<SIZE-1;j++)

    {

    if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])

    count++;

    }

    }

    for(j=0;j<SIZE;j++)

    {

    for(i=1;i<SIZE-1;i++)

    {

    if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])

    count++;

    }

    }

    return count>0?0:1;

    }

    void math(int n[][SIZE],char c)

    {

    switch(c)

    {

    case 'w':tow(n);break;

    case 'a':toa(n);break;

    case 's':tos(n);break;

    case 'd':tod(n);break;

    default :;

    }

    }

    void tow(int n[][SIZE])

    {

    int i,j,a;

    int m[SIZE];

    for(a=0;a<SIZE;a++)

    m[a]=0;

    for(j=0;j<SIZE;j++)

    {

    for(a=0;a<SIZE;a++)

    {

    for(i=0;i<SIZE-1;i++)

    {

    if(n[i][j]==0)

    {

    n[i][j]=n[i+1][j];

    n[i+1][j]=0;

    }

    }

    }

    }

    for(j=0;j<SIZE;j++)

    {

    for(a=0,i=0;i<SIZE;i++)

    {

    if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)

    {

    m[a++]=n[i][j];

    n[i][j]=0;

    }

    else if(n[i][j]==n[i+1][j])

    {

    m[a++]=n[i][j]+n[i+1][j];

    score+=m[a-1];

    n[i][j]=0,n[i+1][j]=0;

    }

    }

    for(i=0;i<SIZE;i++)

    {

    n[i][j]=m[i];

    m[i]=0;

    }

    }

    }

    void toa(int n[][SIZE])

    {

    int i,j,a;

    int m[SIZE];

    for(a=0;a<SIZE;a++)

    m[a]=0;

    for(i=0;i<SIZE;i++)

    {

    for(a=0;a<SIZE;a++)

    {

    for(j=0;j<SIZE-1;j++)

    {

    if(n[i][j]==0)

    {

    n[i][j]=n[i][j+1];

    n[i][j+1]=0;

    }

    }

    }

    }

    for(i=0;i<SIZE;i++)

    {

    for(a=0,j=0;j<SIZE;j++)

    {

    if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)

    {

    m[a++]=n[i][j];

    n[i][j]=0;

    }

    else if(n[i][j]==n[i][j+1])

    {

    m[a++]=n[i][j]+n[i][j+1];

    score+=m[a-1];

    n[i][j]=0,n[i][j+1]=0;

    }

    }

    for(j=0;j<SIZE;j++)

    {

    n[i][j]=m[j];

    m[j]=0;

    }

    }

    }

    void tos(int n[][SIZE])

    {

    int i,j,a;

    int m[SIZE];

    for(a=0;a<SIZE;a++)

    m[a]=0;

    for(j=SIZE-1;j>=0;j--)

    {

    for(a=SIZE-1;a>=0;a--)

    {

    for(i=SIZE-1;i>0;i--)

    {

    if(n[i][j]==0)

    {

    n[i][j]=n[i-1][j];

    n[i-1][j]=0;

    }

    }

    }

    }

    for(j=SIZE-1;j>=0;j--)

    {

    for(a=SIZE-1,i=SIZE-1;i>=0;i--)

    {

    if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)

    {

    m[a--]=n[i][j];

    n[i][j]=0;

    }

    else if(n[i][j]==n[i-1][j])

    {

    m[a--]=n[i][j]+n[i-1][j];

    score+=m[a+1];

    n[i][j]=0,n[i-1][j]=0;

    }

    }

    for(i=SIZE-1;i>=0;i--)

    {

    n[i][j]=m[i];

    m[i]=0;

    }

    }

    }

    void tod(int n[][SIZE])

    {

    int i,j,a;

    int m[SIZE];

    for(a=0;a<SIZE;a++)

    m[a]=0;

    for(i=SIZE-1;i>=0;i--)

    {

    for(a=SIZE-1;a>=0;a--)

    {

    for(j=SIZE-1;j>0;j--)

    {

    if(n[i][j]==0)

    {

    n[i][j]=n[i][j-1];

    n[i][j-1]=0;

    }

    }

    }

    }

    for(i=SIZE-1;i>=0;i--)

    {

    for(a=SIZE-1,j=SIZE-1;j>=0;j--)

    {

    if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)

    {

    m[a--]=n[i][j];

    n[i][j]=0;

    }

    else if(n[i][j]==n[i][j-1])

    {

    m[a--]=n[i][j]+n[i][j-1];

    score+=m[a+1];

    n[i][j]=0,n[i][j-1]=0;

    }

    }

    for(j=SIZE-1;j>=0;j--)

    {

    n[i][j]=m[j];

    m[j]=0;

    }

    }

    }

③ C语言程序设计24点游戏,能算出24的运算法则的代码,很急

#include<iostream>
#include<math.h>
using namespace std;
const double MIN=1E-6;
void Print(int *Rank,double *FourNum)
{
for(int i=0;i<4;i++)
cout<<FourNum[Rank[i]]<<" ";
cout<<endl;
}
void Calculate_24(int *Rank,int *FourNum,char *Oper,int i,int j,int k,bool &def)
{
double res=0;
switch(i)
{
case 0:
res=FourNum[Rank[0]]+FourNum[Rank[1]];
break;
case 1:
res=FourNum[Rank[0]]-FourNum[Rank[1]];
break;
case 2:
res=FourNum[Rank[0]]*FourNum[Rank[1]];
break;
case 3:
res=FourNum[Rank[0]]/FourNum[Rank[1]];
break;
}
switch(j)
{
case 0:
res=res+FourNum[Rank[2]];
break;
case 1:
res=res-FourNum[Rank[2]];
break;
case 2:
res=res*FourNum[Rank[2]];
break;
case 3:
res=res/FourNum[Rank[2]];
break;
}
switch(k)
{
case 0:
res=res+FourNum[Rank[3]];
break;
case 1:
res=res-FourNum[Rank[3]];
break;
case 2:
res=res*FourNum[Rank[3]];
break;
case 3:
res=res/FourNum[Rank[3]];
break;
}
if(fabs(res-24)>MIN)
return;
else
{
def=true;
for(int num=1;num<=7;num++)
{
switch(num)
{
case 1:
cout<<FourNum[Rank[0]];
break;
case 3:
cout<<FourNum[Rank[1]];
break;
case 5:
cout<<FourNum[Rank[2]];
break;
case 7:
cout<<FourNum[Rank[3]];
break;
case 2:
cout<<Oper[i];
break;
case 4:
cout<<Oper[j];
break;
case 6:
cout<<Oper[k];
break;
}
}
cout<<endl;
}
}
void SearchTree(int Depth,int *Rank,int *FourNum,char *Oper,bool &def)
{
int i,j,k;
if(Depth==4)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
Calculate_24(Rank,FourNum,Oper,i,j,k,def);
}
else
{
for(i=0;i<4;i++)
{
int Remember=0;
for(j=0;j<Depth;j++)
{
if(Rank[j]==i)
Remember=1;
}
if(Remember)
continue;
Rank[Depth]=i;
SearchTree(Depth+1,Rank,FourNum,Oper,def);
}
}
}
int main()
{
int a[4],b[4],time;
char c[4]={'+','-','*','/'};
bool def=false;
cin>>time;
while(time--)
{
for(int i=0;i<4;i++)//输入测试数据
cin>>a[i];
cout<<"所有可能的结果:"<<endl;
SearchTree(0,b,a,c,def);
if(def==false)
cout<<"No"<<endl;
}
return 0;
}

④ c语言怎么做应用程序或游戏啊

C语言做游戏

点灯游戏(把灯都打开变黄):
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int j[8][6][6]={
0,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,1,0,1,0,1,1,0,1,1,
0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,0,0,1,0,1,0,0,1,0,1,0,1,0,1,0,1,0,1,
0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,0,1,1,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,
0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,0
},x,y,s,i,h=0,r=0,n;
pr(int u)
{ int g,f;
for (g=1;g<=5;g++)
for (f=1;f<=5;f++)
if (j[u][g][f]==1)
{gotoxy(f,g); textcolor(1); putch('o');}
else {gotoxy(f,g); textcolor(14); putch('o');}
}
hkg(int t,int y)
{ if (j[h][t][y]==1) {j[h][t][y]=0;}
else {j[h][t][y]=1;}
}
kg()
{ int e,r;
hkg(y,x); hkg(y-1,x); hkg(y,x+1); hkg(y+1,x); hkg(y,x-1); pr(h);
}
inkey()
{ char o;
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==13) {
kg(); r++;
gotoxy(7,5); printf("Bu Shu: %d",r);
if (r>=299) {over(); getch(); exit(0);}
}
if (o==0) {
o=getch();
if (o==72&&y>1) {y--;}
if (o==77&&x<5) {x++;}
if (o==80&&y<5) {y++;}
if (o==75&&x>1) {x--;}
}
}
}
test()
{ int o,p,k=0;
for (o=1;o<=5;o++)
for (p=1;p<=5;p++)
if (j[h][o][p]==1) {k=1;}
if (k==0)
{gotoxy(7,3); printf("Ni Ying Le! An Jian Ji Xu."); getch(); h++; pr(h);
gotoxy(7,3); printf(" ");
textcolor(10); gotoxy(3,7); printf("Di %d Guan",h+1);
}
}
over()
{ int v,b;
for (v=1;v<=24;v++)
for (b=1;b<=40;b++)
{gotoxy(b,v); textcolor(v); putch(219); delay(1000);}
for (v=1;v<=40;v++)
for (b=1;b<=24;b++)
{gotoxy(v,b); textcolor(v); putch(219); delay(1000);}
gotoxy(20,12); printf("Ni Shu Le!");
}
zai()
{ int g,h,y; gotoxy(15,10); printf("Zai Ru......");
gotoxy(15,15); printf("Zai Ru : Dong Hua ");
for (g=10;g<=15;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
gotoxy(15,15); printf("Zai Ru : Di Tu ");
for (g=16;g<=25;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
gotoxy(15,15); printf("Zai Ru : Tu Xing He Zi Ti");
for (g=26;g<=30;g++)
{gotoxy(g,12); textcolor(2); putch(219);
for (h=1;h<=5;h++) delay(g*10000000);
gotoxy(19,17); printf("%d%",(g-10)*5);}
for (y=1;y<=5;y++) delay(10000);
}
main()
{ textmode(C40); randomize(); clrscr(); zai(); clrscr(); textcolor(10);
gotoxy(7,5); printf("Bu Shu: %d",0);
gotoxy(3,7); printf("Di %d Guan",h+1);
for (i=1;i<=n;i++) {
do {x=rand()%5+1; y=rand()%5+1;} while (j[h][y][x]==1);
j[h][y][x]=1;
} pr(h); x=1; y=1;
while (1)
{ inkey(); gotoxy(x,y); test();
}
getch();
}

作者: 54qiuwenda

2008-7-26 12:17 回复此发言

--------------------------------------------------------------------------------

3 回复:C语言做游戏
走迷宫(用工具走到*):
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
main()
{ int map[20][20]={1,1,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,7,
1,0,1,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,7,
1,0,1,1,1,1,3,3,1,1,0,1,0,0,1,0,0,1,0,7,
1,0,0,0,1,2,4,2,0,1,0,1,1,0,1,0,0,1,1,7,
1,0,0,0,1,2,1,2,1,1,0,0,1,0,1,2,1,1,2,7,
1,0,1,1,1,0,1,2,2,2,2,1,1,0,2,2,1,2,2,7,
1,0,1,0,0,0,1,2,1,2,2,1,1,0,1,2,2,2,2,7,
1,0,1,1,1,0,1,2,1,2,2,1,0,0,1,1,2,1,2,7,
1,0,0,0,1,0,1,2,1,2,2,1,0,1,1,0,2,1,2,7,
1,0,1,0,0,0,1,2,1,1,0,1,0,0,0,0,2,1,2,7,
1,0,1,1,1,1,1,2,2,3,0,1,1,1,1,2,2,1,2,7,
1,0,0,0,2,2,2,2,8,1,0,0,1,2,2,2,1,1,2,7,
1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,2,7,
1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,2,7,
1,2,1,1,1,1,0,0,1,1,1,1,2,2,1,2,2,2,2,7,
1,2,1,6,0,1,0,0,0,0,0,2,2,2,1,2,1,1,1,7,
1,2,1,1,5,1,1,1,1,1,1,2,2,2,1,2,1,2,0,7,
1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,0,9,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
,x,y,q,w,
z[5][2]={{-1,0},{0,1},{1,0},{0,-1},{0,0}},l=4,s=0,d=0,j,i,u=0;
char o;
textmode(C40); randomize(); q=2; w=2; clrscr();
while (1) {
gotoxy(22,3); printf("D: ChuanQiang");
gotoxy(22,4); printf("(CrossWall)");
gotoxy(22,6); putch(219); printf(": QiangBi(Wall)");
gotoxy(22,8); printf("=: Shui(Water)");
gotoxy(22,10); printf("F: ShuiShang");
gotoxy(22,11); printf("FeiXing(FlyOn");
gotoxy(22,12); printf("Water)");
gotoxy(22,14); printf("/: Men(Door)");
gotoxy(22,16); printf("*: BaoZang");
gotoxy(22,17); printf("(Treasure)");
gotoxy(22,19); printf("Esc:TuiChu(Exit)");
for (y=0;y<20;y++) {
for (x=0;x<20;x++) {
if (map[y][x]==1||map[y][x]==7) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==9) {gotoxy(x+1,y+1); putch('*');}
if (map[y][x]==3) {gotoxy(x+1,y+1); putch(219);}
if (map[y][x]==2) {gotoxy(x+1,y+1); putch('=');}
if (map[y][x]==5) {gotoxy(x+1,y+1); putch('/');}
if (map[y][x]==8) {gotoxy(x+1,y+1); putch('8');}
if (map[y][x]==4) {gotoxy(x+1,y+1); putch('F');}
if (map[y][x]==6) {gotoxy(x+1,y+1); putch('D');}
}
}
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==72) {l=0;}
if (o==77) {l=1;}
if (o==80) {l=2;}
if (o==75) {l=3;}
j=map[q-1+z[l][0]][w-1+z[l][1]];
if (j!=0&&j!=4&&j!=8&&j!=3&&j!=s*2&&j!=9&&j!=6&&j!=u*1&&j!=7)
{l=4;}
gotoxy(w,q); putch(' ');
gotoxy(22,22); for (i=1;i<=18;i++) putch(' ');
q=q+z[l][0]; w=w+z[l][1]; l=4;
if (j==4) {map[q-1][w-1]=0; s=1; gotoxy(35,10); printf(" /");
gotoxy(35,11); printf(" \\/");}
if (j==8) {map[q-1][w-1]=0; map[17][4]=0; gotoxy(5,18); putch(' ');
gotoxy(35,14); printf(" /"); gotoxy(35,15); printf(" \\/");}
if (j==3) {gotoxy(22,22); printf("AnMen(SecretDoor)");}
if (j==6) {map[q-1][w-1]=0; u=1; gotoxy(4,16); putch(' ');
gotoxy(35,3); printf(" /"); gotoxy(35,4); printf(" \\/");}
} gotoxy(w,q); putch(178);
if (map[q-1][w-1]==9)
{gotoxy(10,23); printf("You won! Very good!"); gotoxy(35,16);
printf(" /"); gotoxy(35,17); printf(" \\/"); break;}
delay(8000);
}
getch();
}

贪吃蛇(上下左右移动吃食物):
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int x=5,y=2,r=1,mx[4]={0,1,0,-1},my[4]={-1,0,1,0},s[40][3],u=5,g=1,
map[40][24]={0},score=0;
boxx(int x1,int y1,int x2,char c)
{ int j;
for (j=x1;j<=x2;j++) {gotoxy(j,y1); putch&;;}
}
boxy(int x1,int y1,int y2,char c)
{ int j;
for (j=y1;j<=y2;j++) {gotoxy(x1,j); putch&;;}
}
inkey()
{ char o;
randomize();
if (kbhit()) {
o=getch();
if (o==27) {exit(0);}
if (o==0) {
o=getch();
if (o==72) r=0;
if (o==77) r=1;
if (o==80) r=2;
if (o==75) r=3;
}
}
x=x+mx[r]; y=y+my[r];
if (x<2||x>39) {x=x-mx[r];}
if (y<2||y>23) {y=y-my[r];}
gotoxy(x,y); putch(178); p(u); if (map[x][y]==1) {score++;
gotoxy(20,1); printf("Score : %d",score); map[x][y]=0;
u++; s[u][1]=s[u-1][1]; s[u][2]=s[u-1][2];
}
gotoxy(s[g][1],s[g][2]); putch(' ');
s[g][1]=x; s[g][2]=y; g=g++;
if (g>u) g=1;
}
p(int h)
{int z; for (z=1;z<=h;z++) {gotoxy(s[z][1],s[z][2]); putch(178);}
}
egg()
{int ex,ey,h;
for (h=1;h<=30;h++){ex=rand()%38+2; ey=rand()%22+2;
map[ex][ey]=1; gotoxy(ex,ey); putch('O');}
}
main()
{ randomize();
textmode(C40); textcolor(rand()%16);
s[1][1]=2; s[1][2]=2; s[2][1]=3; s[2][2]=2; s[3][1]=4;
s[3][2]=2; s[4][1]=5; s[4][2]=2; s[5][1]=6; s[5][2]=2;
boxx(1,1,40,219); boxx(1,24,40,219);
boxy(1,1,24,219); boxy(40,1,24,219); egg();
gotoxy(20,1); printf("Score : %d",score);
while (1) {
gotoxy(x,y); putch(' ');
inkey();
delay(30000); delay(30000);
}
getch();
}

猫捉老鼠(躲开猫吃食物):
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
main()
{ int cx,cy,x,y,xx,yy,fx,fy,n,kb,r=0,p,o,g=0,sc=0; int set[25][41]={0};
int mx[2][5]={{0,1,0,-1,0},{0,2,0,-2,0}},
my[2][5]={{0,0,-1,0,1},{0,0,-2,0,2}};
clrscr(); textmode(C40); randomize(); n=0;
while (n<50) { do{ fx=rand()%38+2; fy=rand()%22+2;} while(set[fy][fx]!=0);
set[fy][fx]=1; gotoxy(fx,fy); putch('.'); n++;} n=3;
while (n) { do{ fx=rand()%38+2;fy=rand()%22+2;} while (set[fy][fx]!=0);
set[fy][fx]=2; gotoxy(fx,fy); putch('o'); n--;}
gotoxy(2,2); cx=2; cy=2; putch('&');
for (x=1;x<=40;x++) {gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);
if (x<=24) {gotoxy(1,x); putch(219); gotoxy(40,x); putch(219);}}
gotoxy(15,1); printf("Score: %d",sc);
gotoxy(39,23); putch('@'); x=39; y=23;
gotoxy(40,8); putch(' '); gotoxy(1,16); putch(' ');
while (1)
{ if (kbhit()) { kb=getch(); if (kb==27) exit(0); kb=getch();
if (x==40&&y==8) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=x-1; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=2; gotoxy(x,y); putch('@');}
}
if (x==1&&y==16) {
if (kb==75)
{gotoxy(x,y); putch(' '); x=39; gotoxy(x,y); putch('@');}
if (kb==77)
{gotoxy(x,y); putch(' '); x=x+1; gotoxy(x,y); putch('@');}
}
if (kb==77&&y==8&&x==39) {gotoxy(x,y); putch(' ');
x=x+1; gotoxy(x,y); putch('@');} else
if (kb==75&&y==16&&x==2) {gotoxy(x,y); putch(' ');
x=x-1; gotoxy(x,y); putch('@');} else
{if (kb==77&&x<39) r=1; else if (kb==72&&y>2) r=2; else
if (kb==75&&x>2) r=3; else if (kb==80&&y<23) r=4; else r=0;
gotoxy(x,y); putch(' '); xx=x+mx[0][r]; yy=y+my[0][r];
if (g==0) {x=x+mx[0][r]; y=y+my[0][r];} else
{x=x+mx[1][r]; y=y+my[1][r]; g=g-1;
gotoxy(3,1); printf("Gao neng:%d",g); putch(219);}
if ((x==39&&kb==77)||(x==38&&kb==77&&g>0)) x=2;
if ((y==2&&kb==72)||(y==3&&kb==72&&g>0)) y=23;
if ((x==2&&kb==75)||(x==3&&kb==75&&g>0)) x=39;
if ((y==23&&kb==80)||(y==22&&kb==80&&g>0)) y=2;
gotoxy(x,y); putch('@'); if (set[y][x]==1||set[yy][xx]==1)
{gotoxy(15,1); printf("Score: %d",sc+1); sc=sc+1; set[y][x]=0;
gotoxy(xx,yy); putch(' '); gotoxy(x,y); putch('@');} else
if (set[y][x]==2||set[yy][xx]==2)
{gotoxy(3,1); printf("Gao neng:%d",g+10); putch(219); g=g+10;
set[y][x]=0; set[yy][xx]=0;}
if (g==0) { gotoxy(3,1); for (n=1;n<=11;n++) putch(219);}}
if (sc==20) break; if (x==cx&&y==cy) break;}
if ((x==40&&y==8)||(x==1&&y==16)) {} else {
gotoxy(cx,cy); if (set[cy][cx]==1) putch('.'); else putch(' ');
if (cx<x) cx=cx+1; if (cx>x) cx=cx-1;
if (cy<y) cy=cy+1; if (cy>y) cy=cy-1;
gotoxy(cx,cy); putch('&'); delay(10000); if (x==cx&&y==cy) break;}
}
if (cx==x&&cy==y) {gotoxy(15,3); printf("The cat win!");} else
if (sc>=20) {gotoxy(15,3); printf("The mouse(you) win!");}
getch();
}

俄罗斯方块(移动方块消行):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int x[8][4]={0,0,0,0,
0,1,2,0,
0,1,-1,0,
0,0,0,0,
0,0,1,2,
0,1,0,1,
0,-1,0,1,
0,1,0,-1};
int y[8][4]={0,0,0,0,
0,0,0,-1,
0,0,0,-1,
0,1,2,3,
0,1,0,0,
0,0,1,1,
0,0,1,1,
0,0,1,1};
int fxj[4]={0,1,0,-1},fyj[4]={-1,0,1,0},k[8]={0,0,0,3,1,3,2,2};
int fx,fy,j=2,map[25][41]={0},mapc[25][41]={0},h,w=0,z,nx[4],ny[4],score;
p(int t,char o,int b)
{ int i;
for (i=0;i<=3;i++)
{textcolor(t); gotoxy(fx+nx[i],fy+ny[i]); putch(o);
map[fy+ny[i]][fx+nx[i]]=b;
}
}
inkey()
{ int l,e,k,nnx[4],nny[4]; char o;
if (kbhit()) {
o=getch();
if (o==27) exit(0);
if (o==0) {
o=getch();
if (o==77)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]+1==41) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]+1]==1) {e=1; break;}
}
if (e==0) {j=1;}
}
if (o==75)
{e=0; for (l=0;l<=3;l++) {
if (fx+nx[l]-1==10) {e=1; break;}
if (map[fy+ny[l]][fx+nx[l]-1]==1) {e=1; break;}
}
if (e==0) {j=3;}
}
if (o==80)
{e=0; for (l=0;l<=3;l++) {
if (map[fy+ny[l]+1][fx+nx[l]]==1) {e=1; break;}
}
if (e==0) {w=10; z=19;}
}
if (o==72)
{ k=0;
for (l=0;l<=3;l++) {
e=nx[l]; nx[l]=ny[l]*(-1); ny[l]=e;
if (map[fy+ny[l]][fx+nx[l]]==1) {k=1;}
if (fx+nx[l]<11||fx+nx[l]>40) {k=1;}
if (fy+ny[l]>24||fy+ny[l]<2) {k=1;}
}
if (k==1)
for (l=0;l<=3;l++) {e=nx[l]; nx[l]=ny[l]; ny[l]=e/(-1);}
}
}
}
}
win()
{gotoxy(20,12); textcolor(3); printf("You won!");
}
over()
{gotoxy(19,12); textcolor(14); printf("Game over!");
}
main()
{ int g,c,u,b,v; textmode(C40); clrscr(); randomize(); h=rand()%7+1;
for (g=1;g<=24;g++) {gotoxy(10,g); putch('|');}
gotoxy(3,3); printf("Next :"); gotoxy(3,15); printf("Score:");
gotoxy(3,16); printf("%d",0); gotoxy(3,11); printf("Esc=");
gotoxy(3,12); printf("TuiChu");
while (1) {
c=0; for (g=0;g<=3;g++) {nx[g]=x[h][g]; ny[g]=y[h][g];}
b=rand()%7+1; fx=25; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);
}
while (c==0) {
inkey();
p(h,219,0); for (g=1;g<=30-z;g++) delay(1000);
w=w-1; if (w==0) z=0;
p(h,32,0); if (j!=2) {fx=fx+fxj[j]; fy=fy+fyj[j];}
j=2; fx=fx+fxj[2]; fy=fy+fyj[2];
for (g=0;g<=3;g++) {
if (fy+ny[g]>24) {c=1; break;}
if (map[fy+ny[g]][fx+nx[g]]==1) {c=1; break;}
}
} fx=fx-fxj[j]; fy=fy-fyj[j];
p(h,219,1); u=0; w=0;
for (g=0;g<=3;g++) {mapc[fy+ny[g]][fx+nx[g]]=h;}
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(32);
}
for (g=1;g<=40;g++) if (map[2][g]==1||map[1][g]) {u=1; break;}
if (u==1)
{ over(); b=rand()%7+1; fx=30; fy=2;
for (g=0;g<=3;g++)
{textcolor(b); gotoxy(3+x[b][g],5+y[b][g]); putch(219);}
getch(); exit(0);
}
h=b; z=1;
do {
u=0; for (g=11;g<=40;g++) {if (map[z][g]==0) {u=1; break;}}
if (u==0) {
for (g=11;g<=40;g++) {gotoxy(g,z); putch(32);}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {map[g+1][v]=map[g][v];}
for (g=z-1;g>=1;g--) for (v=11;v<=40;v++) {mapc[g+1][v]=mapc[g][v];}
for (g=1;g<=24;g++) for (v=11;v<=40;v++)
{gotoxy(v,g); textcolor(mapc[g][v]); putch(219);}
score=score+10; gotoxy(3,16); printf("%d",score);
if (score>=30) {win(); getch(); exit;}
}
z++;
} while (z<25); z=0;
}
}
圈圈叉叉游戏(光标控制):
#include <conio.h>
#include <stdio.h>
int w[3][3]={2,1,2,
1,3,1,
2,1,2},i,j,k,l,kk,ll,x,y,m,prx=3,pry=3,s=0,f,g,e,r,h,m,q;
inkey()
{ char o,c[1];
if (kbhit())
{ o=getch();
if (o==27) {exit(0);}
if (o==13) {
gettext(x,y,x,y,&c);
if (c[0]==32) {gotoxy(x,y); putch('o'); s=1;
w[(y-pry)/2][(x-prx)/2]=-1;
}
}
if (o==0) {
o=getch();
if (o==72&&y>pry) {y--;}
if (o==77&&x<prx+4) {x++;}
if (o==80&&y<pry+4) {y++;}
if (o==75&&x>prx) {x--;}
}
}
}
pr()
{ gotoxy(prx,pry+0); printf(" | | ");
gotoxy(prx,pry+1); printf("-+-+-");
gotoxy(prx,pry+2); printf(" | | ");
gotoxy(prx,pry+3); printf("-+-+-");
gotoxy(prx,pry+4); printf(" | | ");
}
p()
{ for (h=0;h<=2;h++) {
q=0; for (m=0;m<=2;m++) if (w[h][m]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-1) q++;
if (q==3) {gotoxy(10,5); printf("Ni Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[h][m]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (m=0;m<=2;m++) if (w[m][h]==-2) q++;
if (q==3) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
}
q=w[0][0]+w[1][1]+w[2][2];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=w[0][2]+w[1][1]+w[2][0];
if (q==-6) {gotoxy(10,5); printf("Wo Ying Le"); getch(); exit(0);}
q=0; for (h=0;h<=2;h++) for (m=0;m<=2;m++) if (w[h][m]>0) {q=1;}
if (q==0) {gotoxy(10,5); printf("Ping Ju"); getch(); exit(0);}
}
main()
{ textmode(C40); pr(); gotoxy(prx+2,pry+2); putch('x');
w[1][1]=-2; x=prx; y=pry; gotoxy(10,7); printf("Esc Tui Chu");
while (1) {
inkey(); gotoxy(x,y);
if (s==1) {s=0; e=-1; r=-1;
for (i=0;i<=2;i++)
{ l=0; f=-1; g=-1; ll=0; for (j=0;j<=2;j++) {
if (w[i][j]==-1) {l++;}
if (w[i][j]==-2) {ll++;}
if (w[i][j]>0) {f=i; g=j;}
}
if ((l==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (ll==2&&f>=0&&g>=0) {e=f; r=g;}
k=0; f=-1; g=-1; kk=0; for (j=0;j<=2;j++) {
if (w[j][i]==-1) {k++;}
if (w[j][i]==-2) {kk++;}
if (w[j][i]>0) {f=j; g=i;}
}
if ((k==2)&&(e<0||r<0)&&f>=0&&g>=0) {e=f; r=g;}
if (kk==2&&f>=0&&g>=0) {e=f; r=g;}
}
k=0; f=-1; g=-1;
if (w[0][0]==-1) {k++;} if (w[0][0]>0) {f=0; g=0;}
if (w[1][1]==-1) {k++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][2]==-1) {k++;} if (w[2][2]>0) {f=2; g=2;}
if (k==2) {e=f; r=g;}
l=0; f=-1; g=-1;
if (w[0][2]==-1) {l++;} if (w[0][2]>0) {f=0; g=0;}
if (w[1][1]==-1) {l++;} if (w[1][1]>0) {f=1; g=1;}
if (w[2][0]==-1) {l++;} if (w[2][0]>0) {f=2; g=2;}
if (l==2&&f>=0&&g>=0) {e=f; r=g;}
if (e<0||r<0) {
k=0; l=0; m=0;
for (i=0;i<=2;i++)
for (j=0;j<=2;j++)
if (w[i][j]>m) {m=w[i][j]; k=i; l=j;}
gotoxy(l+prx+l,k+pry+k); w[k][l]=-2; putch('x');
} else {gotoxy(r+prx+r,e+pry+e); putch('x'); w[e][r]=-2;}
p();
}
}
}
弹球游戏(WASD和8546控制):
#include <stdio.h>
#include <conio.h>
main()
{ int x,y,xf=1,yf=1,bf=6,bg=22,pf=30,pg=22,k=800,o,j,i,score=0;
char p;
clrscr(); textmode(C40);
for (x=1;x<=40;x++) { gotoxy(x,1); putch(219); gotoxy(x,24); putch(219);}
for (y=1;y<=24;y++) { gotoxy(1,y); putch(219); gotoxy(40,y); putch(219);}
gotoxy(3,3); putch('O'); x=3; y=3; gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(3,1); printf("<=-20--Lost");
gotoxy(16,1); printf("Score: %d **",score);
gotoxy(30,1); printf(">=50--Win");
while (k&&score<50&&score>-20)
{ gotoxy(bf,bg); printf("----");
gotoxy(pf,pg); printf("----");
gotoxy(x,y); putch('O'); delay(1000000000);
gotoxy(x,y); putch(' ');
if (x==39) {xf=-1;} if (x==2) {xf=1;}
if (y==23) {yf=-1;} if (y==2) {yf=1;}
x+=xf; y+=yf;
if (y==bg-1&&x>=(bf-1)&&x<=(bf+6)&&yf==1)
if ((x==bf-1&&xf==1)||(x>=bf&&x<=(bf+5))||(x==bf+6&&xf==-1)) {
if (x==bf-1&&xf==1) xf=-1; if (x==bf+6&&xf==-1) xf=1; yf=-1;
if (yf==1) {score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
}
if (y==pg-1&&x>=(pf-1)&&x<=(pf+6))
if ((x==pf-1&&xf==1)||(x>=pf&&x<=(pf+5))||(x==pf+6&&xf==-1)) {
yf=-1; score+=5; gotoxy(15,1);
printf("Score: %d +5",score); putch(219);}
if (y==23) {score-=5; gotoxy(15,1);
printf("Score: %d -5",score); putch(219);}
if (kbhit())
{ p=getch(); o=p;
if (o==27) exit();
if (p=='a'&&bf>3) {gotoxy(bf,bg); printf(" "); bf=bf-2;
gotoxy(bf,bg); printf("----");}
if (p=='d'&&bf+5<pf-1) {gotoxy(bf,bg); printf(" "); bf=bf+2;
gotoxy(bf,bg); printf("----");}
if (p=='w'&&bg-1>1) {gotoxy(bf,bg); printf(" "); bg=bg-1;
gotoxy(bf,bg); printf("----");}
if (p=='s'&&bg+1<24) {gotoxy(bf,bg); printf(" "); bg=bg+1;
gotoxy(bf,bg); printf("----");}
if (p=='4'&&pf>bf+6) {gotoxy(pf,pg); printf(" "); pf=pf-2;
gotoxy(pf,pg); printf("----");}
if (p=='6'&&pf+5<38) {gotoxy(pf,pg); printf(" "); pf=pf+2;
gotoxy(pf,pg); printf("----");}
if (p=='8'&&pg-1>1) {gotoxy(pf,pg); printf(" "); pg=pg-1;
gotoxy(pf,pg); printf("----");}
if (p=='5'&&pg+1<24) {gotoxy(pf,pg); printf(" "); pg=pg+1;
gotoxy(pf,pg); printf("----");}
}

k--;
} gotoxy(x,y); putch('O');
if (score>=50) {gotoxy(10,3); printf("Score>=50! You win!");}
if (score<=-20) {gotoxy(10,3); printf("Score<=-20! You lost!");}
getch();
}

⑤ C语言如何编程让一个按键轮流点灯。按一下1灯亮,在按下1灯灭2灯亮,在按下3灯亮,1 2灯是灭,一直到8个灯

C51独立按键的识别示例程序

每按一次独立键盘的S2键,与P1口相连的一个发光二极管往下移动一位。

#include <reg52.h>

sbit BY1=P3^4; //定义按键的输入端S2键

unsigned char count; //按键计数,每按一下,count加1

unsigned char temp;
unsigned char a,b;

void delay10ms(void) //延时程序

{

unsigned char i,j;

for(i=20;i>0;i--)

for(j=248;j>0;j--);

}

key() //按键判断程序

{

if(BY1==0) //判断是否按下键盘,当单片机上电时所有IO口为//高电平,S2键一端接地另一端接P3.4,所以当键被按下时P3.4口//直接接地,此时检测P3.4肯定为低电平。

{

delay10ms(); //延时,软件去干扰

if(BY1==0) //确认按键按下

{

count++; //按键计数加1

if(count==8) //计8次重新计数

{

count=0; //将count清零

}

}

while(BY1==0);//等待按键释放,如果键未释放则一直在此等待。

}

}

move() //广告灯向下移动移动函数

{

a=temp<<count; //这三句为一个循环移位,相当于前面提到的

b=temp>>(8-count);// _crol_()函数

P1=a|b;

}

main()

{

count=0; //初始化参数设置

temp=0xfe;

P1=0xff;

P1=temp;

while(1) //永远循环,扫描判断按键是否按下

{

key(); //调用按键识别函数

move(); //调用广告灯移动函数

}

}

⑥ 怎样用C语言编写一个小游戏

“贪吃蛇”C代码:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戏框的宽,x轴

#define H 26 //游戏框的高,y轴

int dir=3; //方向变量,初值3表示向“左”

int Flag=0; //吃了食物的标志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐标

int y; //食物的y坐标

}fod; //结构体fod有2个成员

struct snake{ int len; //身长

int speed; //速度

int x[100];

int y[100];

}snk; //结构体snk有4个成员

void gtxy( int x,int y) //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下声明要用到的几个自编函数

void csh( ); //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( ); //投放食物

int Over( ); //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( ) //主函数

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{system(“cls”);

gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0); //设定打印颜色为绿字黑底

gtxy(i,0); printf("■"); //打印上边框

gtxy(i,H); printf("■"); //打印下边框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //打印左边框

gtxy(W,i); printf("■"); //打印右边框

}

while(1)

{ srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0); //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物

snk.len=3; //蛇身长

snk.speed=350; //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2; //蛇头纵坐标

setColor(9, 0); //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身

}

setColor(7, 0); //恢复默认的白字黑底

return;

}

void keymove( ) //按键操作移动蛇

{ int key;

if( kbhit( ) ) //如有按键输入才执行下面操作

{ key=getch( );

if (key==224) //值为224表示按下了方向键,下面要再次获取键值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键

if(key==80&&dir!=1)dir=2; //80为向下

if(key==75&&dir!=4)dir=3; //75为向左

if(key==77&&dir!=3)dir=4; //77为向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停

}

if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; //dir=1要向上移动

case 2: snk.y[0]++; break; //dir=2要向下移动

case 3: snk.x[0]-=2; break; //dir=3要向左移动

case 4: snk.x[0]+=2; break; //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作

{ printf("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是响铃

else Flag = 0; //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值

fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值

for (i = 0; i < snk.len; i++) //随机生成的食物不能在蛇的身体上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0; //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 );

} //用来设定颜色的函数

⑦ C语言可以写哪些小游戏

C语言可以编手机游戏. 你叫他去死 不过我这有 贪吃蛇的代码,你倒可以看看 (用TC 编译一定过)

#include
#include
#include
#include
#include
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS);
void SetTimer(void interrupt (*IntProc)(__CPPARGS));
void KillTimer(void);
void Initgra(void);
void TheFirstBlock(void);
void DrawMap(void);
void Initsnake(void);
void Initfood(void);
void Snake_Headmv(void);
void Flag(int,int,int,int);
void GameOver(void);
void Snake_Bodymv(void);
void Snake_Bodyadd(void);
void PrntScore(void);
void Timer(void);
void Win(void);
void TheSecondBlock(void);
void Food(void);
void Dsnkorfd(int,int,int);
void Delay(int);
struct Snake
{int x;int y;int color;}Snk[12];
struct Food
{int x;int y;int color;}Fd;
int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,
checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse;
char Sco[2],Time[6];
void main()
{ Initgra();
SetTimer(newhandler);
TheFirstBlock();
while(1)
{DrawMap();
Snake_Headmv();
GameOver();
Snake_Bodymv();
Snake_Bodyadd();
PrntScore();
Timer();
Win();
if(key==ESC)
break;
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
TheSecondBlock();
Food();
Delay(Snkspeed);
}
closegraph();
KillTimer();
}
void interrupt newhandler(__CPPARGS)
{
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
oldhandler=getvect(0x1c);
disable();
setvect(0x1c,IntProc);
enable();
}

void KillTimer()
{
disable();
setvect(0x1c,oldhandler);
enable();
}
void Initgra()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}
void TheFirstBlock()
{setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The First Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=10;
num=2;
Times=0;
key=0;
TureorFalse=1;
TimerCounter=0;
Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0';
}
else if(key==ESC) cleardevice();
else goto loop;
}
void DrawMap()
{line(10,10,470,10);
line(470,10,470,470);
line(470,470,10,470);
line(10,470,10,10);
line(480,20,620,20);
line(620,20,620,460);
line(620,460,480,460);
line(480,460,480,20);
}
void Initsnake()
{randomize();
num=2;
Snk[0].x=random(440);
Snk[0].x=Snk[0].x-Snk[0].x%20+50;
Snk[0].y=random(440);
Snk[0].y=Snk[0].y-Snk[0].y%20+50;
Snk[0].color=4;
Snk[1].x=Snk[0].x;
Snk[1].y=Snk[0].y+20;
Snk[1].color=4;
}
void Initfood()
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
}
void Snake_Headmv()
{if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:Flag(1,0,0,0);break;
case DOWN:Flag(0,1,0,0);break;
case LEFT:Flag(0,0,1,0);break;
case RIGHT:Flag(0,0,0,1);break;

default:break;
}
}
if(flag1)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag2)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag3)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag4)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
}
void Flag(int a,int b,int c,int d)
{flag1=a;flag2=b;flag3=c;flag4=d;}
void GameOver()
{int i;
if(Snk[0].x460||Snk[0].y460)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop1:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else
goto loop1;
}
for(i=3;i<num;i++)
{if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop2:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else goto loop2;
}
}
}
void Snake_Bodymv()
{int i,s,t;
for(i=1;i<num;i++)
{Dsnkorfd(checkx,checky,Snk[i].color);
Dsnkorfd(Snk[i].x,Snk[i].y,0);
s=Snk[i].x;
t=Snk[i].y;
Snk[i].x=checkx;
Snk[i].y=checky;
checkx=s;
checky=t;
}
}
void Food()
{if(flag5)
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
flag5=0;
}
Dsnkorfd(Fd.x,Fd.y,Fd.color);
}
void Snake_Bodyadd()
{if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y)
{if(Snk[num-1].x>Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x+20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].x<Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x-20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y>Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y+20;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y<Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y-20;
Snk[num-1].color=Fd.color;
}
flag5=1;
Score++;
}
}
void PrntScore()
{if(Hscore!=Score)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,100,"SCORE");
setcolor(2);
setfillstyle(1,0);
rectangle(520,140,580,180);
floodfill(530,145,2);
Sco[0]=(char)(Score+48);
Sco[1]='\0';
Hscore=Score;
setcolor(4);
settextstyle(0,0,3);
outtextxy(540,150,Sco);
}
}
void Timer()
{if(TimerCounter>18)
{Time[4]=(char)(Time[4]-1);
if(Time[4]<'0')
{Time[4]='9';
Time[3]=(char)(Time[3]-1);
}
if(Time[3]<'0')
{Time[3]='5';
Time[1]=(char)(Time[1]-1);
}
if(TureorFalse)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,240,"TIMER");
setcolor(2);
setfillstyle(1,0);
rectangle(490,280,610,320);
floodfill(530,300,2);
setcolor(11);
settextstyle(0,0,3);
outtextxy(495,290,Time);
TureorFalse=0;
}
if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0')
{setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else if(key==ESC) cleardevice();
else goto loop;
}
TimerCounter=0;
TureorFalse=1;
}
}
void Win()
{if(Score==3)
Times++;
if(Times==2)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"You Win");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void TheSecondBlock()
{if(Score==3)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The Second Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=8;
num=2;
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void Dsnkorfd(int x,int y,int color)
{setcolor(color);
setfillstyle(1,color);
circle(x,y,10);
floodfill(x,y,color);
}
void Delay(int times)
{int i;
for(i=1;i<=times;i++)
delay(15000);
}

⑧ c语言 点灯问题

#include"stdio.h"
struct Light
{
int flag;
} light[2000001];
void initLight(Light light[] ,long n)
{long i;
for( i=1;i<=n;i++)
{light[i].flag=-1;}
}
int change(Light light[],long n,long num)
{long i;
if(num==1){for(i=1;i<=n;i++)light[i].flag=-light[i].flag;}
else for(i=num;i<=n;i=i+num)
{if(i<=0&&i>2000000) return 0;
light[i].flag=-light[i].flag;
}
}

void print(Light light[],long n)
{long count=0;
for(long i=1;i<=n;i++)
if(light[i].flag==1)
{printf("%ld号灯亮着\n",i);count++;}
printf("总共有%ld盏的灯亮着\n",count);
}
void main()
{long n;
do{
printf("请输入猩猩的数目:(1~2000000)\n");
scanf("%ld",&n);
if(n==0)break;
if(n>2000000)continue;
initLight(light,n);
for(long i=1;i<=n;i++)
change(light,n,i);
print(light,n);}while(1);
}

⑨ 如何用C语言做一个24点游戏的程序

/*6.3.4 源程序*/
#define N 20
#define COL 100
#define ROW 40
#include "stdio.h"
#include "time.h" /*系统时间函数*/
#include "graphics.h" /*图形函数*/
#include "alloc.h"/*动态地址分配函数*/
#include "stdlib.h" /*库函数*/
#include "string.h" /*字符串函数*/
#include "ctype.h" /*字符操作函数*/
char p[4][13]={
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},/*扑克牌,10用0来表示*/
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'},
{'A','2','3','4','5','6','7','8','9','0','J','Q','K'}};
typedef struct node
{
int data;
struct node *link;
}STACK1; /*栈1*/
typedef struct node2
{
char data;
struct node2 *link;
}STACK2; /*栈2*/
void init(void);/*图形驱动*/
void close(void);/*图形关闭*/
void play(void);/*发牌的具体过程*/
void rand1(int j);/*随机发牌函数*/
void change(char *e,char *a); /*中缀变后缀函数*/
int computer(char *s); /*后缀表达式计算函数*/
STACK1 *initstack1(STACK1 *top); /*栈1初始化*/
STACK1 *push(STACK1 *top,int x); /*栈1入栈运算*/
STACK1 *pop(STACK1 *top); /*栈1删除栈顶元素*/
int topx(STACK1 *top); /*栈1读栈顶元素*/
STACK1 *ptop(STACK1 *top,int *x); /*栈1读出栈顶元素值并删除栈顶元素*/
int empty(STACK1 *top); /*判栈1是否为空函数*/
STACK2 *initstack2(STACK2 *top); /*栈2初始化*/
STACK2 *push2(STACK2 *top,char x); /*栈2入栈运算*/
STACK2 *pop2(STACK2 *top); /*栈2删除栈顶元素*/
char topx2(STACK2 *top); /*栈2读栈顶元素*/
STACK2 *ptop2(STACK2 *top,char *x); /*栈2读出栈顶元素值并删除栈顶元素*/
int empty2(STACK2 *top); /*判栈2是否为空函数*
int text1(char *s) ; /*显示文本*/
main()
{
char s[N],s1[N],ch;
int i,result;
int gdriver, gmode;
clrscr(); /*清屏*/
init(); /*初始化函数*/
while(1)
{
setbkcolor(BLACK); /*设置背景颜色*/
cleardevice();/*清屏*/
play(); /*发牌*/
gotoxy(1,15); /*移动光标*/
printf("--------------------Note-------------------\n");
printf(" Please enter express accroding to above four number\n"); /*提示信息*/
printf(" Format as follows:2.*(5.+7.)\n");/*提示输入字符串格式*/
printf(" ----------------------------------------------\n");
scanf("%s%c",s1,&ch); /*输入字符串压回车键*/
change(s1,s); /*调用change函数将中缀表达式s1转换为后缀表达式s*/
result=computer(s); /*计算后缀表达式的值,返回结果result */
if(result==24) /*如果结果等于24*/
text1("very good"); /*调用函数text1显示字符串"very good"*/
else
text1("wrong!!!");/*否则函数text1显示字符串"wrong!!!"*/
printf("Continue (y/n)?\n"); /*提示信息,是否继续*/
scanf("%c",&ch); /*输入一字符*/
if(ch=='n'||ch=='N') /*如果该字符等于n或N*/
break; /*跳出循环,程序结束*/
} /*否则,开始下一轮循环*/
close();
return; /*返回*/
}
void rand1(int j)/*随机发牌函数*/
{
int kind,num;
char str[3],n;
randomize();
while(1)/*循环直到有牌发*/
{
kind=random(4); /*花色随机数*/
num=random(13); /*大小随机数*/
if(p[kind][num]!=-1) /*该数未取过*/
{
n=p[kind][num]; /*取相应位置的扑克牌数*/
p[kind][num]=-1; /*牌发好以后相应位置的元素置-1*/
break;
}
}
switch(kind)/*花式的判断*/
{
case 0:setcolor(RED);sprintf(str,"%c",3);break; /*红桃*/
case 1:setcolor(BLACK);sprintf(str,"%c",3);break; /*黑桃*/
case 2:setcolor(RED);sprintf(str,"%c",4);break; /*方片*/
case 3:setcolor(BLACK);sprintf(str,"%c",5);break; /*草花*/
}
settextstyle(0,0,2);
outtextxy(COL+j*100-30,ROW+100-46,str);/*显示左上角花色*/
outtextxy(COL+j*100+16,ROW+100+32,str); /*显示右下角花色*/
if(n!='0')/*输出其他牌*/
{
settextstyle(0,0,3);
sprintf(str,"%c",n);
outtextxy(COL+j*100-5,ROW+100-5,str);/*显示牌的大小*/
}
else/*输出10的时候*/
{
sprintf(str,"%d",10);
outtextxy(COL+j*100-6,ROW+100-5,str);
}
}
void play(void)/*发牌的具体过程*/
{
int j;
for(j=0;j<4;j++)
{
bar(COL+j*100-35,ROW+100-50,COL+j*100+35,ROW+1*100+50);/*画空牌*/
setcolor(BLUE);
rectangle(COL+j*100-32,ROW+100-48,COL+j*100+32,ROW+100+48); /*画矩形框*/
rand1(j); /*随机取牌*/
delay(10000); /*延时显示*/
}
}
void init(void)/*图形驱动*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void close(void)/*图形关闭*/
{
closegraph();
}
void change(char *e,char *a) /*中缀字符串e转后缀字符串a函数*/
{
STACK2 *top=NULL; /* 定义栈顶指针*/
int i,j;char w;
i=0;
j=0;
while(e[i]!='\0') /*当字符串没有结束时*/
{
if(isdigit(e[i])) /*如果字符是数字*/
{
do{
a[j]=e[i]; /*将数字原样拷贝到数组a中*/
i++; /*e数组的下标加1*/
j++; /*a数组的下标加1*/
}while(e[i]!='.'); /*直到字符为数字结束符“.”为止*/
a[j]='.';j++; /*将数字结束符“.”拷贝到a数组依然保持结束标记*/
}
if(e[i]=='(') /*如果字符是“(”时*/
top=push2(top,e[i]); /*将其压入堆栈*/
if(e[i]==')') /*如果字符是“)”时*/
{
top=ptop2(top,&w); /*取出栈顶元素,并从栈顶删除该元素*/
while(w!='(') /*如果字符不是“(”时反复循环*/
{
a[j]=w; /*将栈顶元素存入a数组*/
j++; /*下标加1*/
top=ptop2(top,&w) ; /*取出栈顶元素,并从栈顶删除该元素*/
}
}
if(e[i]=='+'||e[i]=='-') /*如果字符是加或减号时*/
{
if(!empty2(top)) /*如栈不为空*/
{
w=topx2(top);
while(w!='(') /*当栈顶元素不是“(”时反复循环*/
{
a[j]=w;
j++; /*将栈顶元素存入表达式a中,a的下标加1*/
top=pop2(top); /*删除栈顶元素*/
if(empty2(top)) /*如果栈为空*/
break; /*跳出循环*/
else
w=topx2(top); /*否则读栈顶元素*/
}
}
top=push2(top,e[i]); /*将当前e的字符元素压入堆栈*/
}
if(e[i]=='*'||e[i]=='/') /*如果字符是乘或除号时*/
{
if(!empty2(top)) /*如栈不为空*/
{
w=topx2(top); /*读栈顶元素存入w*/
while(w=='*'||w=='/')/*当栈顶元素是乘或除时反复循环*/
{
a[j]=w;
j++; /*将栈顶元素存入字符串a中,a的下标加1*/
top=pop2(top); /*删除栈顶元素*/
if(empty2(top)) /*如果栈为空*/
break; /*跳出循环*/
else
w=topx2(top); /*否则读栈顶元素*/
}
}
top=push2(top,e[i]); /*将当前e字符元素压入堆栈*/
}
i++; /*e的下标加1*/
}
while(!empty2(top)) /*当不为空时反复循环*/
top=ptop2(top,&a[j++]); /*将栈顶元素存入数组a中*/
a[j]='\0'; /*将字符串结束标记写入最后一个数组元素中构成字符串*/
}
int computer(char *s) /* 计算函数*/
{
STACK1 *top=NULL;
int i,k,num1,num2,result;
i=0;
while(s[i]!='\0') /*当字符串没有结束时作以下处理*/
{
if(isdigit(s[i])) /*判字符是否为数字*/
{
k=0; /*k初值为0*/
do{
k=10*k+s[i]-'0'; /*将字符连接为十进制数字*/
i++; /*i加1*/
}while(s[i]!='.'); /*当字符不为‘.’时重复循环*/
top=push(top,k); /*将生成的数字压入堆栈*/
}
if(s[i]=='+') /*如果为'+'号*/
{
top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num2+num1; /*将num1和num2相加存入result中*/
top=push(top,result); /*将result压入堆栈*/
}
if(s[i]=='-') /*如果为'-'号*/
{
top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1-num2; /*将num1减去num2结果存入result中*/
top=push(top,result); /*将result压入堆栈*/
}
if(s[i]=='*') /*如果为'*'号*/
{
top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1*num2; /*将num1与num2相乘结果存入result中*/
top=push(top,result); /*将result压入堆栈*/
}
if(s[i]=='/') /*如果为'/'号*/
{
top=ptop(top,&num2); /*将栈顶元素取出存入num2中*/
top=ptop(top,&num1); /*将栈顶元素取出存入num1中*/
result=num1/num2; /*将num1除num2结果存入result中*
top=push(top,result); /*将result压入堆栈*/
}
i++; /*i加1*/
}
top=ptop(top,&result); /*最后栈顶元素的值为计算的结果*/
return result; /*返回结果*/
}
STACK1 *initstack1(STACK1 *top) /*初始化*/
{
top=NULL; /*栈顶指针置为空*/
return top; /*返回栈顶指针*/
}
STACK1 *push(STACK1 *top,int x) /*入栈函数*/
{
STACK1 *p; /*临时指针类型为STACK1*/
p=(STACK1 *)malloc(sizeof(STACK1)); /*申请STACK1大小的空间*/
if(p==NULL) /*如果p为空*/
{
printf("memory is overflow\n!!"); /*显示内存溢出*/
exit(0); /*退出*/
}
p->data=x; /*保存值x到新空间*/
p->link=top; /*新结点的后继为当前栈顶指针*/
top=p; /*新的栈顶指针为新插入的结点*/
return top; /*返回栈顶指针*/
}
STACK1 *pop(STACK1 *top) /*出栈*/
{
STACK1 *q; /*定义临时变量*/
q=top; /*保存当前栈顶指针*/
top=top->link; /*栈顶指针后移*/
free(q); /*释放q*/
return top; /*返回栈顶指针*/
}
int topx(STACK1 *top) /*读栈顶元素*/
{
if(top==NULL) /*栈是否为空*/
{
printf("Stack is null\n"); /*显示栈为空信息*/
return 0; /*返回整数0*/
}
return top->data; /*返回栈顶元素*/
}
STACK1 *ptop(STACK1 *top,int *x) /*取栈顶元素,并删除栈顶元素*/
{
*x=topx(top); /*读栈顶元素*/
top=pop(top); /*删除栈顶元素*/
return top; /*返回栈顶指针*/
}
int empty(STACK1 *top) /*判栈是否为空*/
{
if(top==NULL) /*如果为空*/
return 1; /*返回1*/
else
return 0; /*否则返回0*/
}
STACK2 *initstack2(STACK2 *top) /*初始化*/
{
top=NULL; /*栈顶指针置为空*/
return top; /*返回栈顶指针*/
}
STACK2 *push2(STACK2 *top,char x) /*入栈函数*/
{
STACK2 *p; /*临时指针类型为STACK2*/
p=(STACK2 *)malloc(sizeof(STACK2)); /*申请STACK2大小的空间*/
if(p==NULL) /*如果p为空*/
{
printf("memory is overflow\n!!"); /*显示内存溢出*/
exit(0); /*退出*/
}
p->data=x; /*保存值x到新空间*/
p->link=top; /*新结点的后继为当前栈顶指针*/
top=p; /*新的栈顶指针为新插入的结点*/
return top; /*返回栈顶指针*/
}
STACK2 *pop2(STACK2 *top) /*出栈*/
{
STACK2 *q; /*定义临时变量*/
q=top; /*保存当前栈顶指针*/
top=top->link; /*栈顶指针后移*/
free(q); /*释放q*/
return top; /*返回栈顶指针*/
}
char topx2(STACK2 *top) /*读栈顶元素*/
{
if(top==NULL) /*栈是否为空*/
{
printf("Stack is null\n"); /*显示栈为空信息*/
return ''; /*返回空字符*/
}
return top->data; /*返回栈顶元素*/
}
STACK2 *ptop2(STACK2 *top,char *x) /*取栈顶元素,并删除栈顶元素*/
{
*x=topx2(top); /*读栈顶元素*/
top=pop2(top); /*删除栈顶元素*/
return top; /*返回栈顶指针*/
}
int empty2(STACK2 *top) /*判栈是否为空*/
{
if(top==NULL) /*如果为空*/
return 1; /*返回1*/
else
return 0; /*否则返回0*/
}

int text1(char *s)
{
setbkcolor(BLUE); /*设置背景颜色为蓝色*/
cleardevice(); /*清除屏幕*/
setcolor(12); /*设置文本颜色为淡红色*/
settextstyle(1, 0, 8);/*三重笔划字体, 放大8倍*/
outtextxy(120, 120, s); /*输出字符串s*/
setusercharsize(2, 1, 4, 1);/*水平放大2倍, 垂直放大4倍*/
setcolor(15); /*设置文本颜色为*白色/
settextstyle(3, 0, 5); /*无衬字笔划, 放大5倍*/
outtextxy(220, 220, s); /*输出字符串s*/
getch(); /*键盘输入任一字符*/
return ; /*返回*/
}

⑩ 编程C语言 24点游戏

{
printf("%-5d%-8s%-6s%-13s%-15s%-15s\n",per[i-1].score,per[i-1].name,per[i-1].age,per[i-1].num,per[i-1].adds,per[i-1].email);
if(i>1&&i%10==0)
{
printf("\t-----------------------------------\n");
printf("\t");
system("pause");
printf("\t-----------------------------------\n");
}
}