Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Urzadzenie
- {
- string Nazwa;
- protected:
- Urzadzenie(string _Nazwa){ Nazwa = _Nazwa; }
- public:
- virtual int Moc()=0;
- virtual float Cena()=0;
- void Informacja()
- {
- if (Nazwa.length() > 8)
- throw 1;
- cout << Nazwa << endl;
- cout << "moc:" << Moc() << endl;
- cout << "cena: " << Cena() << endl;
- //Moc();
- //Cena();
- }
- };
- class Gofry
- {
- public:
- Gofry(int x)
- {
- if (x < 0)
- throw 1;
- }
- };
- class Zasilacz :public Urzadzenie
- {
- int MocZasilacza;
- int IloscWentylatorow;
- Gofry gofry;
- public:
- Zasilacz(int _MocZasilacza, int _IloscWentylatorow, string _Nazwa)
- try : gofry(-1), IloscWentylatorow(_IloscWentylatorow), Urzadzenie(_Nazwa)
- {
- if (_MocZasilacza >= 300)
- {
- //throw "Nie udalo sie zainicjalizowac obiektu";
- throw 1;
- }
- else
- {
- MocZasilacza = _MocZasilacza;
- }
- }
- catch (...)
- {
- printf("jak to ujemne gofry\n");
- }
- virtual int Moc()
- {
- return MocZasilacza;
- }
- virtual float Cena()
- {
- int wartosc1 = (MocZasilacza / 50) * 45;
- int wartosc2_i = IloscWentylatorow * 25;
- return wartosc1 + wartosc2_i;
- }
- };
- class PlytaGlowna :public Urzadzenie
- {
- int LiczbaProcesorow;
- int IloscSATA;
- int IloscPCI;
- public:
- PlytaGlowna(int _LiczbaProcesowrow, int _LiczbaSATA, int _LiczbaPCI, string _Nazwa)
- :LiczbaProcesorow(_LiczbaProcesowrow), IloscSATA(_LiczbaSATA), IloscPCI(_LiczbaPCI), Urzadzenie(_Nazwa){}
- virtual int Moc()
- {
- return ((LiczbaProcesorow * 100) + (IloscPCI * 20) + (IloscSATA * 10));
- }
- virtual float Cena()
- {
- return (300 + (LiczbaProcesorow * 400) + (IloscPCI * 50) + (IloscSATA * 20));
- }
- };
- class DyskTwardy :public Urzadzenie
- {
- int Pojemnosc;
- public:
- DyskTwardy(int _Pojemnosc, string _Nazwa) : Pojemnosc(_Pojemnosc), Urzadzenie(_Nazwa){}
- virtual int Moc()
- {
- return 70;
- }
- virtual float Cena()
- {
- return (Pojemnosc < 200) ? 200 : Pojemnosc;;
- }
- };
- int main()
- {
- Urzadzenie **urzadzenia = NULL;
- char buffor[64];
- unsigned int size = 1;
- try
- {
- FILE *Plik = fopen("Text.txt", "r");
- if (Plik == NULL)
- throw "Nie udalo sie otworzyc pliku!";
- while (!feof(Plik))
- {
- fscanf(Plik, "%s", &buffor);
- urzadzenia = (Urzadzenie**)realloc(urzadzenia, sizeof(Urzadzenie*)*size);
- try
- {
- if (strcmp(buffor, "DyskTwardy") == 0)
- {
- int wartos1;
- fscanf(Plik, "%s %d", &buffor, &wartos1);
- urzadzenia[size - 1] = new DyskTwardy(wartos1, buffor);
- }
- else if (strcmp(buffor, "Zasilacz") == 0)
- {
- int wartos1, wartos2;
- fscanf(Plik, "%s %d %d", &buffor, &wartos1, &wartos2);
- urzadzenia[size - 1] = new Zasilacz(wartos1, wartos2, buffor);
- }
- else if (strcmp(buffor, "PlytaGlowna") == 0)
- {
- int wartos1, wartos2, wartos3;
- fscanf(Plik, "%s %d %d %d", &buffor, &wartos1, &wartos2, &wartos3);
- urzadzenia[size - 1] = new PlytaGlowna(wartos1, wartos2, wartos3, buffor);
- }
- size++;
- }
- catch (bad_alloc)
- {
- urzadzenia[size - 1] = NULL;
- }
- catch (int komunikat)
- {
- printf("Blad typu - %i\n", komunikat);
- }
- }
- fclose(Plik);
- }
- catch (char *komunikat)
- {
- printf("%s\n", komunikat);
- }
- if (urzadzenia != NULL)
- {
- for (unsigned int i = 0; i < size - 1; i++)
- {
- try
- {
- urzadzenia[i]->Informacja();
- }
- catch (...)
- {
- printf("Cos tam jest. \n");
- }
- }
- //Zwolnij pamiec
- for (unsigned int i = 0; i < size - 1; i++)
- {
- if(urzadzenia[i] != NULL) delete urzadzenia[i];
- }
- free(urzadzenia);
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement