ALMA Computing Group

Length.cpp

Go to the documentation of this file.
00001 using namespace std;
00002 #include <string>
00003 #include "Length.h"
00004 
00005 namespace atm {
00006   
00007   // Constructors
00008   
00009   Length::Length(){valueIS_=0.0;}
00010   
00011   Length::Length(double length){valueIS_=length;}
00012   
00013   Length::Length(double length, string units){
00014     if(units=="km"||units=="KM"){
00015       valueIS_=1.0E+3*length;
00016     }else if(units=="m"||units=="M"){
00017       valueIS_=length;
00018     }else if(units=="mm"||units=="MM"){
00019       valueIS_=1.0E-3*length;
00020     }else if(units=="micron"||units=="MICRON"){
00021       valueIS_=1.0E-6*length;
00022     }else if(units=="nm"||units=="NM"){
00023       valueIS_=1.0E-9*length;
00024     }else{
00025       valueIS_=length;
00026     }
00027   }
00028   Length::~Length(){}
00029   
00030   // Accessors
00031   double Length::get()const{return valueIS_;}
00032   double Length::get(string units)const{  
00033     if(units=="km"||units=="KM"){
00034       return 1.0E-3*valueIS_;
00035     }else if(units=="m"||units=="M"){
00036       return valueIS_;
00037     }else if(units=="mm"||units=="MM"){
00038       return 1.0E+3*valueIS_;
00039     }else if(units=="micron"||units=="MICRON"){
00040       return 1.0E+6*valueIS_;
00041     }else if(units=="nm"||units=="NM"){
00042       return 1.0E+9*valueIS_;
00043     }else{
00044       return valueIS_;
00045     }
00046   }
00047   
00048   Length& Length::operator= (const Length & rhs){valueIS_=rhs.valueIS_; return *this;}
00049   Length& Length::operator= (const double & rhs){valueIS_=rhs;          return *this;}
00050   
00051   Length  Length::operator+ (const Length & rhs){return Length(valueIS_+rhs.get());}
00052   Length  Length::operator- (const Length & rhs){return Length(valueIS_-rhs.get());}
00053   Length  Length::operator* (const double & scf){return Length(valueIS_*scf);}
00054   Length  Length::operator* (const float  & scf){return Length(valueIS_*(double)scf);}
00055   Length  Length::operator* (const int    & scf){return Length(valueIS_*(double)scf);}
00056   Length  Length::operator/ (const double & scf){return Length(valueIS_/scf);}
00057   Length  Length::operator/ (const float  & scf){return Length(valueIS_/(double)scf);}
00058   Length  Length::operator/ (const int    & scf){return Length(valueIS_/(double)scf);}
00059   
00060   
00061   bool    Length::operator< (const Length & rhs){return (valueIS_<rhs.get());}
00062   bool    Length::operator> (const Length & rhs){return (valueIS_>rhs.get());}
00063   bool    Length::operator<=(const Length & rhs){return (valueIS_<=rhs.get());}
00064   bool    Length::operator>=(const Length & rhs){return (valueIS_>=rhs.get());}
00065   bool    Length::operator==(const Length & rhs){return (valueIS_==rhs.get());}
00066   bool    Length::operator!=(const Length & rhs){return (valueIS_!=rhs.get());}
00067   
00068 }
00069 
00070 #if     defined(_TESTBED_)
00071 
00072 #include <iostream>
00073 int main()
00074 {
00075   Length longueur(10); 
00076   cout << "longueur=" << longueur.get("km") << "km" << endl;
00077   cout << "longueur=" << longueur.get() << "m" << endl;
00078   
00079   Length h(10,"km"), dh(10,"m"), hsum, hdif;
00080   cout << h.get()+dh.get() << endl;
00081   hsum = h+dh;
00082   cout << hsum.get() << endl;
00083   cout << hsum.get("KM") << "KM" << endl;
00084   cout << hsum.get("km") << "km" << endl;
00085   Length z;
00086   z = 20;
00087   Length dz(20,"mm");
00088   hdif = z-dz; 
00089   cout << hdif.get() << " SI units" << endl;
00090   cout << hdif.get("mm") << "mm" <<  endl;
00091   
00092   Length hscaled;
00093   int scf=3;
00094   
00095   hscaled = h*scf;
00096   cout <<h.get("km")<<"km * "<< scf <<" = "<< hscaled.get() << "m" << endl;
00097   
00098   
00099   Length P(10); cout<< "Length P(10) ==> P="<<P.get() << endl;
00100   
00101   Length p = P; cout<< "Length p=P; ==> p="<<p.get() << endl;
00102   cout << "Test for the relational operators:" << endl;
00103   if(p>=P)cout << "p>=P" << endl;
00104   if(p<=P)cout << "p<=P" << endl;
00105   if(p==P)cout << "p==P" << endl;
00106   cout << "Arithmetic operations:" << endl;
00107   p = P+p; cout<< "p=P+p ==> p=" << p.get() << endl;
00108   cout << "P=" <<P.get() << " must not change of value!" << endl;
00109   p = p+P; cout<< "p=p+P ==> p=" << p.get() << endl;
00110   cout << "P=" <<P.get() << " must not change of value!" << endl;
00111   Length p0; cout << "Let declare p0 be of type Length using the default constructor: Length p0;" << endl;
00112   p0 = p+P; cout <<"p0 = p+P with p="<<p.get()<<" and P="<<P.get()<<" ==> p0=p+P="<<p0.get()<<endl; 
00113   Length p1; cout << "Let declare p0 be of type Length using the default constructor: Length p1;" << endl;
00114   cout << "With no assignement its value must be 0: p1="<<p1.get() << endl;
00115   if(P<p){
00116     cout << "Test relational operator P>p: OK" << endl;
00117   }
00118   if(p!=P){
00119     cout << "Test relational operator p!=P: OK" << endl;
00120   } 
00121   if(p<P)cout << "p<P" << endl;
00122   cout << "Some more tests for arithmetic expressions " << endl;
00123   p1 = P; cout << "Assign the value of P to p1: p1=P ==> p1=" << p1.get() << endl;
00124   p1 = p1+p+P;cout << "Arithmetic sum: p1+p+P=" << p1.get() << endl;
00125   p1 = p1-p-P;cout << "Arithmetic difference: p1-p-P=" << p1.get() << endl;
00126   p1 = p1*2.0;cout << "Product by a scalar p1=p1*2.0=" << p1.get("mm") << "mm" << endl;
00127   cout <<"p0="<<p0.get()<<endl;
00128   p1 = p0*2.0;cout << "p1=p0*2.0=" << p1.get("mm") << "mm" << endl;
00129   cout <<"--------------------------------------------------------------------------" << endl;
00130   cout <<"| Note that the commutativity of the oprator * has not been implemented! |" << endl;
00131   cout <<"--------------------------------------------------------------------------" << endl;
00132   cout <<"p0="<<p0.get()<<" must have not changed of value" << endl;
00133   cout <<"p1="<<p1.get()<<endl;
00134   p1 = p1/2.0e0;cout << "p1=p1/2.0=" << p1.get("mm") << "mm" << endl;
00135   p1 = p0/2.0;cout << "p1=p0/2.0=" << p1.get("mm") << "mm" << endl;
00136   cout <<"-----------------------------------------------------------------------" << endl;
00137   cout <<"| Note that dividing by a physical quantity has not been implemented! |" << endl;
00138   cout <<"-----------------------------------------------------------------------" << endl;
00139   cout << endl;
00140   cout << "Some more tests for arithmetic expressions " << endl;
00141   cout <<"p0="<<p0.get()<<"  must have not changed of value" << endl;
00142   cout << "P=" << P.get() << endl;
00143   p1 = p0*2.0e0+P;
00144   cout <<"p1=p0*2.0E0+P="<<p1.get()<<endl;
00145   cout << endl;
00146   cout << "TESTBED done " << endl;
00147   
00148   return 0;
00149 }
00150 
00151 #endif