The logic of your program changes significantly if you change from logging-as-you-go to logging-by-function.
I.e. the original program reads the records one after the other and reports all actions immediately. No need to store any values.
If you now want to report in a different sequence that you read, you better load the data first into some appropriate structure and than work on these.
Since you tagged your post as
C++, attached please find a possible solution in
C++.
Cheers
Andi
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
using namespace std;
typedef float length_t;
typedef float angle_deg_t;
typedef float angle_rad_t;
class PointAsPolar {
private:
length_t _radius;
angle_deg_t _angleInDeg;
public:
PointAsPolar()
: _radius(0.0), _angleInDeg(0.0)
{}
PointAsPolar(length_t radius, angle_deg_t angleInDeg)
: _radius(radius), _angleInDeg(angleInDeg)
{}
length_t calcX() const { return length_t(_radius * cos(calcAngleInRad())); }
length_t calcY() const { return length_t(_radius * sin(calcAngleInRad())); }
length_t getRadius() const { return _radius; }
angle_deg_t getAngleInDeg() const { return _angleInDeg; }
angle_rad_t calcAngleInRad() const {
return angle_rad_t(_angleInDeg * 3.14159265358979323846 / 180.0);
}
length_t calcDistance(const PointAsPolar& other) const {
length_t dx = other.calcX() - calcX();
length_t dy = other.calcY() - calcY();
return sqrt(dx*dx + dy*dy);
}
friend istream& operator>> (istream& input, PointAsPolar& p);
};
inline istream& operator>> (istream& input, PointAsPolar& p) {
return input >> p._radius >> p._angleInDeg;
}
inline ostream& operator<< (ostream& output, const PointAsPolar& p) {
return output << "r = "
<< p.getRadius()
<< ", q = "
<< p.getAngleInDeg()
<< " (x = "
<< p.calcX()
<< ", y = "
<< p.calcY()
<< ")";
}
class Polygon {
private:
vector<PointAsPolar> _points;
public:
Polygon(istream& input)
{ PointAsPolar p; while(input >> p) _points.push_back(p); }
size_t size() const
{ return _points.size(); }
const PointAsPolar& operator[](size_t pos) const
{ return _points[pos % size()]; }
};
inline ostream& operator<< (ostream &output, const Polygon &poly) {
size_t n = poly.size();
for(size_t i = 0; i < n; ++i) {
output << "Point "
<< (i+1)
<< ": "
<< poly[i]
<< endl;
}
return output;
}
int main()
{
Polygon poly(ifstream("input.txt"));
cout << poly;
cout << "Sides:" << endl;
length_t perimeter = 0.0;
size_t n = poly.size();
for(size_t i = 0; i < n; ++i) {
length_t distance = poly[i].calcDistance(poly[i+1]);
perimeter += distance;
cout << i+1
<< " to "
<< (((i+1)%n)+1)
<< ": "
<< distance
<< " Accumulated perimeter "
<< perimeter
<< endl;
}
cout << "Total perimeter: " << perimeter << endl;
}
The resulting output is
Point 1: r = 3, q = 180 (x = -3, y = -2.62268e-007)
Point 2: r = 4.47214, q = 116.565 (x = -2, y = 4)
Point 3: r = 5.20096, q = 268.898 (x = -0.1, y = -5.2)
Point 4: r = 5.004, q = 87.7094 (x = 0.199999, y = 5)
Point 5: r = 4.71699, q = 57.9946 (x = 2.5, y = 4)
Point 6: r = 4.92443, q = 23.9625 (x = 4.5, y = 2)
Point 7: r = 6.40312, q = 308.66 (x = 4, y = -5)
Point 8: r = 5.20096, q = 268.898 (x = -0.1, y = -5.2)
Sides:
1 to 2: 4.12311 Accumulated perimeter 4.12311
2 to 3: 9.39415 Accumulated perimeter 13.5173
3 to 4: 10.2044 Accumulated perimeter 23.7217
4 to 5: 2.50799 Accumulated perimeter 26.2297
5 to 6: 2.82843 Accumulated perimeter 29.0581
6 to 7: 7.01783 Accumulated perimeter 36.0759
7 to 8: 4.10487 Accumulated perimeter 40.1808
8 to 1: 5.95399 Accumulated perimeter 46.1348
Total perimeter: 46.1348