Advertisement
DacCum

Untitled

Nov 9th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4.  
  5. #define ERROR_DIV_ZERO   "ERROR: can not be divided by zero.\n"
  6. #define ERROR_CALC_PARAM   "ERROR: parameter was not calculated.\n"
  7. #define ERROR_OPEN_FILE   "ERROR: the file can not be opened.\n"
  8. #define ERROR_CREATE_FILE    "ERROR: the file can not be created.\n"
  9. #define inputfile  "input.txt"
  10. #define outputfile  "output.txt"
  11.  
  12. using namespace std;
  13.  
  14. class calculationObject{
  15.     protected:
  16.        
  17.         double *calculatedParameter;
  18.  
  19.     public:
  20.  
  21.         virtual void set() = 0;
  22.         virtual void calcParameter() = 0;
  23.         virtual void get() = 0;
  24.         virtual double getResult() = 0;
  25. };
  26. class Charge : virtual public calculationObject{
  27.     private:
  28.        
  29.         double q1, q2, eps, r;
  30.    
  31.     public:
  32.        
  33.         Charge() : calculationObject(){
  34.             q1 = q2 = eps = r = 0;
  35.             calculatedParameter = NULL;
  36.         }
  37.    
  38.         Charge(double _q1, double _q2, double _eps, double _r) : calculationObject(), q1(_q1), q2(_q2), eps(_eps), r(_r){}
  39.    
  40.         Charge(const Charge& x){
  41.             q1 = x.q1;
  42.             q2 = x.q2;
  43.             eps = x.eps;
  44.             r = x.r;
  45.         }
  46.         void set(){
  47.             cout << " please, enter information about charge  " << endl;
  48.             cout << " q1 = ";
  49.             cin >> q1;
  50.             cout << " q2 = ";
  51.             cin >> q2;
  52.             cout << " eps = ";
  53.             cin >> eps;
  54.             cout << " r = ";
  55.             cin >> r;
  56.             cout << endl;
  57.         }
  58.         void get(){
  59.             cout << " information about charge  " << endl;
  60.             cout << " q1 = " << q1 << endl;
  61.             cout << " q2 = " << q2 << endl;
  62.             cout << " eps = " << eps << endl;
  63.             cout << " r = " << r << endl;
  64.             cout << endl;
  65.         }
  66.    
  67.         void calcParameter(){
  68.             if(r == 0 || eps == 0) {
  69.                 cout << ERROR_DIV_ZERO;
  70.                 return;
  71.             }
  72.             calculatedParameter = new double;
  73.             *calculatedParameter = (q1 * q2) / (eps * pow(2.,r)) ;
  74.         }
  75.         double getResult(){
  76.             if(calculatedParameter == NULL) {
  77.                 cout << ERROR_CALC_PARAM << endl;
  78.                 return 0;
  79.             }
  80.             cout << " calculated parameter = " << *calculatedParameter << endl;
  81.             return *calculatedParameter;
  82.         }
  83.         Charge& operator = (const Charge& x){
  84.             q1 = x.q1;
  85.             q2 = x.q2;
  86.             eps = x.eps;
  87.             r = x.r;
  88.             return *this;
  89.         }
  90.     bool operator == (const Charge& x) {return (calculatedParameter == x.calculatedParameter);}
  91.     bool operator != (const Charge& x) {return (calculatedParameter != x.calculatedParameter);}
  92.     bool operator < (const Charge& x) {return (calculatedParameter < x.calculatedParameter);}
  93.     bool operator <= (const Charge& x) {return (calculatedParameter <= x.calculatedParameter);}
  94.     bool operator > (const Charge& x) {return (calculatedParameter > x.calculatedParameter);}
  95.     bool operator >= (const Charge& x) {return (calculatedParameter >= x.calculatedParameter);}
  96.  
  97.     friend ifstream& operator >> (ifstream&, Charge&);
  98.     friend ofstream& operator << (ofstream&, const Charge&);
  99.     friend istream& operator >> (istream&, Charge&);
  100.     friend ostream& operator << (ostream&, const Charge&);
  101. };
  102. ifstream& operator >> (ifstream& fin, Charge& x) {
  103.         if(!fin.is_open()){
  104.             cout << ERROR_OPEN_FILE;
  105.             return fin;
  106.         }
  107.         fin >> x.q1 >> x.q2 >> x.eps >> x.r;
  108.     return fin;
  109. }
  110. ofstream& operator << (ofstream& fout, const Charge& x) {
  111.         if(!fout.is_open()){
  112.             cout << ERROR_CREATE_FILE;
  113.             return fout;
  114.         }
  115.         fout << " information about charge:  " << endl;
  116.         fout << " q1 = " << x.q1 << endl;
  117.         fout << " q2 = " << x.q2 << endl;
  118.         fout << " eps = " << x.eps << endl;
  119.         fout << " r = " << x.r << endl;
  120.         fout << endl;
  121.     return fout;
  122. }
  123. istream& operator >> (istream& in, Charge& x) {
  124.         cout << " please, enter information about charge " << endl;
  125.         cout << " q1 = ";
  126.         in >> x.q1;
  127.         cout << " q2 = ";
  128.         in >> x.q2;
  129.         cout << " eps = ";
  130.         in >> x.eps;
  131.         cout << " r = ";
  132.         in >> x.r;
  133.         cout << endl;
  134.     return in;
  135. }
  136. ostream& operator << (ostream& out, const Charge& x) {
  137.         out << " information about charge  " << endl;
  138.         out << " q1 = " << x.q1 << endl;
  139.         out << " q2 = " << x.q2 << endl;
  140.         out << " eps = " << x.eps << endl;
  141.         out << " r = " << x.r << endl;
  142.         out << endl;
  143.     return out;
  144. }
  145. int main(){
  146.    
  147.         Charge x;
  148.         cout << endl << "Default constructor:" << endl;
  149.         cout << x;
  150.    
  151.         Charge y(5.3, 2, 0.3, 7);
  152.         cout << "Init constructor:" << endl;
  153.         cout << y;
  154.    
  155.         Charge z = y;
  156.         cout << " Copy constructor " << endl;
  157.         cout << z;
  158.    
  159.         cout << " Set function " << endl;
  160.         x.set();
  161.    
  162.         cout << " Get function " << endl;
  163.         x.get();
  164.    
  165.         x.calcParameter();
  166.         y.calcParameter();
  167.         cout << " Result function " << endl;
  168.         cout << " x ";
  169.         x.getResult();
  170.        
  171.         cout << endl << endl;
  172.         cout << " y ";
  173.         y.getResult();
  174.         cout << endl;
  175.         cout << " Operator == for x, y returned " << (x == y) << endl << endl;
  176.         cout << " Operator != for x, y returned " << (x != y) << endl << endl;
  177.         cout << " Operator < for x, y returned " << (x < y) << endl << endl;
  178.         cout << " Operator <= for x, y returned " << (x <= y) << endl << endl;
  179.         cout << " Operator > for x, y returned " << (x > y) << endl << endl;
  180.         cout << " Operator >= for x, y returned " << (x >= y) << endl << endl;
  181.        
  182.         cout << endl;
  183.         cout << " Reading input file" << endl;
  184.    
  185.         Charge a;
  186.         ifstream fin(inputfile);
  187.         ofstream fout(outputfile);
  188.    
  189.         fin >> a;
  190.         cout << a;
  191.         fout << a;
  192.         a.calcParameter();
  193.         fout << "calculated parameter = " << a.getResult() << endl;
  194.  
  195.     return 0;
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement