#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"typedef struct
{
int x, y; //坐标
int dir; //方向
}ElemType;typedef struct StackNode//构造栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
typedef struct
{
int maze[100][100];
}MG;
int InitStack(SqStack *head)//初始化栈
{
head->base=(ElemType *)malloc(100*sizeof(ElemType));
if(!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top=head->base;
head->stacksize=100;
return 1;
}int Push(SqStack *head,ElemType e)//进栈操作
{
if(head->top-head->base>=head->stacksize)
{
head->base = (ElemType *)realloc(head->base,(head->stacksize+10)*sizeof(ElemType));
if (!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top =head->base+head->stacksize;
head->stacksize +=10;
}
*head->top++=e;
return 1;
}int Pop(SqStack *head,ElemType *e)//出栈操作
{
if(head->top==head->base)
{
return 0;
}
*e=*--head->top;
//printf("%d\n",e);
int GetPOp(SqStack *head,ElemType *e);
{
if(head->top==head->base)
return 0;
e=(head->top-1);
return 1;
}
/*int StackEmpty(SqStack *head)//判断栈是否为空
{
if(head->top==head->base)
return 1;
else
return 0;
}*/MG printmazeA(MG ma)
{
int maze[10][10]=
{{0,0,0,0,0,0,0,0,0,0},
{0,2,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,2,0},
{0,0,0,0,0,0,0,0,0,0}
};
for(int i=0;i<10;i++)
{
printf("\n");
for(int j=0;j<10;j++)
{
if(maze[i][j]==1)
printf("□");
if(maze[i][j]==0)
printf("■");
if(maze[i][j]==0)
printf("☆");
}
}
for(int i=0;i<10;i++ )
{
for(int j=0;j<10;j++)
{
ma.maze[i][j]=maze[i][j];
}
}
return ma;
}
void findway(SqStack *head,MG ma)
{
int startx=1,starty=1,endx=8,endy=8;
road *a;
road *e;
e.x=startx;
e.y=starty;
e.dir=0;
do
{
switch(e.dir)
{
case 0:{
e.y++;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 1:{
e.x++;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 2:{
e.y--;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 3:{
e.x--;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 4: {
if(head.top!=head.base)
{
Pop(head,e);
e=GetTop(head,e);
e.dir++;
break:
}
else
{
printf("\n这个迷宫没有出路!\n");
}
}
if(e.x==endx&&e.y==endy)
{
break;
}
}while(head.top!=head.base); if(head.top!=head.base)
{
do
{
e=GetTop(head,e);
ma.maze[e.x][e.y]=2;
Pop(head,e);
}
while(head.top!=head.base);
}
}
}void printma(MG ma)
{
for(int i=0;i<10;i++)
{
printf("\n");
for(int j=0;j<10;j++)
{
if(ma.maze[i][j]==1)
printf("□");
if(ma.maze[i][j]==0)
printf("■");
if(ma.maze[i][j]==0)
printf("☆");
}
}
}void main()
{
SqStack *head; MG ma;
InitStack(head);
ma=printmazeA(ma);
findway(head,ma);
printma(ma);
}
mig.cpp
D:\专利\mig.cpp(83) : error C2601: 'printmazeA' : local function definitions are illegal
D:\专利\mig.cpp(128) : error C2601: 'findway' : local function definitions are illegal
D:\专利\mig.cpp(250) : error C2601: 'printma' : local function definitions are illegal
D:\专利\mig.cpp(268) : error C2601: 'main' : local function definitions are illegal
D:\专利\mig.cpp(277) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.mig.obj - 1 error(s), 0 warning(s)
#include"malloc.h"
#include"stdlib.h"typedef struct
{
int x, y; //坐标
int dir; //方向
}ElemType;typedef struct StackNode//构造栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
typedef struct
{
int maze[100][100];
}MG;
int InitStack(SqStack *head)//初始化栈
{
head->base=(ElemType *)malloc(100*sizeof(ElemType));
if(!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top=head->base;
head->stacksize=100;
return 1;
}int Push(SqStack *head,ElemType e)//进栈操作
{
if(head->top-head->base>=head->stacksize)
{
head->base = (ElemType *)realloc(head->base,(head->stacksize+10)*sizeof(ElemType));
if (!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top =head->base+head->stacksize;
head->stacksize +=10;
}
*head->top++=e;
return 1;
}int Pop(SqStack *head,ElemType *e)//出栈操作
{
if(head->top==head->base)
{
return 0;
}
*e=*--head->top;
//printf("%d\n",e);
int GetPOp(SqStack *head,ElemType *e);
{
if(head->top==head->base)
return 0;
e=(head->top-1);
return 1;
}
/*int StackEmpty(SqStack *head)//判断栈是否为空
{
if(head->top==head->base)
return 1;
else
return 0;
}*/MG printmazeA(MG ma)
{
int maze[10][10]=
{{0,0,0,0,0,0,0,0,0,0},
{0,2,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,2,0},
{0,0,0,0,0,0,0,0,0,0}
};
for(int i=0;i<10;i++)
{
printf("\n");
for(int j=0;j<10;j++)
{
if(maze[i][j]==1)
printf("□");
if(maze[i][j]==0)
printf("■");
if(maze[i][j]==0)
printf("☆");
}
}
for(int i=0;i<10;i++ )
{
for(int j=0;j<10;j++)
{
ma.maze[i][j]=maze[i][j];
}
}
return ma;
}
void findway(SqStack *head,MG ma)
{
int startx=1,starty=1,endx=8,endy=8;
road *a;
road *e;
e.x=startx;
e.y=starty;
e.dir=0;
do
{
switch(e.dir)
{
case 0:{
e.y++;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 1:{
e.x++;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 2:{
e.y--;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 3:{
e.x--;
a=GetTop(head ,e);
if(a.x==e.x&&a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
}
case 4: {
if(head.top!=head.base)
{
Pop(head,e);
e=GetTop(head,e);
e.dir++;
break:
}
else
{
printf("\n这个迷宫没有出路!\n");
}
}
if(e.x==endx&&e.y==endy)
{
break;
}
}while(head.top!=head.base); if(head.top!=head.base)
{
do
{
e=GetTop(head,e);
ma.maze[e.x][e.y]=2;
Pop(head,e);
}
while(head.top!=head.base);
}
}
}void printma(MG ma)
{
for(int i=0;i<10;i++)
{
printf("\n");
for(int j=0;j<10;j++)
{
if(ma.maze[i][j]==1)
printf("□");
if(ma.maze[i][j]==0)
printf("■");
if(ma.maze[i][j]==0)
printf("☆");
}
}
}void main()
{
SqStack *head; MG ma;
InitStack(head);
ma=printmazeA(ma);
findway(head,ma);
printma(ma);
}
mig.cpp
D:\专利\mig.cpp(83) : error C2601: 'printmazeA' : local function definitions are illegal
D:\专利\mig.cpp(128) : error C2601: 'findway' : local function definitions are illegal
D:\专利\mig.cpp(250) : error C2601: 'printma' : local function definitions are illegal
D:\专利\mig.cpp(268) : error C2601: 'main' : local function definitions are illegal
D:\专利\mig.cpp(277) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.mig.obj - 1 error(s), 0 warning(s)
解决方案 »
- 有关win32中启动画面的问题
- 模拟按键
- 如果做一个选文件路径的的按钮?
- 开个百分讨论贴:C++的缺点.(本人狂热喜爱C++,正因为喜爱,所以更想全面了解C++的缺点)
- 在CListView中可以用GetListCtrl获得一个列表视图(CListCtrl)对象myList,把myList子类化到我自己写的一个CTheListCtrl,怎么弄?
- 为什么MSDN中关于枚举硬件的例子不能编译啊??
- 关于状态栏和最大、最小化按钮
- 高分寻求一 OLE DB 数据库程序例子!!
- <<Microsoft Windows 应用程序设计>>这本书写的是什么?怎么样?
- 如何在 MFC AppWizard(dll) 的工程中使用 WM_TIMER
- UI线程挂起
- CString的GetData和GetBuffer到底有什么不同?
该函数的前一个 却结束 ‘}’
#include"malloc.h"
#include"stdlib.h"typedef struct
{
int x, y; //坐标
int dir; //方向
}ElemType;typedef struct StackNode//构造栈
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
typedef struct
{
int maze[100][100];
}MG;int InitStack(SqStack *head)//初始化栈
{
head->base=(ElemType *)malloc(100*sizeof(ElemType));
if(!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top=head->base;
head->stacksize=100;
return 1;
}int Push(SqStack *head,ElemType e)//进栈操作
{
if(head->top-head->base>=head->stacksize)
{
head->base = (ElemType *)realloc(head->base,(head->stacksize+10)*sizeof(ElemType));
if (!head->base)
{
printf("memory allocation failed,goodbye");
exit(1);
}
head->top =head->base+head->stacksize;
head->stacksize +=10;
}
*head->top++=e;
return 1;
}int Pop(SqStack *head,ElemType *e)//出栈操作
{
if(head->top==head->base)
{
return 0;
}
*e=*--head->top;
return 0;
//printf("%d\n",e);
}int GetPop(SqStack *head,ElemType *e)
{
if(head->top==head->base) return 0;
e=(head->top-1);
return 1;
}/*int StackEmpty(SqStack *head)//判断栈是否为空
{
if(head->top==head->base)
return 1;
else
return 0;
}*/MG printmazeA(MG ma)
{
int i,j;
int maze[10][10]=
{{0,0,0,0,0,0,0,0,0,0},
{0,2,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,2,0},
{0,0,0,0,0,0,0,0,0,0}
};
for(i=0;i<10;i++)
{
printf("\n");
for(j=0;j<10;j++)
{
if(maze[i][j]==1) printf("□");
if(maze[i][j]==0) printf("■");
if(maze[i][j]==0) printf("☆");
}
}
for(i=0;i<10;i++ )
{
for(j=0;j<10;j++)
{
ma.maze[i][j]=maze[i][j];
}
}
return ma;
}
void findway(SqStack *head,MG ma)
{
int startx=1,starty=1,endx=8,endy=8;
ElemType a;
ElemType e;
e.x=startx;
e.y=starty;
e.dir=0;
//
a.x=startx;
a.y=starty;
a.dir=0; switch(e.dir)
{
case 0:
e.y++;
GetPop(head , &e);
if(a.x==e.x && a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
break;// 9
case 1:
e.x++;
GetPop(head,&e);
if(a.x==e.x && a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
break;//1
case 2:
e.y--;
GetPop(head ,&e);
if((a.x ==e.x) && (a.y==e.y))
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
break;//2
case 3:
e.x--;
GetPop(head , &e);
if(a.x==e.x && a.y==e.y)
{
e.dir++;
}
if(ma.maze[e.x][e.y]==0)
{
e.dir++;
break;
}
else
{
Push(head,e);
e.dir=0;
break;
}
break;//3
case 4:
if(head->top!=head->base)
{
Pop(head,&e);
GetPop(head,&e);
e.dir ++;
break;
}
else
{
printf("\n这个迷宫没有出路!\n");
}
do
{
if(e.x==endx&&e.y==endy)
{
break;
}
}while(head->top!=head->base); if(head->top!=head->base)
{
do
{
GetPop(head,&e);
ma.maze[e.x][e.y]=2;
Pop(head,&e);
} while(head->top!=head->base);
}
break;// end 4
}//switch(e.dir)
}//////////////////////////
void printma(MG ma)
{
int i,j;
for( i=0;i<10;i++)
{
printf("\n");
for(j=0;j<10;j++)
{
if(ma.maze[i][j]==1) printf("□");
if(ma.maze[i][j]==0) printf("■");
if(ma.maze[i][j]==0) printf("☆");
}
}
}void main()
{
SqStack *head=0;
MG ma;
InitStack(head);
ma=printmazeA(ma);
findway(head,ma);
printma(ma);
}
SqStack *head=0;
//...
findway(head,ma);head都没分配内存就用了,那肯定导致AV呀。还有这findway的算法也是错误,可能导致穿墙,或者循环。