高手在帮忙看一下吧……关系到毕业啊……小弟多谢了!!!

sunzhao45 2006-04-30 05:46:55
把一个无环有向图存储电脑里,随机生成遍历序列的程序,编译没有出错,运行的时候老是提示非法操作,debug一下的提示是:Unhandled exception in mysearch.exe:)xC0000005:Access Violation.
,还有对v的值,提示:CXX0030:Error:expression cannot be evaluated。搞了以下午了,头都大了,求求各位大侠帮忙调调吧,小弟多谢!!!源码是这样的:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#define n 3 // 图中顶点数
#define e 3 // 图中边数
typedef struct Graph
{
int v[n+1]; // 存放顶点信息v1,v2,….vn,不使用v[0]存储空间
int arcs[n+1][n+1]; // 邻接矩阵
} graph;


void creatadj(graph* p);
void randomsearch(int i, graph *p, int *path,int depth);
void main()
{
graph *p;
p=(graph *)malloc(sizeof(struct Graph));
int m;
for(m=1;m<=n;m++)
p->v[m]=0;
creatadj(p);
int a,b;
for(a=1;a<=n;a++)
{
for(b=1;b<=n;b++)
cout<<p->arcs[a][b];
cout<<endl;
}
cout<<"1 ";
int h;
int path[n];
for(h=0;h<n;h++)
path[h]=0;
randomsearch(1, p, path,0);
}

void randomsearch(int i, graph *p, int *path,int depth)
{
cout<<"aaa";
int outdegree=0; //i的出度

int nozero[n];
int j,k;
int l=0;
int m;
for(m=0;m<n;m++)
nozero[m]=0;
cout<<"2 ";//
path[depth]=i;
for(j=1;j<=n;j++)
{
outdegree=outdegree+(p->arcs[i][j]);
if(p->arcs[i][j]!=0)
{
nozero[l]=j;
l=l+1;
}
}
cout<<" 3 ";
if(outdegree!=0)
{
int t;
depth=depth+1;
t=(rand()%outdegree);
path[depth]=nozero[t];
randomsearch(t,p,path,depth);
cout<<"4";
}


else
{
cout<<" 5 ";
for(k=0;k<=depth;k++)
cout<<path[k]<<",";
cout<<endl;
}
}


void creatadj(graph* p)
{
int i,j,k ;
for (k=1; k<=n; k++)
{
cout<<"输入第"<<k<<"个顶点。"<<endl;
cin>>p->v[k];
}

for (i=1; i<=n; i++ )
for (j=1; j<=n; j++)
p->arcs[i][j]=0;
for (k=1; k<=e; k++)
{ cout<<"输入已存在的边"<<endl;
cin>>i>>j;
p->arcs[i][j]=1;

}
}

...全文
122 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BaiYangSpirit 2006-04-30
  • 打赏
  • 举报
回复
你的这个地方可能写错了:
void randomsearch(int i, graph *p, int *path,int depth)
{
cout<<"aaa";
int outdegree=0; //i的出度

int nozero[n];
int j,k;
int l=0;
int m;
for(m=0;m<n;m++)
nozero[m]=0;
cout<<"2 ";//
path[depth]=i;
for(j=1;j<=n;j++)
{
outdegree=outdegree+(p->arcs[i][j]);
if(p->arcs[i][j]!=0)
{
nozero[l]=j;
l=l+1;
}
}
cout<<" 3 ";
if(outdegree!=0)
{
int t;
depth=depth+1;
t=(rand()%outdegree);
path[depth]=nozero[t];
// randomsearch(t,p,path,depth); //把这个地方修改一下
randomsearch(nozero[t],p,path,depth); //你将再次遍历时的一个参数写错了
cout<<"4";
}


else
{
cout<<" 5 ";
for(k=0;k<=depth;k++)
cout<<path[k]<<",";
cout<<endl;
}
}

llf_hust 2006-04-30
  • 打赏
  • 举报
回复
没有错误呀,按它要求输入就行了
sunzhao45 2006-04-30
  • 打赏
  • 举报
回复
不好意思,我是新手,不太明白您的意思……
blh 2006-04-30
  • 打赏
  • 举报
回复
单步跟以下不就知道了

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧