#include <conio.h>
#include <dos.h>
#include <graphics.h>
main()
{
int gd=VGA,gm=VGAHI
int mousex,mousey
int poly[8]
union REGS inregs,outregs
inregs.x.ax=3
initgraph(&gd,&gm,"")
setwritemode(1)
setcolor(GREEN)
int86(0x33,&inregs,&outregs)
mousex=outregs.x.cx
mousey=outregs.x.dx
poly[0]=mousex
poly[1]=mousey
poly[2]=mousex+10
poly[3]=mousey+25
poly[4]=mousex+25
poly[5]=mousey+10
poly[6]=mousex
poly[7]=mousey
drawpoly(4,poly)
while (!kbhit())
{
setwritemode(1)
setcolor(GREEN)
int86(0x33,&inregs,&outregs)
if ((mousex!=outregs.x.cx) &&(mousey!=outregs.x.dx))
{
drawpoly(4,poly)
mousex=outregs.x.cx
mousey=outregs.x.dx
poly[0]=mousex
poly[1]=mousey
poly[2]=mousex+10
poly[3]=mousey+25
poly[4]=mousex+25
poly[5]=mousey+10
poly[6]=mousex
poly[7]=mousey
drawpoly(4,poly)
}
}
}
上面的代码请不要修改!!!否则运行时会得到不理想的后果!!!
因为平时我们用的c语言是在文本方式下运行的,所以不能够直接用c语言画图,在用c 语言惊醒画图之前,首先应该把c语言从文本模式转换为图形模式,初始化函数是 intgraph(图形驱动器名,图形驱动器模式,图形驱动器路径);一般图形驱动器名定为DETECT,模式为gmode,路径在你安装c语言文件夹里,BGI子目录,画线的函数是line(x1,y1,x2,y2)分别是线的起始坐标。
其库函数包含在<graphics.h>里
/*深度优先*/#include<stdio.h>
#include<stdlib.h>
struct node/*图的顶点结构*/
{
int vertex
int flag
struct node *nextnode
}
typedef struct node *graph
struct node vertex_node[10]
void creat_graph(int *node,int n)
{
int start,end,i
for(i=0i<ni++)
{
start=node[i*2]/*边的起点*/
end=node[i*2+1]/*边的终点*/
newnode=(graph)malloc(sizeof(struct node))
newnode->vertex=end/*新结点的内容为边终点处顶点的内容*/
newnode->nextnode=NULL
p=&(vertex_node[start])/*设置指针位置*/
while(p->nextnode!=NULL)
p=p->nextnode/*寻找链尾*/
p->nextnode=newnode/*在链尾处插入新结点*/
}
}
void dfs(int k)
{
graph p
vertex_node[k].flag=1/*将标志位置1,证明该结点已访问过*/
printf("vertex[%d]",k)
p=vertex_node[k].nextnode/*指针指向下个结点*/
while(p!=NULL)
{
if(vertex_node[p->vertex].flag==0)/*判断该结点的标志位是否为0*/
dfs(p->vertex)/*继续遍历下个结点*/
p=p->nextnode/*若已遍历过p指向下一个结点*/
}
}
main()
{
graph p
int node[100],i,sn,vn
printf("please input the number of sides:\n")
scanf("%d",&sn)/*输入无向图的边数*/
printf("please input the number of vertexes\n")
scanf("%d",&vn)
printf("please input the vertexes which connected by the sides:\n")
for(i=0i<4*sni++)
scanf("%d",&node[i])/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for(i=1i<=vni++)
{
vertex_node[i].vertex=i/*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode=NULL
}
creat_graph(node,2*sn)/*调用函数创建邻接表*/
printf("the result is:\n")
for(i=1i<=vni++)/*将邻接表内容输出*/
{
printf("vertex%d:",vertex_node[i].vertex)/*输出顶点内容*/
p=vertex_node[i].nextnode
while(p!=NULL)
{
printf("->%3d",p->vertex)/*输出邻接顶点的内容*/
p=p->nextnode/*指针指向下个邻接顶点*/
}
printf("\n")
}
printf("the result of depth-first search is:\n")
dfs(1)/*调用函数进行深度优先遍历*/
printf("\n")
}
/***************************广度优先*******************************/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int vertex
struct node *nextnode
}
typedef struct node *graph
struct node vertex_node[10]
#define MAXQUEUE 100
int queue[MAXQUEUE]
int front = - 1
int rear = - 1
int visited[10]
void creat_graph(int *node, int n)
{
graph newnode, p/*定义一个新结点及指针*/
int start, end, i
for (i = 0i <ni++)
{
start = node[i *2]/*边的起点*/
end = node[i *2+1]/*边的终点*/
newnode = (graph)malloc(sizeof(struct node))
newnode->vertex = end/*新结点的内容为边终点处顶点的内容*/
newnode->nextnode = NULL
p = &(vertex_node[start])/*设置指针位置*/
while (p->nextnode != NULL)
p = p->nextnode
/*寻找链尾*/
p->nextnode = newnode/*在链尾处插入新结点*/
}
}
int enqueue(int value) /*元素入队列*/
{
if (rear >= MAXQUEUE)
return - 1
rear++/*移动队尾指针*/
queue[rear] = value
}
int dequeue() /*元素出队列*/
{
if (front == rear)
return - 1
front++/*移动队头指针*/
return queue[front]
}
void bfs(int k) /*广度优先搜索*/
{
graph p
enqueue(k)/*元素入队*/
visited[k] = 1
printf("vertex[%d]", k)
while (front != rear)
/*判断是否对空*/
{
k = dequeue()/*元素出队*/
p = vertex_node[k].nextnode
while (p != NULL)
{
if (visited[p->vertex] == 0)
/*判断其是否被访问过*/
{
enqueue(p->vertex)
visited[p->vertex] = 1/*访问过的元素置1*/
printf("vertex[%d]", p->vertex)
}
p = p->nextnode/*访问下一个元素*/
}
}
}
main()
{
graph p
int node[100], i, sn, vn
printf("please input the number of sides:\n")
scanf("%d", &sn)/*输入无向图的边数*/
printf("please input the number of vertexes\n")
scanf("%d", &vn)
printf("please input the vertexes which connected by the sides:\n")
for (i = 0i <4 *sni++)
scanf("%d", &node[i])
/*输入每个边所连接的两个顶点,起始及结束位置不同,每边输两次*/
for (i = 1i <= vni++)
{
vertex_node[i].vertex = i/*将每个顶点的信息存入数组中*/
vertex_node[i].nextnode = NULL
}
creat_graph(node, 2 *sn)/*调用函数创建邻接表*/
printf("the result is:\n")
for (i = 1i <= vni++)
/*将邻接表内容输出*/
{
printf("vertex%d:", vertex_node[i].vertex)/*输出顶点内容*/
p = vertex_node[i].nextnode
while (p != NULL)
{
printf("->%3d", p->vertex)/*输出邻接顶点的内容*/
p = p->nextnode/*指针指向下个邻接顶点*/
}
printf("\n")
}
printf("the result of breadth-first search is:\n")
bfs(1)/*调用函数进行深度优先遍历*/
printf("\n")
}