Vladislav_Bezruk

Untitled

Nov 18th, 2021 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <cassert>
  5. #include <graphics.h>
  6.  
  7. #define IN_FILE         "files/input.txt"
  8. #define OUT_FILE        "files/output.txt"
  9.  
  10. #define HEIGHT          720
  11. #define WIDTH           1280
  12. #define STR_LEN         99
  13.  
  14. #define DIV_ERROR       "Cannot be divided by zero!"
  15. #define SQRT_ERROR      "Cannot take the root of a negative number!"
  16. #define OPEN_ERROR      "The file cannot be opened!"
  17. #define CREATE_ERROR    "The file cannot be created!"
  18. #define DATA_ERROR      "Invalid data!"
  19.  
  20. #define p0A             3
  21. #define p0B             5
  22. #define TA              400
  23. #define TB              600
  24.  
  25. using namespace std;
  26.  
  27. class CompObj {
  28.     protected:
  29.         double calcPar;
  30.    
  31.     public:
  32.         virtual void set() = 0;
  33.    
  34.         virtual void get() = 0;
  35.    
  36.         virtual void calc() = 0;
  37.    
  38.         virtual void getResult() = 0;
  39. };
  40.  
  41. class PerfectGas : virtual public CompObj {
  42.     private:
  43.         double R;
  44.         double T;
  45.         double Kg;
  46.         double M;
  47.         double p;
  48.         double p0;
  49.         double K;
  50.        
  51.     void checkParams() {
  52.         assert(R > 0 && T >= TA && TB >= T && M > 0
  53.                  && p0 > p && p > 0 && p0 >= p0A
  54.                  && p0B >= p0 && K > 0 && DATA_ERROR);
  55.     }
  56.  
  57.     public:
  58.         PerfectGas() { R = T = Kg = M = p = p0 = K = 0; }
  59.    
  60.         PerfectGas(double _R, double _T, double _K, double _M, double _p, double _p0) : R(_R), T(_T), K(_K), M(_M), p(_p), p0(_p0) {checkParams();}
  61.    
  62.         PerfectGas(const PerfectGas& x) {
  63.             R   = x.R;
  64.             T   = x.T;
  65.             Kg  = x.Kg;
  66.             M   = x.M;
  67.             p   = x.p;
  68.             p0  = x.p0;
  69.             K   = x.K;
  70.             checkParams();
  71.         }
  72.    
  73.         void set() {
  74.             cout << "Enter info about perfect gas:" << endl;
  75.             cout << " R = ";    cin >> R;
  76.             cout << " T = ";    cin >> T;
  77.             cout << " M = ";    cin >> M;
  78.             cout << " p = ";    cin >> p;
  79.             cout << " p0 = ";   cin >> p0;
  80.             cout << " K = ";    cin >> K;
  81.             cout << endl;
  82.             checkParams();
  83.         }
  84.    
  85.         void get() {
  86.             cout << "Info about perfect gas:" << endl;
  87.             cout << " R = " << R << endl;
  88.             cout << " T = " << T << endl;
  89.             cout << " Kg = " << Kg << endl;
  90.             cout << " M = " << M << endl;
  91.             cout << " p = " << p << endl;
  92.             cout << " p0 = " << p0 << endl;
  93.             cout << " K = " << K << endl;
  94.             cout << endl;
  95.         }
  96.    
  97.         void calc() {
  98.             assert(Kg * M && p0 && DIV_ERROR);
  99.    
  100.             calcPar = (2 * R * T / (Kg * M)) * (1 - pow(p / p0, Kg));
  101.    
  102.             assert(calcPar >= 0 && SQRT_ERROR);
  103.    
  104.             calcPar = sqrt(calcPar);
  105.         }
  106.    
  107.         void calcKg() {
  108.             assert(K && DIV_ERROR);
  109.    
  110.             Kg = (K - 1) / K;
  111.         }
  112.    
  113.         void getResult() {
  114.             cout << "Calculated parameter = " << calcPar << endl;
  115.             cout << endl;
  116.         }
  117.    
  118.         PerfectGas& operator = (const PerfectGas& x) {
  119.             R   = x.R;
  120.             T   = x.T;
  121.             Kg  = x.Kg;
  122.             M   = x.M;
  123.             p   = x.p;
  124.             p0  = x.p0;
  125.             K   = x.K;
  126.             checkParams();
  127.             return *this;
  128.         }
  129.    
  130.         bool operator == (const PerfectGas& x) {return calcPar == x.calcPar;}
  131.    
  132.         bool operator != (const PerfectGas& x) {return calcPar != x.calcPar;}
  133.    
  134.         bool operator < (const PerfectGas& x) {return calcPar < x.calcPar;}
  135.    
  136.         bool operator <= (const PerfectGas& x) {return calcPar <= x.calcPar;}
  137.    
  138.         bool operator > (const PerfectGas& x) {return calcPar > x.calcPar;}
  139.    
  140.         bool operator >= (const PerfectGas& x) {return calcPar >= x.calcPar;}
  141.    
  142.         friend ifstream& operator >> (ifstream& ifs, PerfectGas& x);
  143.    
  144.         friend ofstream& operator << (ofstream& ofs, const PerfectGas& x);
  145.    
  146.         friend istream& operator >> (istream& is, PerfectGas& x);
  147.    
  148.         friend ostream& operator << (ostream& os, const PerfectGas& x);
  149.        
  150.         friend void tabulate(PerfectGas data, double dp, double p0, double p1,
  151.                         string xLabel, string yLabel, string xSystem, string ySystem, ofstream& ofs);
  152. };
  153.  
  154. ifstream& operator >> (ifstream& ifs, PerfectGas& x) {
  155.     assert(ifs && OPEN_ERROR);
  156.  
  157.     ifs >> x.R >> x.T >> x.K >> x.M >> x.p >> x.p0;
  158.     x.checkParams();
  159.     return ifs;
  160. }
  161.  
  162. ofstream& operator << (ofstream& ofs, const PerfectGas& x) {
  163.     assert(ofs && CREATE_ERROR);
  164.  
  165.     ofs << "Info about perfect gas:" << endl;
  166.     ofs << " R  = " << x.R << endl;
  167.     ofs << " T  = " << x.T << endl;
  168.     ofs << " Kg = " << x.Kg << endl;
  169.     ofs << " M  = " << x.M << endl;
  170.     ofs << " p  = " << x.p << endl;
  171.     ofs << " p0 = " << x.p0 << endl;
  172.     ofs << " K  = " << x.K << endl;
  173.     ofs << endl;
  174.  
  175.     return ofs;
  176. }
  177.  
  178. istream& operator >> (istream& is, PerfectGas& x) {
  179.     cout << "Enter info about perfect gas:" << endl;
  180.     cout << " R  = ";   is >> x.R;
  181.     cout << " T  = ";   is >> x.T;
  182.     cout << " K  = ";   is >> x.K;
  183.     cout << " M  = ";   is >> x.M;
  184.     cout << " p  = ";   is >> x.p;
  185.     cout << " p0 = ";   is >> x.p0;
  186.     cout << endl;
  187.     x.checkParams();
  188.     return is;
  189. }
  190.  
  191. ostream& operator << (ostream& os, const PerfectGas& x) {
  192.     os << "Info about perfect gas:" << endl;
  193.     os << " R  = " << x.R << endl;
  194.     os << " T  = " << x.T << endl;
  195.     os << " Kg = " << x.Kg << endl;
  196.     os << " M  = " << x.M << endl;
  197.     os << " p  = " << x.p << endl;
  198.     os << " p0 = " << x.p0 << endl;
  199.     os << " K  = " << x.K << endl;
  200.     os << endl;
  201.  
  202.     return os;
  203. }
  204.  
  205. double getMin(double arr[], int n) {
  206.     int i, iMin = 0;
  207.    
  208.     for (i = 1; i < n; i++) {
  209.         if (arr[iMin] > arr[i]) {
  210.             iMin = i;
  211.         }
  212.     }
  213.    
  214.     return arr[iMin];
  215. }
  216.  
  217. double getMax(double arr[], int n) {
  218.     int i, iMax = 0;
  219.    
  220.     for (i = 1; i < n; i++) {
  221.         if (arr[iMax] < arr[i]) {
  222.             iMax = i;
  223.         }
  224.     }
  225.    
  226.     return arr[iMax];
  227. }
  228.  
  229. int convert(double c, double dc, double cMin) {
  230.     return round((c - cMin) / dc); 
  231. }
  232.  
  233. void draw(double* x, double* y, int n, string xLabel, string yLabel, string xSystem, string ySystem) {
  234.     string xLabelG;
  235.     string yLabelG;
  236.    
  237.     double xMin = getMin(x, n);
  238.     double xMax = getMax(x, n);
  239.     double yMin = getMin(y, n);
  240.     double yMax = getMax(y, n);
  241.    
  242.     int w = getmaxx();
  243.     int h = getmaxy();
  244.    
  245.     double dx = (xMax - xMin) / w;
  246.     double dy = (yMax - yMin) / (h - 10);
  247.    
  248.     string label = "Result";
  249.     char labelBkp[STR_LEN];
  250.    
  251.     int i;
  252.    
  253.     setbkcolor(WHITE);
  254.     setlinestyle(SOLID_LINE, 0, 5);
  255.     cleardevice();
  256.     setcolor(RED);
  257.    
  258.     for (i = 0; i < n - 1; i++) {
  259.         line(convert(x[i], dx, xMin),
  260.         h - convert(y[i], dy, yMin),
  261.         convert(x[i + 1], dx, xMin),
  262.         h - convert(y[i + 1], dy, yMin));
  263.     }
  264.    
  265.     setcolor(BLUE);
  266.    
  267.     outtextxy(w / 2 - 10 * label.length(), 0, &label[0]);
  268.    
  269.     xLabelG = xLabel + ", " + xSystem;
  270.     yLabelG = yLabel + ", " + ySystem;
  271.    
  272.     outtextxy(0, 0, &yLabelG[0]);
  273.     outtextxy(w - 10 * xLabelG.length(), h - 20, &xLabelG[0]);
  274.    
  275.     setcolor(GREEN);
  276.    
  277.     snprintf(labelBkp, STR_LEN, "%lg", y[0]);
  278.    
  279.     outtextxy(0, h - 20, labelBkp);
  280.    
  281.     snprintf(labelBkp, STR_LEN, "%lg", y[n - 1]);
  282.    
  283.     outtextxy(0, 20, labelBkp);
  284.    
  285.     snprintf(labelBkp, STR_LEN, "%lg", (y[0] + y[n - 1]) / 2);
  286.    
  287.     outtextxy(0, h / 2, labelBkp);
  288.    
  289.     snprintf(labelBkp, STR_LEN, "%lg", x[0]);
  290.    
  291.     outtextxy(20, h - 30, labelBkp);
  292.    
  293.     snprintf(labelBkp, STR_LEN, "%lg", x[n - 1]);
  294.    
  295.     outtextxy(w - 30, h - 30, labelBkp);
  296.    
  297.     snprintf(labelBkp, STR_LEN, "%lg", (x[0] + x[n - 1]) / 2);
  298.    
  299.     outtextxy(w / 2, h - 30, labelBkp);
  300. }
  301.  
  302. void tabulate(PerfectGas data, double dp, double p0, double p1,
  303.             string xLabel, string yLabel, string xSystem, string ySystem, ofstream& ofs) {
  304.     double p = p0;
  305.     int n = ((p1 - p0) / dp) + 1, i = 0;
  306.     double x[n], y[n];
  307.    
  308.     do {
  309.         x[i] = p;
  310.        
  311.         data.p0 = p;
  312.         data.calc();
  313.        
  314.         y[i] = data.calcPar;
  315.        
  316.         p += dp;
  317.        
  318.         cout << xLabel << " = " << x[i] << " => " << yLabel << " = " << y[i] << endl;
  319.         ofs << xLabel << " = " << x[i] << " => " << yLabel << " = " << y[i] << endl;
  320.        
  321.         i++;
  322.     } while (p <= (p1 + 1e-7));
  323.    
  324.     draw(x, y, n, xLabel, yLabel, xSystem, ySystem);
  325.     getch();
  326. }
  327.  
  328. int main() {
  329.     initwindow(WIDTH, HEIGHT);
  330.    
  331.     cout << "###Constructors test###" << endl << endl;
  332.  
  333.     PerfectGas x;
  334.  
  335.     cout << "Default constructor:" << endl << x;
  336.  
  337.     PerfectGas y(8.31696, 400, 1.402, 28.96, 1, 3);
  338.  
  339.     y.calcKg();
  340.  
  341.     cout << "Init constructor:" << endl << y;
  342.  
  343.     PerfectGas z = y;
  344.  
  345.     cout << "Copy constructor:" << endl << z;
  346.    
  347.     cout << "###Functions test###" << endl << endl;
  348.  
  349.     cout << "Set function:" << endl;
  350.     x.set();
  351.  
  352.     cout << "Get function:" << endl;
  353.     x.get();
  354.  
  355.     x.calcKg(); x.calc();
  356.  
  357.     cout << "Result function:" << endl;
  358.     x.getResult();
  359.    
  360.     cout << "###Operators test###" << endl << endl;
  361.    
  362.     cout << "x object:" << endl;
  363.     x.get();
  364.    
  365.     cout << "y object:" << endl;
  366.     y.get();
  367.  
  368.     cout << "Operator == between obj x & y" << endl;
  369.     cout << "result = " << boolalpha << (x == y) << endl << endl;
  370.  
  371.     cout << "Operator != between obj x & y" << endl;
  372.     cout << "result = " << boolalpha << (x != y) << endl << endl;
  373.  
  374.     cout << "Operator < between obj x & y" << endl;
  375.     cout << "result = " << boolalpha << (x < y) << endl << endl;
  376.  
  377.     cout << "Operator <= between obj x & y"  << endl;
  378.     cout << "result = " << boolalpha << (x <= y) << endl << endl;
  379.  
  380.     cout << "Operator > between obj x & y" << endl;
  381.     cout << "result = " << boolalpha << (x > y) << endl << endl;
  382.  
  383.     cout << "Operator >= between obj x & y" << endl;
  384.     cout << "result = " << boolalpha << (x >= y) << endl << endl;
  385.    
  386.     cout << "Operator = between obj x & y" << endl;
  387.     x = y;
  388.    
  389.     cout << "x object:" << endl;
  390.     x.get();
  391.    
  392.     cout << "y object:" << endl;
  393.     y.get();
  394.  
  395.     cout << "###Reading from file and calculating value###" << endl << endl;
  396.  
  397.     PerfectGas a; ifstream ifs(IN_FILE); ofstream ofs(OUT_FILE);
  398.  
  399.     ifs >> a;
  400.  
  401.     a.calcKg(); a.calc();
  402.  
  403.     cout << a;
  404.  
  405.     a.getResult();
  406.    
  407.     cout << "###Tabulate func###" << endl << endl;
  408.    
  409.     double dp, p0, p1;
  410.    
  411.     cout << "Enter dp, p0 & p1: ";
  412.     cin >> dp >> p0 >> p1;
  413.    
  414.     cout << endl << "Result:" << endl;
  415.     ofs << "Result:" << endl;
  416.    
  417.     tabulate(a, dp, p0, p1, "p0", "w", "ata", "m/s", ofs);
  418.  
  419.     return 0;
  420. }
Add Comment
Please, Sign In to add comment