Advertisement
Marisichka

Untitled

Dec 17th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.41 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 convert(double c, double dc, double cMin) { return round((c - cMin) / dc); }
  152.  
  153. void edit(double* x, double* y, int n, string xLabel, string yLabel, string xSystem, string ySystem) {
  154.     string xLabelG;
  155.     string yLabelG;
  156.  
  157.     //навіщ
  158.  
  159.     int w = getmaxx();
  160.     int h = getmaxy();
  161.  
  162.     double dx = (xMax - xMin) / w;
  163.     double dy = (yMax - yMin) / (h - 10);
  164.  
  165.     string label = "Result";
  166.     char labelBkp[STR_LEN];
  167.  
  168.     int i;
  169.  
  170.     setbkcolor(WHITE);
  171.     setlinestyle(SOLID_LINE, 0, 5);
  172.     cleardevice();
  173.     setcolor(RED);
  174.  
  175.     for (i = 0; i < n - 1; i++) {
  176.         line(convert(x[i], dx, xMin),
  177.             h - convert(y[i], dy, yMin),
  178.             convert(x[i + 1], dx, xMin),
  179.             h - convert(y[i + 1], dy, yMin));
  180.     }
  181.  
  182.     setcolor(BLUE);
  183.  
  184.     outtextxy(w / 2 - 10 * label.length(), 0, &label[0]);
  185.  
  186.     xLabelG = xLabel + ", " + xSystem;
  187.     yLabelG = yLabel + ", " + ySystem;
  188.  
  189.     outtextxy(0, 0, &yLabelG[0]);
  190.     outtextxy(w - 10 * xLabelG.length(), h - 20, &xLabelG[0]);
  191.  
  192.     setcolor(GREEN);
  193.  
  194.     snprintf(labelBkp, STR_LEN, "%lg", y[0]);
  195.  
  196.     outtextxy(0, h - 20, labelBkp);
  197.  
  198.     snprintf(labelBkp, STR_LEN, "%lg", y[n - 1]);
  199.  
  200.     outtextxy(0, 20, labelBkp);
  201.  
  202.     snprintf(labelBkp, STR_LEN, "%lg", (y[0] + y[n - 1]) / 2);
  203.  
  204.     outtextxy(0, h / 2, labelBkp);
  205.  
  206.     snprintf(labelBkp, STR_LEN, "%lg", x[0]);
  207.  
  208.     outtextxy(20, h - 30, labelBkp);
  209.  
  210.     snprintf(labelBkp, STR_LEN, "%lg", x[n - 1]);
  211.  
  212.     outtextxy(w - 30, h - 30, labelBkp);
  213.  
  214.     snprintf(labelBkp, STR_LEN, "%lg", (x[0] + x[n - 1]) / 2);
  215.  
  216.     outtextxy(w / 2, h - 30, labelBkp);
  217. }
  218.  
  219. void tabulate(Force data, double dp, double p0, double p1,
  220.     string xLabel, string yLabel, string xSystem, string ySystem, ofstream& ofs) {
  221.     double p = p0;
  222.     int n = ((p1 - p0) / dp) + 1, i = 0; // кількість точок
  223.     double x[n], y[n];
  224.  
  225.     cout.precision(3);
  226.     cout << scientific;
  227.  
  228.     ofs.precision(3);
  229.     ofs << scientific;
  230.  
  231.     do {
  232.         x[i] = p;
  233.  
  234.         data.p0 = p;
  235.         data.calc();
  236.  
  237.         y[i] = data.calc;
  238.  
  239.         p += dp;
  240.  
  241.         cout << xLabel << " = " << x[i] << endl << yLabel << " = " << y[i] << endl << endl;
  242.         ofs << xLabel << " = " << x[i] << endl << yLabel << " = " << y[i] << endl;
  243.  
  244.         i++;
  245.     } while (p <= (p1 + 1e-7));
  246.  
  247.     edit(x, y, n, xLabel, yLabel, xSystem, ySystem);
  248.     _getch();
  249. }
  250.  
  251. int main() {
  252.  
  253.     cout << "TEST BY CONSTRUCTORS" << endl;
  254.     Force x;
  255.     cout << "Default constructor:" << endl << x;
  256.     Force y(1.37, 1000./1500, 900./1700, 140./300);
  257.     double yRes = y.calc();
  258.     cout << "Init constructor:" << endl << y;
  259.     Force z = y;
  260.     cout << "Copy constructor:" << endl << z;
  261.  
  262.     cout << "Operator ==" << endl;
  263.     cout << "Output: " << boolalpha << (x == y) << endl << endl;
  264.  
  265.     cout << "Operator !=" << endl;
  266.     cout << "Output: " << boolalpha << (x != y) << endl << endl;
  267.  
  268.     cout << "Operator < " << endl;
  269.     cout << "Output: " << boolalpha << (x < y) << endl << endl;
  270.  
  271.     cout << "Operator <=" << endl;
  272.     cout << "Output: " << boolalpha << (x <= y) << endl << endl;
  273.  
  274.     cout << "Operator >" << endl;
  275.     cout << "Output: " << boolalpha << (x > y) << endl << endl;
  276.  
  277.     cout << "Operator >=" << endl;
  278.     cout << "Output: " << boolalpha << (x >= y) << endl << endl;
  279.  
  280.     cout << "Operator =" << endl;
  281.     x = y;
  282.     cout << "X object:" << endl;
  283.     x.get();
  284.     cout << "Y object:" << endl;
  285.     y.get();
  286.  
  287.  
  288.     cout << endl << endl << "TEST BY OPERATORS" << endl << endl;
  289.     cout << "X object:" << endl;
  290.     x.get();
  291.     cout << "Y object:" << endl;
  292.     y.get();
  293.  
  294.  
  295.     cout << endl << endl << "TEST BY FUNCTIONS" << endl << endl;
  296.     cout << "Set function:" << endl;
  297.     x.set();
  298.     cout << "Get function:" << endl;
  299.     x.get();
  300.     x.calc();
  301.     cout << "Result function:" << endl;
  302.     x.getResult();
  303.  
  304.     Force a;
  305.     ifstream ifs("inout.txt");
  306.     ofstream ofs("output.txt");
  307.  
  308.     ifs >> a;
  309.     double aRes = a.calc();
  310.  
  311.     cout << a << aRes;
  312.     ofs << a << aRes;
  313.  
  314.     return 0;
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement