#include #include #include "MatrixGraph.h" using namespace std; Graph::Graph() { data.kind = UDG; } Graph::~Graph() { } int Graph::init() { cout << "请输入顶点个数:"; cin >> this->data.verCount; cout << "请输入边的个数:"; cin >> this->data.arcCount; cout << "请输入顶点信息:" << endl; /** 记录顶点数据*/ for (int i = 0; i < this->data.arcCount; i++) { this->data.verTexs[i] = new char[verTexStrCount]; cout << "顶点 " << i + 1<< " :"; cin >> (this->data.verTexs[i]); } /** 初始化邻接矩阵 - 置零*/ for (int i = 0; i < this->data.verCount; i++) { for (int j = 0; j < this->data.verCount; j++) { this->data.arc[i][j] = 0; } } cout << "请输入顶点和邻接顶点信息,构建邻接矩阵:\n"; for (int i = 0; i < this->data.arcCount; i++) { verTexType vex1 = new char[verTexStrCount]; verTexType vex2 = new char[verTexStrCount]; cout << "顶点:"; cin >> vex1; cout << "邻接点:"; cin >> vex2; int x = this->localIndex(vex1); int y = this->localIndex(vex2); if (x == -1 || y == -1) { return ERROR; } this->setMatrixValue(x, y, 1); this->setMatrixValue(y, x, 1); delete[] vex1; delete[] vex2; } return OK; } int Graph::localIndex(verTexType str) { int index = 0; while (index < this->data.arcCount) { if (strcmp(str, this->data.verTexs[index]) == 0) { break; } index++; } return (index == this->data.arcCount) ? ERROR : index; } void Graph::setMatrixValue(int x, int y, int value) { this->data.arc[x][y]=value; } void Graph::outPutMatrix() { cout << '\t'; for (int i = 0; i < this->data.verCount; i++) { cout << "\t" << this->data.verTexs[i]; } cout << endl; for (int i = 0; i < this->data.verCount; i++) { cout << '\t' << this->data.verTexs[i]; for (int j = 0; j < this->data.verCount; j++) { cout << '\t' << this->data.arc[i][j]; } cout << endl; } }