forked from rahatlou/CMP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
866 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "Calculator.h" | ||
|
||
|
||
Calculator::Calculator() { | ||
|
||
} | ||
|
||
|
||
Datum | ||
Calculator::weightedAverage(const std::vector<Datum>& data) { | ||
Datum res(1.,0.1); | ||
return res; | ||
} | ||
|
||
Datum | ||
Calculator::arithmeticAverage(const std::vector<Datum>& data) { | ||
Datum res(1.,0.1); | ||
return res; | ||
} | ||
|
||
Datum | ||
Calculator::geometricAverage(const std::vector<Datum>& data) { | ||
Datum res(1.,0.1); | ||
return res; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef Calculator_h | ||
#define Calculator_h | ||
|
||
#include <vector> | ||
#include "Datum.h" | ||
|
||
class Calculator { | ||
public: | ||
Calculator(); | ||
|
||
static Datum weightedAverage(const std::vector<Datum>&); | ||
static Datum arithmeticAverage(const std::vector<Datum>&); | ||
static Datum geometricAverage(const std::vector<Datum>&); | ||
|
||
}; | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include "Datum.h" | ||
#include <iostream> | ||
#include <cmath> | ||
|
||
using std::cout; | ||
using std::ostream; | ||
using std::endl; | ||
|
||
Datum::Datum() { | ||
value_ = 0.0; | ||
error_ = 0.0; | ||
} | ||
|
||
Datum::Datum(double x, double y) { | ||
value_ = x; | ||
error_ = y; | ||
} | ||
|
||
Datum::Datum(const Datum& datum) { | ||
value_ = datum.value_; | ||
error_ = datum.error_; | ||
} | ||
|
||
double | ||
Datum::significance() const { | ||
return value_/error_; | ||
} | ||
|
||
void Datum::print() const { | ||
using namespace std; | ||
cout << "datum: " << value_ | ||
<< " +/- " << error_ << endl; | ||
} | ||
|
||
Datum Datum::operator+( const Datum& rhs) const { | ||
|
||
// sum of central values | ||
double val = value_ + rhs.value_; | ||
// assume data are uncorrelated. sum in quadrature of errors | ||
double err = sqrt( error_*error_ + (rhs.error_)*(rhs.error_) ); | ||
|
||
// result of the sum | ||
return Datum(val,err); | ||
} | ||
|
||
|
||
Datum Datum::sum( const Datum& rhs) const { | ||
|
||
// sum of central values | ||
double val = value_ + rhs.value_; | ||
// assume data are uncorrelated. sum in quadrature of errors | ||
double err = sqrt( error_*error_ + (rhs.error_)*(rhs.error_) ); | ||
|
||
// result of the sum | ||
return Datum(val,err); | ||
} | ||
|
||
const Datum& Datum::operator+=(const Datum& rhs) { | ||
value_ += rhs.value_; | ||
error_ = sqrt( rhs.error_*rhs.error_ + error_*error_ ); | ||
return *this; | ||
} | ||
|
||
const Datum& Datum::operator=(const Datum& rhs) { | ||
value_ = rhs.value_; | ||
error_ = rhs.error_; | ||
|
||
return *this; | ||
} | ||
|
||
|
||
bool Datum::operator<(const Datum& rhs) const { | ||
return ( value_ < rhs.value_ ); | ||
} | ||
|
||
|
||
Datum Datum::operator*(const Datum& rhs) const { | ||
double val = value_*rhs.value_; | ||
|
||
// propagate correctly the error for x*y | ||
double err = sqrt( rhs.value_*rhs.value_*error_*error_ + | ||
rhs.error_*rhs.error_*value_*value_ ); | ||
return Datum(val,err); | ||
} | ||
|
||
Datum Datum::operator/(const Datum& rhs) const { | ||
double val = value_ / rhs.value_; | ||
|
||
// propagate correctly the error for x / y | ||
double err = fabs(val) * sqrt( (error_/value_)*(error_/value_) + | ||
(rhs.error_/rhs.value_)*(rhs.error_/rhs.value_) ); | ||
|
||
return Datum(val,err); | ||
} | ||
|
||
Datum Datum::operator*(const double& rhs) const { | ||
return Datum(value_*rhs,error_*rhs); | ||
} | ||
|
||
// global functions | ||
Datum productDoubleDatum(const double& lhs, const Datum& rhs){ | ||
return Datum(lhs*rhs.value(), lhs*rhs.error() ); | ||
} | ||
|
||
Datum operator*(const double& lhs, const Datum& rhs){ | ||
return Datum(lhs*rhs.value(), lhs*rhs.error() ); | ||
} | ||
|
||
ostream& operator<<(ostream& os, const Datum& rhs){ | ||
using namespace std; | ||
os << rhs.value() << " +/- " << rhs.error(); // NB: no endl! | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef Datum_h | ||
#define Datum_h | ||
// Datum.h | ||
#include <iostream> | ||
|
||
class Datum { | ||
public: | ||
Datum(); | ||
Datum(double x, double y); | ||
Datum(const Datum& datum); | ||
~Datum() { }; | ||
|
||
double value() const { return value_; } | ||
double error() const { return error_; } | ||
double significance() const; | ||
void print() const; | ||
|
||
Datum operator+( const Datum& rhs ) const; | ||
const Datum& operator+=( const Datum& rhs ); | ||
|
||
Datum sum( const Datum& rhs ) const; | ||
|
||
const Datum& operator=( const Datum& rhs ); | ||
|
||
bool operator<(const Datum& rhs) const; | ||
|
||
Datum operator*( const Datum& rhs ) const; | ||
Datum operator/( const Datum& rhs ) const; | ||
|
||
Datum operator*( const double& rhs ) const; | ||
|
||
private: | ||
double value_; | ||
double error_; | ||
}; | ||
Datum operator*(const double& lhs, const Datum& rhs); | ||
Datum productDoubleDatum(const double& lhs, const Datum& rhs); | ||
std::ostream& operator<<(std::ostream& os, const Datum& rhs); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "DatumNew.h" | ||
#include <iostream> | ||
#include <cmath> | ||
|
||
Datum::Datum() { | ||
value_ = 0.0; | ||
error_ = 0.0; | ||
} | ||
|
||
Datum::Datum(double x, double y) { | ||
value_ = x; | ||
error_ = y; | ||
} | ||
|
||
Datum::Datum(const Datum& datum) { | ||
value_ = datum.value_; | ||
error_ = datum.error_; | ||
} | ||
|
||
double | ||
Datum::significance() const { | ||
return value_/error_; | ||
} | ||
|
||
void Datum::print() const { | ||
using namespace std; | ||
cout << "datum: " << value_ | ||
<< " +/- " << error_ << endl; | ||
} | ||
|
||
Datum Datum::operator+( const Datum& rhs) const { | ||
|
||
// sum of central values | ||
double val = value_ + rhs.value_; | ||
// assume data are uncorrelated. sum in quadrature of errors | ||
double err = sqrt( error_*error_ + (rhs.error_)*(rhs.error_) ); | ||
|
||
// result of the sum | ||
return Datum(val,err); | ||
} | ||
|
||
|
||
Datum Datum::sum( const Datum& rhs) const { | ||
|
||
// sum of central values | ||
double val = value_ + rhs.value_; | ||
// assume data are uncorrelated. sum in quadrature of errors | ||
double err = sqrt( error_*error_ + (rhs.error_)*(rhs.error_) ); | ||
|
||
// result of the sum | ||
return Datum(val,err); | ||
} | ||
|
||
const Datum& Datum::operator+=(const Datum& rhs) { | ||
value_ += rhs.value_; | ||
error_ = sqrt( rhs.error_*rhs.error_ + error_*error_ ); | ||
return *this; | ||
} | ||
|
||
const Datum& Datum::operator=(const Datum& rhs) { | ||
value_ = rhs.value_; | ||
error_ = rhs.error_; | ||
|
||
return *this; | ||
} | ||
|
||
|
||
bool Datum::operator<(const Datum& rhs) const { | ||
return ( value_ < rhs.value_ ); | ||
} | ||
|
||
|
||
Datum Datum::operator*(const Datum& rhs) const { | ||
double val = value_*rhs.value_; | ||
|
||
// propagate correctly the error for x*y | ||
double err = sqrt( rhs.value_*rhs.value_*error_*error_ + | ||
rhs.error_*rhs.error_*value_*value_ ); | ||
return Datum(val,err); | ||
} | ||
|
||
Datum Datum::operator/(const Datum& rhs) const { | ||
double val = value_ / rhs.value_; | ||
|
||
// propagate correctly the error for x / y | ||
double err = fabs(val) * sqrt( (error_/value_)*(error_/value_) + | ||
(rhs.error_/rhs.value_)*(rhs.error_/rhs.value_) ); | ||
|
||
return Datum(val,err); | ||
} | ||
|
||
Datum Datum::operator*(const double& rhs) const { | ||
return Datum(value_*rhs,error_*rhs); | ||
} | ||
|
||
// global functions | ||
Datum operator*(const double& lhs, const Datum& rhs){ | ||
return Datum(lhs*rhs.value_, lhs*rhs.error_ ); | ||
} | ||
|
||
ostream& operator<<(ostream& os, const Datum& rhs){ | ||
using namespace std; | ||
os << rhs.value_ << " +/- " << rhs.error_; // NB: no endl! | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef DatumNew_h | ||
#define DatumNew_h | ||
// DatumNew.h | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Datum { | ||
public: | ||
Datum(); | ||
Datum(double x, double y); | ||
Datum(const Datum& datum); | ||
~Datum() { }; | ||
|
||
double value() const { return value_; } | ||
double error() const { return error_; } | ||
double significance() const; | ||
void print() const; | ||
|
||
Datum operator+( const Datum& rhs ) const; | ||
const Datum& operator+=( const Datum& rhs ); | ||
|
||
Datum sum( const Datum& rhs ) const; | ||
|
||
const Datum& operator=( const Datum& rhs ); | ||
|
||
bool operator<(const Datum& rhs) const; | ||
|
||
Datum operator*( const Datum& rhs ) const; | ||
Datum operator/( const Datum& rhs ) const; | ||
|
||
Datum operator*( const double& rhs ) const; | ||
|
||
friend Datum operator*(const double& lhs, const Datum& rhs); | ||
friend ostream& operator<<(ostream& os, const Datum& rhs); | ||
|
||
private: | ||
double value_; | ||
double error_; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "InputService.h" | ||
|
||
InputService::InputService() { | ||
|
||
} | ||
|
||
std::vector<Datum> | ||
InputService::readDataFromUser() { | ||
return std::vector<Datum>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef InputService_h | ||
#define InputService_h | ||
|
||
#include <vector> | ||
#include "Datum.h" | ||
|
||
class InputService { | ||
public: | ||
InputService(); | ||
static std::vector<Datum> readDataFromUser(); | ||
|
||
private: | ||
|
||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "Unit.h" | ||
using namespace std; | ||
|
||
// init. static data member. NB: No static keyword necessary. Otherwise... compilation error! | ||
int Unit::counter_ = 0; | ||
|
||
Unit::Unit(const std::string& name) { | ||
name_ = name; | ||
counter_++; | ||
} | ||
|
||
Unit::~Unit() { | ||
counter_--; | ||
} | ||
|
||
ostream& | ||
operator<<(ostream& os, const Unit& unit) { | ||
os << unit.name_ << " Total Units: " << unit.counter_; | ||
return os; | ||
} | ||
|
||
|
||
|
Oops, something went wrong.