On 18 Jan 2006 02:38:43 -0800 in comp.lang.c++, "Fredda"
<fr****@home.se> wrote,
Here comes a compilable program with parts of the code.
I changed kompisar to vector<string> and a few other things.
the constructor and destructor are now unnecessary, but I left them
in anyway just for example.
See if you like it?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Transaktion
{
private:
string datum;
string typ;
string namn;
double belopp;
vector<string> kompisar;
public:
Transaktion();
Transaktion(const Transaktion &t);
~Transaktion();
//string haemta_namn();
double haemta_belopp() const;
int haemta_ant_kompisar() const;
bool finnsKompis( const string& ) const;
//bool laesEnTrans( istream &is );
void skrivEnTrans( ostream & os );
}; //End Transaktion
// Standardkonstruktor
Transaktion :: Transaktion()
: datum("060118"), typ("Food"), namn("Mike"), belopp(10.0)
{
kompisar.push_back("Fredrik");
kompisar.push_back("Karl");
kompisar.push_back("Richard");
kompisar.push_back("Linn");
}
// Kopieringskonstruktor
Transaktion :: Transaktion(const Transaktion &t)
: typ(t.typ), datum(t.datum), namn(t.namn),
belopp(t.belopp), kompisar(t.kompisar)
{
}
// Destruktor
Transaktion :: ~Transaktion() { }
double Transaktion :: haemta_belopp() const {
return belopp;
}
int Transaktion :: haemta_ant_kompisar() const {
return kompisar.size();
}
void Transaktion :: skrivEnTrans(ostream &os) {
os << datum << "\t" << typ << "\t" << namn << "\t" << belopp
<< "\t" << kompisar.size() << "\t";
copy(kompisar.begin(), kompisar.end(),
ostream_iterator<string>(os, "\t"));
os << endl;
}
bool Transaktion :: finnsKompis(const string& namnet) const {
return kompisar.end() !=
find(kompisar.begin(), kompisar.end(), namnet);
}
class TransaktionsLista
{
private:
int antalTrans;
Transaktion *trans;
public:
TransaktionsLista();
~TransaktionsLista();
//void laesin( istream & is );
void skrivut( ostream & os );
void laggTill( Transaktion & t );
//double totalkostnad();
//double liggerUteMed( string namnet );
double aerSkyldig( string namnet );
//PersonLista FixaPersoner();
};// End TransaktionsLista
TransaktionsLista :: TransaktionsLista()
: antalTrans(0), trans(0)
{ ; }
TransaktionsLista :: ~TransaktionsLista() {
//delete [] trans;
}
void TransaktionsLista :: laggTill(Transaktion &t) {
if (antalTrans > 0) {
Transaktion *temp = 0;
temp = new Transaktion[antalTrans+1];
for (int i = 0; i < antalTrans; i++)
temp[i] = trans[i];
temp[antalTrans] = t;
delete [] trans;
trans = temp;
antalTrans++;
}
else {
trans = new Transaktion[1];
trans[0] = t;
antalTrans++;
}
}
double TransaktionsLista :: aerSkyldig(string namnet) {
double belopp = 0.0;
for (int i = 0; i < antalTrans; i++) {
Transaktion const & temp = trans[i];
if ( temp.finnsKompis(namnet) )
belopp += temp.haemta_belopp() /
(temp.haemta_ant_kompisar() + 1);
}
return belopp;
}
void TransaktionsLista :: skrivut(ostream &os) {
for (int i = 0; i < antalTrans; i++) {
trans[i].skrivEnTrans(os);
}
}
int main () {
Transaktion a;
Transaktion b;
Transaktion c;
TransaktionsLista tl;
tl.laggTill(a);
tl.laggTill(b);
tl.laggTill(c);
ofstream os("temp.txt");
os << tl.aerSkyldig("Karl") << '\n';
tl.skrivut(os);
os.close();
return (0);
}