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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <ostream>
std::string MakeLower(std::string);
// ============================================================================
// For handling vectors in 3 dimension coordinates
struct ThreeVector
{
double fx; // x component
double fy; // y component
double fz; // z component
// for initialization, e.g. ThreeVector(0),
// ThreeVector(),ThreeVector(1,2,3)
ThreeVector():fx(0),fy(0),fz(0) {}
ThreeVector(double val):fx(val),fy(val),fz(val) {}
ThreeVector(double xv, double yv, double zv)
: fx(xv),fy(yv),fz(zv) {}
// return functions
double x() { return fx; }
double y() { return fy; }
double z() { return fz; }
};
// ============================================================================
// Particle data, you can add to this as needed
struct Particle
{
std::string type; // proton, alpha, etc
int charge; // -1,0,+1, etc
double mass; // choose units
ThreeVector position;
ThreeVector velocity;
ThreeVector acceleration;
// initial data of declared particle
Particle():type(""),charge(0),mass(0),position(0),
velocity(0),acceleration() {}
};
// ============================================================================
// Predeclaration of functions used after main but used in main (required)
std::vector<std::string> DelimitLine(std::string,char);
void PrintThreeVector(ThreeVector,std::ostream&);
void PrintParticleData(std::vector<Particle>,std::ostream&);
double StringToDouble(std::string);
int StringToInt(std::string);
// ============================================================================
// Assign the value of k here, along with any other global variables you want
//const double k = ...;
// ============================================================================
// The main driver function
int main(int argc, char* argv[])
{
std::ifstream in;
// check to make sure there is an input file
if(argc < 2) {
std::cout << argv[0] << " needs an input file." << std::endl;
exit(0);
} else {
in.open(argv[1]);
if(!in) {
std::cout << "Could not open " << argv[1] << std::endl;
exit(0);
}
}
// map for storing particle data
std::map<std::string,std::vector<Particle> > particles;
std::string line;
while(!in.eof()) {
getline(in,line,'\n'); // put entire line into one string
// Below sorts line from above using spaces as delimited
// can replace with commas by ',' and changing input file
std::vector<std::string> delimitedLine = DelimitLine(line,' ');
// declare particle variable
Particle particle;
if(delimitedLine.size() >= 3) {
// access/assign struct particle data with . and variable
particle.type = delimitedLine.at(0);
particle.charge = StringToInt(delimitedLine.at(1));
particle.mass = StringToDouble(delimitedLine.at(2));
} else if(line.size() == 0) {
continue;
} else {
std::cout << "Line: " << line
<< " doesn't have enough parameters"
<< std::endl;
}
if(delimitedLine.size() == 7) {
particle.position.fx = StringToDouble(delimitedLine.at(3));
particle.position.fy = StringToDouble(delimitedLine.at(4));
particle.velocity.fx = StringToDouble(delimitedLine.at(5));
particle.velocity.fy = StringToDouble(delimitedLine.at(6));
}
particles[delimitedLine.at(0)].push_back(particle);
}
// for output
std::map<std::string,std::vector<Particle> >::iterator ite;
for(ite = particles.begin(); ite != particles.end(); ++ite) {
// print to screen
PrintParticleData(ite->second,std::cout);
}
// here is where you want to do all your computations
// handle the data using the map iterator
// for maps, you access the first part (the key) with ->first
// -> is similar to . except the arrow is for pointers (i.e. the ite variable
// is "pointing to" the element it is on.
// and the second part (the data stored according to key) with ->second
// for vector iterators, see PrintParticleData function
return 0;
}
// ============================================================================
void PrintThreeVector(ThreeVector three, std::ostream& out)
{
out << three.x() << " " << three.y() << " " << three.z() << " ";
}
// ============================================================================
void PrintParticleData(std::vector<Particle> p, std::ostream& out)
{
std::vector<Particle>::iterator ite;
for(ite = p.begin(); ite != p.end(); ++ite) {
out << (*ite).type << " " << (*ite).charge << " "
<< (*ite).mass << " ";
PrintThreeVector((*ite).position,out); // print position data
PrintThreeVector((*ite).velocity,out); // print velocity data
PrintThreeVector((*ite).acceleration,out); // print accel data
out << std::endl;
}
}
// ============================================================================
std::vector<std::string> DelimitLine(std::string line, char delimiter)
{
std::vector<std::string> delimitedLine;
std::string token;
std::istringstream iss(line);
while( getline(iss,token,delimiter) ) {
if( !token.empty() ) { delimitedLine.push_back(token); }
}
return delimitedLine;
}
// ============================================================================
std::string MakeLower(std::string str)
{
for(std::string::size_type i = 0; i < str.size(); ++i) {
str[i] = tolower(str[i]);
}
}
// ============================================================================
int StringToInt(std::string str)
{
std::istringstream converter(str);
int result;
converter >> result;
return result;
}
// ============================================================================
double StringToDouble(std::string str)
{
std::istringstream converter(str);
double result;
converter >> result;
return result;
}
|