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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?