给我一个C语言程序要求百行以上,谢谢
发布网友
发布时间:2022-08-18 16:29
我来回答
共2个回答
热心网友
时间:2023-08-23 22:45
红黑树的一个演示程序,不一定完全正确
大概300+吧
// Copyright 1988-2006 By Zor X. Liu
#include <stdio.h>
#include <conio.h>
enum Color { red,black };
struct RBTNode
{
int dat;
Color c;
RBTNode *p,*l,*r;
};
RBTNode *root,*nil;
RBTNode* Insert(int);
void Delete(RBTNode*);
RBTNode* Successor(RBTNode*);
RBTNode* Minium(RBTNode*);
RBTNode* Search(int);
void LeftRotate(RBTNode*);
void RightRotate(RBTNode*);
void InorderWalk(RBTNode*);
void PreorderWalk(RBTNode*);
void Free(RBTNode*);
int main()
{
nil=new RBTNode;
nil->c=black,nil->dat=0;
nil->p=nil->r=nil->l=NULL;
RBTNode *tmp;
int choice,dat;
while(true)
{
clrscr();
printf("------------------------------\n");
printf("Current:");InorderWalk(root);printf("\n");
printf("------------------------------\n");
printf(" 1.Insert\n");
printf(" 2.Delete\n");
printf(" 3.Preorder Walk\n");
printf(" q.Quit\n");
printf("------------------------------\n");
do {
choice=getch();
} while(choice!='q'&&(choice>'3'||choice<'1'));
if(choice=='q')
break;
if(choice=='1'||choice=='2')
{
printf("Data:");
scanf("%d",&dat);
}
switch(choice)
{
case '1':
Insert(dat);
printf("Inserted.\n");
break;
case '2':
tmp=Search(dat);
if(!tmp)
{
printf("Cannot find it.\n");
break;
}
Delete(tmp);
printf("Deleted.\n");
break;
case '3':
PreorderWalk(root);
break;
}
getch();
}
return 0;
}
void InorderWalk(RBTNode* x)
{
if(x!=nil&&x)
{
InorderWalk(x->l);
printf("%d",x->dat);
if(x->c==red)
printf("r ");
else
printf("b ");
InorderWalk(x->r);
}
}
void PreorderWalk(RBTNode* x)
{
if(x!=nil&&x)
{
printf("%d",x->dat);
if(x->c==red)
printf("r ");
else
printf("b ");
PreorderWalk(x->l);
PreorderWalk(x->r);
}
}
void Free(RBTNode* x)
{
if(x!=nil&&x)
{
Free(x->l);
Free(x->r);
delete x;
}
}
RBTNode* Search(int dat)
{
RBTNode *x=root;
if(root)
{
while(x!=nil)
{
if(dat==x->dat)
return x;
else
if(dat>x->dat)
x=x->r;
else
x=x->l;
}
}
return NULL;
}
RBTNode* Minium(RBTNode* x)
{
while(x->l!=nil)
x=x->l;
return x;
}
RBTNode* Successor(RBTNode* x)
{
if(x->r)
return Minium(x->r);
RBTNode *y=x->p;
while(y&&x==y->r)
{
x=y;
y=y->p;
}
return y;
}
void LeftRotate(RBTNode* x)
{
RBTNode *y=x->r,*z=x->p;
if(y!=nil)
{
x->p=y;
x->r=y->l;y->l->p=x;
y->p=z;
y->l=x;
if(y->p)
if(x==y->p->l)
y->p->l=y;
else
y->p->r=y;
else
root=y;
}
}
void RightRotate(RBTNode* x)
{
RBTNode *y=x->l,*z=x->p;
if(y!=nil)
{
x->p=y;
x->l=y->r;y->r->p=x;
y->p=z;
y->r=x;
if(y->p)
if(y->p->l==x)
y->p->l=y;
else
y->p->r=y;
else
root=y;
}
}
RBTNode* Insert(int dat)
{
RBTNode *n;
n=new RBTNode;
n->dat=dat,n->c=red;
n->p=NULL,n->l=n->r=nil;
if(!root)
{
root=n;
n->c=black;
return n;
}
RBTNode *x=root,*y=NULL;
while(x!=nil)
{
y=x;
if(dat>x->dat)
x=x->r;
else
x=x->l;
}
n->p=y;
if(dat>y->dat)
y->r=n;
else
y->l=n;
if(y->c==red)
{
x=n;
while(x->p->p&&x->c==red&&x->p->c==red)
{
if(x->p->p->l==x->p)
{
y=x->p->p->r;
if(y->c==red)
{
x->p->p->c=red;
x->p->c=y->c=black;
x=x->p->p;
}
else
{
if(x==x->p->r)
{
LeftRotate(x->p);
x=x->l;
}
x->p->c=black;
x->p->p->c=red;
RightRotate(x->p->p);
}
}
else
{
y=x->p->p->l;
if(y->c==red)
{
x->p->p->c=red;
x->p->c=y->c=black;
x=x->p->p;
}
else
{
if(x==x->p->l)
{
RightRotate(x->p);
x=x->r;
}
x->p->c=black;
x->p->p->c=red;
LeftRotate(x->p->p);
}
}
}
}
root->c=black;
}
void Delete(RBTNode* x)
{
RBTNode *y,*z;
if(x->l==nil||x->r==nil)
y=x;
else
y=Successor(x);
if(y->l!=nil)
z=y->l;
else
z=y->r;
z->p=y->p;
if(y->p)
if(y==y->p->r)
y->p->r=z;
else
y->p->l=z;
else
root=z,z->p=NULL;
if(y!=x)
x->dat=y->dat;
if(y->c==black)
{
x=z;
while(x!=root&&x->c==black)
{
if(x==x->p->l)
{
z=x->p->r;
if(z->c==red)
{
x->p->c=red;
z->c=black;
LeftRotate(x->p);
}
if(z->c==black)
{
if(z->l->c==z->r->c==black)
{
z->c=red;
x=x->p;
}
else if(z->l->c==red&&z->r->c==black)
{
RightRotate(z);
z=z->p;
}
if(z->r->c==red)
{
z->r->c=black;
z->c=z->p->c;
z->p->c=black;
LeftRotate(z->p);
x=root;
}
}
}
else
{
z=x->p->l;
if(z->c==red)
{
x->p->c=red;
z->c=black;
RightRotate(x->p);
}
if(z->c==black)
{
if(z->r->c==z->l->c==black)
{
z->c=red;
x=x->p;
}
else if(z->r->c==red&&z->l->c==black)
{
LeftRotate(z);
z=z->p;
}
if(z->l->c==red)
{
z->l->c=black;
z->c=z->p->c;
z->p->c=black;
RightRotate(z->p);
x=root;
}
}
}
}
}
delete y;
}
热心网友
时间:2023-08-23 22:46
晕!要做什么呀!给你个蛇吃蛋的游戏……
——————————————————————
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=50000;
struct Food
{
int x;
int y;
int yes;
}food;
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
void Init(void);
void Close(void);
void DrawK(void);
void GameOver(void);
void GamePlay(void);
void PrScore(void);
void main(void)
{
Init();
DrawK();
GamePlay();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
}
void GamePlay(void)
{
randomize();
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1)
{
while(!kbhit())
{
if(food.yes==1)
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;
}
if(food.yes==0)
{
setcolor(GREEN);
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++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)
{
GameOver();
snake.life=1;
}
if(snake.life==1)
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++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4);
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
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);
}
if(snake.life==1)
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;
}
}
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
——————————————————
继续学习“八荣八耻ing……”