首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

请各位大神帮小弟我看上这代码的有关问题

2014-04-18 
请各位大神帮我看下这代码的问题#includestdio.h#includemalloc.h#includestdlib.htypedef struct{i

请各位大神帮我看下这代码的问题
#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)


[解决办法]
错误实在太多!

C/C++ code
#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;    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;elsereturn 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);} 

热点排行