Advertisement
Marisichka

Untitled

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