Advertisement
Vladislav_Bezruk

Untitled

Dec 17th, 2021
1,410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.35 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;
  24.  
  25.         void checkParams() {
  26.             assert(k > 0 && T > 0 && R > 0 && F > 0 &&
  27.                    T >= 1000 && T <= 1500 && R >= 900 && R <= 1700 &&
  28.                    F >= 140 && F <= 300);
  29.         }
  30.  
  31.     public:
  32.         Force() {
  33.             k = 0, T = 0, R = 0, F = 0;
  34.         }
  35.  
  36.         Force(double _k, double _T, double _R, double _F) :k(_k), T(_T), R(_R), F(_F) {}
  37.         Force(const Force& x) {
  38.             k = x.k;
  39.             T = x.T;
  40.             R = x.R;
  41.             F = x.F;
  42.  
  43.         }
  44.         void set() {
  45.             cout << "Enter information about: " << endl;
  46.  
  47.             cout << "k = ";
  48.             cin >> k;
  49.             cout << endl;
  50.             cout << "T = ";
  51.             cin >> T;
  52.             cout << endl;
  53.             cout << "R = ";
  54.             cin >> R;
  55.             cout << endl;
  56.             cout << "△F = ";
  57.             cin >> F;
  58.  
  59.             cout << endl << endl;
  60.         }
  61.         void get() {
  62.             cout << "Information about" << endl;
  63.             cout << "k = " << k;
  64.             cout << endl;
  65.             cout << "T = " << T;
  66.             cout << endl;
  67.             cout << "R = " << R;
  68.             cout << endl;
  69.             cout << "△F = " << F;
  70.             cout << endl << endl;
  71.         }
  72.         double calc() {
  73.  
  74.             formula = sqrt(4*k*T*R*F);
  75.             return formula;
  76.         }
  77.         void getResult() {
  78.             cout << "Evaluated object: " << formula << endl;
  79.         }
  80.         Force& operator = (const Force& x) {
  81.  
  82.             k = x.k;
  83.             T = x.T;
  84.             R = x.R;
  85.             F = x.F;
  86.  
  87.             return *this;
  88.         }
  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.         bool operator < (const Force& x) {
  100.             return formula < x.formula;
  101.         }
  102.         bool operator >= (const Force& x) {
  103.             return formula >= x.formula;
  104.         }
  105.         bool operator <= (const Force& x) {
  106.             return formula <= x.formula;
  107.         }
  108.  
  109.         friend ifstream& operator >> (ifstream& ifs, Force& x);
  110.         friend ofstream& operator << (ofstream& ofs, Force& x);
  111.         friend istream& operator >> (istream& is, Force& x);
  112.         friend ostream& operator << (ostream& os, Force& x);
  113.         friend void tabulate(Force data, double dR, double R0, double R1, ofstream& ofs);
  114. };
  115.  
  116. ifstream& operator >> (ifstream& ifs, Force& x) {
  117.     ifs >> x.k >> x.T >> x.R >> x.F;
  118.     return ifs;
  119. }
  120.  
  121. ofstream& operator << (ofstream& ofs, Force& x) {
  122.     ofs << "Information about:" << endl;
  123.     ofs << "k = " << x.k;
  124.     ofs << endl;
  125.     ofs << "T = " << x.T;
  126.     ofs << endl;
  127.     ofs << "R = " << x.R;
  128.     ofs << endl;
  129.     ofs << "△F = " << x.F;
  130.     ofs << endl << endl;
  131.     return ofs;
  132. }
  133.  
  134. istream& operator >> (istream& is, Force& x) {
  135.  
  136.     cout << "Information about: " << endl;
  137.     cout << "k = ";
  138.     is >> x.k;
  139.     cout << endl;
  140.     cout << "T = ";
  141.     is >> x.T;
  142.     cout << endl;
  143.     cout << "R = ";
  144.     is >> x.R;
  145.     cout << endl;
  146.     cout << "△F = ";
  147.     is >> x.F;
  148.     cout << endl << endl;
  149.     return is;
  150. }
  151.  
  152. ostream& operator << (ostream& os, Force& x) {
  153.     os << "Information about:" << endl;
  154.     os << "k = " << x.k << endl;
  155.     os << "T = " << x.T << endl;
  156.     os << "R = " << x.R << endl;
  157.     os << "△F = " << x.F << endl << endl;
  158.     return os;
  159. }
  160.  
  161. void tabulate(Force data, double dR, double R0, double R1, ofstream& ofs) {
  162.     double R = R0;
  163.     int n = ((R1 - R0) / dR) + 1, i = 0; // кількість точок
  164.     double x[n], y[n];
  165.  
  166.     cout.precision(3);
  167.     cout << scientific;
  168.  
  169.     ofs.precision(3);
  170.     ofs << scientific;
  171.  
  172.     do {
  173.         x[i] = R;
  174.  
  175.         data.R = R;
  176.         data.calc();
  177.  
  178.         y[i] = data.calc();
  179.  
  180.         R += dR;
  181.  
  182.         cout << "R = " << x[i] << endl << "E = " << y[i] << endl << endl;
  183.         ofs << "R = " << x[i] << endl << "E = " << y[i] << endl << endl;
  184.  
  185.         i++;
  186.     } while (R <= (R1 + 1e-7)); // умова виходу із табулювання
  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