c++ - 如何对向量中的所有元素求和?
问题描述
我想输入形状的数量和形状的面积之和。
输入的第一行是您要计算的形状数量,在第一行之后,您可以输入形状的类型和形状的大小。
但是我在计算三角形和矩形的面积时遇到了问题,除了圆形。只有圆的面积计算得很好..
$ ./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;
}
解决方案
有许多有趣的错误:
- 我不知道你为什么将 a 传递
vector<Shape *>
给该area()
方法。 - 我不明白你为什么要从
0 .. n+1
. 它与您提供的输入不匹配。 - 如评论中所述,您将继续引用指向超出范围的对象的指针。使用堆是解决此问题的简单方法。使用 a
unique_ptr
还可以确保清理内存。 - 由于您使用的是继承,因此最好使基类具有虚拟析构函数。
#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;
}
推荐阅读
- python - 在海龟图形中使用 clear() 函数时如何停止闪烁
- c# - Blazor 创建服务,从 SQL Server 存储过程中提取数据
- matlab - 需要帮助构建 STL 文件
- sql - 如何将计数用于 case 语句
- google-analytics - Google Analytics Report API v4 在没有明显原因的情况下多次返回 503
- node.js - 使用 Nodejs Sharp 模块时出错。模块解析失败:意外字符 '' (1: 0)
- vb.net - 在 Outlook Web App 上通过 VB.NET 下载电子邮件附件
- swift - CALayer垂直居中文本XCode 11.6
- javascript - 如何在反应导航反应导航中通过注销重置用户历史记录?
- python - 我正在尝试通过 Rs485 从 RasPi 与 arduino 通信。我在 RasPi 端使用 USB 到 Rs485 转换器,在 arduino 端使用 Max485 芯片