question archive In C++ Could really use some help with using the index of the source, and address of the target, call the connect() function and with implementing the connect() function to add an edge to a node and implementing the put() function so that it displays the nodes values, followed by the values of the nodes immediate neighbors as seen in example output below: Example output Enter file name: Data
Subject:Computer SciencePrice: Bought3
In C++
Could really use some help with using the index of the source, and address of the target, call the connect() function and with implementing the connect() function to add an edge to a node and implementing the put() function so that it displays the nodes values, followed by the values of the nodes immediate neighbors as seen in example output below:
Example output
Enter file name: Data.txt
Sunday
Monday
Tuesday
___________________________________________
Main.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
#include "node.h"
/******************************
* main()
******************************/
int main() {
int i, n;
int source_index, target_index, distance;
string fname, source_name, target_name;
fstream in;
node map[NODE_MAX];
// Initialize
n = 0;
cout << left;
// Get file name
cout << "Enter file name: ";
cin >> fname;
// Open file
in.open(fname, ios::in);
// Loop through file
while (!in.eof()) {
in >> source_name >> target_name >> distance;
i = 0;
bool isSourcePresent = false;
while (i < n) {
if (map[i].get_value() == source_name) {
isSourcePresent = true;
break;
}
i++;
}
// Add to array
if (!isSourcePresent)
map[n++].set_value(source_name);
bool isTargetPresent = false;
int target_index = -1;//Index of target node
for (int i = 0; i < n; i++) {
if (map[i].get_value() == target_name) {//Target node is present in the array
isTargetPresent = true;
target_index = i;//index of the target node;
break;
}
}
//If target node is not found in the array add it to the array and increment n
if (isTargetPresent == false) {
map[n++].set_value(target_name);
}
// if(in.good()) {
// cout << setw(12) << source_name;
// cout << setw(12) << target_name;
// cout << setw(4) << distance;
// cout << endl;
// };
};
// Close file
in.close();
// Display array
for (i = 0; i < n; i++)
cout << map[i].get_value() << endl;
}
Node.h
// Node Declarations
#define ERR -1
#define NODE_MAX 20
#define EDGE_MAX 4
// Node class
class node
{
public:
node(); // Constructor
void set_value(string); // Set string value
string get_value(); // Return string value
void connect(node*); // Connect this node to another
void put(ostream&); // Output node and neighbors
private:
string value; // Node value
node* edge[EDGE_MAX]; // Edges array
};
Node.cpp
#include <iostream>
#include <string>
using namespace std;
#include "Node.h"
/******************************
* Null constructor
******************************/
node::node()
{ int i;
value = "";
for (i = 0; i < EDGE_MAX; i++)
edge[i] = NULL;
}
/******************************
* set_value()
******************************/
void node::set_value(string arg)
{ value = arg;
}
/******************************
* get_value()
******************************/
string node::get_value()
{ return value;
}
/******************************
* connect()
******************************/
// Add code here
/******************************
* put()
******************************/
// add code here
Data.txt
Sunday Monday 10
Sunday Tuesday 20
Monday Thursday 30
Tuesday Friday 30
Wednesday Monday 20
Wednesday Tuesday 10
Thursday Sunday 50
Friday Sunday 60
Thursday Wednesday 10
Friday Wednesday 20
Thursday Saturday 20
Friday Saturday 10