Vladislav_Bezruk

Untitled

Jan 11th, 2022
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4.  
  5. #define INPUT_FILE  "input.txt"
  6. #define OUTPUT_FILE "output.txt"
  7.  
  8. using namespace std;
  9.  
  10. class Vector {
  11.     private:
  12.         int n;
  13.        
  14.         float* data;
  15.        
  16.     public:
  17.         Vector() {
  18.             n = 0;
  19.             data = NULL;
  20.         }
  21.        
  22.         Vector(int n) { allocate(n); }
  23.        
  24.         ~Vector() { clear(); }
  25.        
  26.         void allocate(int n) {
  27.             this->n = n;
  28.             data = new float[n];
  29.         }
  30.        
  31.         void clear() {
  32.             this->n = 0;
  33.             delete data;
  34.         }
  35.        
  36.         void read(string filename) {
  37.             ifstream ifs(filename);
  38.            
  39.             assert(ifs && "File cannot open!!!");
  40.            
  41.             ifs >> *this;
  42.         }
  43.        
  44.         float calcAvg() {
  45.             float avg = 0.;
  46.            
  47.             for (int i = 0; i < n; i++)
  48.                 avg += data[i];
  49.            
  50.             assert(n && "n = 0!!!");
  51.            
  52.             return avg / n;
  53.         }
  54.        
  55.         float calcMult() {
  56.             bool flag = false;
  57.             float mult = 1., avg = calcAvg();
  58.            
  59.             for (int i = 0; i < n; i++)
  60.                 if (data[i] > avg) {
  61.                     flag = true;
  62.                     mult *= data[i];
  63.                 }
  64.             assert(flag && "There are no elements > avg!!!");  
  65.            
  66.             return mult;   
  67.         }
  68.        
  69.         void write(string filename) {
  70.             ofstream ofs(filename);
  71.            
  72.             assert(ofs && "File cannot create!!!");
  73.            
  74.             ofs << *this;
  75.         }
  76.        
  77.         friend ifstream& operator >> (ifstream& ifs, Vector& x);
  78.        
  79.         friend ofstream& operator << (ofstream& ofs, Vector& x);
  80.        
  81.         friend istream& operator >> (istream& is, Vector& x);
  82.        
  83.         friend ostream& operator << (ostream& os, Vector& x);
  84. };
  85.  
  86. ifstream& operator >> (ifstream& ifs, Vector& x) {
  87.     ifs >> x.n;
  88.     x.allocate(x.n);
  89.            
  90.     for (int i = 0; i < x.n; i++)
  91.         ifs >> x.data[i];
  92.        
  93.     return ifs;
  94. }
  95.  
  96. ofstream& operator << (ofstream& ofs, Vector& x) {
  97.     ofs << "n = " << x.n << endl;
  98.            
  99.     if (x.n) {
  100.         ofs << "data: ";
  101.         for (int i = 0; i < x.n; i++)
  102.             ofs << x.data[i] << " ";
  103.         ofs << endl;
  104.     }
  105.    
  106.     float avg = x.calcAvg();
  107.     float mult = x.calcMult();
  108.    
  109.     ofs << "avg = " << avg << endl;
  110.     ofs << "mult = " << mult << endl;
  111.    
  112.     return ofs;
  113. }
  114.  
  115. ostream& operator << (ostream& os, Vector& x) {
  116.     cout << "n = " << x.n << endl;
  117.            
  118.     if (x.n) {
  119.         cout << "data: ";
  120.         for (int i = 0; i < x.n; i++)
  121.             cout << x.data[i] << " ";
  122.         cout << endl;
  123.     }
  124.    
  125.     float avg = x.calcAvg();
  126.     float mult = x.calcMult();
  127.    
  128.     cout << "avg = " << avg << endl;
  129.     cout << "mult = " << mult << endl;
  130.    
  131.     return os;
  132. }
  133.        
  134. istream& operator >> (istream& is, Vector& x) {
  135.     cin >> x.n;
  136.     x.allocate(x.n);
  137.            
  138.     for (int i = 0; i < x.n; i++)
  139.         cin >> x.data[i];
  140.        
  141.     return is;
  142. }
  143.        
  144. int main() {
  145.     Vector x;
  146.    
  147.     x.read(INPUT_FILE);
  148.     x.write(OUTPUT_FILE);
  149.    
  150.     cout << x;
  151.    
  152.     return 0;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment