CenterOfMassCalculationException with test of it
This commit is contained in:
48
pr5.cpp
48
pr5.cpp
@@ -114,6 +114,15 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//My exception
|
||||||
|
class CenterOfMassCalculationException : public Exception {
|
||||||
|
public:
|
||||||
|
CenterOfMassCalculationException(const char* s) : Exception(s) {}
|
||||||
|
virtual void print() {
|
||||||
|
cout << "CenterOfMassCalculationException: " << str << "; " << what();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class BaseMatrix
|
class BaseMatrix
|
||||||
{
|
{
|
||||||
@@ -253,11 +262,19 @@ public:
|
|||||||
centerY += j * this->ptr[i][j];
|
centerY += j * this->ptr[i][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (totalMass != 0)
|
/*if (totalMass != 0)
|
||||||
{
|
{
|
||||||
centerX /= totalMass;
|
centerX /= totalMass;
|
||||||
centerY /= totalMass;
|
centerY /= totalMass;
|
||||||
|
}*/
|
||||||
|
if (totalMass == 0)
|
||||||
|
{
|
||||||
|
throw CenterOfMassCalculationException("Total mass is zero, cannot calculate center of mass.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
centerX /= totalMass;
|
||||||
|
centerY /= totalMass;
|
||||||
|
|
||||||
return make_pair(centerX, centerY);
|
return make_pair(centerX, centerY);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,16 +439,39 @@ int main() {
|
|||||||
cout << a << ", " << b << "\n";
|
cout << a << ", " << b << "\n";
|
||||||
double traceA = A.Trace(); // calculate trace
|
double traceA = A.Trace(); // calculate trace
|
||||||
cout << "Trace of A: " << traceA << endl;
|
cout << "Trace of A: " << traceA << endl;
|
||||||
|
|
||||||
|
|
||||||
|
// Test CenterOfMassCalculationException
|
||||||
|
Matrix<double> ZeroMatrix(2, 2);
|
||||||
|
for (int i = 0; i < 2; ++i) {
|
||||||
|
for (int j = 0; j < 2; ++j) {
|
||||||
|
ZeroMatrix(i, j) = 0;
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException& ex) {
|
}
|
||||||
|
cout << "ZeroMatrix:" << endl;
|
||||||
|
ZeroMatrix.print();
|
||||||
|
tie(a, b) = ZeroMatrix.calculateCenterOfMass();
|
||||||
|
cout << "Center of Mass of ZeroMatrix:" << endl;
|
||||||
|
cout << a << ", " << b << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (IndexOutOfBoundsException& ex)
|
||||||
|
{
|
||||||
cout << "IndexOutOfBoundsException has been caught: ";
|
cout << "IndexOutOfBoundsException has been caught: ";
|
||||||
ex.print();
|
ex.print();
|
||||||
}
|
}
|
||||||
catch (Exception& ex) {
|
catch (CenterOfMassCalculationException& ex)
|
||||||
|
{
|
||||||
|
cout << "CenterOfMassCalculationException has been caught: ";
|
||||||
|
ex.print();
|
||||||
|
}
|
||||||
|
catch (Exception& ex)
|
||||||
|
{
|
||||||
cout << "Exception has been caught: ";
|
cout << "Exception has been caught: ";
|
||||||
ex.print();
|
ex.print();
|
||||||
}
|
}
|
||||||
catch (exception& ex) {
|
catch (exception& ex)
|
||||||
|
{
|
||||||
cout << "Standard exception has been caught: " << ex.what();
|
cout << "Standard exception has been caught: " << ex.what();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user