2023 5 23

xuxingkai / 2023-05-23 / 原文

#include<iostream>
#include<iomanip>
#define PI 3.14159f
using namespace std;
class shape {
public:
    shape() {}
    ~shape() {}
    virtual float s() { return 0; };
};
class circle :public shape {
private:
    float r;
public:
    circle(float a) {
        r = a;
    }
    ~circle() {}
    float s() {
        return PI*r*r;
    }
};
class square :public shape {
private:
    float l;
public:
    square(float a) {
        l = a;
    }
    ~square() {}
    float s() {
        return l*l;
    }
};
class rectangle :public shape {
private:
    float l, w;
public:
    rectangle(float a, float b) {
        l = a;
        w = b;
    }
    ~rectangle() {}
    float s() {
        return l * w;
    };
};
class trapezoid :public shape {
private:
    float o, d, h;
public:
    trapezoid(float a, float b, float c) {
        o = a;
        d = b;
        h = c;
    }
    ~trapezoid() {}
    float s() {
        return (o+d)*h/2;
    }
};
class triangle :public shape {
private:
    float l, h;
public:
    triangle(float a, float b) {
        l = a;
        h = b;
    }
    ~triangle() {}
    float s() {
        return l*h / 2;
    }
};
int main() {
    shape* p[5];
    int i;
    float a[9];
    float S;
    for (i = 0; i < 9; i++) {
        cin >> a[i];
    }
    circle c1(a[0]);
    square s1(a[1]);
    rectangle r1(a[2], a[3]);
    trapezoid tr1(a[4], a[5], a[6]);
    triangle t1(a[7], a[8]);
    p[0] = &c1;
    p[1] = &s1;
    p[2] = &r1;
    p[3] = &tr1;
    p[4] = &t1;
    S = p[0]->s() + p[1]->s() + p[2]->s() + p[3]->s() + p[4]->s();
    cout <<setprecision(6)<< S;
    return 0;
}
#include<iostream>
using namespace std;
#include<cmath>
//2017final函数模板

class Point
{
public:
    //构造函数赋初值
    Point(double a, double b, double c) :m_x(a), m_y(b), m_z(c) {}
    //把重载函数声明为类的友元,可以访问类中的私有元素
    //也可以不声明友元,直接把那三个坐标写到public 里面
    friend double operator -(Point p1, Point p2);
private:
    double m_x;
    double m_y;
    double m_z;

};
//重载减号(-)
double operator -(Point p1, Point p2)
{
    //空间内两点求距离
    //sqrt计算开方
    //pow double pow(double x, double y) 返回 x 的 y 次幂.
    //p1.m_x - p2.m_x, 2.0) 返回两数差的2次方
    return sqrt(pow(p1.m_x - p2.m_x, 2.0) + pow(p1.m_y - p2.m_y, 2.0) + pow(p1.m_z - p2.m_z, 2.0));
}
//间距模板
template<class T1 , class T2>
double Distance(T1 a, T2 b)
{
    return abs(a - b);  //返回距离的绝对值
}
int main()
{
    int n;   //元素类型变量
    cin >> n;
    while (n!=0)
    {
        if (n == 1)  //整型元素
        {
            int a, b;
            cin >> a >> b;
            cout << Distance(a, b) << endl;  //调用模板函数,输出两点之间的距离
        }
        if (n == 2)  //浮点型数据
        {
            float a, b;
            cin >> a >> b;
            cout << Distance(a, b) << endl;
        }
        if (n == 3)   //point 类型
        {
            double a1, b1, c1, a2, b2, c2;
            cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;
            Point p1(a1, b1, c1); 
            Point p2(a2, b2, c2);
            cout <<Distance(p1, p2) << endl;

        }
        cin >> n;   //输入0跳出while循环
    }
    return 0;

}