求一段c语言源程序代码!

一、求一段c语言源程序代码!

int main()

{

int a=3,b=2,c=2.5;

求一段c语言源程序代码!

float y=0;

y=(float)(a+b)/3(“%f”,y);

return 0;

}

二、C++程序源代码

程序源码如下; #include “stdafx.h” #include “iostream” using namespace std; template class seqstack { t data[maxsize]; int top; public: seqstack(); t pop(); void push(t x); t top(); bool empty(); }; template seqstack::seqstack() { top=-1; } template t seqstack::pop() { t x; if(top==-1){cerr<<“下溢”;exit(1);} x=data[top]; top–; return x; } template void seqstack::push(t x) { if(top==maxsize-1){cerr<<“上溢”;exit(1);} top++; data[top]=x; } template t seqstack::top(

三、用C++编写的小游戏源代码

以下是贪吃蛇源代码:

#include

#include

#include

#include

#include

#define N 21

void gotoxy(int x,int y)//位置函数

{

COORD pos;

pos.X=2x;

pos.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);

}

void color(int a)//颜色函数

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);

}

void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)

{

int i,j;//初始化围墙

int wall[N+2][N+2]={{0}};

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

{

for(j=1;j<=N;j++)

wall[i][j]=1;

}

color(11);

for(i=0;i

{

for(j=0;j

{

if(wall[i][j])

cout<<“■”;

else cout<<“□” ;

}

cout<

}

gotoxy(N+3,1);//显示信息

color(20);

cout<<“按 W S A D 移动方向”<

gotoxy(N+3,2);

color(20);

cout<<“按任意键暂停”<

gotoxy(N+3,3);

color(20);

cout<<“得分:”<

apple[0]=rand()%N+1;//苹果

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout<<“●”<

}

int main()

{

int i,j;

int** snake=NULL;

int apple[2];

int score=0;

int tail[2];

int len=3;

char ch=‘p’;

srand((unsigned)time(NULL));

init(apple);

snake=(int**)realloc(snake,sizeof(int)len);

for(i=0;i

snake[i]=(int)malloc(sizeof(int)*2);

for(i=0;i

{

snake[i][0]=N/2;

snake[i][1]=N/2+i;

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout<<“★”<

}

while(1)//进入消息循环

{

tail[0]=snake[len-1][0];

tail[1]=snake[len-1][1];

gotoxy(tail[0],tail[1]);

color(11);

cout<<“■”<

for(i=len-1;i>0;i–)

{

snake[i][0]=snake[i-1][0];

snake[i][1]=snake[i-1][1];

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout<<“★”<

}

if(kbhit())

{

gotoxy(0,N+2);

ch=getche();

}

switch(ch)

{

case ‘w’:snake[0][1]–;break;

case ’s’:snake[0][1]++;break;

case ‘a’:snake[0][0]–;break;

case ’d’:snake[0][0]++;break;

default: break;

}

gotoxy(snake[0][0],snake[0][1]);

color(14);

cout<<“★”<

Sleep(abs(200-0.5score));

if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1

{

score++;

len++;

snake=(int**)realloc(snake,sizeof(int)len);

snake[len-1]=(int)malloc(sizeof(int)*2);

apple[0]=rand()%N+1;

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout<<“●”<

gotoxy(N+5,3);

color(20);

cout<

}

if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败

{

gotoxy(N/2,N/2);

color(30);

cout<<“失败!!!”<

for(i=0;i

free(snake[i]);

Sleep(INFINITE);

exit(0);

}

}

return 0;

}参考资料:从C++吧看来的

本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。