Advertisement
Marisichka

Untitled

Nov 30th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. #define M_PI 3.1415926
  8.  
  9. class Evaluate {
  10.  
  11. protected:
  12.     double formula;
  13. public:
  14.     virtual void set() = 0;
  15.     virtual void get() = 0;
  16.     virtual double calc() = 0;
  17.     virtual void getResult() = 0;
  18. };
  19.  
  20. class Force : public Evaluate {
  21. private:
  22.     double k, T, R, F;
  23. public:
  24.     Force(){
  25.         k = 0, T = 0, R = 0, F = 0;
  26.     }
  27.     Force(double _k, double _T, double _R, double _F) :k(_k), T(_T), R(_R), F(_F) {}
  28.     Force(const Force& x) {
  29.         k = x.k;
  30.         T = x.T;
  31.         R = x.R;
  32.         F = x.F;
  33.      
  34.     }
  35.     void set() {
  36.         cout << "Enter information about: " << endl;
  37.  
  38.         cout << "k = ";
  39.         cin >> k;
  40.         cout << endl;
  41.         cout << "T = ";    
  42.         cin >> T;
  43.         cout << endl;
  44.         cout << "R = ";
  45.         cin >> R;
  46.         cout << endl;
  47.         cout << "△F = ";    
  48.         cin >> F;
  49.      
  50.         cout << endl << endl;
  51.     }
  52.     void get() {
  53.         cout << "Information about" << endl;
  54.         cout << "k = " << k;
  55.         cout << endl;
  56.         cout << "T = " << T;
  57.         cout << endl;
  58.         cout << "R = " << R;
  59.         cout << endl;
  60.         cout << "△F = " << F;
  61.         cout << endl << endl;
  62.     }
  63.     double calc() {
  64.  
  65.         formula = sqrt(4*k*T*R*F);
  66.         return formula;
  67.     }
  68.     void getResult() {
  69.         cout << "Evaluated object: " << formula << endl;
  70.     }
  71.     Force& operator = (const Force& x) {
  72.  
  73.         k = x.k;
  74.         T = x.T;
  75.         R = x.R;
  76.         F = x.F;
  77.  
  78.         return *this;
  79.     }
  80.  
  81.     bool operator == (const Force& x) {
  82.         return formula == x.formula;
  83.     }
  84.     bool operator != (const Force& x) {
  85.         return formula != x.formula;
  86.     }
  87.     bool operator > (const Force& x) {
  88.         return formula > x.formula;
  89.     }
  90.     bool operator < (const Force& x) {
  91.         return formula < x.formula;
  92.     }
  93.     bool operator >= (const Force& x) {
  94.         return formula >= x.formula;
  95.     }
  96.     bool operator <= (const Force& x) {
  97.         return formula <= x.formula;
  98.     }
  99.  
  100.     friend ifstream& operator >> (ifstream& ifs, Force& x);
  101.     friend ofstream& operator << (ofstream& ofs, Force& x);
  102.     friend istream& operator >> (istream& is, Force& x);
  103.     friend ostream& operator << (ostream& os, Force& x);
  104. };
  105.  
  106. ifstream& operator >> (ifstream& ifs, Force& x) {
  107.     ifs >> x.k >> x.T >> x.R >> x.F;
  108.     return ifs;
  109. }
  110.  
  111. ofstream& operator << (ofstream& ofs, Force& x) {
  112.     ofs << "Information about:" << endl;
  113.     ofs << "k = " << x.k;
  114.     ofs << endl;
  115.     ofs << "T = " << x.T;
  116.     ofs << endl;
  117.     ofs << "R = " << x.R;
  118.     ofs << endl;
  119.     ofs << "△F = " << x.F;
  120.     ofs << endl << endl;
  121.     return ofs;
  122. }
  123.  
  124. istream& operator >> (istream& is, Force& x) {
  125.  
  126.     cout << "Information about: " << endl;
  127.     cout << "k = ";        
  128.     is >> x.k;
  129.     cout << endl;
  130.     cout << "T = ";        
  131.     is >> x.T;
  132.     cout << endl;
  133.     cout << "R = ";    
  134.     is >> x.R;
  135.     cout << endl;
  136.     cout << "△F = ";        
  137.     is >> x.F;
  138.     cout << endl << endl;
  139.     return is;
  140. }
  141.  
  142. ostream& operator << (ostream& os, Force& x) {
  143.     os << "Information about:" << endl;
  144.     os << "k = " << x.k << endl;
  145.     os << "T = " << x.T << endl;
  146.     os << "R = " << x.R << endl;
  147.     os << "△F = " << x.F << endl << endl;
  148.     return os;
  149. }
  150.  
  151. int main() {
  152.  
  153.     cout << "TEST BY CONSTRUCTORS" << endl;
  154.     Force x;
  155.     cout << "Default constructor:" << endl << x;
  156.     Force y(1.37, 1000/1500, 900/1700, 140/300);
  157.     double yRes = y.calc();
  158.     cout << "Init constructor:" << endl << y;
  159.     Force z = y;
  160.     cout << "Copy constructor:" << endl << z;
  161.  
  162.     cout << "Operator ==" << endl;
  163.     cout << "Output: " << boolalpha << (x == y) << endl << endl;
  164.  
  165.     cout << "Operator !=" << endl;
  166.     cout << "Output: " << boolalpha << (x != y) << endl << endl;
  167.  
  168.     cout << "Operator < " << endl;
  169.     cout << "Output: " << boolalpha << (x < y) << endl << endl;
  170.  
  171.     cout << "Operator <=" << endl;
  172.     cout << "Output: " << boolalpha << (x <= y) << endl << endl;
  173.  
  174.     cout << "Operator >" << endl;
  175.     cout << "Output: " << boolalpha << (x > y) << endl << endl;
  176.  
  177.     cout << "Operator >=" << endl;
  178.     cout << "Output: " << boolalpha << (x >= y) << endl << endl;
  179.  
  180.     cout << "Operator =" << endl;
  181.     x = y;
  182.     cout << "X object:" << endl;
  183.     x.get();
  184.     cout << "Y object:" << endl;
  185.     y.get();
  186.  
  187.  
  188.     cout << endl << endl << "TEST BY OPERATORS" << endl << endl;
  189.     cout << "X object:" << endl;
  190.     x.get();
  191.     cout << "Y object:" << endl;
  192.     y.get();
  193.  
  194.  
  195.     cout << endl << endl << "TEST BY FUNCTIONS" << endl << endl;
  196.     cout << "Set function:" << endl;
  197.     x.set();
  198.     cout << "Get function:" << endl;
  199.     x.get();
  200.     x.calc();
  201.     cout << "Result function:" << endl;
  202.     x.getResult();
  203.  
  204.     Force a;
  205.     ifstream ifs("inout.txt");
  206.     ofstream ofs("output.txt");
  207.  
  208.     ifs >> a;
  209.     double aRes = a.calc();
  210.  
  211.     cout << a << aRes;
  212.     ofs << a << aRes;
  213.  
  214.     return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement