题目
【题目】以几何体类solid为基类,公有派生了长方体类box和圆柱体类cylinder,在这3个类中分别给定了数据成员和函数成员。并在此基础上构建了几何体集合类solid_aggregate,其中各成员的作用见代码区中的注释,最后在入口主函数中对其进行了测试。
【要求】
1)将后续的代码复制到编辑器当中,调试并修改已给代码中的五处错误(main函数没有错误请勿修改),并用行注释的方式在错误所在行末尾标明错误,如“//错误1”(共5处总计40分)
2)请补充五处缺失的代码(60分)
【输出示例】本案例输出文件为data.out,运行后输出结果为:
代码
#include<iostream>
#include<fstream>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include <algorithm>
using namespace std;
#define PI 3.14159
#define N 50
class solid//几何体类
{
protected:
char solid_name[20];//几何体名称
float ssa;//比表面积,值可等于表面积/体积
public:
solid(const char* sname = "未命名");//构造函数
float get_ssa();//获取比表面积的值
virtual float area() = 0;//求取几何体表面积
virtual float volume() = 0;//求取几何体体积
virtual void savefile(const char* filename);//错误五,应该定义为虚函数//在磁盘文件的尾部写入几何体信息
};
solid::solid(const char* sname)
{
ssa = 0;
if (sname)
{
//strcpy(solid_name, sname);
strcpy_s(solid_name, 20, sname); //本语句在vs2017编译器替换使用,无误
}
else
strcpy_s(solid_name,20, "未命名"); //strcpy_s(solid_name, 20, "未命名");本语句在vs2017编译器替换使用,无误
}
float solid::get_ssa()
{
return area() / volume();//错误一,加括号表示调用函数,不接括号是使用值
}
void solid::savefile(const char* filename)
{
ofstream out(filename, ios::app);
out << setw(15) << solid_name << endl;
out.close();
}
class box :public solid//长方体类
{
protected:
float length, width, height;//长度,宽度,高度
public:
box(float a = 2, float b = 1, float c = 3, const char* sname = "box_0#"); //构造函数
float area();//求取长方体表面积
float volume();//求取长方体体积
void savefile(const char* filename);//在磁盘文件的尾部写入长方体信息
};
box::box(float a, float b, float c, const char* sname) ://错误三:在定义函数时可以不指定默认参数
solid(sname)//错误四:没初始化基类
{
length = a, width = b, height = c;
ssa = area() / volume();
}
float box::area()
{
return 2 * (length * width + width * height + length * height);
}
float box::volume()
{
return length * width * height;
}
void box::savefile(const char* filename)
{//在此补充本段代码,在磁盘文件的尾部写入长方体信息
ofstream out(filename, ios::app);
out << setw(15) << solid_name;
out << setw(15) << "length="<<length;
out << setw(15) << "width=" << width;
out << setw(15) << "height=" << height;
out << setw(15) << "ssa=" << get_ssa() << endl;
out.close();
}
class cylinder :public solid//圆柱体类
{
protected:
float radius, height;//半径,高度
public:
cylinder(float r = 1, float h = 1, const char* sname = "cylinder_0#");//构造函数
float area();//求取圆柱体表面积
float volume();//求取圆柱体体积
void savefile(const char* filename);//在磁盘文件的尾部写入圆柱体信息
};
cylinder::cylinder(float r , float h , const char* sname)://错误三:在定义函数时可以不指定默认参数
solid(sname)//错误四:没初始化基类
{
radius = r, height = h;
ssa = area() / volume();
}
float cylinder::area()
{
return 2*PI * radius * radius + 2*PI * radius * height;//错误二,加乘号还少了个2倍
}
float cylinder::volume()
{
return PI * radius * radius * height;
}
void cylinder::savefile(const char* filename)
{//在此补充本段代码,在磁盘文件的尾部写入圆柱体信息
ofstream out(filename, ios::app);
out << setw(15) << solid_name;
out << setw(15) << "radius=" << radius;
out << setw(15) << "height=" << height<<" ";
out << setw(15) << "ssa=" << get_ssa() << endl;
out.close();
}
class solid_aggregate//几何体集合类
{
solid* s[N];//指针数组s最多可记录N个几何体对象的指针
int n;//s中有效元素个数即已记录了n个几何体对象
public:
solid_aggregate();//构造函数
solid_aggregate& operator+=(solid* sp);//添加几何体
void sort();//对数组元素指向的几何体对象,按比表面积降序排列
void save(const char* filename);//将所记录的n个几何体信息保存到磁盘文件中
};
solid_aggregate::solid_aggregate()
{
n = 0;
}
solid_aggregate& solid_aggregate::operator+=(solid* sp)
{//在此补充本段代码,向指针数组s中添加几何体,注意防止元素溢出
if (n >= N)return *this;
s[n] = sp;
n++;
}
void solid_aggregate::sort()
{//在此补充本段代码,对数组s指向的几何体对象,按比表面积降序排列
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (s[j]->get_ssa() < s[j + 1]->get_ssa())
{
solid* temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
void solid_aggregate::save(const char* filename)
{//在此补充本段代码,将数组s所记录的n个几何体信息保存到磁盘文件的尾部
for (int i = 0; i < n; i++) {
s[i]->savefile(filename);
}
}
int main()
{
box b1, b2(1, 1, 1, "box_1#"), b3(2, 3, 4, "box_2#");
cylinder c1, c2(2, 1, "cylinder_2#"), c3(2, 2, "cylinder_3#");
solid_aggregate s1;
s1 += &b1;
s1 += &b2;
s1 += &b3;
s1 += &c1;
s1 += &c2;
s1 += &c3;
s1.save("data.out");
solid_aggregate s2 = s1;
s2.sort();
s2.save("data.out");
return 0;
}