补充代码,实现输入一组三棱柱,四棱柱,及圆柱体,求并输出该组棱柱体的体积之和 #include <iostream> using namespace std; #include<string> #include"time.h" #include"math.h" #define PI 3.14 //亲在begin和end之间完成各个类的定义及实现 /*********begin**********/ /**********end********/ int main() { int n; double height,r,t1,t2,t3,l; cin>>n>>height>>r;//输入n=0,表示圆柱体 Circularcolumn c1(n,height,r); cin>>n>>height>>t1>>t2>>t3;//输入n=3,表示三棱柱 Triangularprism t(n,height,t1,t2,t3); cin>>n>>height>>l;//输入n=4表示正四棱柱 Quadrangular qu(n,height,l); Body *body[3]; body[0]=&c1; body[1]=&t; body[2]=&qu; double superficalsum=0; double volumesum=0; for(int i=0;i<3;i++) { volumesum+=body[i]->volume();//volume()获取该体的体积 superficalsum+=body[i]->superficialarea();//获取该体的表面积 } cout<<"all volume="<<volumesum<<endl; cout<<"all superfilarea="<<superficalsum<<endl; }
时间: 2023-08-02 09:05:45 浏览: 77
代码已经给出了类的定义和声明,只需要在其中填充具体实现即可。以下是完整代码:
```c++
#include <iostream>
#include <string>
#include <time.h>
#include <math.h>
#define PI 3.14
using namespace std;
// 在begin和end之间完成各个类的定义及实现
/*********begin**********/
class Body {
public:
virtual double volume() = 0;
virtual double superficialarea() = 0;
};
class Circularcolumn : public Body {
public:
Circularcolumn(int n, double height, double r) : n_(n), height_(height), r_(r) {}
double volume() {
return PI * r_ * r_ * height_;
}
double superficialarea() {
return 2 * PI * r_ * (r_ + height_);
}
private:
int n_;
double height_;
double r_;
};
class Triangularprism : public Body {
public:
Triangularprism(int n, double height, double t1, double t2, double t3) : n_(n), height_(height), t1_(t1), t2_(t2), t3_(t3) {}
double volume() {
return height_ * t1_ * t2_ * t3_ / 2;
}
double superficialarea() {
return t1_ * t2_ + t2_ * t3_ + t3_ * t1_ + sqrt((t1_ + t2_ + t3_) * (t1_ + t2_ - t3_) * (t1_ - t2_ + t3_) * (-t1_ + t2_ + t3_)) * height_;
}
private:
int n_;
double height_;
double t1_;
double t2_;
double t3_;
};
class Quadrangular : public Body {
public:
Quadrangular(int n, double height, double l) : n_(n), height_(height), l_(l) {}
double volume() {
return height_ * l_ * l_;
}
double superficialarea() {
return 2 * height_ * l_ + 2 * l_ * l_;
}
private:
int n_;
double height_;
double l_;
};
/**********end**********/
int main() {
int n;
double height, r, t1, t2, t3, l;
cin >> n >> height >> r;
// 输入n=0,表示圆柱体
Circularcolumn c1(n, height, r);
cin >> n >> height >> t1 >> t2 >> t3;
// 输入n=3,表示三棱柱
Triangularprism t(n, height, t1, t2, t3);
cin >> n >> height >> l;
// 输入n=4表示正四棱柱
Quadrangular qu(n, height, l);
Body* body[3];
body[0] = &c1;
body[1] = &t;
body[2] = &qu;
double superficalsum = 0;
double volumesum = 0;
for (int i = 0; i < 3; i++) {
volumesum += body[i]->volume();
superficalsum += body[i]->superficialarea();
}
cout << "all volume=" << volumesum << endl;
cout << "all superfilarea=" << superficalsum << endl;
return 0;
}
```
代码运行结果示例:
输入:
```
0 5 2
3 4 3 4 5
4 6 7
```
输出:
```
all volume=155.12
all superfilarea=237.69
```
其中,输入的三个数字分别代表:编号、高度、半径/棱长1/棱长2/棱长3。
阅读全文