Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- PRACA DOMOWA:
- 1) Do klasy Stat doda� metod� licz�c� odchylenie standardowe (StdDev) = sqrt[x^2 - x^2], x^2 = sumaE*x^2 / n
- 2a) Napisz klase A, ktora ma dwa pola x i y i funkcja ktora dodaje x+y
- 2b) Klasa B dziedziczy klase A. Klasa B ma funkcje, ktora mnozy x*y
- */
- #include <iostream>
- #include <fstream>
- #include <math.h>
- using namespace std;
- //================================================
- class Array
- {
- protected:
- int n;
- double * tab;
- public:
- Array();
- ~Array();
- void Wstaw(istream &plik);
- void Wstaw(const char* fname);
- int N() { return n;}
- double &operator[](int i);
- void Display(ostream &os);
- void Display(const char * fname);
- };
- class Stat: public Array
- {
- public:
- double Suma();
- double Srednia();
- double StdDev();
- Stat();
- };
- //================================================
- class A
- {
- protected:
- double x,y;
- public:
- A();
- A(int x, int y) { this->x = x; this->y = y; };
- double Suma() { return x+y; };
- };
- class B: public A
- {
- public:
- B(int x1, int y1): A(x1, y1) {} ;
- double Iloczyn() { return x*y; }
- };
- //================================================
- int main()
- {
- Stat S;
- ifstream pl("tablica1.txt");
- S.Wstaw(pl);
- double sum = S.Suma();
- double sr = S.Srednia();
- double odchstd = S.StdDev();
- cout << "======================================================\nLiczby w tablicy: " << endl;
- S.Display(cout);
- cout << "======================================================" << endl;
- cout << "Suma: " << sum << ", srednia: " << sr << ", odchylenie standardowe: " << odchstd << endl;
- cout << "======================================================" << endl;
- cout << "\n\n\n======================================================" << endl;
- cout << "Podaj 2 liczby: \n";
- double x,y;
- cin >> x >> y;
- B liczby(x, y);
- cout << "======================================================" << endl;
- cout << "Suma: " << liczby.Suma() << ", iloczyn: " << liczby.Iloczyn() << "\n";
- cout << "======================================================" << endl;
- pl.clear();
- pl.close();
- return 0;
- }
- //================== ARRAY CLASS =================
- Array::Array(): n(0), tab(0) {}
- Array::~Array()
- {
- if(tab)delete[] tab;
- }
- void Array::Wstaw(istream &plik)
- {
- if(tab)
- {
- delete[] tab;
- tab = 0, n = 0;
- }
- plik >> n;
- if(n<1) throw "n-error";
- tab = new double[n];
- for(int i=0; i<n; i++) plik >> tab[i];
- }
- void Array::Wstaw(const char* fname)
- {
- if(tab)
- {
- delete[] tab;
- tab = 0, n = 0;
- }
- FILE * file = fopen(fname, "rt");
- fscanf(file, "%d", &n);
- if(n<1) throw "n-error";
- tab = new double[n];
- for(int i=0; i<n; i++)
- {
- fscanf(file, "%lf", &tab[i]);
- }
- }
- double &Array::operator[](int i)
- {
- if(i>=n || i<0) throw "i-error";
- return tab[i];
- }
- void Array::Display(ostream &os)
- {
- for(int i=0; i<n; i++)
- {
- os<< tab[i] << endl;
- }
- }
- void Array::Display(const char * fname)
- {
- FILE* pf = fopen(fname, "wt");
- for(int i=0; i<this->N(); i++)
- fprintf(pf, "%12.3lf\n", tab[i]);
- fclose(pf);
- }
- //================== STAT CLASS ==================
- Stat::Stat(): Array() { }
- double Stat::Suma()
- {
- double wynik=0.0;
- for(int i=0; i<n; i++) wynik+=tab[i];
- return wynik;
- }
- double Stat::Srednia()
- {
- return Suma()/n;
- }
- double Stat::StdDev()
- {
- double mianownik = Srednia();
- double licznik = 0;
- for(int i=0; i<n; i++)
- {
- licznik+=pow(tab[i]-mianownik, 2);
- }
- if(mianownik!=0) return sqrt(licznik/mianownik);
- else return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement