Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #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;
- void checkParams() {
- assert(k > 0 && T > 0 && R > 0 && F > 0 &&
- T >= 1000 && T <= 1500 && R >= 900 && R <= 1700 &&
- F >= 140 && F <= 300);
- }
- 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);
- friend void tabulate(Force data, double dR, double R0, double R1, ofstream& ofs);
- };
- 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;
- }
- void tabulate(Force data, double dR, double R0, double R1, ofstream& ofs) {
- double R = R0;
- int n = ((R1 - R0) / dR) + 1, i = 0; // кількість точок
- double x[n], y[n];
- cout.precision(3);
- cout << scientific;
- ofs.precision(3);
- ofs << scientific;
- do {
- x[i] = R;
- data.R = R;
- data.calc();
- y[i] = data.calc();
- R += dR;
- cout << "R = " << x[i] << endl << "E = " << y[i] << endl << endl;
- ofs << "R = " << x[i] << endl << "E = " << y[i] << endl << endl;
- i++;
- } while (R <= (R1 + 1e-7)); // умова виходу із табулювання
- }
- 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