Cześć
mialem wykonac takie zadanie (i wykonalem, moje pytanie tylko, czy kod wyglada przyzwoicie, bo cos czuje ze to raczej jakas masakra bedzie (mialem duze problemy z zadaniem))
Klasa WIno posiada obiekt skladowy typu string, ktory przechwouje nazwe wina oraz obiekt Pair (opisany ponizej) zawierajacy obiekty typu valarray<int>. Pierwsza skaldowa Pair przechowuje przechowuje rocznik wina, a druga liczbe posiadanych butelek. Klasa wino moze tez przechowywac skladowa - liczbe rocznikow. Oprocz tego mialem utworzyc metody Label, GetBottles, Show - ich dzialanie ok. Pair (szablon) byl podany, wykorzystalem go w kodzie - jednak nie obeszlo sie bez jego modyfikacji (czy aby na pewno te modyfikacje byly konieczne ?)
orygianlny szablon:
// pairs.h -- Definicja szablonu Pair
#ifndef PAIRS_H_
#define PAIRS_H_
template<class T1, class T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const { return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { }
Pair() {}
};
template<class T1, class T2>
T1 & Pair<T1,T2>::first()
{
return a;
}
template<class T1, class T2>
T2 & Pair<T1,T2>::second()
{
return b;
}
#endif
moj kod:
plik naglowkowy:
#ifndef _WINE_H
#define _WINE_H
#include <valarray>
#include <string>
#include <iostream>
template<class T1, class T2>
class Pair
{
private:
T1 rok; //rocznik wina
T2 liczba_but; //liczba butelek tregto roczniak
public:
T1 & first();
T2 & second();
int first(int i) const
{
return rok[i];
}
const int second(const int i) const
{
return liczba_but[i];
}
Pair(const int * aval, const int * bval, int y) : rok(y), liczba_but(y)
{
for (int i = 0; i < y; i++)
{
rok[i] = aval[i];
liczba_but[i] = bval[i];
}
}
Pair(const T1 & aval, const T2 & bval, int y) : rok(y), liczba_but(y)
{
rok=aval;
liczba_but=bval;
}
void zmien_rok(int i, int w)
{
rok[i] = w;
}
void zmien_liczbe(int i, int w)
{
liczba_but[i] = w;
}
Pair()
{
}
};
template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return rok;
}
template<class T1, class T2>
T2 & Pair<T1, T2>::second()
{
return liczba_but;
}
typedef std::valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
std::string nazwa;
PairArray zestaw; //typedef!
int roczniki;
public:
Wine() : nazwa("brak"), zestaw(ArrayInt(0), ArrayInt(0), 0)
{
};
Wine(const char * l, int y, const int yr[], const int bot[]);
Wine(const char *l, int y);
void GetBottles();
const std::string & Label();
int sum();
void Show();
};
#endif /* _WINE_H */
implementacja interfejsu:
#include "Wine.h"
Wine::Wine(const char* l, const int y, const int yr[], const int bot[]) : nazwa(l), roczniki(y), zestaw(yr, bot, y)
{
}
Wine::Wine(const char* l, const int y) : nazwa(l), roczniki(y), zestaw(ArrayInt(y), ArrayInt(y), y)
{
}
void Wine::GetBottles()
{
int k = 0;
std::cout << "Podaj dane o winie " << nazwa << " dla " << roczniki << " rocznikow:\n";
while (k < roczniki)
{
int pomoc;
std::cout << "P0daj rocznik: ";
std::cin >> pomoc;
zestaw.zmien_rok(k, pomoc);
std::cout << "Podaj liczbe butelek: ";
std::cin >> pomoc;
zestaw.zmien_liczbe(k, pomoc);
++k;
}
}
const std::string & Wine::Label()
{
return nazwa;
}
int Wine::sum()
{
int i = 0;
int suma = 0;
while (i < roczniki)
{
suma += zestaw.second(i);
i++;
}
return suma;
}
void Wine::Show()
{
std::cout << "Wino: " << nazwa << std::endl;
std::cout << " " << "Rocznik" << " " << "Butelki" << std::endl;
for (int i = 0; i < roczniki; i++)
std::cout << " " << zestaw.first(i) << " " << zestaw.second(i) << std::endl;
}
program testowy:
#include <stdlib.h>
#include <iostream>
#include "Wine.h"
/*
*
*/
int main(int argc, char** argv)
{
using std::cin;
using std::cout;
using std::endl;
cout << "Podaj nazwe wina: ";
char lab[50];
cin.getline(lab, 50);
cout << "Podaj liczbe rocznikow: ";
int yrs;
cin >> yrs;
Wine holding(lab, yrs);
holding.GetBottles();
holding.Show();
const int YRS = 3;
int y[YRS] = {1993, 1995, 1998};
int b[YRS] = {48, 60, 71};
Wine more("Gushing Grape Red", YRS, y, b);
more.Show();
std::cout << "Laczna liczba butelek wina " << more.Label()
<< ": " << more.sum() << std::endl;
std::cout << "KONIEC!\n";
return (EXIT_SUCCESS);
}
z gory dzieki za opinie o kodzie i za wskazanie bledow/niezby dobrych rozwiazan - o ile takie sa
pozdro