Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- #define M_PI 3.1415926
- class Evaluate {
- protected:
- double formula;
- public:
- virtual void set() = 0;
- virtual void get() = 0;
- virtual double calc() = 0;
- virtual void getResult() = 0;
- };
- class Force : public Evaluate {
- private:
- double k, T, R, F;
- public:
- Force(){
- k = 0, T = 0, R = 0, F = 0;
- }
- Force(double _k, double _T, double _R, double _F) :k(_k), T(_T), R(_R), F(_F) {}
- Force(const Force& x) {
- k = x.k;
- T = x.T;
- R = x.R;
- F = x.F;
- }
- void set() {
- cout << "Enter information about: " << endl;
- cout << "k = ";
- cin >> k;
- cout << endl;
- cout << "T = ";
- cin >> T;
- cout << endl;
- cout << "R = ";
- cin >> R;
- cout << endl;
- cout << "△F = ";
- cin >> F;
- cout << endl << endl;
- }
- void get() {
- cout << "Information about" << endl;
- cout << "k = " << k;
- cout << endl;
- cout << "T = " << T;
- cout << endl;
- cout << "R = " << R;
- cout << endl;
- cout << "△F = " << F;
- cout << endl << endl;
- }
- double calc() {
- formula = sqrt(4*k*T*R*F);
- return formula;
- }
- void getResult() {
- cout << "Evaluated object: " << formula << endl;
- }
- Force& operator = (const Force& x) {
- k = x.k;
- T = x.T;
- R = x.R;
- F = x.F;
- return *this;
- }
- bool operator == (const Force& x) {
- return formula == x.formula;
- }
- bool operator != (const Force& x) {
- return formula != x.formula;
- }
- bool operator > (const Force& x) {
- return formula > x.formula;
- }
- bool operator < (const Force& x) {
- return formula < x.formula;
- }
- bool operator >= (const Force& x) {
- return formula >= x.formula;
- }
- bool operator <= (const Force& x) {
- return formula <= x.formula;
- }
- friend ifstream& operator >> (ifstream& ifs, Force& x);
- friend ofstream& operator << (ofstream& ofs, Force& x);
- friend istream& operator >> (istream& is, Force& x);
- friend ostream& operator << (ostream& os, Force& x);
- };
- ifstream& operator >> (ifstream& ifs, Force& x) {
- ifs >> x.k >> x.T >> x.R >> x.F;
- return ifs;
- }
- ofstream& operator << (ofstream& ofs, Force& x) {
- ofs << "Information about:" << endl;
- ofs << "k = " << x.k;
- ofs << endl;
- ofs << "T = " << x.T;
- ofs << endl;
- ofs << "R = " << x.R;
- ofs << endl;
- ofs << "△F = " << x.F;
- ofs << endl << endl;
- return ofs;
- }
- istream& operator >> (istream& is, Force& x) {
- cout << "Information about: " << endl;
- cout << "k = ";
- is >> x.k;
- cout << endl;
- cout << "T = ";
- is >> x.T;
- cout << endl;
- cout << "R = ";
- is >> x.R;
- cout << endl;
- cout << "△F = ";
- is >> x.F;
- cout << endl << endl;
- return is;
- }
- ostream& operator << (ostream& os, Force& x) {
- os << "Information about:" << endl;
- os << "k = " << x.k << endl;
- os << "T = " << x.T << endl;
- os << "R = " << x.R << endl;
- os << "△F = " << x.F << endl << endl;
- return os;
- }
- int convert(double c, double dc, double cMin) { return round((c - cMin) / dc); }
- void edit(double* x, double* y, int n, string xLabel, string yLabel, string xSystem, string ySystem) {
- string xLabelG;
- string yLabelG;
- //навіщ
- int w = getmaxx();
- int h = getmaxy();
- double dx = (xMax - xMin) / w;
- double dy = (yMax - yMin) / (h - 10);
- string label = "Result";
- char labelBkp[STR_LEN];
- int i;
- setbkcolor(WHITE);
- setlinestyle(SOLID_LINE, 0, 5);
- cleardevice();
- setcolor(RED);
- for (i = 0; i < n - 1; i++) {
- line(convert(x[i], dx, xMin),
- h - convert(y[i], dy, yMin),
- convert(x[i + 1], dx, xMin),
- h - convert(y[i + 1], dy, yMin));
- }
- setcolor(BLUE);
- outtextxy(w / 2 - 10 * label.length(), 0, &label[0]);
- xLabelG = xLabel + ", " + xSystem;
- yLabelG = yLabel + ", " + ySystem;
- outtextxy(0, 0, &yLabelG[0]);
- outtextxy(w - 10 * xLabelG.length(), h - 20, &xLabelG[0]);
- setcolor(GREEN);
- snprintf(labelBkp, STR_LEN, "%lg", y[0]);
- outtextxy(0, h - 20, labelBkp);
- snprintf(labelBkp, STR_LEN, "%lg", y[n - 1]);
- outtextxy(0, 20, labelBkp);
- snprintf(labelBkp, STR_LEN, "%lg", (y[0] + y[n - 1]) / 2);
- outtextxy(0, h / 2, labelBkp);
- snprintf(labelBkp, STR_LEN, "%lg", x[0]);
- outtextxy(20, h - 30, labelBkp);
- snprintf(labelBkp, STR_LEN, "%lg", x[n - 1]);
- outtextxy(w - 30, h - 30, labelBkp);
- snprintf(labelBkp, STR_LEN, "%lg", (x[0] + x[n - 1]) / 2);
- outtextxy(w / 2, h - 30, labelBkp);
- }
- void tabulate(Force data, double dp, double p0, double p1,
- string xLabel, string yLabel, string xSystem, string ySystem, ofstream& ofs) {
- double p = p0;
- int n = ((p1 - p0) / dp) + 1, i = 0; // кількість точок
- double x[n], y[n];
- cout.precision(3);
- cout << scientific;
- ofs.precision(3);
- ofs << scientific;
- do {
- x[i] = p;
- data.p0 = p;
- data.calc();
- y[i] = data.calc;
- p += dp;
- cout << xLabel << " = " << x[i] << endl << yLabel << " = " << y[i] << endl << endl;
- ofs << xLabel << " = " << x[i] << endl << yLabel << " = " << y[i] << endl;
- i++;
- } while (p <= (p1 + 1e-7));
- edit(x, y, n, xLabel, yLabel, xSystem, ySystem);
- _getch();
- }
- int main() {
- cout << "TEST BY CONSTRUCTORS" << endl;
- Force x;
- cout << "Default constructor:" << endl << x;
- Force y(1.37, 1000./1500, 900./1700, 140./300);
- double yRes = y.calc();
- cout << "Init constructor:" << endl << y;
- Force z = y;
- cout << "Copy constructor:" << endl << z;
- cout << "Operator ==" << endl;
- cout << "Output: " << boolalpha << (x == y) << endl << endl;
- cout << "Operator !=" << endl;
- cout << "Output: " << boolalpha << (x != y) << endl << endl;
- cout << "Operator < " << endl;
- cout << "Output: " << boolalpha << (x < y) << endl << endl;
- cout << "Operator <=" << endl;
- cout << "Output: " << boolalpha << (x <= y) << endl << endl;
- cout << "Operator >" << endl;
- cout << "Output: " << boolalpha << (x > y) << endl << endl;
- cout << "Operator >=" << endl;
- cout << "Output: " << boolalpha << (x >= y) << endl << endl;
- cout << "Operator =" << endl;
- x = y;
- cout << "X object:" << endl;
- x.get();
- cout << "Y object:" << endl;
- y.get();
- cout << endl << endl << "TEST BY OPERATORS" << endl << endl;
- cout << "X object:" << endl;
- x.get();
- cout << "Y object:" << endl;
- y.get();
- cout << endl << endl << "TEST BY FUNCTIONS" << endl << endl;
- cout << "Set function:" << endl;
- x.set();
- cout << "Get function:" << endl;
- x.get();
- x.calc();
- cout << "Result function:" << endl;
- x.getResult();
- Force a;
- ifstream ifs("inout.txt");
- ofstream ofs("output.txt");
- ifs >> a;
- double aRes = a.calc();
- cout << a << aRes;
- ofs << a << aRes;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement