DacCum

Untitled

Nov 22nd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.72 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.  // похідний від абстрактного клас
  27. class Charge : virtual public calculationObject{
  28.     private:
  29.         // елементи даних класу
  30.         double q1, q2, eps, r;
  31.    
  32.     public:
  33.         //конструктори: пустий з ініцілізацією,копіювання
  34.         Charge() : calculationObject(){
  35.             q1 = q2 = eps = r = 0;
  36.             calculatedParameter = NULL;
  37.         }
  38.    
  39.         Charge(double _q1, double _q2, double _eps, double _r) : calculationObject(), q1(_q1), q2(_q2), eps(_eps), r(_r){}
  40.    
  41.         Charge(const Charge& x){
  42.             q1 = x.q1;
  43.             q2 = x.q2;
  44.             eps = x.eps;
  45.             r = x.r;
  46.         }
  47.         void set(){
  48.             cout << " please, enter information about charge  " << endl;
  49.             cout << " q1 = ";
  50.             cin >> q1;
  51.             if( q1 <= 1 * pow(10, -9) || q1 >= 2 * pow(10, -9)){
  52.                 cout << " numbers are out of range ";
  53.             exit(1);
  54.             }
  55.             cout << " q2 = ";
  56.             cin >> q2;
  57.             if( q2 <= 1 * pow(10, -9) || q2 >= 2 * pow(10, -9)){
  58.                 cout << " numbers are out of range ";
  59.             exit(1);
  60.             }
  61.             cout << " eps = ";
  62.             cin >> eps;
  63.             if( eps <= 12 * pow(10, -11) || eps >= 15 * pow(10, -11)){
  64.                 cout << " numbers are out of range ";
  65.             exit(1);
  66.             }
  67.             cout << " r = ";
  68.             cin >> r;
  69.              if( r <= 1 * pow(10, -4) || r >= 3 * pow(10, -4)){
  70.                 cout << " numbers are out of range ";
  71.             exit(1);
  72.             }
  73.             cout << endl;
  74.         }
  75.         void get(){
  76.             cout << " information about charge  " << endl;
  77.             cout << " q1 = " << q1 << endl;
  78.             cout << " q2 = " << q2 << endl;
  79.             cout << " eps = " << eps << endl;
  80.             cout << " r = " << r << endl;
  81.             cout << endl;
  82.         }
  83.     // обрахунок сили взаємодії двох точковиї зарядів
  84.         void calcParameter(){
  85.             if(r == 0 || eps == 0) {
  86.                 cout << ERROR_DIV_ZERO;
  87.                 return;
  88.             }
  89.             calculatedParameter = new double;
  90.             *calculatedParameter = (q1 * q2) / (eps * pow(2.,r)) ;
  91.         }
  92.         double getResult(){
  93.             if(calculatedParameter == NULL) {
  94.                 cout << ERROR_CALC_PARAM << endl;
  95.                 return 0;
  96.             }
  97.             cout << " calculated parameter = " << *calculatedParameter << endl;
  98.             return *calculatedParameter;
  99.         }
  100.         Charge& operator = (const Charge& x){
  101.             q1 = x.q1;
  102.             q2 = x.q2;
  103.             eps = x.eps;
  104.             r = x.r;
  105.             return *this;
  106.         }
  107.    
  108.     friend void changes_r(ofstream&, Charge*, double, double, double);
  109.        
  110.    // перевантажені оператори
  111.     bool operator == (const Charge& x) {return (calculatedParameter == x.calculatedParameter);}
  112.     bool operator != (const Charge& x) {return (calculatedParameter != x.calculatedParameter);}
  113.     bool operator < (const Charge& x) {return (calculatedParameter < x.calculatedParameter);}
  114.     bool operator <= (const Charge& x) {return (calculatedParameter <= x.calculatedParameter);}
  115.     bool operator > (const Charge& x) {return (calculatedParameter > x.calculatedParameter);}
  116.     bool operator >= (const Charge& x) {return (calculatedParameter >= x.calculatedParameter);}
  117.  
  118.     friend ifstream& operator >> (ifstream&, Charge&);
  119.     friend ofstream& operator << (ofstream&, const Charge&);
  120.     friend istream& operator >> (istream&, Charge&);
  121.     friend ostream& operator << (ostream&, const Charge&);
  122. };
  123. ifstream& operator >> (ifstream& fin, Charge& x) {
  124.         if(!fin.is_open()){
  125.             cout << ERROR_OPEN_FILE;
  126.             return fin;
  127.         }
  128.         fin >> x.q1 >> x.q2 >> x.eps >> x.r;
  129.     return fin;
  130. }
  131. ofstream& operator << (ofstream& fout, const Charge& x) {
  132.         if(!fout.is_open()){
  133.             cout << ERROR_CREATE_FILE;
  134.             return fout;
  135.         }
  136.         fout << " information about charge:  " << endl;
  137.         fout << " q1 = " << x.q1 << endl;
  138.         fout << " q2 = " << x.q2 << endl;
  139.         fout << " eps = " << x.eps << endl;
  140.         fout << " r = " << x.r << endl;
  141.         fout << endl;
  142.     return fout;
  143. }
  144. istream& operator >> (istream& in, Charge& x) {
  145.         cout << " please, enter information about charge " << endl;
  146.         cout << " q1 = ";
  147.         in >> x.q1;
  148.         cout << " q2 = ";
  149.         in >> x.q2;
  150.         cout << " eps = ";
  151.         in >> x.eps;
  152.         cout << " r = ";
  153.         in >> x.r;
  154.         cout << endl;
  155.     return in;
  156. }
  157. ostream& operator << (ostream& out, const Charge& x) {
  158.         out << " information about charge  " << endl;
  159.         out << " q1 = " << x.q1 << endl;
  160.         out << " q2 = " << x.q2 << endl;
  161.         out << " eps = " << x.eps << endl;
  162.         out << " r = " << x.r << endl;
  163.         out << endl;
  164.     return out;
  165. }
  166.  
  167. void changes_r(ofstream& fout, Charge* x, double r0, double rn, double dr) {
  168.     x->r = r0;
  169.     fout << endl;
  170.     while(x->r <= rn){
  171.         x->calcParameter();
  172.         fout << "F(r("<< x->r <<")) = " << *x->calculatedParameter << endl;
  173.         cout << "F(r("<< x->r <<")) = " << *x->calculatedParameter << endl;
  174.         x->r += dr;    
  175.     }
  176. }
  177.  
  178. int main(){
  179.    
  180.         Charge x;
  181.         cout << endl << "Default constructor:" << endl;
  182.         cout << x;
  183.    
  184.         Charge y(5.3, 2, 0.3, 7);
  185.         cout << "Init constructor:" << endl;
  186.         cout << y;
  187.    
  188.         Charge z = y;
  189.         cout << " Copy constructor " << endl;
  190.         cout << z;
  191.    
  192.         cout << " Set function " << endl;
  193.         x.set();
  194.    
  195.         cout << " Get function " << endl;
  196.         x.get();
  197.    
  198.         x.calcParameter();
  199.         y.calcParameter();
  200.         cout << " Result function " << endl;
  201.         cout << " x ";
  202.         x.getResult();
  203.        
  204.         cout << endl << endl;
  205.         cout << " y ";
  206.         y.getResult();
  207.         cout << endl;
  208.         cout << " Operator == for x, y returned " << (x == y) << endl << endl;
  209.         cout << " Operator != for x, y returned " << (x != y) << endl << endl;
  210.         cout << " Operator < for x, y returned " << (x < y) << endl << endl;
  211.         cout << " Operator <= for x, y returned " << (x <= y) << endl << endl;
  212.         cout << " Operator > for x, y returned " << (x > y) << endl << endl;
  213.         cout << " Operator >= for x, y returned " << (x >= y) << endl << endl;
  214.        
  215.         cout << endl;
  216.         cout << " Reading input file" << endl;
  217.    
  218.         Charge a;
  219.         ifstream fin(inputfile);
  220.         ofstream fout(outputfile);
  221.        
  222.         fin >> a;
  223.         cout << a;
  224.         fout << a;
  225.         a.calcParameter();
  226.         fout << "calculated parameter = " << a.getResult() << endl;
  227.        
  228.         double dr; //крок для відстані
  229.         fin >> dr;
  230.         changes_r(fout, &a, 0.01, 0.1, dr);
  231.        
  232.        
  233.     return 0;
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment