008
19.05.2009, 06:25 Uhr
xyz
|
Danke, jetzt lässt es sich kompilieren. Leider bekomme ich nur diese Ausgabe:
Code: |
--- Node 39 --- Coordinate: 18.378, 59.466, 13.185 Adjacent Nodes: 0
--- Node 40 --- Coordinate: 14.27, 54.556, 18.227 Adjacent Nodes: 0
--- Node 41 --- Coordinate: 30.119, 51.333, 33.732 Adjacent Nodes: 0
--- Node 43 --- Coordinate: 26.326, 51.1, 21.154 Adjacent Nodes: 0
--- Node 44 --- Coordinate: 23.764, 55.05, 18.338 Adjacent Nodes: 0
--- Node 45 --- Coordinate: 17.697, 75.884, 26.178 Adjacent Nodes: 0
--- Node 48 --- Coordinate: 22.351, 66.269, 28.727 Adjacent Nodes: 0
--- Node 49 --- Coordinate: 19.98, 63.046, 33.535 Adjacent Nodes: 0
|
Anscheint wird die numAdjacentNodes Variable in GraphNode::addAdjacentNode(int label) nicht erhöht und somit wird wohl vector<int> adjacencyList nicht ausgeben.
Hier ist der aktuelle Code: main.cpp
Code: |
#include "graph.h" #include <iostream> #include <fstream> #include <vector>
using namespace std;
struct s_node { int label; double x, y, z; };
istream &operator>>(istream &in, s_node &n) { return in >> n.label >> n.x >> n.y >> n.z; }
int main() { //make an empty graph, and an empty temporary graph node Graph *graph = new Graph(); s_node s_nodeTmp;
ifstream in("template.txt"); //construct the nodes while(in >> s_nodeTmp) { //GraphNode *tempNode = new GraphNode(s_nodeTmp.label, GraphNode tempNode (s_nodeTmp.label, s_nodeTmp.x, s_nodeTmp.y, s_nodeTmp.z); graph->addNode(tempNode); //delete tempNode; } //construct the adjency for (int i = 0; i < graph->getNumNodes(); i++) { for (int j = 0; j < graph->getNumNodes(); j++) { if (graph->getNodeAt(i).getLabel() != graph->getNodeAt(j).getLabel()){ graph->getNodeAt(i).addAdjacentNode(graph->getNodeAt(j).getLabel()); } } }
//print the graph //cout << "printing..." << endl; graph->print(); delete graph; return 0; }
|
graph.h
Code: |
#ifndef GRAPH_H_ #define GRAPH_H_
#include <iostream> #include <string> #include <vector>
using namespace std;
//max number of nodes
class GraphNode { private: vector<int> adjacencyList; //list of indices of nodes connected to this node int numAdjacentNodes; //number of adjacent nodes int label; double x,y,z; //x,y,z as in a point in 3-d space.
public: GraphNode(int label, double x, double y, double z);
//get and set functions vector<int> getAdjacencyList(); int getNumAdjacentNodes();
void setLabel(int label); void setX(double x); void setY(double y); void setZ(double z);
int getLabel(); double getX(); double getY(); double getZ();
//utility functions void addAdjacentNode(int label); void print(); void distance(); };
class Graph { private: vector<GraphNode> nodes; //an array of nodes to represent the graph int numNodes; vector<vector<double> > distance_matrix; vector<vector<int> > connects;
public: //get functions int getNumNodes();
//utility functions void print(); void addNode(GraphNode node); GraphNode getNodeAt(int index); void setNodeAt(int index, GraphNode node);
};
#endif /* GRAPH_H_ */
|
graph.cpp
Code: |
#include "graph.h"
using namespace std;
GraphNode::GraphNode(int label, double x, double y, double z){ setLabel(label); setX(x); setY(y); setZ(z); numAdjacentNodes = 0; }
vector<int> GraphNode::getAdjacencyList() { //returns the complete adjacency list //return (*adjacencyList); return adjacencyList; }
int GraphNode::getNumAdjacentNodes() { //returns the number of adjacent nodes return numAdjacentNodes; }
void GraphNode::setLabel(int label){this->label = label;} void GraphNode::setX(double x){this->x = x;} void GraphNode::setY(double y){this->y = y;} void GraphNode::setZ(double z){this->z = z;}
int GraphNode::getLabel(){return label;} double GraphNode::getX(){return x;} double GraphNode::getY(){return y;} double GraphNode::getZ(){return z;}
void GraphNode::addAdjacentNode(int label){ //cout << label << endl; adjacencyList.push_back(label); numAdjacentNodes++; cout << numAdjacentNodes << '\n'; }
void GraphNode::print() { cout << "\t--- Node " << label << " ---"<<endl; cout << "Coordinate:\t\t" << getX() << ", " << getY() << ", " << getZ() << " " << endl;
cout << "Adjacent Nodes:\t"; cout << numAdjacentNodes << '\n'; for (int count = 0; count < numAdjacentNodes; count++) { cout << adjacencyList[count]; if (count != numAdjacentNodes - 1) { cout << ", "; } } cout << endl; }
void GraphNode::distance(){ double x_p, y_p, z_p;
/* for(int i= 1; i <= nodeList.size(); ++i) { for(int j = 1; j <= nodeList.size(); ++j) { if (i == j) { distance_matrix[i][j] = 0; } else { x_p = nodeList[i].getX() - nodeList[j].getX(); y_p = nodeList[i].getY() - nodeList[j].getY(); z_p = nodeList[i].getZ() - nodeList[j].getZ(); this->distance_matrix[i][j] = (x_p * x_p) + (y_p * y_p ) + ( z_p * z_p ); } } }*/ }
int Graph::getNumNodes() { //returns the number of nodes in the graph return numNodes; }
void Graph::addNode(GraphNode node) { nodes.push_back(node); numNodes++; }
GraphNode Graph::getNodeAt(int index) { if ((index < numNodes) && (index >= 0)) { return nodes[index]; } else { //invalid index specified // return GraphNode(); //return an 'empty' node } }
void Graph::print() { for (int count = 0; count < numNodes; count++) { nodes[count].print(); cout << endl << endl; } }
void Graph::setNodeAt(int index, GraphNode node) { //assignments are only allowed into existing locations if ((index < numNodes) && (index >= 0)) { nodes[index] = node; } }
|
Warum wird die numAdjacentNodes Variable in GraphNode::addAdjacentNode(int label) nicht erhöht oder gibt es ein anderes problem warum vector<int> adjacencyList nicht ausgeben wird? |