2 gtt1025 Gtt1025 于 2016.04.28 12:03 提问

为什么,我向图中插入顶点老是失败呢,就是InsertVex那个函数

Graph.h
struct Vex
{
int num;
char name[20];
char desc[1024];
};
struct Edge
{
int vex1;
int vex2;
int weight;
};
class CGraph
{
private:
int m_aAdjMatrix[20][20];
Vex m_aVexs[20];
int m_nVexNum;
public:
CGraph(int M_aAdjMatrix[20][20], Vex M_aVexs[20], int M_nVexNum);
CGraph(void);
~CGraph(void);
void setVex(Vex sVex);
bool InsertVex(Vex sVex);
bool InsertEdge(Edge sEdge);
Vex GetVex(int v);
int FindEdge(int v, Edge aEdge[]);
void Init(void);
int GetVexNum(void);
};
#endif

Graph.cpp
#include <iostream>
#include <stdlib.h>
#include "Graph.h"
using namespace std;
CGraph::CGraph(int M_aAdjMatrix[20][20], Vex M_aVexs[20], int M_nVexNum)
{
    for (int i = 0; i < MAX_VERTEX_NUM; i++)
    {
        for (int j = 0; j < MAX_VERTEX_NUM; j++)
        {
            m_aAdjMatrix[i][j] = M_aAdjMatrix[i][j];
        }
    }
    for (int m = 0; m < MAX_VERTEX_NUM; m++)
    {
        m_aVexs[m].num = M_aVexs[m].num;
        strcpy(m_aVexs[m].name, M_aVexs[m].name);
        strcpy(m_aVexs[m].desc, M_aVexs[m].desc);
    }
    m_nVexNum = M_nVexNum;

}
CGraph::CGraph(void)
{

}
CGraph::~CGraph(void)
{

}
void CGraph::setVex(Vex sVex)
{
    m_aVexs[m_nVexNum].num = sVex.num;
    strcpy(m_aVexs[m_nVexNum].name, sVex.name);
    strcpy(m_aVexs[m_nVexNum].desc, sVex.desc);
}
bool CGraph::InsertVex(Vex sVex)

{
    if (m_nVexNum == MAX_VERTEX_NUM)
    {
        return false;
    }
    else 
    {
        m_aVexs[m_nVexNum].num = sVex.num;
        strcpy(m_aVexs[m_nVexNum].name, sVex.name);
        strcpy(m_aVexs[m_nVexNum].desc, sVex.desc);
        //setVex(sVex);
        m_nVexNum++;
    //  m_aVexs[m_nVexNum++] = sVex;
        return true;
    }
}
bool CGraph::InsertEdge(Edge sEdge)
{
    if (sEdge.vex1 < 0 || sEdge.vex1 >= m_nVexNum || sEdge.vex2 < 0 || sEdge.vex2 >= m_nVexNum)
    {
        return false;
    }
    m_aAdjMatrix[sEdge.vex1][sEdge.vex2] = sEdge.weight;
    m_aAdjMatrix[sEdge.vex2][sEdge.vex1] = sEdge.weight;
    return true;

}
Vex CGraph::GetVex(int v)
{
    return m_aVexs[v];
}
int CGraph::FindEdge(int v, Edge aEdge[])
{
    int k = 0;

    for (int i = 0; i < m_nVexNum; i++)
    {
        aEdge[i].vex1 = v;
        aEdge[i].vex2 = i;
        if ((aEdge[i].weight != 0 )&& (aEdge[i].vex1 == v )&& (aEdge[i].vex2==i))
        {
            k++;
        }

    }
    return k;
}
void CGraph::Init(void)
{

    for (int i = 0; i < MAX_VERTEX_NUM; i++)
    {
        for (int j = 0; j < MAX_VERTEX_NUM; j++)
        {
            m_aAdjMatrix[i][j] = 0;
        }
    }
    for (int m = 0; m < MAX_VERTEX_NUM; m++)
    {
        m_aVexs[m].num = 0;
        strcpy(m_aVexs[m].name, "0");
        strcpy(m_aVexs[m].desc, "0");
    }
    int m_nVexNum = 0;
}
int CGraph::GetVexNum()
{
    return m_nVexNum;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!