1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct ITEM
{
int x, y;
int identifier;
string name;
};
istream & operator >> ( istream &strm, ITEM &item )
{
string line;
getline( strm, line );
const string junk = "[,]-";
replace_if( line.begin(), line.end(), [ junk ]( char c ){ return junk.find( c ) != string::npos; }, ' ' );
stringstream ss( line );
ss >> item.x >> item.y >> item.identifier >> ws;
getline( ss, item.name ); // might be multiple words
return strm;
}
void readData( istream &strm, vector<ITEM> &result )
{
for ( ITEM item; strm >> item; ) result.push_back( item );
}
void plotData( const vector<ITEM> data )
{
const int XMAX = 14, YMAX = 14;
string plot[YMAX]; for ( string &p : plot ) p = string( XMAX, ' ' );
for ( ITEM item : data ) plot[item.y][item.x] = (char)( '0' + item.identifier );
cout << string( XMAX, '-' ) << '\n';
for ( int y = YMAX - 1; y >= 0; y-- ) cout << plot[y] << '\n';
cout << string( XMAX, '-' ) << '\n';
cout << "Key:\n";
for ( ITEM item : data ) cout << item.identifier << ": " << item.name << " (position: " << item.x << " " << item.y << ")\n";
}
int main()
{
// ifstream in( "data.txt" );
stringstream in( "[1,10]-9-Lunamaria\n"
"[7,12]-3-Decathalon\n"
"[2,11]-4-Lupus Magna\n"
"[13,9]-8-Magnaria\n" );
vector<ITEM> data;
readData( in, data );
plotData( data );
}
|