菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

2023.5.23

 

复制代码
 1 #include <iostream>
 2 #include <iomanip>
 3 using namespace std;
 4 float PI = 3.14159f;
 5 class Shape
 6 {
 7 public:
 8     virtual float getArea() = 0;
 9 };
10 class Circle :public Shape
11 {
12 public:
13     float r;
14     float getArea()
15     {
16         return PI * r * r;
17     }
18 };
19 class Square :public Shape
20 {
21 public:
22     float x;
23     float getArea()
24     {
25         return x * x;
26     }
27 };
28 class Rectangle :public Shape
29 {
30 public:
31     float w, l;
32     float getArea()
33     {
34         return w * l;
35     }
36 };
37 class Trapezoid :public Shape
38 {
39 public:
40     float s, x, h;
41     float getArea()
42     {
43         return (s + x) * h / 2;
44     }
45 };
46 class Triangle :public Shape
47 {
48 public:
49     float d, h;
50     float getArea()
51     {
52         return (d * h) / 2;
53     }
54 };
55 void test01()
56 {
57     Circle c;
58     Square s;
59     Rectangle r;
60     Trapezoid t;
61     Triangle a;
62     cin >> c.r >> s.x >> r.w >> r.l >> t.s >> t.x >> t.h >> a.d >> a.h;
63     Shape* arr[5] = { &c,&s,&r,&t,&a };
64     float sumArea = 0;
65     for (int i = 0; i < 5; i++)
66     {
67         sumArea = sumArea + arr[i]->getArea();
68     }
69     cout <<fixed<<setprecision(3)<< sumArea << endl;
70 }
71 int main()
72 {
73     test01();
74     return 0;
75 }
复制代码

 

posted on   hhmzd233  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示