// tuoterekb.cpp /* Ohjelma lukee tiedostoa TUOTTEET.DAT, joka on muotoa: Volvo | 12300 | 1 Audi | 55700 | 2 Saab | 1500 | 4 Volvo | 123400 | 1 Ohjelma tulostaa kuhunkin tuoteluokkaan kuuluvien tuotteiden yhteishinnat ja kappalemäärät sekä koko varaston yhteishinnan ja kappalemäärän. Eli em. tiedostosta tulostetaan: ------------------------------------------- Volvo 135700 2 Audi 111400 2 Saab 6000 4 ------------------------------------------- Yhteensä 253100 8 ------------------------------------------- Vesa Lappalainen 15.3.1996, 8.3.2000 Muutokset: 15.3.2001/vl + muutettu const char * => string Projektiin: tuoterek.cpp, ALI\mjonot.c, ALI\dout.cpp */ #include #include #include #include #include #include using namespace std; #include "mjonot.h" #include "mjonotpp.h" #include "streampr.h" #include "dosout.h" //--------------------------------------------------------------------------- class cTuote { string nimike; double hinta; int kpl; public: int alusta(const string &n,double h,int k=0) { nimike = n; hinta = h; kpl = k; return 0; } int alusta(const string &jono) { // Alustaa jonosta Volvo| 2443 |3 string s(jono); // jonosta kopio jota voi muutella nimike = erota(s,'|'); poista_tyhjat(nimike); string pala; pala = erota(s,'|'); hinta = 0.0; sscanf(pala.c_str(),"%lf",&hinta); pala = erota(s,'|'); kpl = 0; sscanf(pala.c_str(),"%d",&kpl); return 0; } int alusta() { return alusta("",0,0); } cTuote() { alusta(); } cTuote(const string &rivi) { alusta(rivi); } ostream &tulosta(ostream &os) const { cStreamPre pre(os,2); os.setf(ios::left); os << setw(20) << nimike << " " << setiosflags(ios::right) << setw(10) << hinta << " " << setw(4) << kpl; return os; } void ynnaa(const cTuote &tuote) { hinta += tuote.hinta * tuote.kpl; kpl += tuote.kpl; } cTuote &operator+=(const cTuote &tuote) { ynnaa(tuote); return *this; } const string &Nimike() const { return nimike; } }; ostream &operator<<(ostream &os,const cTuote &tuote) { return tuote.tulosta(os); } //--------------------------------------------------------------------------- const int MAX_TUOTTEITA = 10; class cTuotteet { string nimi; int tuotteita; cTuote tuotteet[MAX_TUOTTEITA]; cTuote yhteensa; public: cTuotteet(const string &n) : nimi(n), yhteensa("Yhteensä") { tuotteita = 0; } ostream &tulosta(ostream &os) const; int etsi(const string &tnimi) const; int lisaa(const string &tnimi); int ynnaa(const cTuote &tuote); int lue(const string &s); int lue() { return lue(""); } }; int cTuotteet::etsi(const string &tnimi) const { int i; for (i=0; i= MAX_TUOTTEITA ) return -1; tuotteet[tuotteita].alusta(tnimi,0.0,0); return tuotteita++; } int cTuotteet::ynnaa(const cTuote &tuote) { if ( tuote.Nimike() == "" ) return 1; int i = etsi(tuote.Nimike()); if ( i < 0 ) i = lisaa(tuote.Nimike()); if ( i < 0 ) return 1; tuotteet[i] += tuote; // tuotteet[i].ynnaa(tuote); yhteensa += tuote; // yhteensa.ynnaa(tuote); return 0; } int cTuotteet::lue(const string &s) { string rivi; if ( s != "" ) nimi = s; ifstream f(nimi.c_str()); if ( !f ) return 1; while ( getline(f,rivi) ) { cTuote tuote(rivi); if ( ynnaa(tuote) ) cout << "Rivillä \"" << rivi << "\" jotain pielessä!" << endl; } return 0; } ostream &cTuotteet::tulosta(ostream &os) const { int i; os << "\n\n\n"; os << "-------------------------------------------\n"; for (i=0; i