Start with these: Forum Announcement
Read this before posting
Then you should explain what you mean with: ...output should be in alphabetical order... (give an example)
Do you mean the following?
File looks like:
<em>lalal nn</em>
Output should be:
<em>aalllnn</em>
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
What does qantum.txt look like?
Post a sample or attach the file.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Please post an example of your full code to test.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Your code doesn't do anything.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Nope, still doesn't do anything, are you sure you posted the right code?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
No still nothing:
What compiler are you using?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Nope, I've tested it with both dev-cpp and in ubuntu.
It only does something when I comment out the line:
get_graph(filename, nodes);
under int main
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Ok I've got it, it must have been a problem with the newline at the end of the file or something - it works when I use your attached text file.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>Nope, I've tested it with both dev-cpp and in ubuntu.
Essentially they both use the same compiler :)
To the OP: Have you already noticed number 3 of my signature ?
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
I would do something like:
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <vector>
using namespace std;
class Student
{
public:
string name;
double tuition;
};
//This function sorts by tuitions fees
bool SortStudentByTute ( const Student& left, const Student& right )
{
if ( left.tuition > right.tuition )
{
return false;
}
else if ( left.tuition < right.tuition )
{
return true;
}
}
class Edge;
class Node
{
public:
string name;
list<Edge*> neighbors;
bool visited;
double cost;
Node *back_pointer;
Node ( string const &n ) : name ( n ), visited ( false ), cost ( 0 )
{}
};
class Edge
{
public:
double weight;
Node * dest;
Edge ( double c, Node *d = NULL ) : weight ( c ), dest ( d )
{}
~Edge()
{
if ( dest ) delete dest;
}
};
class NodeMap
{
public:
map<string, Node*> nodemap;
Node* find_in_nodemap ( string const &name )
{
Node* result = nodemap[name];
if ( result == 0 )
{
result = nodemap[name] = new Node ( name );
}
return result;
}
friend ostream& operator<< ( ostream& o, NodeMap nm )
{
map<string, Node*>::iterator im;
for ( im = nm.nodemap.begin(); im != nm.nodemap.end(); im++ )
{
//pair<string,Node*> p = *im;
//o << p.second->name << endl;
o << ( *im ).second->name << endl;
list<Edge*> neighbors = ( *im ).second->neighbors;
list<Edge*>::iterator e;
}
return o;
}
};
struct compare
{
bool operator() ( Node * &a, Node * &b ) const
{
// least to greatest
return b->cost < a->cost;
}
};
// for each solution, reset node information
void reset_nodes ( NodeMap &nm )
{
map<string, Node*>::iterator im;
for ( im = nm.nodemap.begin(); im != nm.nodemap.end(); im++ )
{
( *im ).second->cost = 0;
( *im ).second->back_pointer = 0;
( *im ).second->visited = false;
}
}
void dijkstra ( string s, NodeMap &nodes )
{
Student myStudent [500]; //create an array of objects
int i = 0;
// check and report or abort
Node* source = nodes.nodemap[s];
if ( source == 0 )
{
cout << "Sorry we don't fly from " << s << endl;
return;
}
reset_nodes ( nodes );
// put the source into pq and loop until empty
priority_queue<Node *, deque<Node*>, compare > pq;
pq.push ( source );
while ( !pq.empty() )
{
// process least cost node.
Node* curr = pq.top();
pq.pop();
curr->visited = true;
// process neighbors
list<Edge*>::iterator edge;
for ( edge = curr->neighbors.begin(); edge != curr->neighbors.end(); edge++ )
{
Node *ne = ( *edge )->dest;
if ( !ne->visited )
{
ne->cost += ( *edge )->weight + curr->cost;
ne->visited = true;
ne->back_pointer = curr;
//cout << "Cheapest price to "<< ne->name << " = " << ne->cost << endl;
myStudent[i].name = ne->name;
myStudent[i].tuition = ne->cost;
i++;
pq.push ( ne );
}
}
}
std::sort ( myStudent, myStudent + i, SortStudentByTute );
for ( int j = 0; j < i; j++ )
{
cout << "Cheapest price to " << myStudent[j].name << " = " << myStudent[j].tuition << endl;
}
}
void get_graph ( string const &filename, NodeMap &node_map )
{
ifstream inf ( filename.c_str() );
string from, to;
double weight;
while ( !inf.eof() )
{
inf >> from >> to >> weight;
if ( inf.good() )
{
Node *Target = node_map.find_in_nodemap ( to );
Node *Source = node_map.find_in_nodemap ( from );
Edge *connector = new Edge ( weight, Target );
Source->neighbors.push_back ( connector );
}
}
}
int main()
{
NodeMap nodes;
ifstream inf;
string filename;
cout << "Enter filename : ";
cin >> filename;
inf.open ( filename.c_str() );
get_graph ( filename, nodes );
string s;
cout << "Enter start city : ";
cin >> s;
dijkstra ( s, nodes );
system ( "PAUSE" );
return 0;
}
Not tested?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439