首页 > 解决方案 > 如何对向量中的所有元素求和?

问题描述

我想输入形状的数量和形状的面积之和。

输入的第一行是您要计算的形状数量,在第一行之后,您可以输入形状的类型和形状的大小。

但是我在计算三角形和矩形的面积时遇到了问题,除了圆形。只有圆的面积计算得很好..

$ ./a.out
1
R 2.0 1.0
0.00

$ ./a.out
1
T 2.0
0.00

$ ./a.out
1
C 1.0
3.14

我认为问题来自

 for (int i = 0; i < n ; i++)
    {
        sum += collection[i]->area(collection);
    }

这段代码!我认为我的面积函数只计算向量的第一个元素.....

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

class Shape{
protected:
    int _r;
    int _w;
    int _h;

public:
    Shape(double r) : _r(r) {}
    Shape(double w, double h) : _w(w), _h(h) {}
    virtual double area(vector<Shape *>) = 0;
};

class Circle : public Shape{
public:
    Circle(double r) : Shape(r) {}
    double area(vector<Shape *>) { return _r*_r*atan(1)*4.0; }
};

class Triangle : public Shape{
public:
    Triangle(double s) : Shape(s) {}
    double area(vector<Shape *>) { return sqrt(3) * pow(_r, 2) / 4; }
};

class Rectangle : public Shape{
public:
    Rectangle(double w, double h) :Shape(w, h) {}
    double area(vector<Shape *>) { return  _w * _h ;}
};

int main()
{

    int n;
    char info;
    int value;
    int value2;
    double sum;
    vector<Shape*> collection;
    vector<int> answer;

    sum = 0;

    cin >> n;

    for(int i = 0 ; i < n + 1; i++)
    {
        cin >> info;
        if (info == 'C')
        {
            cin >> value;
            Circle c(value);
            collection.push_back(&c);
        }
        else if (info == 'R')
        {
            cin >> value;
            cin >> value2;
            Rectangle r(value, value2);
            collection.push_back(&r);
        }
        else
        {
            cin >> value;
            Triangle t(value);
            collection.push_back(&t);
        }
    }
    for (int i = 0; i < n + 1 ; i++)
    {
        sum += collection[i] -> area(collection);
    }
    cout << fixed << setprecision(2) << sum << endl;
}

标签: c++classvector

解决方案


有许多有趣的错误:

  1. 我不知道你为什么将 a 传递vector<Shape *>给该area()方法。
  2. 我不明白你为什么要从0 .. n+1. 它与您提供的输入不匹配。
  3. 如评论中所述,您将继续引用指向超出范围的对象的指针。使用堆是解决此问题的简单方法。使用 aunique_ptr还可以确保清理内存。
  4. 由于您使用的是继承,因此最好使基类具有虚拟析构函数。
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <memory>

using namespace std;

class Shape{
  protected:
    int _r;
    int _w;
    int _h;

  public:
    Shape(double r) : _r(r) {}
    Shape(double w, double h) : _w(w), _h(h) {}
    virtual ~Shape() = default;
    virtual double area() = 0;
};

class Circle : public Shape{
  public:
    Circle(double r) : Shape(r) {}
    double area() { return _r*_r*atan(1)*4.0; }
};

class Triangle : public Shape{
  public:
    Triangle(double s) : Shape(s) {}
    double area() { return sqrt(3) * pow(_r, 2) / 4; }
};

class Rectangle : public Shape{
  public:
    Rectangle(double w, double h) :Shape(w, h) {}
    double area() { return  _w * _h ;}
};

int main() {
    int n;
    char info;
    int value;
    int value2;
    double sum = 0
    vector<std::unique_ptr<Shape>> collection;

    cin >> n;

    for(int i = 0 ; i < n; i++)
    {
        cin >> info;
        if (info == 'C')
        {
            cin >> value;
            collection.push_back(std::make_unique<Circle>(value));
        }
        else if (info == 'R')
        {
            cin >> value;
            cin >> value2;
            collection.push_back(std::make_unique<Rectangle>(value, value2));
        }
        else
        {
            cin >> value;
            collection.push_back(std::make_unique<Triangle>(value));
        }
    }
    for (int i = 0; i < n; i++)
    {
        sum += collection[i]->area();
    }
    cout << fixed << setprecision(2) << sum << endl;
}

推荐阅读