Yeah it might be the newlines. Although the CSV displayed fine using "cat" and also "gedit", I can still imagine that my program didn't see them.
I'll post the code now with some "disclaimers":
1. I'm Hungarian, sorry for not changing the variable names and stuff to English.
2. It was a test at university, I was very tired and so the code is not the clearest possible.
3. What the program does is the following:
- Read data about Santa Claus's present deliveries in the following line format:
departure date and time,arrival date and time,town;street;number,present
- Read data about the weight of each present type from a separate file
- How many puppies ("kis kutya") and toy cars ("kisauto") were delivered in each hour?
- To which addresses were dolls ("baba") delivered?
- To which addresses were more than one deliveries made?
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
|
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;
struct sulyok{string ajnev; double s;};
sulyok getsulyfrom(ifstream& in){sulyok su; getline(in, su.ajnev, ','); in>>su.s; in>>ws; return su;}
struct varos_suly{string vnev; double vsuly;};
struct delivery
{
string ind;
string erk;
int erk_ora()
{
stringstream sst;
int i;
sst<<erk;
sst.ignore(100, ' ');
sst>>i;
return i;
}
string cim;
string varos(){return cim.substr(0, cim.find(';'));}
string ajandek;
double ajandeksulya(vector<sulyok> v)
{
double a=0;
for (unsigned int i=0; i<v.size(); i++) if (v[i].ajnev==ajandek) a=v[i].s;
return a;
}
varos_suly fvnev(vector<sulyok> ssulyok){varos_suly vs; vs.vnev=varos(); vs.vsuly=ajandeksulya(ssulyok); return vs;}
}; delivery getdelivfrom(ifstream& in)
{
delivery d;
getline(in, d.ind, ',');
getline(in, d.erk, ',');
getline(in, d.cim, ',');
d.cim[0]=toupper(d.cim[0]);
d.cim[d.cim.find(';')+1]=toupper(d.cim[d.cim.find(';')+1]);
getline(in, d.ajandek);
return d;
}
struct report
{
int kutyaszam, autoszam;
report operator+=(report b){kutyaszam+=b.kutyaszam; autoszam+=b.autoszam; return *this;}
}; report getfrom(delivery d){report r; r.kutyaszam=d.ajandek=="kiskutya"; r.autoszam=d.ajandek=="kis auto"; return r;}
int main()
{
//READ
ifstream bf("naplo.csv");
ifstream sbf("suly.txt");
ofstream kf("jelentes.txt");
vector<delivery> v;
vector<sulyok> vsvs;
while (bf.good()) v.push_back(getdelivfrom(bf));
while (sbf.good()) vsvs.push_back(getsulyfrom(sbf));
//PROCESSING
vector<report> jelentes;
vector<string> babacimek;
jelentes.push_back(getfrom(v[0]));
for (unsigned int i=1; i<v.size(); i++)
{
if (v[i].erk_ora()==v[i-1].erk_ora()) jelentes.back()+=getfrom(v[i]);
else jelentes.push_back(getfrom(v[i]));
if (v[i].ajandek=="baba") babacimek.push_back(v[i].cim);
}
vector<string> cimek;
for (unsigned int i=0; i<v.size(); i++) for (unsigned int j=0; j<v.size(); j++) if (v[i].cim==v[j].cim && v[i].erk!=v[j].erk) cimek.push_back(v[i].cim);
vector<varos_suly> varosonkent;
varosonkent.push_back(v[0].fvnev(vsvs));
for (unsigned int i=1; i<v.size(); i++)
{
if (v[i].varos()==v[i-1].varos()) varosonkent.back().vsuly+=v[i].ajandeksulya(vsvs);
else varosonkent.push_back(v[i].fvnev(vsvs));
}
//OUTPUT
kf<<"- Orankenti kutyak es autok mennyisege:\nOra\tKutya\tAuto\n";
for (unsigned int i=0; i<jelentes.size(); i++) kf<<i<<'\t'<<jelentes[i].kutyaszam<<'\t'<<jelentes[i].autoszam<<endl;
kf<<"\n- Babak a kovetkezo cimekre kerultek szallitasra:";
for (unsigned int i=1; i<babacimek.size(); i++)
{
kf<<endl<<babacimek[i-1];
if (babacimek[i]==babacimek[i-1]) {kf<<" (2 db)"; i++;}
}
kf<<"\n\n- A kovetkezo cimeket erintettuk ketszer:\n";
for (unsigned int i=1; i<cimek.size(); i++) if (cimek[i] != cimek[i-1]) kf<<cimek[i-1]<<endl<<cimek[i]<<endl;
kf<<"\n- Varosonkenti ajandekok sulya:\n";
for (unsigned int i=0; i<varosonkent.size(); i++) kf<<varosonkent[i].vnev<<' '<<varosonkent[i].vsuly<<endl;
cout<<"A jelentes elkeszult a jelentes.txt fajlba.\n";
bf.close(); kf.close(); sbf.close();
}
|
And yeah, it's exactly 100 lines :)