SHARE
TWEET

Untitled

a guest Apr 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. PRACA DOMOWA:
  3.  
  4. 1) Do klasy Stat doda� metod� licz�c� odchylenie standardowe (StdDev) = sqrt[x^2 - x^2], x^2 = sumaE*x^2 / n
  5.  
  6. 2a) Napisz klase A, ktora ma dwa pola x i y  i funkcja ktora dodaje x+y
  7. 2b) Klasa B dziedziczy klase A. Klasa B ma funkcje, ktora mnozy x*y
  8.  
  9. */
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <math.h>
  14.  
  15. using namespace std;
  16.  
  17. //================================================
  18.  
  19. class Array
  20. {
  21. protected:
  22.     int n;
  23.     double * tab;
  24. public:
  25.     Array();
  26.     ~Array();
  27.     void Wstaw(istream &plik);
  28.     void Wstaw(const char* fname);
  29.     int N() { return n;}
  30.     double &operator[](int i);
  31.     void Display(ostream &os);
  32.     void Display(const char * fname);
  33. };
  34.  
  35. class Stat: public Array
  36. {
  37. public:
  38.     double Suma();
  39.     double Srednia();
  40.     double StdDev();
  41.     Stat();
  42. };
  43.  
  44. //================================================
  45.  
  46. class A
  47. {
  48. protected:
  49.     double x,y;
  50. public:
  51.     A();
  52.     A(int x, int y) { this->x = x; this->y = y; };
  53.     double Suma() { return x+y; };
  54. };
  55.  
  56. class B: public A
  57. {
  58. public:
  59.     B(int x1, int y1): A(x1, y1) {} ;
  60.     double Iloczyn() { return x*y; }
  61. };
  62. //================================================
  63. int main()
  64. {
  65.     Stat S;
  66.     ifstream pl("tablica1.txt");
  67.     S.Wstaw(pl);
  68.     double sum = S.Suma();
  69.     double sr = S.Srednia();
  70.     double odchstd = S.StdDev();
  71.     cout << "======================================================\nLiczby w tablicy: " << endl;
  72.     S.Display(cout);
  73.     cout << "======================================================" << endl;
  74.     cout << "Suma: " << sum << ", srednia: " << sr << ", odchylenie standardowe: " << odchstd <<  endl;
  75.     cout << "======================================================" << endl;
  76.  
  77.  
  78.     cout << "\n\n\n======================================================" << endl;
  79.     cout << "Podaj 2 liczby: \n";
  80.     double x,y;
  81.     cin >> x >> y;
  82.     B liczby(x, y);
  83.     cout << "======================================================" << endl;
  84.     cout << "Suma: " << liczby.Suma() << ", iloczyn: " << liczby.Iloczyn() << "\n";
  85.     cout << "======================================================" << endl;
  86.  
  87.  
  88.  
  89.     pl.clear();
  90.     pl.close();
  91.     return 0;
  92. }
  93.  
  94. //================== ARRAY CLASS =================
  95.  
  96. Array::Array(): n(0), tab(0) {}
  97.  
  98. Array::~Array()
  99. {
  100.     if(tab)delete[] tab;
  101. }
  102.  
  103. void Array::Wstaw(istream &plik)
  104. {
  105.     if(tab)
  106.     {
  107.         delete[] tab;
  108.         tab = 0, n = 0;
  109.     }
  110.     plik >> n;
  111.     if(n<1) throw "n-error";
  112.     tab = new double[n];
  113.     for(int i=0; i<n; i++) plik >> tab[i];
  114. }
  115.  
  116. void Array::Wstaw(const char* fname)
  117. {
  118.     if(tab)
  119.     {
  120.         delete[] tab;
  121.         tab = 0, n = 0;
  122.     }
  123.     FILE * file = fopen(fname, "rt");
  124.     fscanf(file, "%d", &n);
  125.     if(n<1) throw "n-error";
  126.     tab = new double[n];
  127.     for(int i=0; i<n; i++)
  128.     {
  129.         fscanf(file, "%lf", &tab[i]);
  130.     }
  131. }
  132.  
  133. double &Array::operator[](int i)
  134. {
  135. if(i>=n || i<0) throw "i-error";
  136. return tab[i];
  137. }
  138.  
  139. void Array::Display(ostream &os)
  140. {
  141.     for(int i=0; i<n; i++)
  142.     {
  143.         os<< tab[i] << endl;
  144.     }
  145. }
  146.  
  147. void Array::Display(const char * fname)
  148. {
  149.     FILE* pf = fopen(fname, "wt");
  150.     for(int i=0; i<this->N(); i++)
  151.     fprintf(pf, "%12.3lf\n", tab[i]);
  152.     fclose(pf);
  153. }
  154.  
  155. //================== STAT CLASS ==================
  156.  
  157. Stat::Stat(): Array() { }
  158.  
  159. double Stat::Suma()
  160. {
  161.     double wynik=0.0;
  162.     for(int i=0; i<n; i++) wynik+=tab[i];
  163.     return wynik;
  164. }
  165.  
  166. double Stat::Srednia()
  167. {
  168.     return Suma()/n;
  169. }
  170.  
  171. double Stat::StdDev()
  172. {
  173.     double mianownik = Srednia();
  174.     double licznik = 0;
  175.  
  176.     for(int i=0; i<n; i++)
  177.     {
  178.         licznik+=pow(tab[i]-mianownik, 2);
  179.     }
  180.     if(mianownik!=0) return sqrt(licznik/mianownik);
  181.     else return -1;
  182. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top