Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- class Shape3D
- {
- public:
- virtual double area() const = 0;
- virtual double volume() const = 0;
- };
- class s : public Shape3D
- {
- public:
- s(double r = 1) : _r(r) { }
- double area() const { return 4 * 3.14 * _r * _r; }
- double volume() const { return 4.0/3 * 3.14 * _r * _r * _r; }
- private:
- double _r;
- };
- class c : public Shape3D
- {
- public:
- c(double r = 1, double h = 1) : _r(r), _h(h) { }
- double area() const { return 3.14 * _r * (_r + sqrt(pow(_r, 2) + pow(_h, 2))); }
- double volume() const { return 1.0/3 * 3.14 * _r * _r; }
- private:
- double _r;
- double _h;
- };
- class p : public Shape3D
- {
- public:
- p(double a = 1, double b = 1, double h = 1) : _a(a), _b(b), _h(h) { }
- double area() const { return (_a + _b) * _h + _a * _b; }
- double volume() const { return 1.0/3 * _h * ((_a + _b) * _h + _a * _b); }
- private:
- double _a;
- double _b;
- double _h;
- };
- class d : public Shape3D
- {
- public:
- d(double r = 1, double h = 1) : _r(r), _h(h) { }
- double area() const { return 2 * 3.14 * _r*(_r + _h); }
- double volume() const { return (2 * 3.14 * _r*(_r + _h)) * _h; }
- private:
- double _r;
- double _h;
- };
- class t : public Shape3D
- {
- public:
- t(double a = 1) : _a(a) { }
- double area() const { return sqrt(3) *_a * _a; }
- double volume() const { return sqrt(2)/12 * _a * _a * _a; }
- private:
- double _a;
- };
- int main()
- {
- int n;
- int z, x, y;
- char a[20];
- ifstream f;
- f.open("D:\\Shape3D.txt");
- f >> n;
- Shape3D **shapes;
- shapes = new Shape3D*[n];
- while (!f.eof())
- {
- for (int i = 0; i < n; i++)
- {
- f >> a;
- switch (*a)
- {
- case 's': f >> z; shapes[i] = new s(z); break;
- case 'c': f >> z; f >> x; shapes[i] = new c(z, x); break;
- case 'p': f >> z; f >> x; f >> y; shapes[i] = new p(z, x, y); break;
- case 'd': f >> z; f >> x; shapes[i] = new d(z, x); break;
- case 't': f >> z; shapes[i] = new t(z); break;
- }
- }
- }
- f.close();
- for (int i = 0; i < 6; ++i)
- {
- cout << shapes[i]->area() << endl;
- cout << shapes[i]->volume() << endl;
- }
- for (int i = 0; i < 5; ++i)
- delete shapes[i];
- delete[] shapes;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement