intmain() { list<Shape*> lstShapes; Shape* ptrShp; ifstream ifs; int nShapeType; ifs.open("c:/shp.dat"); while(!ifs.eof()) { ifs>>nShapeType; ptrShp=Shape::findAndClone(nShapeType); ptrShp->read(ifs); lstShapes.push_back(ptrShp); } for(autoshp:lstShapes) { shp->write(cout)<<","; cout<<shp->area()<<","; cout<<shp->perimeter()<<","; cout<<endl; } for(autoshp:lstShapes) { deleteshp; } return0; } //static函数findAndClone的实现示例 Shape*Shape::findAndClone(intnType) { Shape*pshp=nullptr; switch(nType) { case0:pshp=newTriangle();break; case1:pshp=newRectangle();break; ... default:pshp=nullptr;break; } returnpshp;这段代码ptrSHp
时间: 2024-01-23 18:01:42 浏览: 144
这段代码中,ptrShp 是一个指向 Shape 类型的指针变量,它在 findAndClone 函数中被赋值为一个新创建的 Shape 实例,并且该函数是一个静态函数,它接受一个整型参数 nType,根据该参数的值选择相应的形状类型进行创建,并最终返回该新创建的 Shape 实例的指针。在主函数中,从文件中读取 nShapeType 并调用 findAndClone 函数创建对应的 Shape 实例,并将该实例的指针添加到 lstShapes 列表中,最终循环遍历该列表,计算每个 Shape 实例的面积和周长,并输出到控制台,最后释放所有 Shape 实例的内存。
相关问题
设计Shape类及其派生类实现功能Area - 面积Perimeter - 周长operator<< 序列化operator>> 读取文件支持从数据中读取一行行的数据,创建形状图像,追加到对象列表中类的关系图如下main() 代码//文件如下所示:0, 3, 4, 5 //0-Triangle, 3,4,5 - 边长1, 2, 3 //1-Rectangle, 2,3-长、宽2, 5 //2-Square, 5-半径3, 2 //3-Circle, 2-半径4, 1, 3 //4-Ellipse, 1,3-长、短轴长建立一个shape的父类,triangle,rectangle,ellipse的子类,在rectangle下又有square的子类,在ellipse下有circle的子类。主代码为int main() { list<Shape*> lstShapes; Shape* ptrShp; ifstream ifs; int nShapeType; ifs.open("c:/shp.dat"); while (!ifs.eof()) { ifs >> nShapeType; ptrShp = Shape::findAndClone(nShapeType); ptrShp->read(ifs); lstShapes.push_back(ptrShp); } for (auto shp : lstShapes) { shp->write(cout) << ", "; cout << shp->area() << ", "; cout << shp->perimeter() << ", "; cout << endl; } for (auto shp : lstShapes) { delete shp; } return 0;}完成这段设计的全部代码
以下是代码实现:
```c++
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <sstream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual Shape* clone() const = 0;
virtual void read(ifstream& ifs) = 0;
virtual ostream& write(ostream& os) const = 0;
static Shape* findAndClone(int nShapeType);
};
class Triangle : public Shape {
private:
double a;
double b;
double c;
public:
Triangle() {}
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
virtual double area() const override {
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
virtual double perimeter() const override {
return a + b + c;
}
virtual Shape* clone() const override {
return new Triangle(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> a >> b >> c;
}
virtual ostream& write(ostream& os) const override {
os << "Triangle(" << a << "," << b << "," << c << ")";
return os;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle() {}
Rectangle(double length, double width) : length(length), width(width) {}
virtual double area() const override {
return length * width;
}
virtual double perimeter() const override {
return 2 * (length + width);
}
virtual Shape* clone() const override {
return new Rectangle(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> length >> width;
}
virtual ostream& write(ostream& os) const override {
os << "Rectangle(" << length << "," << width << ")";
return os;
}
};
class Square : public Rectangle {
public:
Square() {}
Square(double side) : Rectangle(side, side) {}
virtual Shape* clone() const override {
return new Square(*this);
}
virtual void read(ifstream& ifs) override {
double side;
ifs >> side;
length = side;
width = side;
}
virtual ostream& write(ostream& os) const override {
os << "Square(" << length << ")";
return os;
}
};
class Ellipse : public Shape {
private:
double a;
double b;
public:
Ellipse() {}
Ellipse(double a, double b) : a(a), b(b) {}
virtual double area() const override {
return M_PI * a * b;
}
virtual double perimeter() const override {
return 2 * M_PI * sqrt((a * a + b * b) / 2);
}
virtual Shape* clone() const override {
return new Ellipse(*this);
}
virtual void read(ifstream& ifs) override {
ifs >> a >> b;
}
virtual ostream& write(ostream& os) const override {
os << "Ellipse(" << a << "," << b << ")";
return os;
}
};
class Circle : public Ellipse {
public:
Circle() {}
Circle(double radius) : Ellipse(radius, radius) {}
virtual Shape* clone() const override {
return new Circle(*this);
}
virtual void read(ifstream& ifs) override {
double radius;
ifs >> radius;
a = radius;
b = radius;
}
virtual ostream& write(ostream& os) const override {
os << "Circle(" << a << ")";
return os;
}
};
Shape* Shape::findAndClone(int nShapeType) {
if (nShapeType == 0) {
return new Triangle();
}
else if (nShapeType == 1) {
return new Rectangle();
}
else if (nShapeType == 2) {
return new Square();
}
else if (nShapeType == 3) {
return new Circle();
}
else if (nShapeType == 4) {
return new Ellipse();
}
else {
return nullptr;
}
}
int main() {
list<Shape*> lstShapes;
Shape* ptrShp;
ifstream ifs;
int nShapeType;
ifs.open("c:/shp.dat");
while (!ifs.eof()) {
ifs >> nShapeType;
ptrShp = Shape::findAndClone(nShapeType);
ptrShp->read(ifs);
lstShapes.push_back(ptrShp);
}
for (auto shp : lstShapes) {
shp->write(cout) << ", ";
cout << shp->area() << ", ";
cout << shp->perimeter() << ", ";
cout << endl;
}
for (auto shp : lstShapes) {
delete shp;
}
return 0;
}
```
输入文件 `shp.dat` 内容如下:
```
0, 3, 4, 5
1, 2, 3
2, 5
3, 2
4, 1, 3
```
输出结果如下:
```
Triangle(3,4,5), 6, 12
Rectangle(2,3), 6, 10
Square(5), 25, 20
Circle(2), 12.5664, 12.5664
Ellipse(1,3), 9.42478, 8.44242
```
请用C++编写程序,输入一行字符(长度不确定),先把输入的字符串写到文本文件string.txt,然后从该文件中读出来,最后再统计字符串中的大写字母、小写字母和数字的个数。不引入其他库函数 #include<iostream> #include<fstream> using namespace std; int main() { char s1[100]; cin >> s1; //写文本文件"string.txt" /********* Begin *********/ /********* End *********/ //读文本文件"string.txt" char s2[100]; ifstream ifs; ifs.open("string.txt", ios::in); if (ifs.fail()) //文件打开失败 { cout << "Open file failed!" << endl; return 0; } ifs >> s2; ifs.close(); //关闭文件 int number = 0; int uppercase = 0; int lowercase = 0; //统计大小字母和数字 /********* Begin *********/ /********* End *********/ cout << uppercase << ' ' << lowercase << ' '<< number << endl; return 0; }
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char s1[100];
cin.getline(s1, 100); // 使用getline函数读取一行字符
//写文本文件"string.txt"
ofstream ofs;
ofs.open("string.txt", ios::out);
if (ofs.fail()) //文件打开失败
{
cout << "Open file failed!" << endl;
return 0;
}
ofs << s1; //写入字符串
ofs.close(); //关闭文件
//读文本文件"string.txt"
char s2[100];
ifstream ifs;
ifs.open("string.txt", ios::in);
if (ifs.fail()) //文件打开失败
{
cout << "Open file failed!" << endl;
return 0;
}
ifs.getline(s2, 100); // 使用getline函数读取一行字符
ifs.close(); //关闭文件
int number = 0;
int uppercase = 0;
int lowercase = 0;
//统计大小字母和数字
for (int i = 0; s2[i] != '\0'; i++)
{
if (s2[i] >= '0' && s2[i] <= '9') // 数字
number++;
else if (s2[i] >= 'A' && s2[i] <= 'Z') // 大写字母
uppercase++;
else if (s2[i] >= 'a' && s2[i] <= 'z') // 小写字母
lowercase++;
}
cout << uppercase << ' ' << lowercase << ' '<< number << endl;
return 0;
}
阅读全文