c语言设计报告游戏
Ⅰ 如何用c语言制作游戏
你可以先去【绘学霸】网站找“游戏设计/游戏制作”板块的【免费】视频教程-【点击进入】完整入门到精通视频教程列表: www.huixueba.net/web/AppWebClient/AllCourseAndResourcePage?type=1&tagid=307&zdhhr-11y04r-1818074554951406228
想要系统的学习可以考虑报一个网络直播课,推荐CGWANG的网络课。老师讲得细,上完还可以回看,还有同类型录播课可以免费学(赠送终身VIP)。
自制能力相对较弱的话,建议还是去好点的培训机构,实力和规模在国内排名前几的大机构,推荐行业龙头:王氏教育。
王氏教育全国直营校区面授课程试听【复制后面链接在浏览器也可打开】: www.huixueba.com.cn/school/3dmodel?type=2&zdhhr-11y04r-1818074554951406228
在“游戏设计/游戏制作”领域的培训机构里,【王氏教育】是国内的老大,且没有加盟分校,都是总部直营的连锁校区。跟很多其它同类型大机构不一样的是:王氏教育每个校区都是实体面授,老师是手把手教,而且有专门的班主任从早盯到晚,爆肝式的学习模式,提升会很快,特别适合基础差的学生。
大家可以先把【绘学霸】APP下载到自己手机,方便碎片时间学习——绘学霸APP下载: www.huixueba.com.cn/Scripts/download.html
Ⅱ c语言实训贪吃蛇报告书
《高级语言程序设计》课程设计 实验报告
题目:贪吃蛇
专业:计算机
班级: 软件
姓名:
成绩:
指导教师:
完成日期:2008年09月23日
一、目的
1. 进一步掌握和利用C语言进行程设计的能力;
2、 进一步理解和运用结构化程设计的思想和方法;
3、 初步掌握开发一个小型实用系统的基本方法;
4、 学会调试一个较长程序的基本方法;
5、 学会利用流程图或N-S图表示算法;
6、 掌握书写程设计开发文档的能力(书写课程设计报告);
二、内容与设计思想。
(1).系统功能与分析(填写你所设计的菜单及流程图)。
功能:进行贪食蛇游戏
分析:定义键盘方向键操作,随机数发生器产生食物,并分别利用函数判断贪食蛇的长度及游戏是否成功等.
并能够输入游戏成绩.
(2).数据结构
struct Food
{
int x;/*食物的横坐标*/
int y;/*食物的纵坐标*/
int yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
struct Snake
}
int x[N];
int y[N];
int node;/*蛇的节数*/
int direction;/*蛇移动方向*/
int life;/* 蛇的生命,0活着,1死亡*/
}snake;
(3).模块设计
根据功能需要:
源文件 :#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
函数名 功能
void main() 游戏主程序
void Init(void); 图形驱动
void Close(void); 图形结束
void DrawK(void); 开始画面
void GameOver(void); 结束游戏
void GamePlay(void); 玩游戏具体过程
void PrScore(void); 输出成绩
(3)总体设计思想:
利用图形驱动,制作美观的游戏界面.
通过随机函数产生随机数,控制游戏过程食物的出现.
定义键盘操作,控制游戏过程蛇的移动方向.
画出边界,并判断游戏是否结束.
统计游戏过程蛇吃的食物数量,计算并输出游戏成绩.
(4)调试过程: 测试数据及结果,出现了哪些问题,如何修改的
这里,你就写一下你平时出错的地方,与如何修改的.这里我也不好帮你写.
(5)程序有待改进的地方及本次实习的收获和建议
收获:加深认识了程序编译过程的中团队合作的重要性.
提高了自己的编程能力
(7)源程序清单(主要代码)
清单只要将上面的程序全部粘帖就可以了.
Ⅲ C语言设计游戏,如何用结构体记录输赢次数
一般用dev C++的图形模式 就可以开发C环境下的小型游戏如果是大型的3D 建议用java去写比如拿 贪吃蛇做一个例子 #define N 200/*定义全局常量*/
#define m 25
#include
#include
#include
#include
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define Esc 0x011b
int i,j,key,k;
struct Food/*构造食物结构体*/
{
int x;
int y;
int yes;
}food;
struct Goods/*构造宝贝结构体*/
{
int x;
int y;
int yes;
}goods;
struct Block/*构造障碍物结构体*/
{
int x[m];
int y[m];
int yes;
}block;
struct Snake{/*构造蛇结构体*/
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
struct Game/*构建游戏级别参数体*/
{
int score;
int level;
int speed;
}game;
/*定义函数*/
void init(void);/*定义图形驱动*/
void close(void);/*定义关闭函数*/
void drawk(void);/*定义界面函数*/
void gameover(void);/*定义游戏结束函数*/
void gameplay(void);/*定义游戏主函数*/
void prscore(void);/*定义得分函数*/
void main(void){/*主函数体,调用以下四个函数*/
init();
setbkcolor(7);
drawk();
gameplay();
close();
}
void init(void){/*构建图形驱动函数*/
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
}
void drawk(void){/*构建游戏界面函数*/
/*setbkcolor(LIGHTGREEN);*/
char str3[50];
setfillstyle(SOLID_FILL,BLUE);/*条型边框,显示版本信息*/
bar3d(48,9,610,38,1,45);
setcolor(YELLOW);/*版本信息*/
sprintf(str3,"Version:5.01,Powerwing Studio");
outtextxy(330,20,str3);
setfillstyle(LTSLASH_FILL,YELLOW);/*设定墙边的填充形式*/
bar3d(48,48,58,462,0,0);/*设定墙边*/
bar3d(48,39,611,48,0,0);
bar3d(48,452,611,462,0,0);
bar3d(602,39,611,462,0,0);
}
void gameplay(void){/*构建游戏主函数*/
/*初始化游戏角色*/
randomize();/*随机数发生器*/
goods.yes=1;
block.yes=1;
food.yes=1;/*场景中需建立新的食物*/
snake.life=1;/*初始化蛇生命值*/
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2;/*蛇初始化节数,共两节只有蛇头*/
/*初始化障碍物的数组*/
block.x[0]=170;block.y[0]=270;/*level 1*/
block.x[1]=410;block.y[1]=310;
block.x[2]=300;block.y[2]=200;
block.x[3]=320;block.y[3]=420;
block.x[4]=250;block.y[4]=350;
block.x[5]=220;block.y[5]=320;/*level 2*/
block.x[6]=310;block.y[6]=410;
block.x[7]=400;block.y[7]=500;
block.x[8]=230;block.y[8]=230;
block.x[9]=280;block.y[9]=280;
block.x[10]=170;block.y[10]=280;/*level 3*/
block.x[11]=420;block.y[11]=310;
block.x[12]=310;block.y[12]=200;
block.x[13]=320;block.y[13]=400;
block.x[14]=250;block.y[14]=260;/*level 4*/
block.x[15]=220;block.y[15]=330;
block.x[16]=130;block.y[16]=410;
block.x[17]=310;block.y[17]=510;
block.x[18]=230;block.y[18]=340;
block.x[19]=280;block.y[19]=380;
block.x[20]=270;block.y[20]=170;/*level 5*/
block.x[21]=410;block.y[21]=450;
block.x[22]=190;block.y[22]=200;
block.x[23]=150;block.y[23]=320;
block.x[24]=270;block.y[24]=350;
block.x[25]=340;block.y[25]=320;
game.score=0;
game.speed=50000;
game.level=1;
prscore();/*得分初始化*/
while(1){/*判断为真可以按Esc退出循环结束游戏*/
while(!kbhit()){/*无按键按下时,蛇自己移动身体*/
if(game.level==1){/*画出障碍物*/
for(j=0;j<5;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==2){/*画出障碍物*/
for(j=0;j<9;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==3){/*画出障碍物*/
for(j=0;j<14;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==4){/*画出障碍物*/
for(j=0;j<19;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(game.level==5){/*画出障碍物*/
for(j=0;j<25;j++){
setcolor(5);/**/
rectangle(block.x[j],block.y[j],block.x[j]+10,block.y[j]-10);
block.yes=0;
}
}
if(food.yes==1){/*需要画出新的食物*/
food.x=rand()%400+60;/*获得间隔60的随机数食物坐标值*/
food.y=rand()%350+60;
while(food.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;/*新的食物已经产生*/
}
if(goods.yes==1){/*需要画出新的宝物*/
goods.x=rand()%380+60;/*获得间隔60的随机数宝贝坐标值*/
goods.y=rand()%320+80;
while(goods.x%10!=0)/*判断坐标值是否满足被10整除,否,自动增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
goods.yes=0;/*新的宝贝已经产生*/
}
if(goods.yes==0){/*新宝贝产生,应显示出来*/
setcolor(0);/*擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
delay(50);/*延时*/
setcolor(YELLOW);
goods.x=goods.x+random(10)-random(20);/*随机数增量*/
goods.y=goods.y+random(10)-random(20);
while(goods.x%10!=0)/*判断变化后的坐标值是否满足被10整除,否,自动增加*/
goods.x++;
while(goods.y%10!=0)
goods.y++;
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);/*重画出宝贝*/
if(goods.x585||goods.y445){/*判定宝贝是否越界*/
setcolor(0);/*擦除越界的宝贝*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*越界后重新生成宝贝*/
}
}
if(food.yes==0){/*新食物产生,应显示出来*/
setcolor(GREEN);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设定当前线型*/
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--){/*取得需重画的蛇的节数*/
snake.x[i]=snake.x[i-1];/*最后一节的坐标值等于倒数第二节的坐标值*/
snake.y[i]=snake.y[i-1];
}
switch(snake.direction){/*判断蛇头的移动方向*/
case 1:snake.x[0]+=10;break;/*向右*/
case 2:snake.x[0]-=10;break;/*向左*/
case 3:snake.y[0]-=10;break;/*向上*/
case 4:snake.y[0]+=10;break;/*向下*/
}
for(i=3;i<snake.node;i++){/*超过4节后,判断蛇自身碰撞*/
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){/*即自身的任一节坐标值与蛇头坐标相等*/
for(i=1;i<snake.node-1;i++){/*擦除自己碰撞后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.life-=1;/*生命值减少一*/
snake.node-=5;
prscore();/*输出结果*/
if(snake.life==0){/*判断生命值是否为0*/
gameover();/*游戏结束*/
break;/*退出内循环*/
}
}
}
if(snake.x[0]595||snake.y[0]455){/*判断蛇是否与墙体碰撞*/
for(i=1;i<snake.node-1;i++){/*擦除撞墙后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.life-=1;/*生命值减少一*/
snake.node-=5;/*相应节数减少5节*/
prscore();
if(snake.life==0){
gameover();
break;
}
}
/*判断蛇与障碍物碰撞,食物是否与障碍物重叠*/
if(game.level==1){/*判断级别,并设定相应的障碍物数量,即数组个数*/
k=5;
}
else if(game.level==2){
k=9;
}
else if(game.level==3){
k=14;
}
else if(game.level==4){
k=19;
}
else if(game.level==5){
k=25;
}
for(j=0;j<k;j++){
if(snake.x[0]==block.x[j]&&snake.y[0]==block.y[j]){
for(i=1;i<snake.node-1;i++){/*擦除撞墙后位置蛇的身子*/
setcolor(0);
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(food.x==block.x[j]&&block.y[j]==food.y){/*防止障碍物与食物重叠*/
setcolor(0);/*设定食物的颜色为背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
food.yes=1;/*食物重新生成*/
}
snake.x[0]=100;snake.y[0]=100;/*蛇头的位置坐标重新初始化*/
snake.x[1]=110;snake.y[1]=100;
snake.direction=1;/*蛇起始的移动方向定义为向右*/
snake.life-=1;
snake.node-=5;
prscore();
if(snake.life==0){
gameover();
break;
}
}
}
if(snake.x[0]==food.x&&snake.y[0]==food.y){/*判断蛇是否吃到食物*/
setcolor(0);/*设定食物的颜色为背景色,即擦除*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;/*新的一节放在不可见的位置*/
snake.y[snake.node]=-20;
snake.node++;/*蛇身增加一节*/
if(snake.node>2){/*当节数每增加5节生命值增加一*/
snake.life=1+fabs((snake.node-2)/5);
}
food.yes=1;/*场景需要增加食物*/
game.score+=20;/*加分*/
prscore();/*输出得分*/
}
if(snake.x[0]==goods.x&&snake.y[0]==goods.y){/*判定蛇是否得到宝贝*/
setcolor(0);/*设定宝贝的颜色为背景色,即擦除*/
rectangle(goods.x,goods.y,goods.x+10,goods.y-10);
goods.yes=1;/*场景需要增加新的宝贝*/
game.score+=100;/*得到宝贝后加100分*/
prscore();/*输出得分*/
}
if(game.score<500){/*设定游戏速度和难度级别*/
game.speed=50000;
game.level=1;}
else if(game.score>=500&&game.score<1000){
game.level=2;
game.speed=40000;}
else if(game.score>=1000&&game.score<1500){
game.level=3;
game.speed=30000;}
else if(game.score>=1500&&game.score<2000){
game.level=4;
game.speed=20000; }
else if(game.score>=5000){
game.level=5;
game.speed=10000;}
setcolor(4);/*画出移动的蛇*/
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设定当前线型*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(game.speed);
setcolor(0);/*用背景色擦去最后一节*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(! kbhit) */
if(snake.life==0)/*判断循环结束条件:蛇死或者检测到Esc按键*/
break;
key=bioskey(0);/*判断按键*/
if(key==Esc)
break;
/*判断蛇头接收到的用户按键响应的移动方向*/
else if(key==UP&&snake.direction!=4)
snake.direction=3;
else if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
void gameover(void){/*游戏结束处理*/
cleardevice();/*清屏*/
prscore();/*输出得分*/
setcolor(RED);/*打印出“Game Over”字样*/
settextstyle(0,0,4);
outtextxy(200,200,"Game Over!");
getch();
}
void prscore(void){/*定义分数输出函数*/
char str1[10];
char str2[10];
char str4[20];
setfillstyle(SOLID_FILL,BLUE);/*用于清除旧的显示信息*/
bar(49,10,320,37);
setcolor(WHITE);
settextstyle(0,0,1);
sprintf(str1,"score:%d",game.score);/*输出得分*/
outtextxy(55,20,str1);
sprintf(str2,"level:%d",game.level);/*输出级别*/
outtextxy(250,20,str2);
sprintf(str4,"life:%d",snake.life);/*输出级别*/
outtextxy(150,20,str4);
}
void close(void){/*定义关闭函数,退出图形模式*/
getch();
closegraph();
}
Ⅳ 用C语言设计小游戏的程序急!!!
用c++实现的"贪吃蛇"游戏源码
// greedsnake.cpp
#include <bios.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include "conf.h"
typedef struct node
{
int x,y;
struct node *next;
}Node;
typedef struct
{
Node *head,*tail;
int length;
}Snake;
typedef struct
{
int left,top,right,bottom;
}Frame;
typedef enum //四个方向
{
up,down,left,right
}Direction;
typedef enum
{
false,true
}bool;//*/
void InitGraphMode(); //初始化图形驱动
void CloseGraphMode();
void Foot(int,int);
void Head(int,int);
void CreateFrame(); //完成整个游戏框架的绘制
void CreateSnake(); //创建一条两个节点的蛇,蛇的每一节是队列中的一个节点
bool PlayGame(); //游戏的主体函数,
int Hit(int,int); //判断是否越界,或者撞到自身,两个参数分别是新的头接点的x,y坐标
bool GameOver(); //绘制游戏结束时弹出的对话框
void Enqueue(Node); //入队函数
Node Dequeue(); //出队函数
void ClearKeyBuf(); //清除键盘缓冲,此函数可以消除不停的按无效键的影响
Snake snake;
Frame frame;
void main()
{
InitGraphMode();
do
{
CreateFrame();
}while(PlayGame());
CloseGraphMode();
}
void InitGraphMode()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"../bgi/");
cleardevice();
}
void CloseGraphMode()
{
cleardevice();
closegraph();
}
void CreateFrame()
{
setbkcolor(CYAN);
//下面的四行代码用于计算主框架的左上角和右下角的坐标
frame.left=(getmaxx()+1-BlockWidth*RowOfFrame)/2;
frame.top=(getmaxy()+1-BlockHeight*ColumnOfFrame)/2;
frame.right=frame.left+BlockWidth*RowOfFrame;
frame.bottom=frame.top+BlockHeight*ColumnOfFrame;
Head(frame.left+100,frame.top-20);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(frame.left,frame.top,frame.right,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(DARKGRAY);
line(frame.left,frame.top,frame.right,frame.top);
line(frame.left,frame.top,frame.left,frame.bottom);
setlinestyle(SOLID_LINE,1,1);
setcolor(WHITE);
line(frame.left,frame.bottom,frame.right,frame.bottom);
line(frame.right,frame.top,frame.right,frame.bottom);
setlinestyle(DOTTED_LINE,1,1);
setcolor(BLUE);
for(int row=1;row<RowOfFrame;row++)
line(frame.left+row*BlockWidth,frame.top,frame.left+row*BlockWidth,frame.bottom);
for(int column=1;column<ColumnOfFrame;column++)
line(frame.left,frame.top+column*BlockHeight,frame.right,frame.top+column*BlockHeight);
Foot(frame.left,frame.bottom+20);
}
void CreateSnake()
{
Node *node1=new Node;
Node *node2=new Node;
node1->x=frame.left+BlockWidth;
node1->y=frame.top;
node1->next=NULL;
snake.tail=node1;
node2->x=frame.left;
node2->y=frame.top;
node2->next=snake.tail;
snake.head=node2;
snake.length=2;
setfillstyle(SOLID_FILL,BLUE);
bar(snake.head->x+1,snake.head->y+1,snake.head->x+BlockWidth-1,snake.head->y+BlockHeight-1);
bar(snake.tail->x+1,snake.tail->y+1,snake.tail->x+BlockWidth-1,snake.tail->y+BlockHeight-1);
}
bool PlayGame()
{
int speed=300,key;
Direction CurrentDirection=right;
Node randomNode;
Node newNode,outNode;
bool neednode=true;
bool overlap=false;
int randx,randy;
CreateSnake();
while(true)
{
if(neednode==true)
{
randomize();
do
{
randx=frame.left+rand()%RowOfFrame*BlockWidth;
randy=frame.top+rand()%ColumnOfFrame*BlockHeight;
for(Node *p=snake.head;p!=NULL;p=p->next)//hit itself
if(randx==p->x&&randy==p->y)
{overlap=true;break;}
}
while(overlap==true);
randomNode.x=randx;
randomNode.y=randy;
randomNode.next=NULL;
setfillstyle(SOLID_FILL,RED);
bar(randomNode.x+1,randomNode.y+1,randomNode.x+BlockWidth-1,randomNode.y+BlockHeight-1);
neednode=false;
}
if((key=bioskey(1))!=0)
{
switch(key)
{
case ESC: return false;
case UP:
if(CurrentDirection!=down)
CurrentDirection=up;
ClearKeyBuf();
break;
case DOWN:
if(CurrentDirection!=up)
CurrentDirection=down;
ClearKeyBuf();
break;
case LEFT:
if(CurrentDirection!=right)
CurrentDirection=left;
ClearKeyBuf();
break;
case RIGHT:
if(CurrentDirection!=left)
CurrentDirection=right;
ClearKeyBuf();
break;
case PAGEUP:speed=speed-100;
if(speed<100)
speed=100;
ClearKeyBuf();
break;
case PAGEDOWN:speed=speed+100;
if(speed>500)
speed=500;
ClearKeyBuf();
break;
default :break;
}
}
int headx=snake.tail->x;
int heady=snake.tail->y;
switch(CurrentDirection)
{
case up: heady-=BlockHeight;break;
case down: heady+=BlockHeight;break;
case left: headx-=BlockWidth;break;
case right: headx+=BlockWidth;break;
}
if(Hit(headx,heady)) //whether the snake hit the wall or itself
return GameOver();
else
{ //eat
if(headx==randomNode.x&&heady==randomNode.y)
{
Enqueue(randomNode);
setfillstyle(SOLID_FILL,BLUE);
bar(randomNode.x+1,randomNode.y+1,randomNode.x-1+BlockWidth,randomNode.y-1+BlockHeight);
neednode=true;
}
else //no eat
{
newNode.x=headx;
newNode.y=heady;
newNode.next=NULL;
Enqueue(newNode);
outNode=Dequeue();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(outNode.x+1,outNode.y+1,outNode.x+BlockWidth-1,outNode.y+BlockHeight-1);
setfillstyle(SOLID_FILL,BLUE);
bar(newNode.x+1,newNode.y+1,newNode.x-1+BlockWidth,newNode.y-1+BlockHeight);
}
}
delay(speed);
}
}
void ClearKeyBuf()
{
do
bioskey(0);
while(bioskey(1));
}
void Foot(int x,int y)
{
setcolor(BLUE);
outtextxy(x,y,"writer:[T]RealXL E-MAIL:[email protected]");
}
void Head(int x,int y)
{
setcolor(RED);
outtextxy(x,y,"GREEDY SNAKE");
}
void Enqueue(Node inNode)
{
Node *p=new Node;
p->x=inNode.x;
p->y=inNode.y;
p->next=inNode.next;
snake.tail->next=p;
snake.tail=p;
snake.length++;
}
Node Dequeue()
{
Node *p=snake.head;
Node outNode=*p;
snake.head=p->next;
snake.length--;
delete p;
return outNode;
}
int Hit(int x,int y)
{
if(x<frame.left||x>=frame.right||y<frame.top||y>=frame.bottom)//hit the wall
return 1;
Node *p=snake.head->next;
for(int i=snake.length-1;i>3;i--,p=p->next)//hit itself
if(x==p->x&&y==p->y)
return 1;
return 0;
}
bool GameOver()
{
int x=getmaxx()/2-50;
int y=getmaxy()/2-20;
setfillstyle(SOLID_FILL,DARKGRAY);
bar(x+3,y+3,x+103,y+43);
setfillstyle(SOLID_FILL,MAGENTA);
bar(x,y,x+100,y+40);
setlinestyle(0,3,1);
setcolor(RED);
rectangle(x,y,x+100,y+40);
outtextxy(x+20,y+10,"GAGE OVER!");
char c;
while(true) //按q或Q表示退出程序,按r或R表示重新开始游戏
{
c=getch();
if(c=='q'||c=='Q')
return false;
else if(c=='r'||c=='R')
return true;
}
}
C++五子棋源程序:
#include
#include
#include
#define backcolor CYAN
#define defaultcolor BLACK
#define linecolor MAGENTA
#define player1_color RED
#define player2_color WHITE
#define error_color RED
#define winner_color RED
const int left=40;
const int top=390;
const int d=30;
const int line_num=9;
const int turn=0;
const int r=d/3;
const int j=10;
int x,y,k=1,step=(line_num+1)*(line_num+1);
union REGS regs1,regs2;
class player1;
class player2;
class qipan{
public:
qipan();
~qipan(){};
void init_qipan();
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2,qipan &num);
private:
int point[line_num+1][line_num+1];
};
class player1{
public:
player1();
~player1(){};
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2);
friend int judge_winner(player1 &num1,player2 &num2);
private:
int point1[line_num+1][line_num+1];
};
class player2{
public:
player2();
~player2(){};
friend void fall(player1 &num1,player2 &num2,qipan &num);
friend void input(player1 &num1,player2 &num2,qipan &num);
friend int judge_winner(player1 &num1,player2 &num2);
private:
int point2[line_num+1][line_num+1];
};
void input(player1 &num1,player2 &num2);
void fall(player1 &num1,player2 &num2,qipan &num);
int judge_winner(qipan &num,player1 &num1,player2 &num2);
void inputerror();
void display_winner(int);
void main()
{
int driver=DETECT,mode;
initgraph(&driver,&mode,"e:\tc30\bgi");
qipan num;
player1 num1;
player2 num2;
while(step--)
{
input(num1,num2,num);
fall(num1,num2,num);
if(judge_winner(num1,num2))
{
display_winner(k);
}
}
// getchar();
}
qipan::qipan(void)
{ int j,i;
char ch[2]="0";
setbkcolor(backcolor);
setcolor(linecolor);
for(i=0;i<=line_num;i++)
{
line(left,top-i*d,left+line_num*d,top-i*d);
}
for(i=0;i<=line_num;i++)
{
line(left+i*d,top,left+i*d,top-line_num*d);
}
for(i=0;i<=line_num;i++)
{ if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+i*d-2,top+r+3,ch);
(*ch)=(*ch)+1;
}
*ch='0';
for(i=0;i<=line_num;i++)
{if(*ch=='9'+1) *ch='a';
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left-r-10,top-d*i-3,ch);
(*ch)=(*ch)+1;
}
setcolor(defaultcolor);
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point[i][j]=0;
}
}
void fall(player1 &num1,player2 &num2,qipan &num)
{
int flag=k%2;
if(flag)
{ setcolor(player2_color);
num2.point2[x][y]=1;
num.point[x][y]=2;
circle(left+d*x,top-d*y,r);
setfillstyle(1,player2_color);
floodfill(left+d*x,top-d*y,player2_color);
}
else
{ num1.point1[x][y]=1;
num.point[x][y]=1;
setcolor(player1_color);
circle(left+d*x,top-d*y,r);
setfillstyle(1,player1_color);
floodfill(left+d*x,top-d*y,player1_color);
}
setcolor(defaultcolor);
}
void input(player1 &num1,player2 &num2,qipan &num)
{ char xx,yy;
k++;
while(1)
{
regs1.h.ah=0;
xx=int86(22,®s1,®s1)-'0';
if(xx==('q'-'0')||xx==('Q'-'0'))
{ step=0;
return;
}
regs1.h.ah=0;
yy=int86(22,®s1,®s1)-'0';
if(yy==('q'-'0')||yy==('Q'-'0'))
{
step=0;
return ;
}
if(xx<0||xx>line_num)
{ inputerror();
continue;
}
if(yy<0||yy>line_num)
{inputerror();
continue;
}
if(num.point[xx][yy]==0)
{
break;
}
else
{
inputerror();
continue;
}
}
x=(int)xx;
y=(int)yy;
setcolor(backcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}
player1::player1()
{
int i,j;
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point1[i][j]=0;
}
}
player2::player2()
{ int i,j;
for(i=0;i<=line_num;i++)
{
for(j=0;j<=line_num;j++)
point2[i][j]=0;
}
}
void inputerror(void)
{ setcolor(error_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(left+d*line_num/3,top+d*2,"Input error");
setcolor(defaultcolor);
}
int judge_winner(player1 &num1,player2 &num2)
{
int count=0,m=0,n=0,a=0,b=0,xx0,yy0;
int flag=k%2;
xx0=x; yy0=y;
if(!flag)
{ //left <-------> right
while(xx0>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up <------> down
count=0; xx0=x; m=0; n=0;
while(yy0>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
if(num1.point1[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up <-----> left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
if(num1.point1[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;
}
else
{
//left <-------> right
while(xx0>=1&&m<4) {xx0--;m++;}
while(n<9&&xx0<=line_num)
{
if(num1.point1[xx0][y]==1)
{
count++;
if(count==5) return 1;
}
else
{
count=0;
}
n++;
xx0++;
}
//up <------> down
count=0; xx0=x; m=0; n=0;
while(yy0>=1&&m<4){yy0--;m++;}
while(n<9&&yy0<=line_num)
{
if(num2.point2[x][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
n++;
yy0++;
}
//left up ----- right down
xx0=x;
yy0=y;
m=0;
n=0;
count=0;
while(xx0>=1&&m<4){ xx0--; a++; m++;} m=0;
while(yy0<=line_num&&m<4){ yy0++; b++; m++;}
if(a<=b)
{
xx0=x-a; yy0=y+a;
}
else
{
xx0=x-b; yy0=y+b;
}
while(xx0<=line_num&&yy0>=0&&n<9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
{
count=0;
}
xx0++;
yy0--;
n++;
}
//right up <-----> left down
count=0;
a=0;
b=0;
n=0;
m=0;
xx0=x;
yy0=y;
while(xx0while(yy0if(a<=b)
{
xx0=x+a;
yy0=y+a;
}
else
{
xx0=x+b;
yy0=y+b;
}
while(xx0>=0&&yy0>=0&&n<9)
{
if(num2.point2[xx0][yy0]==1)
{
count++;
if(count==5)
return 1;
}
else
count=0;
xx0--;
yy0--;
n++;
}
//no winer
return 0;
}
}
void display_winner(int k)
{
int flag=k%2;
if(!flag)
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+d*2,top+40,"Red is winner");
setcolor(defaultcolor);
step=0;
getchar();
}
else
{ setcolor(winner_color);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(left+2*d,top+40,"White is winner");
setcolor(defaultcolor);
step=0;
}
}
Ⅳ C语言程序设计课程设计扑克牌游戏,怎么做
#include<conio.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
int jisuan(int);
int comptotal;
char s1[]="A234567890JQK";
//char s2[4][5]={"红桃","黑桃","草花","方块"};
char s2[4]={3,4,5,6};
int poke[52];
int ch;
int win=0;
int computer[5],user[5];
int usertotal;
int users;
int k;
int main()
{ void xipai(int poke[]);
void ai();
int i,j;
////////////////////////////////////////////////////////上面是变量和声明
printf("\n这是简单的廿一点游戏:\n");
for(i=0;i<52;i++)
{
if(i%13==0)putchar('\n');
poke[i]=i;
printf("%c%c%c ",s2[i/13],s1[i%13]=='0'?'1':' ',s1[i%13]);
}
putchar('\n');
/////////////////////////////////////////////////////////主代码
k=0;
xipai(poke);
while(ch!=27)
{ comptotal=0;
usertotal=0;
if(k>=42)
{
printf("\n剩余牌数不足十张,重新洗牌");
xipai(poke);
k=0;
}
printf("\n\n\n\n\n\n\n\n新局开始:\n");
printf("现在共有牌%2d张\n",52-k);
if(win==0)
{
computer[0]=k++;
user[0]=k++;
printf("\n电脑做庄,要牌:");
ai();
}
else
{
printf("\n玩家做庄,要牌:\n\t回车要牌\n\t空格过牌");
user[0]=k++;
computer[0]=k++;
}
printf("\n玩家开始要牌:\n");
usertotal=jisuan(poke[user[0]]);
printf("%c%c%c 共%2d点\t",s2[poke[user[0]]/13],s1[poke[user[0]]%13]=='0'?'1':' ',s1[poke[user[0]]%13],usertotal);
users=0;
ch=1;
while(ch!=32&&users<4)
{
ch=getch();
switch(ch)
{
case 27:
goto end;
break;
case 32:
break;
case 13:
user[++users]=k;
usertotal+=jisuan(poke[user[users]]);
printf("\b\b\b\b\b\b\b\b\b%c%c%c 共%2d点\t",s2[poke[k]/13],s1[poke[k]%13]=='0'?'1':' ',s1[poke[k]%13],usertotal);
k++;
if(usertotal>=21)ch=32;
break;
default:
break;
}
}
if(win==1)
{
printf("\n电脑开始要牌:\n");
ai();
}
printf("\n\n\n玩家的点数是%2d",usertotal);
printf("\n电脑的点数是%2d",comptotal);
printf("\n\n本局结算:");
if(comptotal>21&&usertotal<=21)
{
printf("\n\n电脑爆牌了");
win=1;
printf("\n恭喜,你赢了");
}
if(usertotal>21&&comptotal<=21)
{
printf("\n\n你爆牌了");
printf("\n下次小心点");
win=0;
}
if(usertotal>21&&comptotal>21)
{
printf("\n\n你们两个,怎么都这么不小心啊,都撑死了还要吗");
}
if(usertotal<=21&&comptotal<=21)
{
if(usertotal>comptotal)
{
win=1;
printf("\n\n不错,你赢了");
}
else if(usertotal<comptotal)
{
win=0;
printf("\n\n撑死胆大的,饿死胆小的,没胆子,输了吧");
}
else
printf("\n\n平了,算你走运");
}
getch();
}
end:
return 0;
}
void xipai(int poke[])
{
int y,tmp,i,j;
for(j=0;j<7;j++)
for(i=0;i<52;i++)
{
srand(time(0));
y=rand()%10;
tmp=poke[i];
poke[i]=poke[(y*i*i)%52];
poke[(y*i*i)%52]=tmp;
}
}
///////////////////////////////////////////////子函数
void ai()
{
int i;
comptotal=jisuan(poke[computer[0]]);
printf("\n%c%c%c 共%2d点\t",s2[poke[computer[0]]/13],s1[poke[computer[0]]%13]=='0'?'1':' ',s1[poke[computer[0]]%13],comptotal);
for(i=0;i<4;i++)
{
if(comptotal<17)
{
computer[i+1]=k++;
comptotal+=jisuan(poke[computer[i+1]]);
printf("\b\b\b\b\b\b\b\b\b%c%c%c 共%2d点\t",s2[poke[computer[i+1]]/13],s1[poke[computer[i+1]]%13]=='0'?'1':' ',s1[poke[computer[i+1]]%13],comptotal);
}
}
}
int jisuan(int i)
{int dian;
switch(i%13)
{
case 0:
case 10:
case 11:
case 12:
dian=1;
break;
default:
dian=i%13+1;
}
return dian;
}
Ⅵ C语言程序设计实验报告怎么写啊
通过本试验初步培养计算机逻辑解题能力。熟练掌握赋值语句和if语句的应用;掌握switch多路分支语句和if嵌套语句的使用
Ⅶ C语言编程 课程设计 数学游戏
我先编一编哦,代码写好了我会放在我的网络空间的文章里边的,到时候你到那里去找吧
你最好也自己编一编吧,编程这东西是越编越会的,不编永远也不会的,加油啦!
Ⅷ 用C语言设计一个游戏,华容道的游戏,请高手帮忙~~
package 华容道;
import java.awt.*;
import java.awt.event.*;
//主函数
public class Main {
public static void main(String[] args) {
new Hua_Rong_Road();
}
}
//人物按钮颜色
class Person extends Button implements FocusListener{
int number;
Color c=new Color(255,245,170);
Person(int number,String s)
{
super(s);
setBackground(c);//人物的颜色背景是黄色
this.number=number;
c=getBackground();
addFocusListener(this);//好像是焦点监听器
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);//只要单击该按钮则按钮变颜色
}
public void focusLost(FocusEvent e) {
setBackground(c);//上一个按钮回复原先的颜色
}
}
//华容道总类
class Hua_Rong_Road extends Frame implements MouseListener,KeyListener,ActionListener{
Person person[] = new Person[10];
Button left,right,above,below;
Button restart = new Button("Start");//重新开始按钮
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);//设置Frame为可见,默认为不可见
validate();
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100, 320, 120, 25);
restart.addActionListener(this);
String name[]={"我","陆逊","姜维","陈宫","许攸","邓艾","周瑜","庞统","诸葛亮","贾诩"};
for(int k=0;k<name.length;k++)
{
person[k]=new Person(k,name[k]);
person[k].addMouseListener(this);
person[k].addKeyListener(this);
add(person[k]);
}//为所有的按钮注册所需的东西
person[0].setBounds(104, 54, 100, 100);
person[1].setBounds(104,154, 100, 50);
person[2].setBounds(54, 154, 50, 100);
person[3].setBounds(204, 154, 50, 100);
person[4].setBounds(54, 54, 50, 100);
person[5].setBounds(204, 54, 50, 100);
person[6].setBounds(54, 254,50, 50);
person[7].setBounds(204, 254, 50, 50);
person[8].setBounds(104, 204, 50, 50);
person[9].setBounds(154, 204, 50, 50);
//初始化按钮的位子
person[0].requestFocus();
left=new Button();
right=new Button();
above=new Button();
below=new Button();
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
Person man=(Person)e.getSource();
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
go(man,below);
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
go(man,above);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
go(man,left);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
go(man,right);
}
}
public void mousePressed(MouseEvent e)
{
Person man =(Person)e.getSource();
int x=-1,y=-1;
x=e.getX();
y=e.getY();
int w=man.getBounds().width;
int h=man.getBounds().height;
if(y>h/2)
{
go(man,below);
}
if(y<h/2)
{
go(man,above);
}
if(x<w/2)
{
go(man,left);
}
if(x>w/2)
{
go(man,right);
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void go(Person man,Button direction)
{
boolean move=true;
Rectangle manRect=man.getBounds();
int x=man.getBounds().x;
int y=man.getBounds().y;
if(direction==below)
y=y+50;
else if(direction==above)
y=y-50;
else if(direction==left)
x=x-50;
else if(direction==right)
x=x+50;
manRect.setLocation(x,y);
Rectangle directionRect=direction.getBounds();
for(int k=0;k<10;k++)
{
Rectangle personRect=person[k].getBounds();
if((manRect.intersects(personRect))&&(man.number!=k))
{
move=false;
}
}
if(manRect.intersects(directionRect))
{
move=false;
}
if(move==true)
{
man.setLocation(x,y);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
new Hua_Rong_Road();
}
}
Ⅸ 求助:c语言程序设计报告 猜数游戏 谢谢!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int n,i,j=0,cnt=0;
char c;
srand(time(0));
do
{ j=0;
cnt=0;
i=rand()%100;
do
{
printf("输入一个0~99的数字:");
cnt++;
scanf("%d",&n);
getchar();
if(n==i){printf("WINER!\nPlay again?Y/N:");j=1;}
else if(n>i)printf("GREATER THEN!\n");
else printf("LESS THEN!\n");
if(cnt>10){printf("END.\nThe answer=%d!\nPlay again?Y/N:",i);j=1;break;}
}
while(j==0);
c=getchar();
}
while(c=='y'||c=='Y');
return 0;
}
Ⅹ c语言程序设计实验报告80~100行,关于一种小游戏的,语句简单些,
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_PARKING_SIZE 10//停车场最大停车数量
#define PRIZE 10.00//停留每小时单价
#define true 1
#define false 0
typedef struct stack
{
long pos[MAX_PARKING_SIZE];//存储车牌号码
int time[MAX_PARKING_SIZE];//存储进入车站的时间
int point;//最后一辆车的位置指针
}Stack;//定义栈-模拟停车场
typedef struct queue
{
int num;//存储车牌号
struct queue *next;//指向下一辆车
}Queue;//定义队列-模拟停车场外
void InitStack(Stack *s)
{
s->point=-1;
}//初始化栈
Queue *InitQueue()//初始化队列
{
Queue *q;
q=(Queue *)malloc(sizeof(Queue));
q->next=NULL;
return q;
}
int StackPop(Stack *s,long *i,int *j)//退栈函数
{
if(s->point==-1)return false;
else
{
*i=s->pos[s->point];
*j=s->time[s->point];
s->point--;
return true;
}
}
int StackPush(Stack *s,long i,int j)//压栈函数
{
if(s->point==MAX_PARKING_SIZE-1)return false;
else
{
s->point++;
s->pos[s->point]=i;
s->time[s->point]=j;
return true;
}
}
int QueuePop(Queue **qH,long *i)//退队函数
{
Queue *temp;
if((*qH)->next==NULL)return false;
else
{
temp=(*qH)->next;
(*qH)->next=temp->next;
*i=temp->num;
free(temp);
return true;
}
}
int QueuePush(Queue **q,long i)//入队函数
{
Queue *temp;
if((temp=(Queue *)malloc(sizeof(Queue)))==NULL)return false;
else
{
(*q)->next=temp;
temp->num=i;
temp->next=NULL;
*q=temp;
return true;
}
}
int main()
{
int time,i,j,inStack,inQueue;
long num;
char state;
Stack park;
Queue *H,*p,*temp,*temp2;
H=InitQueue();
p=H;
system("color 9E");
InitStack(&park);//初始化队列和栈
printf("**********这里是停车场管理程序,欢迎使用**************\n");
printf("\n停车场最大能停车%d辆,停车时间不得超过24小时,现在停车的单价为每小时%.2f元\n",MAX_PARKING_SIZE,PRIZE);
while(1)
{
inStack=inQueue=0;temp=H;
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ 停车场管理系统 ┃\n");
printf("┃ (A或a)汽车到达和已到汽车查询 ┃\n");
printf("┃ (D或d)汽车离开 ┃\n");
printf("┃ (E或e)程序退出 ┃\n");
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
printf("\n您的选择是:");
state=getche();
if(state=='a'||state=='A'||state=='d'||state=='D')
{
printf("\n输入车牌号(数字):");
scanf("%ld",&num);
printf("\n输入到达或查询或离开的的时间(24小时制,单位:小时):");
scanf("%d",&time);
}
if(state=='a'||state=='A')
{
for(i=0;i<=park.point;i++)
if(park.pos[i]==num)
{
inStack=1;
break;
}//在车站中查找
for(j=1;temp->next!=NULL;j++)
{
temp2=temp;
temp=temp2->next;
if(temp->num==num)
{
inQueue=1;
break;
}
}//在车站外查找
if(inStack)
printf("\n查询结果:输入的汽车已经在车站中第%d个位置停留了%d个小时!\n",i+1,time>park.time[i]?time-park.time[i]:time+24-park.time[i]);
else if(inQueue)
printf("\n查询结果:输入的汽车已经在车站外第%d个位置等候\n",j);
else
{
if(StackPush(&park,num,time)==false)
{
QueuePush(&p,num);
printf("\n车站已满,车辆停在场外。\n");
}
else printf("\n车辆成功进站!\n");
}//如果车辆到达,当车站已满,停在车站外,否则,进入车站
}
else if(state=='d'||state=='D')//如果是离开
{
for(i=0;i<=park.point;i++)
if(park.pos[i]==num)
{
inStack=1;
break;
}//在车站中查找
if(inStack)//如果在车站中
{
printf("正要离开的汽车在车站中第%d个位置停留了%d个小时,应付%.2f元\n",i+1,time>park.time[i]?time-park.time[i]:time+24-park.time[i],time>park.time[i]?(time-park.time[i])*PRIZE:(time+24-park.time[i])*PRIZE);
while(i<park.point)
{
park.pos[i]=park.pos[i+1];
park.time[i]=park.time[i+1];
i++;
}
park.point--;//把离开的车辆从车站中删除
if(H->next!=NULL)
{
QueuePop(&H,&num);
if(H->next==NULL)p=H;
StackPush(&park,num,time);
printf("\n停车场空出一位置。场外等候的%d号汽车入站了!\n",num);
}//如果车站外有车,入站
}
else//不在车站中
{
for(i=1;temp->next!=NULL;i++)
{
temp2=temp;temp=temp2->next;
if(temp->num==num)
{
inQueue=1;
break;
}
}//查找是否在车站外
if(inQueue)
{
printf("\n汽车在停车场外,不收费\n",i);
temp2->next=temp->next;
if(temp==p)p=temp2;
free(temp);
}//在车站外
else printf("\n对不起,您输入了不存在的车牌号!\n");
}
}
else if(state=='e'||state=='E')
{
printf("\n");
break;
}
else printf("\n输入错误!\n");
}
return 0;
}