| [1571] | 1 | #include <stdlib.h> | 
|---|
|  | 2 | #include <stdio.h> | 
|---|
|  | 3 |  | 
|---|
|  | 4 | #include <exception> | 
|---|
|  | 5 | #include <string> | 
|---|
|  | 6 |  | 
|---|
| [2803] | 7 | #include <iostream> | 
|---|
| [1571] | 8 | #include <vector> | 
|---|
|  | 9 | #include <map> | 
|---|
| [1863] | 10 | #include <complex> | 
|---|
| [1571] | 11 |  | 
|---|
| [1863] | 12 | // ::::::::::::::::::::::::::::::::::::::::::::::::::::: | 
|---|
|  | 13 | // Test du support de la STL par le compilateur | 
|---|
|  | 14 | // string, vector<T>, map<T>, complex<T> exception | 
|---|
|  | 15 | // ::::::::::::::::::::::::::::::::::::::::::::::::::::: | 
|---|
|  | 16 |  | 
|---|
| [1571] | 17 | using namespace std; | 
|---|
|  | 18 |  | 
|---|
| [1863] | 19 | // -------------------------------------------------------------------- | 
|---|
|  | 20 | // definition d'une classe de conversion de C (complexes) <> R (reel) | 
|---|
|  | 21 | class convCR { | 
|---|
|  | 22 | public: | 
|---|
|  | 23 | inline convCR() { dv = 0.;  dv_im = 0.; } | 
|---|
| [1868] | 24 | inline convCR(convCR const& x) { dv = x.dv;  dv_im = x.dv_im; } | 
|---|
| [1863] | 25 | inline convCR(float v) { dv = (double)v;  dv_im = 0.; } | 
|---|
|  | 26 | inline convCR(double v) { dv = v;  dv_im = 0.; } | 
|---|
| [1866] | 27 | inline convCR(complex<float> const & z) { dv = (double)z.real();  dv_im = (double)z.imag(); } | 
|---|
|  | 28 | inline convCR(complex<double> const & z) { dv = z.real();  dv_im = z.imag(); } | 
|---|
| [1868] | 29 | inline convCR&  operator= (float v) { dv = (double)v;  dv_im = 0.; return (*this); } | 
|---|
|  | 30 | inline convCR&  operator= (double v) { dv = v;  dv_im = 0.; return (*this); } | 
|---|
|  | 31 | inline convCR&  operator= (complex<float> const & z) | 
|---|
|  | 32 | { dv = (double)z.real(); dv_im = (double)z.imag(); return (*this); } | 
|---|
|  | 33 | inline  convCR& operator= (complex<double> const & z) | 
|---|
|  | 34 | { dv = z.real(); dv_im = z.imag(); return (*this); } | 
|---|
| [1571] | 35 |  | 
|---|
| [1863] | 36 | inline operator float() const  { return((float)dv); } | 
|---|
|  | 37 | inline operator double() const  { return(dv); } | 
|---|
|  | 38 | inline operator complex<float>() const { return(complex<float>((float)dv, (float)dv_im)); } | 
|---|
| [1867] | 39 | inline operator complex<double>() const { return(complex<double>(dv, dv_im)); } | 
|---|
|  | 40 | inline complex<float> ToComplexF() const { return(complex<float>((float)dv, (float)dv_im)); } | 
|---|
|  | 41 | inline complex<double> ToComplexD() const { return(complex<double>(dv, dv_im)); } | 
|---|
| [1863] | 42 | double dv; | 
|---|
|  | 43 | double dv_im;   /* for holding imaginary part of a complex */ | 
|---|
|  | 44 | }; | 
|---|
|  | 45 | // -------------------------------------------------------------------- | 
|---|
|  | 46 |  | 
|---|
| [1571] | 47 | int main(int narg, char *arg[]) | 
|---|
|  | 48 | { | 
|---|
| [1863] | 49 | cout << "\n :::::::: Test complex / vector et map de la stl :::::::: \n" ; | 
|---|
| [1571] | 50 | int rc = 0; | 
|---|
|  | 51 | try { | 
|---|
|  | 52 | vector<int> vi; | 
|---|
|  | 53 | int k; | 
|---|
|  | 54 | for(k=0; k<5; k++)    vi.push_back(k*5); | 
|---|
| [1863] | 55 | cout << " -----> vector<int> vi.size() = " << vi.size() << endl; | 
|---|
| [1571] | 56 | for(k=0; k<vi.size(); k++) | 
|---|
|  | 57 | cout << "vi[" << k << "]= " << vi[k] << endl; | 
|---|
|  | 58 | map<string, int> msi; | 
|---|
|  | 59 | msi["Un"] = 1; | 
|---|
|  | 60 | msi["Deux"] = 2; | 
|---|
|  | 61 | msi["Cinq"] = 5; | 
|---|
|  | 62 | msi["Douze"] = 12; | 
|---|
|  | 63 | map<string, int>::iterator it; | 
|---|
| [1863] | 64 | cout << " -----> map<string, int> msi.size() = " << msi.size() << endl; | 
|---|
| [1571] | 65 | for(it=msi.begin(); it!=msi.end(); it++) | 
|---|
|  | 66 | cout << " S= " << (*it).first << " I=" << (*it).second << endl; | 
|---|
| [1863] | 67 | cout << " ---->  complex numbers and convCR : " << endl; | 
|---|
|  | 68 | complex<float> z,z1(2,2),z2(3,-1); | 
|---|
|  | 69 | complex<double> zd,zd1(2.5,2.5),zd2(3.3,-1); | 
|---|
|  | 70 | cout << " complex<float>: z1=" << z1 << " z2=" << z2 | 
|---|
|  | 71 | << " z1+z2=" << z1+z2 << endl; | 
|---|
|  | 72 | cout << " complex<double>: z1=" << zd1 << " z2=" << zd2 | 
|---|
|  | 73 | << " zd1+zd2=" << zd1+zd2 << endl; | 
|---|
|  | 74 | convCR czr, czr1(z1), czr2(zd2), czr3; | 
|---|
| [1866] | 75 | /* if defined(__GNUG__) && (__GNUC__ < 3) */ | 
|---|
| [1865] | 76 | complex<float> za; | 
|---|
|  | 77 | complex<double> zb,zc; | 
|---|
| [1867] | 78 | za = czr1.ToComplexF(); zb = czr1.ToComplexD(); zc = czr2.ToComplexD(); | 
|---|
| [1869] | 79 | cout << " convCR:.ToComplexF/D() za=(zb)=czr1" << za << " zc=" << zc | 
|---|
| [1866] | 80 | << " zb+zc=" << zb+zc << endl ; | 
|---|
| [1874] | 81 | #if defined(__GNUG__) | 
|---|
|  | 82 | cout << " GNUG_Version: __GNUC__ = " << __GNUC__ << endl; | 
|---|
| [1865] | 83 | #else | 
|---|
| [1874] | 84 | cout << " convCR: (complex<float>)(czr1(z1))" << (complex<float>)(czr1) | 
|---|
| [1869] | 85 | << " czr2(z2)" << (complex<double>)(czr2) | 
|---|
| [1868] | 86 | << " czr1+czr2= " << (complex<double>)czr2+(complex<double>)czr1 << endl; | 
|---|
| [1865] | 87 | #endif | 
|---|
| [2803] | 88 | complex<double> zdd(2); | 
|---|
|  | 89 | //    zdd = static_cast< complex<double> >(czr1); | 
|---|
|  | 90 | //    cout << " zdd = static_cast< complex<double> >(czr1) = " << zdd << endl; | 
|---|
|  | 91 | // ----------- Mai 2005 , compilation avec icc | 
|---|
|  | 92 | // Suite a l'essai de compilation avec icc (Intel compiler) | 
|---|
|  | 93 | // on fait un appel explicite a l'operateur de comversion | 
|---|
|  | 94 | // object.operator T() | 
|---|
|  | 95 | zdd = czr1.operator complex<double>(); | 
|---|
|  | 96 | cout << " zdd = czr1.operator complex<double>() : " << zdd << endl; | 
|---|
|  | 97 | double xx ; | 
|---|
|  | 98 | xx = czr1; | 
|---|
|  | 99 |  | 
|---|
| [1571] | 100 | } | 
|---|
|  | 101 | catch(exception exc){ | 
|---|
|  | 102 | // Classe de base des exception standard - | 
|---|
|  | 103 | // La methode what() n'est pas forcement conforme a la norme | 
|---|
|  | 104 | string msg = exc.what(); | 
|---|
|  | 105 | cerr << " Catched exception : msg= " << msg << endl; | 
|---|
|  | 106 | rc = 98; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | catch (...) { | 
|---|
|  | 109 | cerr << " Catched Unknown exception ! " << endl; | 
|---|
|  | 110 | rc = 99; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
| [1863] | 113 | cout << " ::::::::: Exiting from vecmapstl.cc ::::::::: \n" << endl; | 
|---|
| [1571] | 114 | exit(rc); | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 |  | 
|---|
|  | 118 |  | 
|---|
|  | 119 |  | 
|---|
|  | 120 |  | 
|---|
|  | 121 |  | 
|---|
|  | 122 |  | 
|---|
|  | 123 |  | 
|---|