Advertisement
Vladislav_Bezruk

Untitled

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