请编写程序,上机调试,题目如下图,请按题目要求编写,并给出程序
5个题
一个都不会啊???太晚了,明天写
1、请设计一个矩形类Rect,能够初始化,和设置矩形的长和宽,能够计算面积和周长。
class Rect {
private:
int length, width;
public:
Rect(int l, int w) {
length = l;
width = w;
}
void setLength(int l) {
length = l;
}
void setWidth(int w) {
width = w;
}
int getArea() {
return length * width;
}
int getPerimeter() {
return 2 * (length + width);
}
};
2、在1的基础上派生一个类Cubic,增加高度,增加体积的计算。
class Cubic : public Rect {
private:
int height;
public:
Cubic(int l, int w, int h) : Rect(l, w) {
height = h;
}
void setHeight(int h) {
height = h;
}
int getVolume() {
return getArea() * height;
}
};
3、目录下有1.txt文件记录多行文本内容,请编写程序将该文件内容复制到文件2.txt中。或者将读到的内容进行处理后再保存到另外的文件。
#include <fstream> // for ifstream and ofstream
#include <string> // for string
int main() {
// Open the input file.
std::ifstream input_file("1.txt");
// Open the output file.
std::ofstream output_file("2.txt");
// Read each line from the input file and write it to the output file.
std::string line;
while (std::getline(input_file, line)) {
output_file << line << '\n';
}
return 0;
}
4、请设计一个线段类,可以输入起点和终点,并计算线段的长度。
#include <cmath>
class LineSegment {
public:
LineSegment(double x1, double y1, double x2, double y2)
: x1_(x1), y1_(y1), x2_(x2), y2_(y2) {}
double Length() const {
double dx = x2_ - x1_;
double dy = y2_ - y1_;
return std::sqrt(dx * dx + dy * dy);
}
private:
double x1_;
double y1_;
double x2_;
double y2_;
};
5、使用虚函数编程序求球体和圆柱体的体积和表面积。
定义个一基类为shape,具有体积和表面积的虚函数。球类sphere和圆柱体类column继承这些虚函数,完成体积和表面积的计算和输出。
#include <iostream>
#include <cmath>
using namespace std;
const double pi = 3.1415926535;
// 形状基类,包含虚函数计算体积和表面积
class Shape
{
public:
virtual double getVolume() = 0;
virtual double getSurfaceArea() = 0;
};
// 球体类,继承自形状基类
class Sphere : public Shape
{
private:
double radius;
public:
Sphere(double r)
{
radius = r;
}
// 重载基类的虚函数
double getVolume()
{
return 4.0 / 3.0 * pi * radius * radius * radius;
}
double getSurfaceArea()
{
return 4 * pi * radius * radius;
}
};
// 圆柱体类,继承自形状基类
class Column : public Shape
{
private:
double radius;
double height;
public:
Column(double r, double h)
{
radius = r;
height = h;
}
// 重载基类的虚函数
double getVolume()
{
return pi * radius * radius * height;
}
double getSurfaceArea()
{
return 2 * pi * radius * (radius + height);
}
};
int main()
{
Sphere sphere(2.0);
Column column(2.0, 3.0);
cout << "球体的体积: " << sphere.getVolume() << endl;
cout << "球体的表面积: " << sphere.getSurfaceArea() << endl;
cout << "圆柱体的体积: " << column.getVolume() << endl;
cout << "圆柱体的表面积: " << column.getSurfaceArea() << endl;
return 0;
}
class Rect
{
public:
Rect() {_length=0; _width=0;}
Rect(double length, double width) {_length=length; _width=width;}
void setSize(double length, double width) {_length=length; _width=width;}
double getArea() {return _length*_width;}
double getPerimeter() {return 2*(_length+_width);}
private:
double _length;
double _width;
};
class Rect
{
public:
Rect() {_length=0; _width=0;}
Rect(double length, double width) {_length=length; _width=width;}
void setSize(double length, double width) {_length=length; _width=width;}
double getArea() {return _length*_width;}
double getPerimeter() {return 2*(_length+_width);}
private:
double _length;
double _width;
};
class Cubic : public Rect
{
public:
Cubic():Rect() {_height=0;}
Cubic(double length, double width, double height):Rect(length, width), _height(height) {}
void setSize(double length, double width, double height) {Rect::setSize(length, width); _height=height;}
void setHeight(double height) {_height=height;}
double getVolume() {return Rect::getArea() * _height;}
private:
double _height;
};
void copyFile(const char* src_file, const char* dst_file) {
FILE *src_fd=fopen(src_file, "r");
FILE *dst_fd=fopen(dst_file, "w");
char buf[1024]={0};
while(fgets(buf, sizeof buf, src_fd) != NULL){
fputs(buf, dst_fd);
memset(buf, 0, sizeof buf);
}
fclose(src_fd);
fclose(dst_fd);
}
class Line
{
public:
Line() {}
void setStartPoint(double x, double y) {_sp.first=x; _sp.second=y;}
void setEndPoint(double x, double y) {_ep.first=x; _ep.second=y;}
double getLength() {return sqrt(pow(abs(_sp.first-_ep.first), 2) + pow(abs(_sp.second-_ep.second), 2));}
private:
pair<double, double> _sp;
pair<double, double> _ep;
};
const double pi = 3.1415926535897932384626;
class shape
{
public:
virtual double getVolume() = 0;
virtual double getSurfaceArea() = 0;
};
class sphere: public shape
{
public:
sphere(double r):_r(r) {}
double getVolume() {return (4/3) * pi * pow(_r, 3);}
double getSurfaceArea() {return 4 * pi * pow(_r, 2);}
private:
double _r;
};
class column: public shape
{
public:
column(double r, double h):_r(r), _h(h) {}
double getVolume() {return pi * pow(_r, 2) * _h;}
double getSurfaceArea() {return 2 * pi * pow(_r, 2) + 2 * pi * _r * _h;}
private:
double _r;
double _h;
};
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
# 测试第一题
Rect r1(2, 5);
cout << "rect1 area:" << r1.getArea() << endl;
cout << "rect1 perimeter:" << r1.getPerimeter() << endl;
Rect r2;
r2.setSize(3, 6);
cout << "rect2 area:" << r2.getArea() << endl;
cout << "rect2 perimeter:" << r2.getPerimeter() << endl;
# 测试第二题
Cubic c1(2, 5, 3);
cout << "cubic1 volume:" << c1.getVolume() << endl;
Cubic c2;
c2.setSize(3, 6, 10);
cout << "cubic2 volume:" << c2.getVolume() << endl;
# 测试第三题
copyFile("1.txt", "2.txt");
# 测试第四题
Line l;
l.setStartPoint(2, 2);
l.setEndPoint(4, -4);
cout << "line length:" << l.getLength() << endl;
# 测试第五题
sphere s(5);
cout << "sphere volume:" << s.getVolume() << " surface area:" << s.getSurfaceArea() << endl;
column c(4, 10);
cout << "column volume:" << c.getVolume() << " surface area:" << c.getSurfaceArea() << endl;
}
测试结果:
-> g++ -Wall test.cpp -o test
-> ./test
rect1 area:10
rect1 perimeter:14
rect2 area:18
rect2 perimeter:18
cubic1 volume:30
cubic2 volume:180
line length:6.32456
sphere volume:392.699 surface area:314.159
column volume:502.655 surface area:351.858
还有机会吗