Vladislav_Bezruk

Untitled

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