CenterOfMassCalculationException with test of it
This commit is contained in:
parent
80c43efab6
commit
eedea90a16
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>
|
||||
class BaseMatrix
|
||||
{
|
||||
@ -253,11 +262,19 @@ public:
|
||||
centerY += j * this->ptr[i][j];
|
||||
}
|
||||
}
|
||||
if (totalMass != 0)
|
||||
/*if (totalMass != 0)
|
||||
{
|
||||
centerX /= 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);
|
||||
}
|
||||
|
||||
@ -422,16 +439,39 @@ int main() {
|
||||
cout << a << ", " << b << "\n";
|
||||
double traceA = A.Trace(); // calculate trace
|
||||
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;
|
||||
}
|
||||
}
|
||||
cout << "ZeroMatrix:" << endl;
|
||||
ZeroMatrix.print();
|
||||
tie(a, b) = ZeroMatrix.calculateCenterOfMass();
|
||||
cout << "Center of Mass of ZeroMatrix:" << endl;
|
||||
cout << a << ", " << b << "\n";
|
||||
}
|
||||
catch (IndexOutOfBoundsException& ex) {
|
||||
|
||||
catch (IndexOutOfBoundsException& ex)
|
||||
{
|
||||
cout << "IndexOutOfBoundsException has been caught: ";
|
||||
ex.print();
|
||||
}
|
||||
catch (Exception& ex) {
|
||||
catch (CenterOfMassCalculationException& ex)
|
||||
{
|
||||
cout << "CenterOfMassCalculationException has been caught: ";
|
||||
ex.print();
|
||||
}
|
||||
catch (Exception& ex)
|
||||
{
|
||||
cout << "Exception has been caught: ";
|
||||
ex.print();
|
||||
}
|
||||
catch (exception& ex) {
|
||||
catch (exception& ex)
|
||||
{
|
||||
cout << "Standard exception has been caught: " << ex.what();
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user