Can I get my program checked for efficiency? As in, see if there are better ways of writing my code, for example, making more efficient use of memory and/or security options like where to use constants?
main.cpp
// MAIN.CPP
//
#include "Force.h"
#include "require.h"
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
//Read in values from "ForceList.txt"
char ctpe;
double real1, real2;
vector<Force> forceCol;
ifstream in("ForceList.txt");
assure(in, "Forcelist.txt"); //verify its open
//Store forces in vector 'forceCol' in cartesian as default
while (in >> ctpe >> real1 >> real2) {
Force cart(ctpe, real1, real2);
if (ctpe == 'c' || ctpe == 'C') {
forceCol.push_back(cart);
}
else if (ctpe == 'p' || ctpe == 'P') {
cart.converter();
forceCol.push_back(cart);
}
}
in.close();
// Print the forces in vector 'forceCol' in cartesian form.
cout << "Cartesian:\n" << endl;
cout << "No." << " x." << " y. \n";
for (unsigned int i = 0; i < forceCol.size(); ++i) {
cout << "Force " << i + 1 << ":";
forceCol[i].print();
}
//Convert 'forceCol' into polar form.
for (unsigned int i = 0; i < forceCol.size(); ++i) {
forceCol[i].converter();
}
// Print the forces in vector 'forceCol' in polar form.
cout << endl << "---------------" << endl;
cout << "\nForces in polar:\n" << endl;
cout << "No." << " Radius." << " Angle. \n";
for (unsigned int i = 0; i < forceCol.size(); ++i) {
cout << "Force " << i + 1 << ":";
forceCol[i].print();
}
//Convert back to cartesian for calculations
for (unsigned int i = 0; i < forceCol.size(); ++i) {
forceCol[i].converter();
}
//Summate the forces in 'forceCol' and print the result in cartesian form.
Force summedForce;
for (unsigned int i = 0; i < forceCol.size(); ++i) {
summedForce = summedForce + forceCol[i];
}
cout << endl << "---------------" << endl;
cout << "\nThe summed force in cartesian form is: " << endl;
summedForce.print();
//Convert 'summedForce' to polar form and print.
summedForce.converter();
cout << "\nThe summed force in polar form is: " << endl;
summedForce.print();
//Print out the resultant force of the first 3 forces in cartesian form
Force resultantForce;
resultantForce = -(forceCol[0] - forceCol[1] + forceCol[2]);
cout << "\nThe resultant force of the first 3 forces in cartesian form is: " << endl;
resultantForce.print();
//Convert 'resultantForce' to polar form and print.
resultantForce.converter();
cout << "\nThe resultant force of the first 3 forces in polar form is: " << endl;
resultantForce.print();
cout << endl;
}
Force.h
#ifndef Force_H
#define Force_H
#include <iostream>
#include <iomanip>
#include "require.h"
//Question 1 - Class 'Force' that reads in cartesian or polar coordinates,
//can discern which is which, and allows conversion and arthithmetical manipulation
class Force {
char coType; // Type of coordinate - cartesian or polar
double r1; // 1st real number
double r2; // 2nd real number
public:
//Constructor that reads in the files and checks that the first value is 'c, C, p or P'
//and assign defeauts
Force(char coType1 = 'c', double re1 = 0.0, double re2 = 0.0) :
coType(coType1), r1(re1), r2(re2) {
require(coType1 == 'p' || coType == 'P' || coType == 'c' || coType == 'C');
};
~Force() {};
//Binary operators for addition and subtraction of 'Force' objects values.
Force operator+(Force& fo){
return Force(coType = fo.coType, r1 + fo.r1, r2 + fo.r2);
};
Force operator-(Force& fo){
return Force(coType = fo.coType, r1 - fo.r1, r2 - fo.r2);
};
//Unary operator to change the sign of the objects values.
Force operator-()
{
return Force(coType, r1 = -r1, r2 = -r2);
}
//Accessors
char get_coType() const { return coType; }
double get_r1() const { return r1; }
double get_r2() const { return r2; }
//mutators
void set_coType(char c) { coType = c; }
void set_r1(double a) { r1 = a; }
void set_r2(double b) { r2 = b; }
//printer
int print() {
using namespace std;
cout << setprecision(2) << fixed;
cout << coType << setw(10) << right << r1 << setw(10) << right << r2 << endl;
return 0;
}
//Converter
void converter();
};
#endif
Force.cpp
#include "Force.h"
#include <cmath>
using namespace std;
//Converter function
void Force::converter() {
//Pass by reference to change original values
double& x = r1;
double& y = r2;
double& rad = r1;
double& theta = r2;
//'Intermeditary' variables
double x1 = x;
double y1 = y;
double rad1 = rad;
double theta1 = theta;
//Check which type of coordinates are being converted
//intermeditary variables used to stop math errors in the conversion
if (coType == 'c' || coType == 'C') {
coType = 'p';
rad = hypot(x1, y1);
theta = atan2(y1, x1);
}
else if (coType == 'p' || coType == 'P') {
coType = 'c';
y = rad1 * sin(theta1);
x = rad1 * cos(theta1);
}
};
ForceList.txt
p 10 0.5
c 12 14
p 25 1
p 100. 0.80
c 50. 50.
p 20 3.14
c -100. 25
p 12 1.14
The first letter is what type of force cartesian or polar, the following 2 numbers are the coordinates for cartesian and the magnitude and angle for polar