// tuoterek.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 Projektiin: tuoterek.cpp,ALI\mjonot.c */ #include #include #include #include #include #include #include using namespace std; #include #include //--------------------------------------------------------------------------- class cTuote { string nimike; double hinta; int kpl; public: int alusta(const char *n,double h,int k=0) { nimike = n; hinta = h; kpl = k; return 0; } int alusta(const char *rivi) { // Alustaa jonosta Volvo|4500|3 istrstream sstr((char *)rivi); return lue(sstr); } int alusta() { return alusta("",0,0); } cTuote() { alusta(); } cTuote(const char *rivi) { alusta(rivi); } int lue(istream &is) { char jono[50]; alusta(); is.getline(N_S(jono),'|'); nimike = poista_tyhjat(jono); is.getline(N_S(jono),'|'); if ( sscanf(jono,"%lf",&hinta) < 1 ) return 1; is.getline(N_S(jono),'\n'); if ( sscanf(jono,"%d",&kpl) < 1 ) return 1; return 0; } ostream &tulosta(ostream &os) const { long oldf = os.setf(ios::left); os << setw(20) << nimike << " " << setiosflags(ios::right) << setw(7) << hinta << " " << setw(4) << kpl; os.flags(oldf); 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); } istream &operator>>(istream &is,cTuote &tuote) { tuote.lue(is); return is; } //--------------------------------------------------------------------------- const int MAX_TUOTTEITA = 10; class cTuotteet { string nimi; int tuotteita; cTuote tuotteet[MAX_TUOTTEITA]; cTuote yhteensa; public: cTuotteet(const char *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 char *s=NULL); }; int cTuotteet::etsi(const string &tnimi) const { int i; for (i=0; i= MAX_TUOTTEITA ) return -1; tuotteet[tuotteita].alusta(tnimi.c_str(),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; yhteensa += tuote; return 0; } int cTuotteet::lue(const char *s) { char rivi[80]; if ( s ) nimi = s; ifstream f(nimi.c_str()); if ( !f ) return 1; while ( f.getline(N_S(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