首页 > 其他分享 >2023 5 23

2023 5 23

时间:2023-05-23 20:24:04浏览次数:29  
标签:return 23 double float Point shape 2023 public

#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;

}

 

标签:return,23,double,float,Point,shape,2023,public
From: https://www.cnblogs.com/xuxingkai/p/17426262.html

相关文章

  • 2023.5.23编程一小时打卡
    一、问题描述:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,输出它们的面积和。要求用基类指针数组,每一个数组元素指向一个派生类的对象。PI=3.14159f,单精度浮点数计算。输......
  • 【2023.03.20】P4710 「物理」平抛运动
    题目传送门:【洛谷】P4710[物理]平抛运动Step1:前置芝士您需要知道并了解以下芝士:数学:三角函数;物理:加速度公式;位移公式;那么如果您并不了解以上芝士,那么请继续向下看;如果您已经掌握以上芝士,那么请跳至“Step2”。三角函数讲解:注意,本题为物理题解,故不会放出......
  • MQCal工程算量V1.2.7.1(20230519)版本发布——土建、水电Excel算量插件工具
    MQCal工程算量加载宏工具是什么?首先说明的是:MQcal不是一个简单的对工程计算式算结果的求值工具。他是我本人结合手工算量经验,充分考虑预算员的需求,从算量表格自己设计、重复项目便捷输入、特殊标记、汇总统计、打印或打印为pdf、造价预估等实用功能一体解决方法。MQCal工程算量......
  • 2023.5.23每日总结
    <%@pageimport="wangzhan.Thesql"%><%@pageimport="wangzhan.Pd_P_assignment"%><%@pageimport="wangzhan.Pd_S_assignment"%><%@pagelanguage="java"contentType="text/html;charset=UTF......
  • 5.23
    #include<stdio.h>voidmain(){intinteger,i,max,min,sum;max=-32768;min=32767;sum=0;for(i=0;i<10;i++){printf("Inputnumber%d:",i);scanf("%d",&integer);sum+=integer;if(integer>max)max=integer;if(integer<min)min=intege......
  • 编程一小时2023.5.23
    1.#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;chartran(charc){return(int)(c-'A'-4)>0?(c-5):(c+21);}stringsd,str,ed,eof="ENDOFINPUT";intmain(){strings;while(getline(ci......
  • 5月23日打卡
    例5-7常成员函举例代码部分: #include<iostream>usingnamespacestd;classR{private:intr1,r2;public:R(intr1,intr2):r1(r1),r2(r2){}voidprint();voidprint()const;};voidR::print(){cout<<r1<<":"&l......
  • 5.23打卡
    #include<bits/stdc++.h>usingnamespacestd;constfloatPI=3.141593;constfloatFENCE_PRICE=35;constfloatCONCRETE_PRICE=20;classCircle{public:Circle(floatr);floatcircumference();floatarea();private:......
  • 2023.5.21学习内容 多态、接口、泛型、反射
    下午1.了解CSS响应式布局和兼容性问题2.浏览IDEA使用手册并修改Maven仓库设置3.复习强化JavaSE的多态、接口、泛型、反射知识importorg.junit.Test;importtest.Hello;importjava.lang.reflect.Field;importjava.util.ArrayList;importjava.util.LinkedList;import......
  • poj-2362
    //132K141MSC++withbeginSearchPos#include<cstdio>#include<cstring>#include<cstdlib>usingnamespacestd;intcmp(constvoid*a,constvoid*b)//降序{return*(longlong*)b-*(longlong*)a;}longlongfourEdgeLeng......